@octanejs/floating-ui 0.1.32 → 0.1.33

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.
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var useMergeRefs_exports = {};
20
+ __export(useMergeRefs_exports, {
21
+ useMergeRefs: () => useMergeRefs
22
+ });
23
+ module.exports = __toCommonJS(useMergeRefs_exports);
24
+ var import_octane = require("octane");
25
+ var import_internal = require("./internal.cjs");
26
+ function useMergeRefs(...args) {
27
+ const [user, slot] = (0, import_internal.splitSlot)(args);
28
+ const refs = user[0] ?? [];
29
+ const cleanupRef = (0, import_octane.useRef)(void 0, (0, import_internal.subSlot)(slot, "cleanup"));
30
+ const refEffect = (0, import_octane.useCallback)(
31
+ (instance) => {
32
+ const cleanups = refs.map((ref) => {
33
+ if (ref == null) {
34
+ return;
35
+ }
36
+ if (typeof ref === "function") {
37
+ const refCallback = ref;
38
+ const refCleanup = refCallback(instance);
39
+ return typeof refCleanup === "function" ? refCleanup : () => {
40
+ refCallback(null);
41
+ };
42
+ }
43
+ ref.current = instance;
44
+ return () => {
45
+ ref.current = null;
46
+ };
47
+ });
48
+ return () => {
49
+ cleanups.forEach((refCleanup) => refCleanup == null ? void 0 : refCleanup());
50
+ };
51
+ },
52
+ refs,
53
+ (0, import_internal.subSlot)(slot, "effect")
54
+ );
55
+ return (0, import_octane.useMemo)(
56
+ () => {
57
+ if (refs.every((ref) => ref == null)) {
58
+ return null;
59
+ }
60
+ return (value) => {
61
+ if (cleanupRef.current) {
62
+ cleanupRef.current();
63
+ cleanupRef.current = void 0;
64
+ }
65
+ if (value != null) {
66
+ cleanupRef.current = refEffect(value);
67
+ }
68
+ };
69
+ },
70
+ refs,
71
+ (0, import_internal.subSlot)(slot, "memo")
72
+ );
73
+ }
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ useMergeRefs
77
+ });
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var useRole_exports = {};
20
+ __export(useRole_exports, {
21
+ useRole: () => useRole
22
+ });
23
+ module.exports = __toCommonJS(useRole_exports);
24
+ var import_octane = require("octane");
25
+ var import_internal = require("./internal.cjs");
26
+ var import_tree = require("./tree.cjs");
27
+ var import_useId = require("./useId.cjs");
28
+ var import_utils = require("./utils/index.cjs");
29
+ const componentRoleToAriaRoleMap = /* @__PURE__ */ new Map([
30
+ ["select", "listbox"],
31
+ ["combobox", "listbox"],
32
+ ["label", false]
33
+ ]);
34
+ function useRole(...args) {
35
+ const [user, slot] = (0, import_internal.splitSlot)(args);
36
+ const context = user[0];
37
+ const props = user[1] ?? {};
38
+ const open = context.open;
39
+ const elements = context.elements;
40
+ const defaultFloatingId = context.floatingId;
41
+ const enabled = props.enabled ?? true;
42
+ const role = props.role ?? "dialog";
43
+ const defaultReferenceId = (0, import_useId.useId)((0, import_internal.subSlot)(slot, "refid"));
44
+ const referenceId = elements.domReference?.id || defaultReferenceId;
45
+ const floatingId = (0, import_octane.useMemo)(
46
+ () => (0, import_utils.getFloatingFocusElement)(elements.floating)?.id || defaultFloatingId,
47
+ [elements.floating, defaultFloatingId],
48
+ (0, import_internal.subSlot)(slot, "m:fid")
49
+ );
50
+ const mapped = componentRoleToAriaRoleMap.get(role);
51
+ const ariaRole = mapped != null ? mapped : role;
52
+ const parentId = (0, import_tree.useFloatingParentNodeId)();
53
+ const isNested = parentId != null;
54
+ const reference = (0, import_octane.useMemo)(
55
+ () => {
56
+ if (ariaRole === "tooltip" || role === "label") {
57
+ return {
58
+ ["aria-" + (role === "label" ? "labelledby" : "describedby")]: open ? floatingId : void 0
59
+ };
60
+ }
61
+ return {
62
+ "aria-expanded": open ? "true" : "false",
63
+ "aria-haspopup": ariaRole === "alertdialog" ? "dialog" : ariaRole,
64
+ "aria-controls": open ? floatingId : void 0,
65
+ ...ariaRole === "listbox" && { role: "combobox" },
66
+ ...ariaRole === "menu" && { id: referenceId },
67
+ ...ariaRole === "menu" && isNested && { role: "menuitem" },
68
+ ...role === "select" && { "aria-autocomplete": "none" },
69
+ ...role === "combobox" && { "aria-autocomplete": "list" }
70
+ };
71
+ },
72
+ [ariaRole, floatingId, isNested, open, referenceId, role],
73
+ (0, import_internal.subSlot)(slot, "m:ref")
74
+ );
75
+ const floating = (0, import_octane.useMemo)(
76
+ () => {
77
+ const floatingProps = {
78
+ id: floatingId,
79
+ ...ariaRole && { role: ariaRole }
80
+ };
81
+ if (ariaRole === "tooltip" || role === "label") {
82
+ return floatingProps;
83
+ }
84
+ return {
85
+ ...floatingProps,
86
+ ...ariaRole === "menu" && { "aria-labelledby": referenceId }
87
+ };
88
+ },
89
+ [ariaRole, floatingId, referenceId, role],
90
+ (0, import_internal.subSlot)(slot, "m:flo")
91
+ );
92
+ const item = (0, import_octane.useCallback)(
93
+ (_ref) => {
94
+ const { active, selected } = _ref;
95
+ const commonProps = {
96
+ role: "option",
97
+ ...active && { id: floatingId + "-fui-option" }
98
+ };
99
+ switch (role) {
100
+ case "select":
101
+ case "combobox":
102
+ return { ...commonProps, "aria-selected": selected };
103
+ }
104
+ return {};
105
+ },
106
+ [floatingId, role],
107
+ (0, import_internal.subSlot)(slot, "cb:item")
108
+ );
109
+ return (0, import_octane.useMemo)(
110
+ () => enabled ? { reference, floating, item } : {},
111
+ [enabled, reference, floating, item],
112
+ (0, import_internal.subSlot)(slot, "m:ret")
113
+ );
114
+ }
115
+ // Annotate the CommonJS export names for ESM import in node:
116
+ 0 && (module.exports = {
117
+ useRole
118
+ });
@@ -0,0 +1,164 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var useTypeahead_exports = {};
20
+ __export(useTypeahead_exports, {
21
+ useTypeahead: () => useTypeahead
22
+ });
23
+ module.exports = __toCommonJS(useTypeahead_exports);
24
+ var import_octane = require("octane");
25
+ var import_internal = require("./internal.cjs");
26
+ var import_utils = require("./utils/index.cjs");
27
+ function useTypeahead(...args) {
28
+ const [user, slot] = (0, import_internal.splitSlot)(args);
29
+ const context = user[0];
30
+ const props = user[1] ?? {};
31
+ const open = context.open;
32
+ const dataRef = context.dataRef;
33
+ const listRef = props.listRef;
34
+ const activeIndex = props.activeIndex;
35
+ const unstableOnMatch = props.onMatch;
36
+ const unstableOnTypingChange = props.onTypingChange;
37
+ const enabled = props.enabled ?? true;
38
+ const findMatch = props.findMatch ?? null;
39
+ const resetMs = props.resetMs ?? 750;
40
+ const ignoreKeys = props.ignoreKeys ?? [];
41
+ const selectedIndex = props.selectedIndex ?? null;
42
+ const timeoutIdRef = (0, import_octane.useRef)(-1, (0, import_internal.subSlot)(slot, "timeout"));
43
+ const stringRef = (0, import_octane.useRef)("", (0, import_internal.subSlot)(slot, "string"));
44
+ const prevIndexRef = (0, import_octane.useRef)(
45
+ (selectedIndex != null ? selectedIndex : activeIndex) ?? -1,
46
+ (0, import_internal.subSlot)(slot, "prev")
47
+ );
48
+ const matchIndexRef = (0, import_octane.useRef)(null, (0, import_internal.subSlot)(slot, "match"));
49
+ const onMatch = (0, import_utils.useEffectEvent)(unstableOnMatch, (0, import_internal.subSlot)(slot, "onmatch"));
50
+ const onTypingChange = (0, import_utils.useEffectEvent)(unstableOnTypingChange, (0, import_internal.subSlot)(slot, "ontyping"));
51
+ const findMatchRef = (0, import_utils.useLatestRef)(findMatch, (0, import_internal.subSlot)(slot, "find"));
52
+ const ignoreKeysRef = (0, import_utils.useLatestRef)(ignoreKeys, (0, import_internal.subSlot)(slot, "ignore"));
53
+ (0, import_utils.useModernLayoutEffect)(
54
+ () => {
55
+ if (open) {
56
+ (0, import_utils.clearTimeoutIfSet)(timeoutIdRef);
57
+ matchIndexRef.current = null;
58
+ stringRef.current = "";
59
+ }
60
+ },
61
+ [open],
62
+ (0, import_internal.subSlot)(slot, "e:open")
63
+ );
64
+ (0, import_utils.useModernLayoutEffect)(
65
+ () => {
66
+ if (open && stringRef.current === "") {
67
+ prevIndexRef.current = (selectedIndex != null ? selectedIndex : activeIndex) ?? -1;
68
+ }
69
+ },
70
+ [open, selectedIndex, activeIndex],
71
+ (0, import_internal.subSlot)(slot, "e:sync")
72
+ );
73
+ const setTypingChange = (0, import_utils.useEffectEvent)(
74
+ (value) => {
75
+ if (value) {
76
+ if (!dataRef.current.typing) {
77
+ dataRef.current.typing = value;
78
+ onTypingChange(value);
79
+ }
80
+ } else {
81
+ if (dataRef.current.typing) {
82
+ dataRef.current.typing = value;
83
+ onTypingChange(value);
84
+ }
85
+ }
86
+ },
87
+ (0, import_internal.subSlot)(slot, "settyping")
88
+ );
89
+ const onKeyDown = (0, import_utils.useEffectEvent)(
90
+ (event) => {
91
+ function getMatchingIndex(list, orderedList, string) {
92
+ const str = findMatchRef.current ? findMatchRef.current(orderedList, string) : orderedList.find(
93
+ (text) => text?.toLocaleLowerCase().indexOf(string.toLocaleLowerCase()) === 0
94
+ );
95
+ return str ? list.indexOf(str) : -1;
96
+ }
97
+ const listContent = listRef.current;
98
+ if (stringRef.current.length > 0 && stringRef.current[0] !== " ") {
99
+ if (getMatchingIndex(listContent, listContent, stringRef.current) === -1) {
100
+ setTypingChange(false);
101
+ } else if (event.key === " ") {
102
+ (0, import_utils.stopEvent)(event);
103
+ }
104
+ }
105
+ if (listContent == null || ignoreKeysRef.current.includes(event.key) || event.key.length !== 1 || event.ctrlKey || event.metaKey || event.altKey) {
106
+ return;
107
+ }
108
+ if (open && event.key !== " ") {
109
+ (0, import_utils.stopEvent)(event);
110
+ setTypingChange(true);
111
+ }
112
+ const allowRapidSuccessionOfFirstLetter = listContent.every(
113
+ (text) => text ? text[0]?.toLocaleLowerCase() !== text[1]?.toLocaleLowerCase() : true
114
+ );
115
+ if (allowRapidSuccessionOfFirstLetter && stringRef.current === event.key) {
116
+ stringRef.current = "";
117
+ prevIndexRef.current = matchIndexRef.current;
118
+ }
119
+ stringRef.current += event.key;
120
+ (0, import_utils.clearTimeoutIfSet)(timeoutIdRef);
121
+ timeoutIdRef.current = window.setTimeout(() => {
122
+ stringRef.current = "";
123
+ prevIndexRef.current = matchIndexRef.current;
124
+ setTypingChange(false);
125
+ }, resetMs);
126
+ const prevIndex = prevIndexRef.current;
127
+ const index = getMatchingIndex(
128
+ listContent,
129
+ [...listContent.slice((prevIndex || 0) + 1), ...listContent.slice(0, (prevIndex || 0) + 1)],
130
+ stringRef.current
131
+ );
132
+ if (index !== -1) {
133
+ onMatch(index);
134
+ matchIndexRef.current = index;
135
+ } else if (event.key !== " ") {
136
+ stringRef.current = "";
137
+ setTypingChange(false);
138
+ }
139
+ },
140
+ (0, import_internal.subSlot)(slot, "keydown")
141
+ );
142
+ const reference = (0, import_octane.useMemo)(() => ({ onKeyDown }), [onKeyDown], (0, import_internal.subSlot)(slot, "m:ref"));
143
+ const floating = (0, import_octane.useMemo)(
144
+ () => ({
145
+ onKeyDown,
146
+ onKeyUp(event) {
147
+ if (event.key === " ") {
148
+ setTypingChange(false);
149
+ }
150
+ }
151
+ }),
152
+ [onKeyDown, setTypingChange],
153
+ (0, import_internal.subSlot)(slot, "m:flo")
154
+ );
155
+ return (0, import_octane.useMemo)(
156
+ () => enabled ? { reference, floating } : {},
157
+ [enabled, reference, floating],
158
+ (0, import_internal.subSlot)(slot, "m:ret")
159
+ );
160
+ }
161
+ // Annotate the CommonJS export names for ESM import in node:
162
+ 0 && (module.exports = {
163
+ useTypeahead
164
+ });