@sit-onyx/headless 1.0.0-beta.0 → 1.0.0-beta.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/package.json +1 -1
- package/src/composables/comboBox/TestCombobox.ct.tsx +1 -1
- package/src/composables/comboBox/createComboBox.ts +3 -3
- package/src/composables/helpers/useGlobalListener.spec.ts +93 -0
- package/src/composables/helpers/useGlobalListener.ts +64 -0
- package/src/composables/helpers/useOutsideClick.ts +34 -0
- package/src/composables/{typeAhead.spec.ts → helpers/useTypeAhead.spec.ts} +1 -1
- package/src/composables/{typeAhead.ts → helpers/useTypeAhead.ts} +2 -2
- package/src/composables/listbox/TestListbox.ct.tsx +1 -1
- package/src/composables/listbox/createListbox.ts +1 -1
- package/src/composables/menuButton/TestMenuButton.ct.tsx +1 -1
- package/src/composables/menuButton/TestMenuButton.vue +1 -1
- package/src/composables/menuButton/createMenuButton.ts +1 -1
- package/src/composables/tooltip/createTooltip.ts +6 -2
- package/src/index.ts +1 -0
- package/src/playwright.ts +3 -3
- package/src/utils/builder.ts +8 -6
- package/src/utils/math.spec.ts +14 -0
- package/src/utils/math.ts +6 -0
- package/src/utils/vitest.ts +36 -0
- package/src/composables/outsideClick.ts +0 -52
- /package/src/composables/comboBox/{createComboBox.ct.ts → createComboBox.testing.ts} +0 -0
- /package/src/composables/listbox/{createListbox.ct.ts → createListbox.testing.ts} +0 -0
- /package/src/composables/menuButton/{createMenuButton.ct.ts → createMenuButton.testing.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { test } from "@playwright/experimental-ct-vue";
|
|
2
|
-
import { comboboxSelectOnlyTesting, comboboxTesting } from "./createComboBox.
|
|
2
|
+
import { comboboxSelectOnlyTesting, comboboxTesting } from "./createComboBox.testing";
|
|
3
3
|
import SelectOnlyCombobox from "./SelectOnlyCombobox.vue";
|
|
4
4
|
import TestCombobox from "./TestCombobox.vue";
|
|
5
5
|
|
|
@@ -2,13 +2,13 @@ import { computed, unref, type MaybeRef, type Ref } from "vue";
|
|
|
2
2
|
import { createBuilder } from "../../utils/builder";
|
|
3
3
|
import { createId } from "../../utils/id";
|
|
4
4
|
import { isPrintableCharacter, wasKeyPressed, type PressedKey } from "../../utils/keyboard";
|
|
5
|
+
import { useOutsideClick } from "../helpers/useOutsideClick";
|
|
6
|
+
import { useTypeAhead } from "../helpers/useTypeAhead";
|
|
5
7
|
import {
|
|
6
8
|
createListbox,
|
|
7
9
|
type CreateListboxOptions,
|
|
8
10
|
type ListboxValue,
|
|
9
11
|
} from "../listbox/createListbox";
|
|
10
|
-
import { useOutsideClick } from "../outsideClick";
|
|
11
|
-
import { useTypeAhead } from "../typeAhead";
|
|
12
12
|
|
|
13
13
|
export type ComboboxAutoComplete = "none" | "list" | "both";
|
|
14
14
|
|
|
@@ -225,7 +225,7 @@ export const createComboBox = createBuilder(
|
|
|
225
225
|
});
|
|
226
226
|
|
|
227
227
|
useOutsideClick({
|
|
228
|
-
|
|
228
|
+
element: templateRef,
|
|
229
229
|
onOutsideClick() {
|
|
230
230
|
if (!isExpanded.value) return;
|
|
231
231
|
onToggle?.(true);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { ref, type Ref } from "vue";
|
|
3
|
+
import { mockVueLifecycle } from "../../utils/vitest";
|
|
4
|
+
import { useGlobalEventListener } from "./useGlobalListener";
|
|
5
|
+
|
|
6
|
+
let unmount: () => Promise<void> | undefined;
|
|
7
|
+
|
|
8
|
+
describe("useGlobalEventListener", () => {
|
|
9
|
+
let target: Ref<HTMLButtonElement>;
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
vi.clearAllMocks();
|
|
13
|
+
unmount = mockVueLifecycle();
|
|
14
|
+
target = ref(document.createElement("button"));
|
|
15
|
+
document.body.appendChild(target.value);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should be defined", () => {
|
|
19
|
+
expect(useGlobalEventListener).toBeDefined();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should listen to global events", () => {
|
|
23
|
+
// ARRANGE
|
|
24
|
+
const listener = vi.fn();
|
|
25
|
+
useGlobalEventListener({ type: "click", listener });
|
|
26
|
+
// ACT
|
|
27
|
+
const event = new MouseEvent("click", { bubbles: true });
|
|
28
|
+
target.value.dispatchEvent(event);
|
|
29
|
+
// ASSERT
|
|
30
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
31
|
+
expect(listener).toBeCalledWith(event);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should stop to listen to global events after unmount", async () => {
|
|
35
|
+
// ARRANGE
|
|
36
|
+
const listener = vi.fn();
|
|
37
|
+
useGlobalEventListener({ type: "click", listener });
|
|
38
|
+
// ACT
|
|
39
|
+
await unmount();
|
|
40
|
+
expect(listener).toHaveBeenCalledTimes(0);
|
|
41
|
+
target.value.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
42
|
+
// ASSERT
|
|
43
|
+
expect(listener).toHaveBeenCalledTimes(0);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should allow for multiple of the same listener types", async () => {
|
|
47
|
+
// ARRANGE
|
|
48
|
+
vi.useFakeTimers();
|
|
49
|
+
const listener = vi.fn();
|
|
50
|
+
const disabled = ref(false);
|
|
51
|
+
const listener2 = vi.fn();
|
|
52
|
+
useGlobalEventListener({ type: "click", listener, disabled });
|
|
53
|
+
useGlobalEventListener({ type: "click", listener: listener2 });
|
|
54
|
+
// ACT
|
|
55
|
+
target.value.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
56
|
+
// ASSERT
|
|
57
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
58
|
+
expect(listener2).toHaveBeenCalledTimes(1);
|
|
59
|
+
// ACT
|
|
60
|
+
disabled.value = true;
|
|
61
|
+
await vi.runAllTimersAsync();
|
|
62
|
+
// ACT
|
|
63
|
+
target.value.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
64
|
+
// ASSERT
|
|
65
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
66
|
+
expect(listener2).toHaveBeenCalledTimes(2);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should not listen to events when disabled", async () => {
|
|
70
|
+
// ARRANGE
|
|
71
|
+
vi.useFakeTimers();
|
|
72
|
+
const disabled = ref(false);
|
|
73
|
+
const listener = vi.fn();
|
|
74
|
+
useGlobalEventListener({ type: "click", listener, disabled });
|
|
75
|
+
// ACT
|
|
76
|
+
await vi.runAllTimersAsync();
|
|
77
|
+
target.value.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
78
|
+
// ASSERT
|
|
79
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
80
|
+
// ACT
|
|
81
|
+
disabled.value = true;
|
|
82
|
+
await vi.runAllTimersAsync();
|
|
83
|
+
target.value.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
84
|
+
// ASSERT
|
|
85
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
86
|
+
// ACT
|
|
87
|
+
disabled.value = false;
|
|
88
|
+
await vi.runAllTimersAsync();
|
|
89
|
+
target.value.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
|
90
|
+
// ASSERT
|
|
91
|
+
expect(listener).toHaveBeenCalledTimes(2);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { onBeforeMount, onBeforeUnmount, reactive, watchEffect, type Ref } from "vue";
|
|
2
|
+
|
|
3
|
+
type DocumentEventType = keyof DocumentEventMap;
|
|
4
|
+
type GlobalListener<K extends DocumentEventType = DocumentEventType> = (
|
|
5
|
+
event: DocumentEventMap[K],
|
|
6
|
+
) => void;
|
|
7
|
+
|
|
8
|
+
export type UseGlobalEventListenerOptions<K extends DocumentEventType> = {
|
|
9
|
+
type: K;
|
|
10
|
+
listener: GlobalListener<K>;
|
|
11
|
+
disabled?: Ref<boolean>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const GLOBAL_LISTENERS = reactive(new Map<DocumentEventType, Set<GlobalListener>>());
|
|
15
|
+
|
|
16
|
+
const updateRemainingListeners = (type: DocumentEventType, remaining?: Set<GlobalListener>) => {
|
|
17
|
+
if (remaining?.size) {
|
|
18
|
+
GLOBAL_LISTENERS.set(type, remaining);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
GLOBAL_LISTENERS.delete(type);
|
|
22
|
+
document.removeEventListener(type, GLOBAL_HANDLER);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const removeGlobalListener = <K extends DocumentEventType>(
|
|
26
|
+
type: K,
|
|
27
|
+
listener: GlobalListener<K>,
|
|
28
|
+
) => {
|
|
29
|
+
const globalListener = GLOBAL_LISTENERS.get(type);
|
|
30
|
+
globalListener?.delete(listener as GlobalListener);
|
|
31
|
+
|
|
32
|
+
updateRemainingListeners(type, globalListener);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const addGlobalListener = <K extends DocumentEventType>(type: K, listener: GlobalListener<K>) => {
|
|
36
|
+
const globalListener = GLOBAL_LISTENERS.get(type) ?? new Set();
|
|
37
|
+
globalListener.add(listener as GlobalListener);
|
|
38
|
+
GLOBAL_LISTENERS.set(type, globalListener);
|
|
39
|
+
|
|
40
|
+
document.addEventListener(type, GLOBAL_HANDLER);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A single and unique function for all event types.
|
|
45
|
+
* We use the fact that `addEventListener` and `removeEventListener` are idempotent when called with the same function reference.
|
|
46
|
+
*/
|
|
47
|
+
const GLOBAL_HANDLER = (event: Event) => {
|
|
48
|
+
const type = event.type as DocumentEventType;
|
|
49
|
+
GLOBAL_LISTENERS.get(type)?.forEach((cb) => cb(event));
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const useGlobalEventListener = <K extends DocumentEventType>({
|
|
53
|
+
type,
|
|
54
|
+
listener,
|
|
55
|
+
disabled,
|
|
56
|
+
}: UseGlobalEventListenerOptions<K>) => {
|
|
57
|
+
onBeforeMount(() =>
|
|
58
|
+
watchEffect(() =>
|
|
59
|
+
disabled?.value ? removeGlobalListener(type, listener) : addGlobalListener(type, listener),
|
|
60
|
+
),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
onBeforeUnmount(() => removeGlobalListener(type, listener));
|
|
64
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Ref } from "vue";
|
|
2
|
+
import { useGlobalEventListener } from "./useGlobalListener";
|
|
3
|
+
|
|
4
|
+
export type UseOutsideClickOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* HTML element of the component where clicks should be ignored
|
|
7
|
+
*/
|
|
8
|
+
element: Ref<HTMLElement | undefined>;
|
|
9
|
+
/**
|
|
10
|
+
* Callback when an outside click occurred.
|
|
11
|
+
*/
|
|
12
|
+
onOutsideClick: () => void;
|
|
13
|
+
/**
|
|
14
|
+
* If `true`, event listeners will be removed and no outside clicks will be captured.
|
|
15
|
+
*/
|
|
16
|
+
disabled?: Ref<boolean>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Composable for listening to click events that occur outside of a component.
|
|
21
|
+
* Useful to e.g. close flyouts or tooltips.
|
|
22
|
+
*/
|
|
23
|
+
export const useOutsideClick = ({ element, onOutsideClick, disabled }: UseOutsideClickOptions) => {
|
|
24
|
+
/**
|
|
25
|
+
* Document click handle that closes then tooltip when clicked outside.
|
|
26
|
+
* Should only be called when trigger is "click".
|
|
27
|
+
*/
|
|
28
|
+
const listener = ({ target }: MouseEvent) => {
|
|
29
|
+
const isOutsideClick = !element.value?.contains(target as HTMLElement);
|
|
30
|
+
if (isOutsideClick) onOutsideClick();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
useGlobalEventListener({ type: "click", listener, disabled });
|
|
34
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isPrintableCharacter } from "
|
|
2
|
-
import { debounce } from "
|
|
1
|
+
import { isPrintableCharacter } from "../../utils/keyboard";
|
|
2
|
+
import { debounce } from "../../utils/timer";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Enhances typeAhead to combine multiple inputs in quick succession and filter out non-printable characters.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { test } from "@playwright/experimental-ct-vue";
|
|
2
2
|
import TestListbox from "./TestListbox.vue";
|
|
3
|
-
import { listboxTesting } from "./createListbox.
|
|
3
|
+
import { listboxTesting } from "./createListbox.testing";
|
|
4
4
|
|
|
5
5
|
test("listbox", async ({ mount, page }) => {
|
|
6
6
|
await mount(<TestListbox />);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { computed, ref, unref, watchEffect, type MaybeRef, type Ref } from "vue";
|
|
2
2
|
import { createId } from "../..";
|
|
3
3
|
import { createBuilder, type HeadlessElementAttributes } from "../../utils/builder";
|
|
4
|
-
import { useTypeAhead } from "../
|
|
4
|
+
import { useTypeAhead } from "../helpers/useTypeAhead";
|
|
5
5
|
|
|
6
6
|
export type ListboxValue = string | number | boolean;
|
|
7
7
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { test } from "@playwright/experimental-ct-vue";
|
|
2
|
-
import { menuButtonTesting } from "./createMenuButton.
|
|
2
|
+
import { menuButtonTesting } from "./createMenuButton.testing";
|
|
3
3
|
import TestMenuButton from "./TestMenuButton.vue";
|
|
4
4
|
|
|
5
5
|
test("menuButton", async ({ mount, page }) => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { computed, onBeforeMount, onBeforeUnmount, ref, unref, type MaybeRef } from "vue";
|
|
2
2
|
import { createId } from "../..";
|
|
3
3
|
import { createBuilder } from "../../utils/builder";
|
|
4
|
-
import { useOutsideClick } from "../
|
|
4
|
+
import { useOutsideClick } from "../helpers/useOutsideClick";
|
|
5
5
|
|
|
6
6
|
export type CreateTooltipOptions = {
|
|
7
7
|
open: MaybeRef<TooltipOpen>;
|
|
@@ -22,6 +22,7 @@ export const TOOLTIP_TRIGGERS = ["hover", "click"] as const;
|
|
|
22
22
|
export type TooltipTrigger = (typeof TOOLTIP_TRIGGERS)[number];
|
|
23
23
|
|
|
24
24
|
export const createTooltip = createBuilder((options: CreateTooltipOptions) => {
|
|
25
|
+
const rootRef = ref<HTMLElement>();
|
|
25
26
|
const tooltipId = createId("tooltip");
|
|
26
27
|
const _isVisible = ref(false);
|
|
27
28
|
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -87,7 +88,7 @@ export const createTooltip = createBuilder((options: CreateTooltipOptions) => {
|
|
|
87
88
|
|
|
88
89
|
// close tooltip on outside click
|
|
89
90
|
useOutsideClick({
|
|
90
|
-
|
|
91
|
+
element: rootRef,
|
|
91
92
|
onOutsideClick: () => (_isVisible.value = false),
|
|
92
93
|
disabled: computed(() => openType.value !== "click"),
|
|
93
94
|
});
|
|
@@ -106,6 +107,9 @@ export const createTooltip = createBuilder((options: CreateTooltipOptions) => {
|
|
|
106
107
|
|
|
107
108
|
return {
|
|
108
109
|
elements: {
|
|
110
|
+
root: {
|
|
111
|
+
ref: rootRef,
|
|
112
|
+
},
|
|
109
113
|
trigger: computed(() => ({
|
|
110
114
|
"aria-describedby": tooltipId,
|
|
111
115
|
onClick: openType.value === "click" ? handleClick : undefined,
|
package/src/index.ts
CHANGED
package/src/playwright.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./composables/comboBox/createComboBox.
|
|
2
|
-
export * from "./composables/listbox/createListbox.
|
|
3
|
-
export * from "./composables/menuButton/createMenuButton.
|
|
1
|
+
export * from "./composables/comboBox/createComboBox.testing";
|
|
2
|
+
export * from "./composables/listbox/createListbox.testing";
|
|
3
|
+
export * from "./composables/menuButton/createMenuButton.testing";
|
package/src/utils/builder.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import type { ComputedRef, HtmlHTMLAttributes, Ref } from "vue";
|
|
1
|
+
import type { ComputedRef, HtmlHTMLAttributes, Ref, VNodeRef } from "vue";
|
|
2
2
|
import type { IfDefined } from "./types";
|
|
3
3
|
|
|
4
|
+
export type ElementAttributes = HtmlHTMLAttributes & { ref?: VNodeRef };
|
|
5
|
+
|
|
4
6
|
export type IteratedHeadlessElementFunc<T extends Record<string, unknown>> = (
|
|
5
7
|
opts: T,
|
|
6
|
-
) =>
|
|
8
|
+
) => ElementAttributes;
|
|
7
9
|
|
|
8
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
-
export type HeadlessElementAttributes =
|
|
11
|
+
export type HeadlessElementAttributes = ElementAttributes | IteratedHeadlessElementFunc<any>;
|
|
10
12
|
|
|
11
13
|
export type HeadlessElements = Record<
|
|
12
14
|
string,
|
|
@@ -28,10 +30,10 @@ export type HeadlessComposable<
|
|
|
28
30
|
* We use this identity function to ensure the correct typings of the headless composables
|
|
29
31
|
*/
|
|
30
32
|
export const createBuilder = <
|
|
31
|
-
|
|
32
|
-
Elements extends HeadlessElements,
|
|
33
|
+
Args extends unknown[] = unknown[],
|
|
34
|
+
Elements extends HeadlessElements = HeadlessElements,
|
|
33
35
|
State extends HeadlessState | undefined = undefined,
|
|
34
36
|
Internals extends object | undefined = undefined,
|
|
35
37
|
>(
|
|
36
|
-
builder: (
|
|
38
|
+
builder: (...args: Args) => HeadlessComposable<Elements, State, Internals>,
|
|
37
39
|
) => builder;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { MathUtils } from "./math";
|
|
3
|
+
|
|
4
|
+
describe("MathUtils.clamp", () => {
|
|
5
|
+
test.each([
|
|
6
|
+
{ number: 1, min: 1, max: 2, result: 1 },
|
|
7
|
+
{ number: 1, min: 2, max: 2, result: 2 },
|
|
8
|
+
{ number: 1, min: 0, max: 0, result: 0 },
|
|
9
|
+
{ number: 1, min: 1, max: 1, result: 1 },
|
|
10
|
+
])(
|
|
11
|
+
"should return $result for key number:$number min:$min max:$max",
|
|
12
|
+
({ number, min, max, result }) => expect(MathUtils.clamp(number, min, max)).toBe(result),
|
|
13
|
+
);
|
|
14
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { vi, type Awaitable } from "vitest";
|
|
2
|
+
|
|
3
|
+
type Callback = () => Awaitable<void>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Mocks the following vue lifecycle functions:
|
|
7
|
+
* - onBeforeMount
|
|
8
|
+
* - onMounted
|
|
9
|
+
* - onBeforeUnmount
|
|
10
|
+
* - onUnmounted
|
|
11
|
+
*
|
|
12
|
+
* `onBeforeMount` and `onMounted` callbacks are executed immediately.
|
|
13
|
+
* `onBeforeUnmount` and `onUnmounted` are executed when the returned callback is run.
|
|
14
|
+
* @returns a callback to trigger the run of `onBeforeUnmount` and `onUnmounted`
|
|
15
|
+
*/
|
|
16
|
+
export const mockVueLifecycle = () => {
|
|
17
|
+
const { callbacks } = vi.hoisted(() => ({
|
|
18
|
+
callbacks: {
|
|
19
|
+
onBeforeUnmountedCb: null as Callback | null,
|
|
20
|
+
onUnmountedCb: null as Callback | null,
|
|
21
|
+
},
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
vi.mock("vue", async (original) => ({
|
|
25
|
+
...((await original()) as typeof import("vue")),
|
|
26
|
+
onBeforeMount: vi.fn((cb: Callback) => cb()),
|
|
27
|
+
onMounted: vi.fn((cb: Callback) => cb()),
|
|
28
|
+
onBeforeUnmount: vi.fn((cb: Callback) => (callbacks.onBeforeUnmountedCb = cb)),
|
|
29
|
+
onUnmounted: vi.fn((cb: Callback) => (callbacks.onUnmountedCb = cb)),
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
return async () => {
|
|
33
|
+
await callbacks.onBeforeUnmountedCb?.();
|
|
34
|
+
await callbacks.onUnmountedCb?.();
|
|
35
|
+
};
|
|
36
|
+
};
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { onBeforeMount, onBeforeUnmount, watchEffect, type Ref } from "vue";
|
|
2
|
-
|
|
3
|
-
export type UseOutsideClickOptions = {
|
|
4
|
-
/**
|
|
5
|
-
* Function that returns the HTML element of the component where outside clicks should be listened to.
|
|
6
|
-
*/
|
|
7
|
-
queryComponent: () => ReturnType<typeof document.querySelector> | undefined;
|
|
8
|
-
/**
|
|
9
|
-
* Callback when an outside click occurred.
|
|
10
|
-
*/
|
|
11
|
-
onOutsideClick: () => void;
|
|
12
|
-
/**
|
|
13
|
-
* If `true`, event listeners will be removed and no outside clicks will be captured.
|
|
14
|
-
*/
|
|
15
|
-
disabled?: Ref<boolean>;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Composable for listening to click events that occur outside of a component.
|
|
20
|
-
* Useful to e.g. close flyouts or tooltips.
|
|
21
|
-
*/
|
|
22
|
-
export const useOutsideClick = (options: UseOutsideClickOptions) => {
|
|
23
|
-
/**
|
|
24
|
-
* Document click handle that closes then tooltip when clicked outside.
|
|
25
|
-
* Should only be called when trigger is "click".
|
|
26
|
-
*/
|
|
27
|
-
const handleDocumentClick = (event: MouseEvent) => {
|
|
28
|
-
const component = options.queryComponent();
|
|
29
|
-
if (!component || !(event.target instanceof Node)) return;
|
|
30
|
-
|
|
31
|
-
const isOutsideClick = !component.contains(event.target);
|
|
32
|
-
if (isOutsideClick) options.onOutsideClick();
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// add global document event listeners only on/before mounted to also work in server side rendering
|
|
36
|
-
onBeforeMount(() => {
|
|
37
|
-
watchEffect(() => {
|
|
38
|
-
if (options.disabled?.value) {
|
|
39
|
-
document.removeEventListener("click", handleDocumentClick);
|
|
40
|
-
} else {
|
|
41
|
-
document.addEventListener("click", handleDocumentClick);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Clean up global event listeners to prevent dangling events.
|
|
48
|
-
*/
|
|
49
|
-
onBeforeUnmount(() => {
|
|
50
|
-
document.removeEventListener("click", handleDocumentClick);
|
|
51
|
-
});
|
|
52
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|