@aleph-alpha/ui-library 5.2.0 → 5.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/system/components/UiTooltip/UiTooltip.vue.d.ts +1 -0
- package/dist/system/components/UiTooltip/types.d.ts +8 -0
- package/dist/system/components/core/tooltip/TooltipContent.vue.d.ts +2 -0
- package/dist/system/index.d.ts +1 -0
- package/dist/system/lib/useAnchorFloat.d.ts +143 -0
- package/dist/system/lib.js +18787 -18280
- package/dist/system/patterns/UiFloatActions/UiFloatActionItems.vue.d.ts +35 -0
- package/dist/system/patterns/UiFloatActions/UiFloatActions.vue.d.ts +45 -0
- package/dist/system/patterns/UiFloatActions/floatActionItemContext.d.ts +16 -0
- package/dist/system/patterns/UiFloatActions/index.d.ts +3 -0
- package/dist/system/patterns/UiFloatActions/types.d.ts +127 -0
- package/dist/system/patterns/index.d.ts +1 -0
- package/docs/public-docs/quick-start.md +43 -0
- package/package.json +1 -1
- package/src/components/UiAccordion/UiAccordion.stories.ts +2 -2
- package/src/components/UiAutocomplete/UiAutocomplete.stories.ts +2 -2
- package/src/components/UiAutocomplete/UiAutocomplete.vue +3 -3
- package/src/components/UiBadge/UiBadge.stories.ts +9 -9
- package/src/components/UiDropdownMenu/UiDropdownMenu.stories.ts +4 -4
- package/src/components/UiField/UiField.stories.ts +6 -6
- package/src/components/UiInput/UiInput.vue +1 -1
- package/src/components/UiKbd/UiKbd.stories.ts +3 -3
- package/src/components/UiKbd/UiKbd.vue +1 -1
- package/src/components/UiNavigationMenu/UiNavigationMenu.stories.ts +28 -28
- package/src/components/UiPopover/UiPopover.stories.ts +20 -20
- package/src/components/UiRadioGroup/UiRadioGroup.stories.ts +24 -24
- package/src/components/UiResizable/UiResizable.stories.ts +14 -14
- package/src/components/UiSelect/UiSelect.stories.ts +1 -1
- package/src/components/UiSheet/UiSheet.stories.ts +14 -14
- package/src/components/UiSlider/UiSlider.stories.ts +8 -8
- package/src/components/UiSpinner/UiSpinner.stories.ts +8 -8
- package/src/components/UiSwitch/UiSwitch.stories.ts +4 -4
- package/src/components/UiTabs/UiTabs.stories.ts +46 -46
- package/src/components/UiToggleGroup/UiToggleGroup.stories.ts +14 -14
- package/src/components/UiTooltip/UiTooltip.vue +2 -0
- package/src/components/UiTooltip/types.ts +9 -0
- package/src/components/core/accordion/AccordionContent.vue +1 -1
- package/src/components/core/accordion/AccordionTrigger.vue +1 -1
- package/src/components/core/alert/AlertDescription.vue +1 -1
- package/src/components/core/alert/index.ts +1 -1
- package/src/components/core/calendar/Calendar.vue +4 -4
- package/src/components/core/calendar/CalendarCell.vue +1 -1
- package/src/components/core/drawer/DrawerDescription.vue +1 -1
- package/src/components/core/dropdown-menu/DropdownMenuCheckboxItem.vue +1 -1
- package/src/components/core/dropdown-menu/DropdownMenuItem.vue +1 -1
- package/src/components/core/dropdown-menu/DropdownMenuLabel.vue +1 -3
- package/src/components/core/dropdown-menu/DropdownMenuRadioItem.vue +1 -1
- package/src/components/core/dropdown-menu/DropdownMenuShortcut.vue +4 -1
- package/src/components/core/dropdown-menu/DropdownMenuSubTrigger.vue +1 -1
- package/src/components/core/field/FieldDescription.vue +1 -1
- package/src/components/core/field/FieldLegend.vue +2 -2
- package/src/components/core/field/FieldSeparator.vue +1 -1
- package/src/components/core/input/Input.vue +1 -1
- package/src/components/core/item/index.ts +1 -1
- package/src/components/core/label/Label.vue +1 -1
- package/src/components/core/listbox/ListboxFilter.vue +1 -1
- package/src/components/core/listbox/ListboxGroupLabel.vue +1 -1
- package/src/components/core/listbox/ListboxItem.vue +1 -1
- package/src/components/core/native-select/NativeSelect.vue +1 -1
- package/src/components/core/navigation-menu/NavigationMenuLink.vue +1 -1
- package/src/components/core/navigation-menu/index.ts +1 -1
- package/src/components/core/range-calendar/RangeCalendarCell.vue +1 -1
- package/src/components/core/select/SelectItem.vue +1 -1
- package/src/components/core/select/SelectLabel.vue +3 -1
- package/src/components/core/select/SelectTrigger.vue +1 -1
- package/src/components/core/sidebar/SidebarGroupContent.vue +1 -1
- package/src/components/core/sidebar/SidebarGroupLabel.vue +1 -1
- package/src/components/core/sidebar/SidebarMenuBadge.vue +1 -1
- package/src/components/core/sidebar/SidebarMenuSubButton.vue +2 -2
- package/src/components/core/sidebar/index.ts +4 -4
- package/src/components/core/slider/Slider.vue +3 -3
- package/src/components/core/stepper/StepperDescription.vue +1 -1
- package/src/components/core/table/Table.vue +1 -1
- package/src/components/core/table/TableEmpty.vue +1 -1
- package/src/components/core/tabs/TabsTrigger.vue +1 -1
- package/src/components/core/tags-input/TagsInput.vue +1 -1
- package/src/components/core/tags-input/TagsInputItem.vue +1 -1
- package/src/components/core/textarea/Textarea.vue +1 -1
- package/src/components/core/toggle/index.ts +1 -1
- package/src/components/core/toggle-group/index.ts +1 -1
- package/src/components/core/tooltip/TooltipContent.vue +10 -4
- package/src/index.ts +1 -0
- package/src/lib/useAnchorFloat.test.ts +166 -0
- package/src/lib/useAnchorFloat.ts +437 -0
- package/src/lib/useDrag.stories.ts +12 -12
- package/src/patterns/UiDatePicker/UiDatePicker.stories.ts +8 -8
- package/src/patterns/UiFloatActions/UiFloatActionItems.vue +327 -0
- package/src/patterns/UiFloatActions/UiFloatActions.stories.ts +900 -0
- package/src/patterns/UiFloatActions/UiFloatActions.vue +224 -0
- package/src/patterns/UiFloatActions/__tests__/UiFloatActionItems.test.ts +104 -0
- package/src/patterns/UiFloatActions/__tests__/UiFloatActions.a11y.test.ts +152 -0
- package/src/patterns/UiFloatActions/__tests__/UiFloatActions.test.ts +86 -0
- package/src/patterns/UiFloatActions/floatActionItemContext.ts +20 -0
- package/src/patterns/UiFloatActions/index.ts +3 -0
- package/src/patterns/UiFloatActions/types.ts +155 -0
- package/src/patterns/UiSidebar/UiSidebarGroupLabel.stories.ts +2 -2
- package/src/patterns/UiSidebar/UiSidebarMenuButton.stories.ts +3 -3
- package/src/patterns/index.ts +1 -0
package/src/index.ts
CHANGED
|
@@ -8,6 +8,7 @@ export * from './components';
|
|
|
8
8
|
export * from './patterns';
|
|
9
9
|
export * from './templates';
|
|
10
10
|
export * from './lib/useDevice';
|
|
11
|
+
export * from './lib/useAnchorFloat';
|
|
11
12
|
export * from './directives';
|
|
12
13
|
export { Primitive } from 'reka-ui';
|
|
13
14
|
export type PrimitiveProps = RekaPrimitiveProps;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { render } from '@testing-library/vue';
|
|
2
|
+
import { afterEach, describe, expect, test, vi } from 'vitest';
|
|
3
|
+
import { defineComponent, nextTick, ref } from 'vue';
|
|
4
|
+
import { useAnchorFloat } from './useAnchorFloat';
|
|
5
|
+
|
|
6
|
+
const TestComponent = defineComponent({
|
|
7
|
+
props: {
|
|
8
|
+
trigger: {
|
|
9
|
+
type: String as () => 'hover' | 'click' | 'focus' | 'manual',
|
|
10
|
+
default: 'click',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
setup(props) {
|
|
14
|
+
const open = ref(false);
|
|
15
|
+
const float = useAnchorFloat({
|
|
16
|
+
trigger: () => props.trigger,
|
|
17
|
+
open,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return { open, ...float };
|
|
21
|
+
},
|
|
22
|
+
template: `
|
|
23
|
+
<div>
|
|
24
|
+
<button
|
|
25
|
+
ref="referenceRef"
|
|
26
|
+
type="button"
|
|
27
|
+
@mouseenter="onReferenceMouseEnter"
|
|
28
|
+
@mouseleave="onReferenceMouseLeave"
|
|
29
|
+
@focus="onReferenceFocus"
|
|
30
|
+
@blur="onReferenceBlur"
|
|
31
|
+
@click="onReferenceClick"
|
|
32
|
+
>
|
|
33
|
+
Anchor
|
|
34
|
+
</button>
|
|
35
|
+
<div v-if="open" ref="floatingRef">Floating</div>
|
|
36
|
+
</div>
|
|
37
|
+
`,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
vi.restoreAllMocks();
|
|
42
|
+
vi.useRealTimers();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('useAnchorFloat', () => {
|
|
46
|
+
test('opens and closes on click trigger', async () => {
|
|
47
|
+
const { getByRole, queryByText } = render(TestComponent, {
|
|
48
|
+
props: { trigger: 'click' },
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
expect(queryByText('Floating')).not.toBeInTheDocument();
|
|
52
|
+
|
|
53
|
+
await getByRole('button', { name: 'Anchor' }).click();
|
|
54
|
+
await nextTick();
|
|
55
|
+
expect(queryByText('Floating')).toBeInTheDocument();
|
|
56
|
+
|
|
57
|
+
await getByRole('button', { name: 'Anchor' }).click();
|
|
58
|
+
await nextTick();
|
|
59
|
+
expect(queryByText('Floating')).not.toBeInTheDocument();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('opens on hover trigger after delay', async () => {
|
|
63
|
+
vi.useFakeTimers();
|
|
64
|
+
|
|
65
|
+
const { getByRole, queryByText } = render(TestComponent, {
|
|
66
|
+
props: { trigger: 'hover' },
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
await getByRole('button', { name: 'Anchor' }).dispatchEvent(
|
|
70
|
+
new MouseEvent('mouseenter', { bubbles: true }),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
expect(queryByText('Floating')).not.toBeInTheDocument();
|
|
74
|
+
|
|
75
|
+
vi.advanceTimersByTime(150);
|
|
76
|
+
await nextTick();
|
|
77
|
+
|
|
78
|
+
expect(queryByText('Floating')).toBeInTheDocument();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('closes on Escape when open', async () => {
|
|
82
|
+
const { getByRole, queryByText } = render(TestComponent, {
|
|
83
|
+
props: { trigger: 'click' },
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
await getByRole('button', { name: 'Anchor' }).click();
|
|
87
|
+
await nextTick();
|
|
88
|
+
expect(queryByText('Floating')).toBeInTheDocument();
|
|
89
|
+
|
|
90
|
+
document.dispatchEvent(
|
|
91
|
+
new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }),
|
|
92
|
+
);
|
|
93
|
+
await nextTick();
|
|
94
|
+
|
|
95
|
+
expect(queryByText('Floating')).not.toBeInTheDocument();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('opens on focus trigger when anchor receives focus', async () => {
|
|
99
|
+
const { getByRole, queryByText } = render(TestComponent, {
|
|
100
|
+
props: { trigger: 'focus' },
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
expect(queryByText('Floating')).not.toBeInTheDocument();
|
|
104
|
+
|
|
105
|
+
getByRole('button', { name: 'Anchor' }).focus();
|
|
106
|
+
await nextTick();
|
|
107
|
+
|
|
108
|
+
expect(queryByText('Floating')).toBeInTheDocument();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('opens on focus for hover trigger', async () => {
|
|
112
|
+
const { getByRole, queryByText } = render(TestComponent, {
|
|
113
|
+
props: { trigger: 'hover' },
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
getByRole('button', { name: 'Anchor' }).focus();
|
|
117
|
+
await nextTick();
|
|
118
|
+
|
|
119
|
+
expect(queryByText('Floating')).toBeInTheDocument();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('closes on blur when focus leaves anchor and floating content', async () => {
|
|
123
|
+
const { getByRole, queryByText } = render(TestComponent, {
|
|
124
|
+
props: { trigger: 'focus' },
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const anchor = getByRole('button', { name: 'Anchor' });
|
|
128
|
+
anchor.focus();
|
|
129
|
+
await nextTick();
|
|
130
|
+
expect(queryByText('Floating')).toBeInTheDocument();
|
|
131
|
+
|
|
132
|
+
anchor.blur();
|
|
133
|
+
await nextTick();
|
|
134
|
+
|
|
135
|
+
expect(queryByText('Floating')).not.toBeInTheDocument();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('supports manual open state', async () => {
|
|
139
|
+
const ManualComponent = defineComponent({
|
|
140
|
+
setup() {
|
|
141
|
+
const open = ref(false);
|
|
142
|
+
const { show, hide, referenceRef, floatingRef } = useAnchorFloat({
|
|
143
|
+
trigger: 'manual',
|
|
144
|
+
open,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return { open, show, hide, referenceRef, floatingRef };
|
|
148
|
+
},
|
|
149
|
+
template: `
|
|
150
|
+
<div>
|
|
151
|
+
<button ref="referenceRef" type="button" @click="show">Show</button>
|
|
152
|
+
<button type="button" @click="hide">Hide</button>
|
|
153
|
+
<div v-if="open" ref="floatingRef">Manual floating</div>
|
|
154
|
+
</div>
|
|
155
|
+
`,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const { getByRole, queryByText } = render(ManualComponent);
|
|
159
|
+
|
|
160
|
+
await getByRole('button', { name: 'Show' }).click();
|
|
161
|
+
expect(queryByText('Manual floating')).toBeInTheDocument();
|
|
162
|
+
|
|
163
|
+
await getByRole('button', { name: 'Hide' }).click();
|
|
164
|
+
expect(queryByText('Manual floating')).not.toBeInTheDocument();
|
|
165
|
+
});
|
|
166
|
+
});
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import {
|
|
2
|
+
autoUpdate,
|
|
3
|
+
flip,
|
|
4
|
+
offset as offsetMiddleware,
|
|
5
|
+
shift,
|
|
6
|
+
type Placement,
|
|
7
|
+
useFloating,
|
|
8
|
+
} from '@floating-ui/vue';
|
|
9
|
+
import {
|
|
10
|
+
computed,
|
|
11
|
+
onBeforeUnmount,
|
|
12
|
+
onMounted,
|
|
13
|
+
ref,
|
|
14
|
+
toValue,
|
|
15
|
+
watch,
|
|
16
|
+
type MaybeRefOrGetter,
|
|
17
|
+
type Ref,
|
|
18
|
+
} from 'vue';
|
|
19
|
+
|
|
20
|
+
export type AnchorFloatTrigger = 'hover' | 'click' | 'focus' | 'manual';
|
|
21
|
+
|
|
22
|
+
const supportsPopover =
|
|
23
|
+
typeof HTMLElement !== 'undefined' &&
|
|
24
|
+
typeof HTMLElement.prototype.showPopover === 'function';
|
|
25
|
+
|
|
26
|
+
/** Per-axis distance between the anchor and the floating element, in pixels. */
|
|
27
|
+
export interface AnchorFloatOffset {
|
|
28
|
+
/** Distance along the placement axis (e.g. vertical gap for top/bottom). */
|
|
29
|
+
mainAxis?: number;
|
|
30
|
+
/** Distance along the cross axis (shifts the panel sideways). */
|
|
31
|
+
crossAxis?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Options for {@link useAnchorFloat}.
|
|
36
|
+
*
|
|
37
|
+
* @category Composables
|
|
38
|
+
* @useCases contextual toolbar positioning, custom floating panels
|
|
39
|
+
* @keywords float, anchor, floating-ui, composable, positioning
|
|
40
|
+
* @related UiFloatActions, UiFloatActionItems
|
|
41
|
+
*/
|
|
42
|
+
export interface UseAnchorFloatOptions {
|
|
43
|
+
/**
|
|
44
|
+
* How the floating panel opens.
|
|
45
|
+
* - `hover` — pointer hover and keyboard focus
|
|
46
|
+
* - `focus` — keyboard focus
|
|
47
|
+
* - `click` — click or Enter/Space on the anchor
|
|
48
|
+
* - `manual` — controlled via `open`
|
|
49
|
+
* @default 'hover'
|
|
50
|
+
*/
|
|
51
|
+
trigger?: MaybeRefOrGetter<AnchorFloatTrigger>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Preferred placement relative to the anchor element.
|
|
55
|
+
* Auto-flips via floating-ui when there is not enough room.
|
|
56
|
+
* @default 'top'
|
|
57
|
+
*/
|
|
58
|
+
placement?: MaybeRefOrGetter<Placement>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Distance from the anchor. A number sets the main-axis offset only.
|
|
62
|
+
* @default 8
|
|
63
|
+
*/
|
|
64
|
+
offset?: MaybeRefOrGetter<number | AnchorFloatOffset>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Controlled open state. When omitted, the composable manages state internally.
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const open = ref(false);
|
|
71
|
+
* const float = useAnchorFloat({ trigger: 'manual', open });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
open?: Ref<boolean | undefined>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Delay in ms before opening on pointer hover.
|
|
78
|
+
* Does not apply to keyboard focus (opens immediately).
|
|
79
|
+
* @default 150
|
|
80
|
+
*/
|
|
81
|
+
openDelay?: MaybeRefOrGetter<number>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Delay in ms before closing on pointer leave.
|
|
85
|
+
* @default 150
|
|
86
|
+
*/
|
|
87
|
+
closeDelay?: MaybeRefOrGetter<number>;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Close when clicking outside the anchor and floating panel.
|
|
91
|
+
* @default true
|
|
92
|
+
*/
|
|
93
|
+
dismissOnOutsideClick?: MaybeRefOrGetter<boolean>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Close when Escape is pressed and restore focus to the anchor.
|
|
97
|
+
* @default true
|
|
98
|
+
*/
|
|
99
|
+
dismissOnEscape?: MaybeRefOrGetter<boolean>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const DEFAULT_OFFSET = 8;
|
|
103
|
+
const DEFAULT_OPEN_DELAY = 150;
|
|
104
|
+
const DEFAULT_CLOSE_DELAY = 150;
|
|
105
|
+
|
|
106
|
+
function resolveOffset(value: number | AnchorFloatOffset | undefined) {
|
|
107
|
+
if (value === undefined) {
|
|
108
|
+
return offsetMiddleware(DEFAULT_OFFSET);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (typeof value === 'number') {
|
|
112
|
+
return offsetMiddleware(value);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return offsetMiddleware({
|
|
116
|
+
mainAxis: value.mainAxis ?? DEFAULT_OFFSET,
|
|
117
|
+
crossAxis: value.crossAxis ?? 0,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Anchors floating content to a reference element with configurable triggers,
|
|
123
|
+
* placement, and offset. Built on `@floating-ui/vue`.
|
|
124
|
+
*
|
|
125
|
+
* Prefer {@link UiFloatActions} for the standard component API. Use this composable
|
|
126
|
+
* when you need full control over markup, animation, or teleport targets.
|
|
127
|
+
*
|
|
128
|
+
* @param options - Trigger, placement, offset, and dismiss behaviour.
|
|
129
|
+
* @returns Refs, computed styles, open state, and event handlers to wire up
|
|
130
|
+
* anchor and floating elements manually.
|
|
131
|
+
* @example Hover trigger
|
|
132
|
+
* ```ts
|
|
133
|
+
* const {
|
|
134
|
+
* referenceRef,
|
|
135
|
+
* floatingRef,
|
|
136
|
+
* open,
|
|
137
|
+
* floatingStyles,
|
|
138
|
+
* isPositioned,
|
|
139
|
+
* onReferenceMouseEnter,
|
|
140
|
+
* onReferenceMouseLeave,
|
|
141
|
+
* onFloatingMouseEnter,
|
|
142
|
+
* onFloatingMouseLeave,
|
|
143
|
+
* } = useAnchorFloat({ trigger: 'hover', placement: 'top', offset: 8 });
|
|
144
|
+
* ```
|
|
145
|
+
* @example Controlled manual mode
|
|
146
|
+
* ```ts
|
|
147
|
+
* const open = ref(false);
|
|
148
|
+
* const { show, hide, referenceRef, floatingRef, floatingStyles } =
|
|
149
|
+
* useAnchorFloat({ trigger: 'manual', open });
|
|
150
|
+
* ```
|
|
151
|
+
* @category Composables
|
|
152
|
+
* @useCases custom floating panels, headless toolbar positioning
|
|
153
|
+
* @keywords float, anchor, floating-ui, composable
|
|
154
|
+
* @related UiFloatActions, UiFloatActionItems
|
|
155
|
+
*/
|
|
156
|
+
export function useAnchorFloat(options: UseAnchorFloatOptions = {}) {
|
|
157
|
+
const referenceRef = ref<HTMLElement | null>(null);
|
|
158
|
+
const floatingRef = ref<HTMLElement | null>(null);
|
|
159
|
+
|
|
160
|
+
const internalOpen = ref(false);
|
|
161
|
+
const open = options.open ?? internalOpen;
|
|
162
|
+
|
|
163
|
+
const trigger = computed(() => toValue(options.trigger) ?? 'hover');
|
|
164
|
+
const placementOption = computed(() => toValue(options.placement) ?? 'top');
|
|
165
|
+
const offsetOption = computed(() => toValue(options.offset));
|
|
166
|
+
const openDelay = computed(
|
|
167
|
+
() => toValue(options.openDelay) ?? DEFAULT_OPEN_DELAY,
|
|
168
|
+
);
|
|
169
|
+
const closeDelay = computed(
|
|
170
|
+
() => toValue(options.closeDelay) ?? DEFAULT_CLOSE_DELAY,
|
|
171
|
+
);
|
|
172
|
+
const dismissOnOutsideClick = computed(
|
|
173
|
+
() => toValue(options.dismissOnOutsideClick) ?? true,
|
|
174
|
+
);
|
|
175
|
+
const dismissOnEscape = computed(
|
|
176
|
+
() => toValue(options.dismissOnEscape) ?? true,
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
let openTimer: ReturnType<typeof setTimeout> | undefined;
|
|
180
|
+
let closeTimer: ReturnType<typeof setTimeout> | undefined;
|
|
181
|
+
let pointerOverAnchor = false;
|
|
182
|
+
let pointerOverFloating = false;
|
|
183
|
+
|
|
184
|
+
function supportsFocusOpen() {
|
|
185
|
+
return trigger.value === 'hover' || trigger.value === 'focus';
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function isFocusInsideFloating(target: EventTarget | null) {
|
|
189
|
+
if (!target || !(target instanceof Node)) return false;
|
|
190
|
+
return floatingRef.value?.contains(target) ?? false;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const { floatingStyles, placement, middlewareData, isPositioned } =
|
|
194
|
+
useFloating(referenceRef, floatingRef, {
|
|
195
|
+
placement: placementOption,
|
|
196
|
+
strategy: 'fixed',
|
|
197
|
+
// Position via top/left instead of `transform`. Enter animations (e.g.
|
|
198
|
+
// tailwindcss-animate's `animate-in`) animate the `transform` property,
|
|
199
|
+
// which would otherwise fight floating-ui's translate and make the panel
|
|
200
|
+
// appear to fly in from the viewport's top-left corner.
|
|
201
|
+
transform: false,
|
|
202
|
+
whileElementsMounted: autoUpdate,
|
|
203
|
+
middleware: computed(() => [
|
|
204
|
+
resolveOffset(offsetOption.value),
|
|
205
|
+
flip(),
|
|
206
|
+
shift({ padding: 8 }),
|
|
207
|
+
]),
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const side = computed(
|
|
211
|
+
() => placement.value.split('-')[0] as 'top' | 'bottom' | 'left' | 'right',
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
function clearOpenTimer() {
|
|
215
|
+
if (openTimer !== undefined) {
|
|
216
|
+
clearTimeout(openTimer);
|
|
217
|
+
openTimer = undefined;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function clearCloseTimer() {
|
|
222
|
+
if (closeTimer !== undefined) {
|
|
223
|
+
clearTimeout(closeTimer);
|
|
224
|
+
closeTimer = undefined;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function show() {
|
|
229
|
+
if (trigger.value === 'manual' && options.open === undefined) return;
|
|
230
|
+
clearCloseTimer();
|
|
231
|
+
open.value = true;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function hide(restoreFocus = true) {
|
|
235
|
+
clearOpenTimer();
|
|
236
|
+
open.value = false;
|
|
237
|
+
if (restoreFocus) referenceRef.value?.focus();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function toggle() {
|
|
241
|
+
if (open.value) {
|
|
242
|
+
hide();
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
show();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function scheduleShow() {
|
|
249
|
+
if (trigger.value !== 'hover') return;
|
|
250
|
+
clearCloseTimer();
|
|
251
|
+
clearOpenTimer();
|
|
252
|
+
openTimer = setTimeout(show, openDelay.value);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function scheduleHide() {
|
|
256
|
+
if (trigger.value !== 'hover') return;
|
|
257
|
+
clearOpenTimer();
|
|
258
|
+
clearCloseTimer();
|
|
259
|
+
closeTimer = setTimeout(() => {
|
|
260
|
+
if (!pointerOverAnchor && !pointerOverFloating) {
|
|
261
|
+
hide(false);
|
|
262
|
+
}
|
|
263
|
+
}, closeDelay.value);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function onReferenceMouseEnter() {
|
|
267
|
+
pointerOverAnchor = true;
|
|
268
|
+
scheduleShow();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function onReferenceMouseLeave() {
|
|
272
|
+
pointerOverAnchor = false;
|
|
273
|
+
scheduleHide();
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function onFloatingMouseEnter() {
|
|
277
|
+
pointerOverFloating = true;
|
|
278
|
+
clearCloseTimer();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function onFloatingMouseLeave() {
|
|
282
|
+
pointerOverFloating = false;
|
|
283
|
+
scheduleHide();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function onReferenceFocus() {
|
|
287
|
+
if (!supportsFocusOpen()) return;
|
|
288
|
+
clearCloseTimer();
|
|
289
|
+
show();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function onReferenceBlur(event: FocusEvent) {
|
|
293
|
+
if (!supportsFocusOpen()) return;
|
|
294
|
+
if (isFocusInsideFloating(event.relatedTarget)) return;
|
|
295
|
+
if (trigger.value === 'focus') {
|
|
296
|
+
hide(false);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
pointerOverAnchor = false;
|
|
300
|
+
scheduleHide();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function onFloatingFocusIn() {
|
|
304
|
+
if (!supportsFocusOpen()) return;
|
|
305
|
+
clearCloseTimer();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function onFloatingFocusOut(event: FocusEvent) {
|
|
309
|
+
if (!supportsFocusOpen()) return;
|
|
310
|
+
if (isFocusInsideFloating(event.relatedTarget)) return;
|
|
311
|
+
if (referenceRef.value?.contains(event.relatedTarget as Node)) return;
|
|
312
|
+
if (trigger.value === 'focus') {
|
|
313
|
+
hide(false);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
pointerOverFloating = false;
|
|
317
|
+
scheduleHide();
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function onReferenceClick(event: MouseEvent) {
|
|
321
|
+
if (trigger.value !== 'click') return;
|
|
322
|
+
event.stopPropagation();
|
|
323
|
+
toggle();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function onReferenceKeydown(event: KeyboardEvent) {
|
|
327
|
+
if (trigger.value !== 'click') return;
|
|
328
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
329
|
+
event.preventDefault();
|
|
330
|
+
toggle();
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function onDocumentPointerDown(event: PointerEvent) {
|
|
335
|
+
if (!open.value || !dismissOnOutsideClick.value) return;
|
|
336
|
+
|
|
337
|
+
const target = event.target as Node | null;
|
|
338
|
+
if (!target) return;
|
|
339
|
+
if (
|
|
340
|
+
referenceRef.value?.contains(target) ||
|
|
341
|
+
floatingRef.value?.contains(target)
|
|
342
|
+
) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
hide(false);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function onDocumentKeydown(event: KeyboardEvent) {
|
|
350
|
+
if (!open.value || !dismissOnEscape.value) return;
|
|
351
|
+
if (event.key !== 'Escape') return;
|
|
352
|
+
event.preventDefault();
|
|
353
|
+
hide();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Drives the native Popover API when the floating element opts in with a
|
|
358
|
+
* `popover` attribute. Showing it with `{ source }` promotes it to the top
|
|
359
|
+
* layer and, crucially, inserts it into the keyboard focus order right after
|
|
360
|
+
* the anchor — so Tab reaches the floating actions even though they render
|
|
361
|
+
* outside the anchor's DOM subtree. Older browsers (and jsdom) skip this and
|
|
362
|
+
* fall back to plain rendering.
|
|
363
|
+
* @param isOpen - Whether the floating panel should currently be shown.
|
|
364
|
+
*/
|
|
365
|
+
function syncPopover(isOpen: boolean | undefined) {
|
|
366
|
+
const el = floatingRef.value;
|
|
367
|
+
if (!supportsPopover || !el || !el.hasAttribute('popover')) return;
|
|
368
|
+
|
|
369
|
+
try {
|
|
370
|
+
const isShown = el.matches(':popover-open');
|
|
371
|
+
if (isOpen && !isShown) {
|
|
372
|
+
// The `source` option wires the anchor → popover focus-navigation order
|
|
373
|
+
// (Tab from the anchor moves into the panel). It is not yet in the DOM
|
|
374
|
+
// lib types, so cast to a signature that accepts it, and invoke via
|
|
375
|
+
// `call` so `this` stays bound to the popover element.
|
|
376
|
+
const source = referenceRef.value ?? undefined;
|
|
377
|
+
const showPopover = el.showPopover as (options?: object) => void;
|
|
378
|
+
showPopover.call(el, source ? { source } : undefined);
|
|
379
|
+
} else if (!isOpen && isShown) {
|
|
380
|
+
el.hidePopover();
|
|
381
|
+
}
|
|
382
|
+
} catch {
|
|
383
|
+
// Popover state is already in sync or the element detached mid-transition.
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
watch(open, (isOpen) => {
|
|
388
|
+
if (isOpen) {
|
|
389
|
+
document.addEventListener('pointerdown', onDocumentPointerDown, true);
|
|
390
|
+
document.addEventListener('keydown', onDocumentKeydown, true);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
document.removeEventListener('pointerdown', onDocumentPointerDown, true);
|
|
395
|
+
document.removeEventListener('keydown', onDocumentKeydown, true);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
// Run after DOM updates so the floating element is mounted (via `v-if`) before
|
|
399
|
+
// we call `showPopover`, and handle an initially-open (manual) panel on mount.
|
|
400
|
+
watch(open, (isOpen) => syncPopover(isOpen), { flush: 'post' });
|
|
401
|
+
onMounted(() => syncPopover(open.value));
|
|
402
|
+
|
|
403
|
+
onBeforeUnmount(() => {
|
|
404
|
+
clearOpenTimer();
|
|
405
|
+
clearCloseTimer();
|
|
406
|
+
document.removeEventListener('pointerdown', onDocumentPointerDown, true);
|
|
407
|
+
document.removeEventListener('keydown', onDocumentKeydown, true);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
return {
|
|
411
|
+
anchorRef: referenceRef,
|
|
412
|
+
referenceRef,
|
|
413
|
+
floatingRef,
|
|
414
|
+
open,
|
|
415
|
+
side,
|
|
416
|
+
placement,
|
|
417
|
+
middlewareData,
|
|
418
|
+
floatingStyles,
|
|
419
|
+
isPositioned,
|
|
420
|
+
show,
|
|
421
|
+
hide,
|
|
422
|
+
toggle,
|
|
423
|
+
onReferenceMouseEnter,
|
|
424
|
+
onReferenceMouseLeave,
|
|
425
|
+
onReferenceFocus,
|
|
426
|
+
onReferenceBlur,
|
|
427
|
+
onFloatingMouseEnter,
|
|
428
|
+
onFloatingMouseLeave,
|
|
429
|
+
onFloatingFocusIn,
|
|
430
|
+
onFloatingFocusOut,
|
|
431
|
+
onReferenceClick,
|
|
432
|
+
onReferenceKeydown,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** @deprecated Use `useAnchorFloat` instead. */
|
|
437
|
+
export const useFloat = useAnchorFloat;
|