@auth0/quantum-product 2.10.2 → 2.10.3

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.
Files changed (55) hide show
  1. package/command-palette/command-palette-classes.d.ts +5 -0
  2. package/command-palette/command-palette-classes.js +34 -0
  3. package/command-palette/command-palette-context.d.ts +8 -0
  4. package/command-palette/command-palette-context.js +61 -0
  5. package/command-palette/command-palette-dialog.d.ts +3 -0
  6. package/command-palette/command-palette-dialog.js +112 -0
  7. package/command-palette/command-palette-empty.d.ts +3 -0
  8. package/command-palette/command-palette-empty.js +90 -0
  9. package/command-palette/command-palette-footer.d.ts +4 -0
  10. package/command-palette/command-palette-footer.js +111 -0
  11. package/command-palette/command-palette-group.d.ts +3 -0
  12. package/command-palette/command-palette-group.js +118 -0
  13. package/command-palette/command-palette-input.d.ts +3 -0
  14. package/command-palette/command-palette-input.js +144 -0
  15. package/command-palette/command-palette-item.d.ts +3 -0
  16. package/command-palette/command-palette-item.js +235 -0
  17. package/command-palette/command-palette-list.d.ts +3 -0
  18. package/command-palette/command-palette-list.js +101 -0
  19. package/command-palette/command-palette-loading.d.ts +3 -0
  20. package/command-palette/command-palette-loading.js +89 -0
  21. package/command-palette/command-palette-score.d.ts +1 -0
  22. package/command-palette/command-palette-score.js +47 -0
  23. package/command-palette/command-palette-separator.d.ts +3 -0
  24. package/command-palette/command-palette-separator.js +86 -0
  25. package/command-palette/command-palette-tabs.d.ts +4 -0
  26. package/command-palette/command-palette-tabs.js +146 -0
  27. package/command-palette/command-palette-types.d.ts +121 -0
  28. package/command-palette/command-palette-types.js +2 -0
  29. package/command-palette/command-palette.d.ts +5 -0
  30. package/command-palette/command-palette.js +474 -0
  31. package/command-palette/index.d.ts +27 -0
  32. package/command-palette/index.js +36 -0
  33. package/command-palette/use-command-palette-shortcut.d.ts +6 -0
  34. package/command-palette/use-command-palette-shortcut.js +65 -0
  35. package/esm/command-palette/command-palette-classes.js +30 -0
  36. package/esm/command-palette/command-palette-context.js +22 -0
  37. package/esm/command-palette/command-palette-dialog.js +73 -0
  38. package/esm/command-palette/command-palette-empty.js +51 -0
  39. package/esm/command-palette/command-palette-footer.js +72 -0
  40. package/esm/command-palette/command-palette-group.js +79 -0
  41. package/esm/command-palette/command-palette-input.js +105 -0
  42. package/esm/command-palette/command-palette-item.js +196 -0
  43. package/esm/command-palette/command-palette-list.js +62 -0
  44. package/esm/command-palette/command-palette-loading.js +50 -0
  45. package/esm/command-palette/command-palette-score.js +44 -0
  46. package/esm/command-palette/command-palette-separator.js +47 -0
  47. package/esm/command-palette/command-palette-tabs.js +107 -0
  48. package/esm/command-palette/command-palette-types.js +1 -0
  49. package/esm/command-palette/command-palette.js +435 -0
  50. package/esm/command-palette/index.js +14 -0
  51. package/esm/command-palette/use-command-palette-shortcut.js +29 -0
  52. package/esm/index.js +1 -0
  53. package/index.d.ts +1 -0
  54. package/index.js +1 -0
  55. package/package.json +1 -1
