Trigger
Used to pop up a drop-down box by hovering, focusing, or clicking on a element.
Install
npm install @arco-design/web-react@2.66.18
Import @arco-design/web-react/dist/css/arco.css at the entry and set the locale with ConfigProvider. React 19 projects must also import @arco-design/web-react/es/_util/react-19-adapter first, or imperative APIs such as Message and Modal cannot render.
Examples
- Basic: The basic usage. The popup layer has no style by default.
- Controlled: Control the visibility of the popup.
- nest: The popup can be nested.
- multiple trigger: the
triggerproperty can be an array. - algin mouse position: The popup will align mouse position. If you want to keep the pop-up layer displayed when you click in an area, and update the position of the pop-up layer align the mouse position, see the next example.
- Update position
- show arrow: Show arrow node. ## zh-US The popup layer has no style by default.
- popupAlign: Specify moving a few pixels in some directions. For example, when
positionis'top', set popupAlign to{ top: 20}, the popup will move up by20px, and set popupAlign to{top: [100, 20]}, the popup will move horizontally100pxright and20pxup. - updateOnScroll: Listen for container scrolling by setting
updateOnScroll. - Adjust positioning when a specified distance from the viewport: The default pop-up layer position will be positioned and fine-tuned according to the viewport boundary. The
boundaryDistanceparameter can be set to adjust the positioning when it is a certain distance from the viewport boundary. Only takes effect when autoFitPosition=true and alignPoint=false.
Examples come from the official Arco Design repository (MIT) at the commit published as npm 2.66.18. Some use online images or upload endpoints; network requests are disabled in previews.
API
Trigger
| Property | Description | Type | DefaultValue | Version |
|---|---|---|---|---|
| alignPoint | The popup will align mouse position | boolean | - | - |
| autoAlignPopupMinWidth | Set the minimum width of the popup by the width of the child element | boolean | - | - |
| autoAlignPopupWidth | Set the width of the popup by the width of the child element | boolean | - | - |
| autoFitPosition | Whether to automatically adjust the position of the popup according to the viewport | boolean | true | - |
| autoFixPosition | Whether to automatically reposition when the popup's size changes. | boolean | true | - |
| blurToHide | Whether close the popup when the child node losing focus. Only work when the trigger containers focus | boolean | true | - |
| clickToClose | Whether to allow close the popup by clicking the child node.When trigger contains click, contextMenu, the default is true. otherwise false | boolean | - | - |
| containerScrollToClose | Whether to close the popup when the container is scrolled | boolean | - | 2.34.0 |
| defaultPopupVisible | Whether the popup is visible by default | boolean | - | - |
| disabled | Whether to disable | boolean | - | - |
| escToClose | Whether to allow close the popup by pressing ESC. | boolean | - | - |
| mouseLeaveToClose | Whether to allow close the popup by clicking the child node. | boolean | true | 2.22.0 |
| popupHoverStay | Whether the popup is visible when the mouse hovers over the popup. | boolean | true | - |
| popupVisible | Whether the popup is visible | boolean | - | - |
| showArrow | Whether to display arrow node | boolean | - | - |
| unmountOnExit | Whether to destroy the popup when hidden | boolean | true | - |
| updateOnScroll | Whether to update the popover's position when the container is scrolled | boolean | - | 2.32.0 |
| focusDelay | Delay time to show when focus. unit: ms. | number | - | - |
| mouseEnterDelay | Delay time to show when mouse enter. unit: ms. | number | 100 | - |
| mouseLeaveDelay | Delay time to show when mouse leave. unit: ms. | number | 100 | - |
| childrenPrefix | Set an additional class name(${childrenPrefix}-open) for the container of the popup. | string | - | - |
| classNames | Animation class name | string | fadeIn | - |
| position | The position of the popup relative to the child node. | | 'top'| 'tl'| 'tr'| 'bottom'| 'bl'| 'br'| 'left'| 'lt'| 'lb'| 'right'| 'rt'| 'rb' | bottom | - |
| trigger | Types of events that cause the popup to show | | 'hover'| 'click'| 'focus'| 'contextMenu'| Array<'hover' | 'click' | 'focus' | 'contextMenu'> | hover | - |
| arrowProps | The html attributes of the arrow node | HTMLAttributes<HTMLDivElement> | - | - |
| boundaryDistance | The default pop-up layer position will be positioned and fine-tuned according to the viewport boundary. This parameter can be set to adjust the positioning when it is a certain distance from the viewport boundary. Only takes effect when autoFitPosition=true and alignPoint=false. | | { left?: number; bottom?: number }| { left?: number; top?: number }| { right?: number; bottom?: number }| { right?: number; top?: number } | - | 2.59.0 |
| className | Additional css class | string | string[] | - | - |
| clickOutsideToClose | Whether to allow close the popup by clicking the area outside the child node and the popup box.By default, this logic is triggered in the bubbling phase. You can set { capture: true } to specify the triggering phase in the capture phase. | boolean | { capture: boolean } | true | { capture: boolean } in 2.55.0 |
| duration | Animation transition time | number | { exit?: number; enter?: number; appear?: number } | 200 | - |
| onClickOutside | Callback when click the area outside the child and the popup | Function | - | - |
| popupAlign | Adjust the position of the popup. Indicates moving a few pixels in one direction. example | {left?: number | [number, number];right?: number | [number, number];top?: number | [number, number];bottom?: number | [number, number];} | {} | - |
| popupStyle | The css style of the content of the popup | CSSProperties | - | - |
| style | The additional css class | CSSProperties | - | - |
| getDocument | Return a element which will be attached click event to close trigger | () => Element | () => window.document | - |
| getPopupContainer | Set the parent node which the popup will be rendered to. | (node: HTMLElement) => Element | - | - |
| onClick | Callback when click the child node. (Only work when trigger contains click) | (e) => void | - | - |
| onVisibleChange | Callback when the visibility of the popup is changed | (visible: boolean) => void | - | - |
| popup | The content of the popup | () => ReactNode | - | - |
When to fired onVisibleChange
-
When
triggercontainsclick- Click on the area outside the child node and the popup will hide the popup. This behavior can be prevented if you set
clickOutsideToClose=false. - Click on the child node will show or hide the popup. If you set
clickToClose=true, when the popup is displayed, clicking the child node will not hide the popup.
- Click on the area outside the child node and the popup will hide the popup. This behavior can be prevented if you set
-
When
triggercontainscontextMenu
- Click on the area outside the child node and the popup will hide the popup. This behavior can be prevented if you set
clickOutsideToClose=false. - Right-click on the child node will show the popup.
-
When
triggercontainshover- The popup will be hidden when the mouse moved out of the popup, and did not move into the child node within
${mouseLeaveDelay}ms. - The popup will be hidden when the mouse moved out of the child node, and did not move into the popup within
${mouseLeaveDelay}ms.
If
popupHoverStay=falseis set, the mouse cannot be moved into the popup. - The popup will be hidden when the mouse moved out of the popup, and did not move into the child node within
-
When
triggercontainsfocus- The popup will be hidden when the child node losing focus. This behavior can be prevented if you set
blurToHide=false. - Click on the area outside the child node and the popup will hide the popup if you set
blurToHide=false.
- The popup will be hidden when the child node losing focus. This behavior can be prevented if you set
Q&A
-
When
getPopupContaineris set, the popup layer is in the wrong position?The default
autoFitPositionproperty of the component istrue, which means that the component will automatically adjust the position of the popup layer according to the size of the popup layer, its position in the viewport and the mounted parent element. For example, setposition=top, but the space above is insufficient, the pop-up layer will be covered, and the component will determine whether to display the pop-up layer below the trigger element according to the situation. IfgetPopupContaineris set, the style attributeleftof the popup layer is at least 0, that is, the minimum distance from the left border of the parent element is 0, so as to avoid exceeding the parent element and causing it to be blocked. AutoFitPosition can be avoided by settingautoFitPosition=false.