BaseFloater
Base component for creating floating elements tethered to an anchor element. The floating element is hidden by default and can be revealed by one of three triggers: click, focus, or hover. These props trigger event listeners to be registered on the outermost div. This is convenient but can cause some unexpected behavior if you're not careful.
BaseFloater is powered by floating-ui. It is worthwhile to familiarize yourself with their docs. floating-ui also powers useAisFloater, AisTooltip, and AisPopover.
Examples
Click
The following example shows two different methods of using click events to show the floater. The first uses BaseFloater's click prop, which registers a listener on the surrounding div. This works well if your anchor element is a block element. However, notice if you click to the right of the Click prop button, the floater is (surpisingly) shown. This is caused by the fact that the div with the listener spans the full width. This becomes less of an issue when using grid and flex.
The Second example uses a more manual approach and wires up the toggle() (exposed through a slot prop) directly to the button's @click handler. In this case, this provides more expected behavior.
Hover & Focus
Hover and focus events are also obvious triggers for showing a floater. Use the hover and focus props register event listeners on the anchor div. You must be mindful of potential unexpected behavior when using focus -- especially when combined with @click events. BaseButton will receive focus on click and by default give up focus at the end of the click. You can use the keep-focus prop in either BaseButton or AisButton.
Notice the use of anchor-class="inline-block" here to constrain the hover target.
Disabled
Use the disabled prop to put the floater in the disabled state. Setting disabled to true will provide the disabled value to components in both the #default and #anchor slots. Disabling an open floater will not automatically close it.
Manual manipulation
BaseFloater exposes four methods -- isOpen(), show(), hide(), and toggle(), which can be used to manipulate the floater manually. The floater below does not have any active triggers, but the buttons below use its exposed methods to toggle visibility.
Info
Component Reference
Props
Property | Type(s) | Required | Default | Description | Deprecated |
|---|---|---|---|---|---|
| anchorClass | Array, String | () => '' | Add to the class attribute of the anchor element | false | |
| click | Boolean | false | When true, the floating element is shown when the target is clicked | false | |
| clickAway | Boolean | false | When true, clicking outside the floater will hide the floating element.
Be aware that clickAway can interfere when trying to use the exposed methods to
control the floater manually. | false | |
| disabled | Boolean | false | Places component in a disabled state, when true | false | |
| floatingClass | Array, String | () => '' | Add to the class attribute of the floating element | false | |
| focus | Boolean | false | When true, the floating element is shown when the target is focused | false | |
| globalEscape | Boolean | false | When true, the key handler for escape will be registered on the document
instead of on the floater. | false | |
| hover | Boolean | false | Shows the floating element when the target is hovered, when true
When true, the floating element is shown when the target is hovered | false | |
| middleware | Array | [] | Optional Floating UI Middleware options | false | |
| minWidth | String | undefined | Sets the min-width of the floating container | false | |
| placement | String | 'bottom-start' | Controls where the floating element appears in relation to the target, when trueValid values: top, top-start, top-end, right, right-start, right-end, bottom, bottom-start, bottom-end, left, left-start, left-end | false | |
| width | String | 'max-content' | Sets the width of the floating container. Use 100% to match the width of the container. | false |
Slots
Name | Scoped | Description |
|---|---|---|
| anchor | Scoped slot for custom rendering the target element | |
| default | Scoped slot for custom rendering the floating element |
Events
Event | Arguments | Description |
|---|---|---|
| hidden | Emitted immediately after the floating element is hidden | |
| hide | Emitted immediately before the floating element is hidden | |
| show | Emitted immediately before the floating element is shown | |
| shown | Emitted immediately after the floating element is shown |