@@ -0,0 +1,50 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __rest = (this && this.__rest) || function (s, e) {
13
+ var t = {};
14
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
15
+ t[p] = s[p];
16
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
17
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
18
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
19
+ t[p[i]] = s[p[i]];
20
+ }
21
+ return t;
22
+ };
23
+ import * as React from 'react';
24
+ import { Spinner } from '../spinner';
25
+ import { styled } from '../styled';
26
+ import clsx from '../utils/clsx';
27
+ import { commandPaletteClasses, commandPaletteComponentName } from './command-palette-classes';
28
+ var Root = styled('div', {
29
+ name: commandPaletteComponentName,
30
+ slot: 'Loading',
31
+ })(function (_a) {
32
+ var theme = _a.theme;
33
+ return ({
34
+ display: 'flex',
35
+ flexDirection: 'row',
36
+ alignItems: 'center',
37
+ width: '100%',
38
+ gap: theme.spacing(1.5),
39
+ padding: theme.spacing(2),
40
+ color: theme.tokens.color_fg_disabled,
41
+ fontSize: theme.typography.caption.fontSize,
42
+ });
43
+ });
44
+ export var CommandPaletteLoading = React.forwardRef(function (props, ref) {
45
+ var _a = props.progress, progress = _a === void 0 ? 0 : _a, _b = props.children, children = _b === void 0 ? 'Loading...' : _b, className = props.className, loadingProps = __rest(props, ["progress", "children", "className"]);
46
+ return (React.createElement(Root, __assign({ ref: ref, role: "progressbar", "aria-valuenow": progress, "aria-valuemin": 0, "aria-valuemax": 100, "aria-label": "Loading results", className: clsx(commandPaletteClasses.loading, className) }, loadingProps),
47
+ React.createElement(Spinner, null),
48
+ children));
49
+ });
50
+ CommandPaletteLoading.displayName = 'CommandPaletteLoading';
@@ -0,0 +1,44 @@
1
+ export function commandScore(value, search) {
2
+ if (search.length === 0)
3
+ return 1;
4
+ if (value.length === 0)
5
+ return 0;
6
+ var lowerValue = value.toLowerCase();
7
+ var lowerSearch = search.toLowerCase();
8
+ if (lowerValue === lowerSearch)
9
+ return 1;
10
+ if (lowerValue.includes(lowerSearch)) {
11
+ if (lowerValue.startsWith(lowerSearch)) {
12
+ return 0.9;
13
+ }
14
+ return 0.7;
15
+ }
16
+ var searchIdx = 0;
17
+ var score = 0;
18
+ var consecutive = 0;
19
+ var prevMatchIdx = -2;
20
+ for (var i = 0; i < lowerValue.length && searchIdx < lowerSearch.length; i++) {
21
+ if (lowerValue[i] === lowerSearch[searchIdx]) {
22
+ if (i === prevMatchIdx + 1) {
23
+ consecutive++;
24
+ score += consecutive * 2;
25
+ }
26
+ else {
27
+ consecutive = 1;
28
+ score += 1;
29
+ }
30
+ if (i === 0 || /[\s\-_./]/.test(value[i - 1])) {
31
+ score += 3;
32
+ }
33
+ if (i > 0 && value[i] === value[i].toUpperCase() && value[i - 1] === value[i - 1].toLowerCase()) {
34
+ score += 2;
35
+ }
36
+ prevMatchIdx = i;
37
+ searchIdx++;
38
+ }
39
+ }
40
+ if (searchIdx < lowerSearch.length)
41
+ return 0;
42
+ var maxPossibleScore = lowerSearch.length * 6;
43
+ return Math.min(score / maxPossibleScore, 0.6);
44
+ }
@@ -0,0 +1,47 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __rest = (this && this.__rest) || function (s, e) {
13
+ var t = {};
14
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
15
+ t[p] = s[p];
16
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
17
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
18
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
19
+ t[p[i]] = s[p[i]];
20
+ }
21
+ return t;
22
+ };
23
+ import * as React from 'react';
24
+ import { styled } from '../styled';
25
+ import clsx from '../utils/clsx';
26
+ import { commandPaletteClasses, commandPaletteComponentName } from './command-palette-classes';
27
+ import { useCommandPaletteState } from './command-palette-context';
28
+ var Root = styled('div', {
29
+ name: commandPaletteComponentName,
30
+ slot: 'Separator',
31
+ })(function (_a) {
32
+ var theme = _a.theme;
33
+ return ({
34
+ height: 1,
35
+ margin: "".concat(theme.spacing(0), " ").concat(theme.spacing(1.5)),
36
+ backgroundColor: theme.tokens.color_border_default,
37
+ });
38
+ });
39
+ export var CommandPaletteSeparator = React.forwardRef(function (props, ref) {
40
+ var _a = props.alwaysRender, alwaysRender = _a === void 0 ? false : _a, className = props.className, separatorProps = __rest(props, ["alwaysRender", "className"]);
41
+ var search = useCommandPaletteState(function (state) { return state.search; });
42
+ var isSearching = search.length > 0;
43
+ if (!alwaysRender && isSearching)
44
+ return null;
45
+ return (React.createElement(Root, __assign({ ref: ref, role: "separator", className: clsx(commandPaletteClasses.separator, className) }, separatorProps)));
46
+ });
47
+ CommandPaletteSeparator.displayName = 'CommandPaletteSeparator';
@@ -0,0 +1,107 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __rest = (this && this.__rest) || function (s, e) {
13
+ var t = {};
14
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
15
+ t[p] = s[p];
16
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
17
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
18
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
19
+ t[p[i]] = s[p[i]];
20
+ }
21
+ return t;
22
+ };
23
+ import * as React from 'react';
24
+ import useForkRef from '@mui/utils/useForkRef';
25
+ import { styled } from '../styled';
26
+ import clsx from '../utils/clsx';
27
+ import { commandPaletteClasses, commandPaletteComponentName } from './command-palette-classes';
28
+ var Root = styled('div', {
29
+ name: commandPaletteComponentName,
30
+ slot: 'Tabs',
31
+ })(function (_a) {
32
+ var theme = _a.theme;
33
+ return ({
34
+ display: 'flex',
35
+ alignItems: 'center',
36
+ gap: theme.spacing(0.5),
37
+ padding: "".concat(theme.spacing(1), " ").concat(theme.spacing(2)),
38
+ overflowX: 'auto',
39
+ scrollbarWidth: 'none',
40
+ '&::-webkit-scrollbar': {
41
+ display: 'none',
42
+ },
43
+ });
44
+ });
45
+ var TabButton = styled('button', {
46
+ name: commandPaletteComponentName,
47
+ slot: 'Tab',
48
+ })(function (_a) {
49
+ var theme = _a.theme, ownerState = _a.ownerState;
50
+ return (__assign(__assign(__assign({ display: 'inline-flex', alignItems: 'center', justifyContent: 'center', padding: "".concat(theme.spacing(0.5), " ").concat(theme.spacing(1.5)), borderRadius: theme.spacing(1), border: 'none', cursor: 'pointer', fontSize: theme.typography.caption.fontSize, fontWeight: theme.typography.fontWeightSemibold, fontFamily: theme.typography.caption.fontFamily, lineHeight: theme.typography.caption.lineHeight, whiteSpace: 'nowrap', userSelect: 'none', transition: theme.transitions.create(['background-color', 'color'], {
51
+ duration: theme.transitions.duration.shortest,
52
+ }), color: theme.tokens.color_fg_disabled, backgroundColor: 'transparent' }, (ownerState.active && {
53
+ color: theme.tokens.color_fg_default,
54
+ backgroundColor: theme.tokens.color_bg_state_neutral_subtle,
55
+ })), (!ownerState.active && {
56
+ '&:hover': {
57
+ backgroundColor: theme.tokens.color_bg_interactive_hover,
58
+ },
59
+ })), { '&:focus-visible': {
60
+ outline: 'none',
61
+ backgroundColor: theme.tokens.color_bg_interactive_hover,
62
+ } }));
63
+ });
64
+ export var CommandPaletteTabs = React.forwardRef(function (props, ref) {
65
+ var children = props.children, className = props.className, tabsProps = __rest(props, ["children", "className"]);
66
+ var tabsRef = React.useRef(null);
67
+ var handleKeyDown = React.useCallback(function (e) {
68
+ var tabs = tabsRef.current;
69
+ if (!tabs)
70
+ return;
71
+ var tabButtons = Array.from(tabs.querySelectorAll('[role="tab"]'));
72
+ var focused = document.activeElement;
73
+ var currentIndex = tabButtons.indexOf(focused);
74
+ if (currentIndex === -1)
75
+ return;
76
+ var nextIndex = null;
77
+ if (e.key === 'ArrowRight') {
78
+ nextIndex = currentIndex < tabButtons.length - 1 ? currentIndex + 1 : 0;
79
+ }
80
+ else if (e.key === 'ArrowLeft') {
81
+ nextIndex = currentIndex > 0 ? currentIndex - 1 : tabButtons.length - 1;
82
+ }
83
+ else if (e.key === 'Home') {
84
+ nextIndex = 0;
85
+ }
86
+ else if (e.key === 'End') {
87
+ nextIndex = tabButtons.length - 1;
88
+ }
89
+ if (nextIndex !== null) {
90
+ e.preventDefault();
91
+ tabButtons[nextIndex].focus();
92
+ tabButtons[nextIndex].click();
93
+ }
94
+ }, []);
95
+ var mergedRef = useForkRef(tabsRef, ref);
96
+ return (React.createElement(Root, __assign({ ref: mergedRef, role: "tablist", className: clsx(commandPaletteClasses.tabs, className), onKeyDown: handleKeyDown }, tabsProps), children));
97
+ });
98
+ CommandPaletteTabs.displayName = 'CommandPaletteTabs';
99
+ export var CommandPaletteTab = React.forwardRef(function (props, ref) {
100
+ var _a = props.active, active = _a === void 0 ? false : _a, onSelect = props.onSelect, children = props.children, className = props.className, onClick = props.onClick, tabProps = __rest(props, ["active", "onSelect", "children", "className", "onClick"]);
101
+ var handleClick = React.useCallback(function (e) {
102
+ onSelect === null || onSelect === void 0 ? void 0 : onSelect();
103
+ onClick === null || onClick === void 0 ? void 0 : onClick(e);
104
+ }, [onSelect, onClick]);
105
+ return (React.createElement(TabButton, __assign({ ref: ref, role: "tab", type: "button", "aria-selected": active, tabIndex: active ? 0 : -1, ownerState: { active: active }, className: clsx(commandPaletteClasses.tab, active && commandPaletteClasses.tabSelected, className), onClick: handleClick }, tabProps), children));
106
+ });
107
+ CommandPaletteTab.displayName = 'CommandPaletteTab';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,435 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __rest = (this && this.__rest) || function (s, e) {
13
+ var t = {};
14
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
15
+ t[p] = s[p];
16
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
17
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
18
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
19
+ t[p[i]] = s[p[i]];
20
+ }
21
+ return t;
22
+ };
23
+ var __read = (this && this.__read) || function (o, n) {
24
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
25
+ if (!m) return o;
26
+ var i = m.call(o), r, ar = [], e;
27
+ try {
28
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
29
+ }
30
+ catch (error) { e = { error: error }; }
31
+ finally {
32
+ try {
33
+ if (r && !r.done && (m = i["return"])) m.call(i);
34
+ }
35
+ finally { if (e) throw e.error; }
36
+ }
37
+ return ar;
38
+ };
39
+ import * as React from 'react';
40
+ import { styled } from '../styled';
41
+ import { useMergedClasses } from '../styles/classes';
42
+ import clsx from '../utils/clsx';
43
+ import { commandPaletteClasses, commandPaletteComponentName, getCommandPaletteUtilityClass, } from './command-palette-classes';
44
+ import { CommandPaletteContext, CommandPaletteStoreContext } from './command-palette-context';
45
+ import { commandScore } from './command-palette-score';
46
+ var VALUE_ATTR = 'data-value';
47
+ var ITEM_SELECTOR = ".".concat(commandPaletteClasses.item);
48
+ var GROUP_ITEMS_SELECTOR = ".".concat(commandPaletteClasses.groupItems);
49
+ var VALID_ITEM_SELECTOR = "".concat(ITEM_SELECTOR, ":not([aria-disabled=\"true\"])");
50
+ var Root = styled('div', {
51
+ name: commandPaletteComponentName,
52
+ slot: 'Root',
53
+ })(function (_a) {
54
+ var theme = _a.theme;
55
+ return ({
56
+ display: 'flex',
57
+ flexDirection: 'column',
58
+ width: '100%',
59
+ fontFamily: theme.typography.fontFamily,
60
+ overflow: 'hidden',
61
+ borderRadius: theme.typography.pxToRem(12),
62
+ backgroundColor: theme.tokens.color_bg_layer_elevated,
63
+ '&:focus-visible': {
64
+ outline: "none",
65
+ },
66
+ });
67
+ });
68
+ function defaultFilter(value, search, keywords) {
69
+ var allText = (keywords === null || keywords === void 0 ? void 0 : keywords.length) ? "".concat(value, " ").concat(keywords.join(' ')) : value;
70
+ return commandScore(allText, search);
71
+ }
72
+ function useAsRef(value) {
73
+ var ref = React.useRef(value);
74
+ ref.current = value;
75
+ return ref;
76
+ }
77
+ function useLazyRef(fn) {
78
+ var ref = React.useRef(undefined);
79
+ if (ref.current === undefined) {
80
+ ref.current = fn();
81
+ }
82
+ return ref;
83
+ }
84
+ function findNextItem(el, loop) {
85
+ var _a, _b, _c;
86
+ var next = el.nextElementSibling;
87
+ while (next) {
88
+ if (next.matches(VALID_ITEM_SELECTOR))
89
+ return next;
90
+ next = next.nextElementSibling;
91
+ }
92
+ var group = el.closest(GROUP_ITEMS_SELECTOR);
93
+ while (group) {
94
+ var nextGroup = (_a = group.parentElement) === null || _a === void 0 ? void 0 : _a.nextElementSibling;
95
+ if (nextGroup) {
96
+ var items = nextGroup.querySelector(GROUP_ITEMS_SELECTOR);
97
+ if (items) {
98
+ var firstItem = items.querySelector(VALID_ITEM_SELECTOR);
99
+ if (firstItem)
100
+ return firstItem;
101
+ }
102
+ group = nextGroup.querySelector(GROUP_ITEMS_SELECTOR);
103
+ }
104
+ else {
105
+ group = null;
106
+ }
107
+ }
108
+ if (!el.closest(GROUP_ITEMS_SELECTOR)) {
109
+ var sibling = (_b = el.parentElement) === null || _b === void 0 ? void 0 : _b.nextElementSibling;
110
+ while (sibling) {
111
+ var item = ((_c = sibling.matches) === null || _c === void 0 ? void 0 : _c.call(sibling, VALID_ITEM_SELECTOR)) ? sibling : sibling.querySelector(VALID_ITEM_SELECTOR);
112
+ if (item)
113
+ return item;
114
+ sibling = sibling.nextElementSibling;
115
+ }
116
+ }
117
+ if (loop) {
118
+ var list = el.closest(".".concat(commandPaletteClasses.list));
119
+ if (list) {
120
+ return list.querySelector(VALID_ITEM_SELECTOR) || undefined;
121
+ }
122
+ }
123
+ return undefined;
124
+ }
125
+ function findPrevItem(el, loop) {
126
+ var _a;
127
+ var prev = el.previousElementSibling;
128
+ while (prev) {
129
+ if (prev.matches(VALID_ITEM_SELECTOR))
130
+ return prev;
131
+ prev = prev.previousElementSibling;
132
+ }
133
+ var group = el.closest(GROUP_ITEMS_SELECTOR);
134
+ while (group) {
135
+ var prevGroup = (_a = group.parentElement) === null || _a === void 0 ? void 0 : _a.previousElementSibling;
136
+ if (prevGroup) {
137
+ var items = prevGroup.querySelector(GROUP_ITEMS_SELECTOR);
138
+ if (items) {
139
+ var allItems = items.querySelectorAll(VALID_ITEM_SELECTOR);
140
+ if (allItems.length)
141
+ return allItems[allItems.length - 1];
142
+ }
143
+ group = prevGroup.querySelector(GROUP_ITEMS_SELECTOR);
144
+ }
145
+ else {
146
+ group = null;
147
+ }
148
+ }
149
+ if (loop) {
150
+ var list = el.closest(".".concat(commandPaletteClasses.list));
151
+ if (list) {
152
+ var allItems = list.querySelectorAll(VALID_ITEM_SELECTOR);
153
+ return allItems.length ? allItems[allItems.length - 1] : undefined;
154
+ }
155
+ }
156
+ return undefined;
157
+ }
158
+ function getSelectedItem(listInnerRef, value) {
159
+ var _a;
160
+ return ((_a = listInnerRef.current) === null || _a === void 0 ? void 0 : _a.querySelector("".concat(ITEM_SELECTOR, "[").concat(VALUE_ATTR, "=\"").concat(encodeURIComponent(value), "\"]"))) || null;
161
+ }
162
+ function getValidItems(listInnerRef) {
163
+ var _a;
164
+ return Array.from(((_a = listInnerRef.current) === null || _a === void 0 ? void 0 : _a.querySelectorAll(VALID_ITEM_SELECTOR)) || []);
165
+ }
166
+ function useScheduleLayoutEffect() {
167
+ var _a = __read(React.useState(0), 2), count = _a[0], setCount = _a[1];
168
+ var callbackRef = React.useRef(null);
169
+ React.useLayoutEffect(function () {
170
+ if (callbackRef.current) {
171
+ callbackRef.current();
172
+ callbackRef.current = null;
173
+ }
174
+ }, [count]);
175
+ return React.useCallback(function (callback) {
176
+ callbackRef.current = callback;
177
+ setCount(function (c) { return c + 1; });
178
+ }, []);
179
+ }
180
+ export var CommandPalette = React.forwardRef(function (props, ref) {
181
+ var _a = props.label, label = _a === void 0 ? 'Command palette' : _a, searchProp = props.search, _onSearchChange = props.onSearchChange, valueProp = props.value, _onValueChange = props.onValueChange, filterProp = props.filter, _b = props.shouldFilter, shouldFilter = _b === void 0 ? true : _b, _c = props.loop, loop = _c === void 0 ? false : _c, _onSelect = props.onSelect, children = props.children, className = props.className, rootProps = __rest(props, ["label", "search", "onSearchChange", "value", "onValueChange", "filter", "shouldFilter", "loop", "onSelect", "children", "className"]);
182
+ var id = React.useId();
183
+ var listId = "".concat(id, "-list");
184
+ var inputId = "".concat(id, "-input");
185
+ var listInnerRef = React.useRef(null);
186
+ var allItems = React.useRef(new Map());
187
+ var allGroups = React.useRef(new Map());
188
+ var filter = shouldFilter ? filterProp !== null && filterProp !== void 0 ? filterProp : defaultFilter : false;
189
+ var propsRef = useAsRef(props);
190
+ var schedule = useScheduleLayoutEffect();
191
+ var store = useLazyRef(function () {
192
+ var listeners = new Set();
193
+ var state = {
194
+ search: searchProp || '',
195
+ selectedValue: valueProp || '',
196
+ filtered: { count: 0, items: new Map(), groups: new Map() },
197
+ };
198
+ return {
199
+ subscribe: function (callback) {
200
+ listeners.add(callback);
201
+ return function () { return listeners.delete(callback); };
202
+ },
203
+ snapshot: function () {
204
+ return state;
205
+ },
206
+ setState: function (key, value, shouldEmit) {
207
+ var _a;
208
+ var _b, _c;
209
+ if (Object.is(state[key], value))
210
+ return;
211
+ state = __assign(__assign({}, state), (_a = {}, _a[key] = value, _a));
212
+ if (key === 'search') {
213
+ filterItems();
214
+ schedule(function () { return selectFirstItem(); });
215
+ (_c = (_b = propsRef.current).onSearchChange) === null || _c === void 0 ? void 0 : _c.call(_b, value);
216
+ }
217
+ if (shouldEmit !== false) {
218
+ this.emit();
219
+ }
220
+ },
221
+ emit: function () {
222
+ listeners.forEach(function (l) { return l(); });
223
+ },
224
+ };
225
+ });
226
+ var filterItems = React.useCallback(function () {
227
+ if (!filter) {
228
+ store.current.setState('filtered', {
229
+ count: allItems.current.size,
230
+ items: new Map(),
231
+ groups: new Map(),
232
+ });
233
+ return;
234
+ }
235
+ var search = store.current.snapshot().search;
236
+ var items = new Map();
237
+ var groups = new Map();
238
+ var count = 0;
239
+ allItems.current.forEach(function (itemMeta, id) {
240
+ var score = filter(itemMeta.value, search, itemMeta.keywords);
241
+ items.set(id, score);
242
+ if (score > 0)
243
+ count++;
244
+ });
245
+ allGroups.current.forEach(function (groupItems, groupId) {
246
+ var groupCount = 0;
247
+ groupItems.forEach(function (itemId) {
248
+ var score = items.get(itemId) || 0;
249
+ if (score > 0)
250
+ groupCount++;
251
+ });
252
+ groups.set(groupId, groupCount);
253
+ });
254
+ store.current.setState('filtered', { count: count, items: items, groups: groups }, false);
255
+ // eslint-disable-next-line react-hooks/exhaustive-deps
256
+ }, []);
257
+ var selectFirstItem = React.useCallback(function () {
258
+ var _a, _b;
259
+ var items = getValidItems(listInnerRef);
260
+ var item = items.find(function (el) { return el.getAttribute('aria-disabled') !== 'true'; });
261
+ if (item) {
262
+ var value = item.getAttribute(VALUE_ATTR);
263
+ if (value) {
264
+ var decoded = decodeURIComponent(value);
265
+ store.current.setState('selectedValue', decoded);
266
+ (_b = (_a = propsRef.current).onValueChange) === null || _b === void 0 ? void 0 : _b.call(_a, decoded);
267
+ }
268
+ }
269
+ // eslint-disable-next-line react-hooks/exhaustive-deps
270
+ }, []);
271
+ React.useEffect(function () {
272
+ if (searchProp !== undefined) {
273
+ store.current.setState('search', searchProp);
274
+ }
275
+ }, [searchProp, store]);
276
+ React.useEffect(function () {
277
+ if (valueProp !== undefined) {
278
+ store.current.setState('selectedValue', valueProp);
279
+ }
280
+ }, [valueProp, store]);
281
+ var context = React.useMemo(function () { return ({
282
+ value: function (id, value, keywords) {
283
+ var existing = allItems.current.get(id);
284
+ if (existing && existing.value === value && existing.keywords === keywords)
285
+ return;
286
+ allItems.current.set(id, { value: value, keywords: keywords });
287
+ filterItems();
288
+ schedule(function () {
289
+ var state = store.current.snapshot();
290
+ var validItems = getValidItems(listInnerRef);
291
+ var selectedEl = getSelectedItem(listInnerRef, state.selectedValue);
292
+ if (!selectedEl && validItems.length > 0) {
293
+ selectFirstItem();
294
+ }
295
+ store.current.emit();
296
+ });
297
+ },
298
+ item: function (id, groupId) {
299
+ if (groupId) {
300
+ if (!allGroups.current.has(groupId)) {
301
+ allGroups.current.set(groupId, new Set());
302
+ }
303
+ allGroups.current.get(groupId).add(id);
304
+ }
305
+ filterItems();
306
+ store.current.emit();
307
+ return function () {
308
+ var _a;
309
+ allItems.current.delete(id);
310
+ if (groupId) {
311
+ (_a = allGroups.current.get(groupId)) === null || _a === void 0 ? void 0 : _a.delete(id);
312
+ }
313
+ filterItems();
314
+ schedule(function () {
315
+ selectFirstItem();
316
+ store.current.emit();
317
+ });
318
+ };
319
+ },
320
+ group: function (id) {
321
+ if (!allGroups.current.has(id)) {
322
+ allGroups.current.set(id, new Set());
323
+ }
324
+ return function () {
325
+ allGroups.current.delete(id);
326
+ store.current.emit();
327
+ };
328
+ },
329
+ filter: filter,
330
+ label: label,
331
+ listId: listId,
332
+ inputId: inputId,
333
+ listInnerRef: listInnerRef,
334
+ }); },
335
+ // eslint-disable-next-line react-hooks/exhaustive-deps
336
+ [label, listId, inputId]);
337
+ var handleKeyDown = React.useCallback(function (e) {
338
+ var _a, _b, _c, _d, _e, _f;
339
+ var state = store.current.snapshot();
340
+ var updateSelectedByChange = function (change) {
341
+ var _a, _b;
342
+ var selected = getSelectedItem(listInnerRef, state.selectedValue);
343
+ var nextItem;
344
+ if (selected) {
345
+ nextItem = change === 1 ? findNextItem(selected, loop) : findPrevItem(selected, loop);
346
+ }
347
+ else {
348
+ var items = getValidItems(listInnerRef);
349
+ nextItem = change === 1 ? items[0] : items[items.length - 1];
350
+ }
351
+ if (nextItem) {
352
+ var value = nextItem.getAttribute(VALUE_ATTR);
353
+ if (value) {
354
+ var decoded = decodeURIComponent(value);
355
+ store.current.setState('selectedValue', decoded);
356
+ (_b = (_a = propsRef.current).onValueChange) === null || _b === void 0 ? void 0 : _b.call(_a, decoded);
357
+ nextItem.scrollIntoView({ block: 'nearest' });
358
+ }
359
+ }
360
+ };
361
+ switch (e.key) {
362
+ case 'ArrowDown': {
363
+ e.preventDefault();
364
+ updateSelectedByChange(1);
365
+ break;
366
+ }
367
+ case 'ArrowUp': {
368
+ e.preventDefault();
369
+ updateSelectedByChange(-1);
370
+ break;
371
+ }
372
+ case 'Home': {
373
+ e.preventDefault();
374
+ var items = getValidItems(listInnerRef);
375
+ var first = items[0];
376
+ if (first) {
377
+ var value = first.getAttribute(VALUE_ATTR);
378
+ if (value) {
379
+ var decoded = decodeURIComponent(value);
380
+ store.current.setState('selectedValue', decoded);
381
+ (_b = (_a = propsRef.current).onValueChange) === null || _b === void 0 ? void 0 : _b.call(_a, decoded);
382
+ first.scrollIntoView({ block: 'nearest' });
383
+ }
384
+ }
385
+ break;
386
+ }
387
+ case 'End': {
388
+ e.preventDefault();
389
+ var items = getValidItems(listInnerRef);
390
+ var last = items[items.length - 1];
391
+ if (last) {
392
+ var value = last.getAttribute(VALUE_ATTR);
393
+ if (value) {
394
+ var decoded = decodeURIComponent(value);
395
+ store.current.setState('selectedValue', decoded);
396
+ (_d = (_c = propsRef.current).onValueChange) === null || _d === void 0 ? void 0 : _d.call(_c, decoded);
397
+ last.scrollIntoView({ block: 'nearest' });
398
+ }
399
+ }
400
+ break;
401
+ }
402
+ case 'Enter': {
403
+ e.preventDefault();
404
+ var selected = getSelectedItem(listInnerRef, state.selectedValue);
405
+ if (selected) {
406
+ selected.click();
407
+ (_f = (_e = propsRef.current).onSelect) === null || _f === void 0 ? void 0 : _f.call(_e, state.selectedValue);
408
+ }
409
+ break;
410
+ }
411
+ case 'n':
412
+ case 'j': {
413
+ if (e.ctrlKey) {
414
+ e.preventDefault();
415
+ updateSelectedByChange(1);
416
+ }
417
+ break;
418
+ }
419
+ case 'p':
420
+ case 'k': {
421
+ if (e.ctrlKey) {
422
+ e.preventDefault();
423
+ updateSelectedByChange(-1);
424
+ }
425
+ break;
426
+ }
427
+ }
428
+ }, [loop, store, propsRef]);
429
+ var classes = useMergedClasses(commandPaletteClasses, getCommandPaletteUtilityClass, undefined);
430
+ return (React.createElement(CommandPaletteStoreContext.Provider, { value: store.current },
431
+ React.createElement(CommandPaletteContext.Provider, { value: context },
432
+ React.createElement(Root, __assign({ ref: ref, className: clsx(classes.root, className), tabIndex: -1, onKeyDown: handleKeyDown, "aria-label": label }, rootProps), children))));
433
+ });
434
+ CommandPalette.displayName = 'CommandPalette';
435
+ export { VALUE_ATTR };