@crisp-ui-kit/crisp 0.16.0 → 0.16.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.
@@ -37,6 +37,7 @@ exports.MultiSelect = MultiSelect;
37
37
  const jsx_runtime_1 = require("react/jsx-runtime");
38
38
  const lucide_react_1 = require("lucide-react");
39
39
  const React = __importStar(require("react"));
40
+ const select_1 = require("../select");
40
41
  const multiselect_recipe_1 = require("./multiselect.recipe");
41
42
  const TONES = [
42
43
  "brand",
@@ -122,26 +123,24 @@ function MultiSelect({ options, value: valueProp, defaultValue, onValueChange, p
122
123
  setInternalValue(next);
123
124
  onValueChange?.(next);
124
125
  };
125
- const onNavKeys = (e) => {
126
- if (e.key === "ArrowDown") {
127
- e.preventDefault();
128
- setHighlight((h) => Math.min(h + 1, filtered.length - 1));
129
- }
130
- else if (e.key === "ArrowUp") {
131
- e.preventDefault();
132
- setHighlight((h) => Math.max(h - 1, 0));
133
- }
134
- else if (e.key === "Enter") {
135
- e.preventDefault();
126
+ // crisp's shared popup-listbox model (#198). Hand-rolling a second one is
127
+ // how MultiSelect ended up with no Home/End, arrows that dead-end instead of
128
+ // wrapping, and a popup left open behind a Tab — while Select, the same
129
+ // widget with single selection, had all three.
130
+ const { onKeyDown: onNavKeys } = (0, select_1.useListboxKeyboard)({
131
+ // every rendered option is selectable: MultiSelect has no disabled state
132
+ enabledIndexes: filtered.map((_, i) => i),
133
+ highlight,
134
+ setHighlight,
135
+ commit: () => {
136
136
  const opt = filtered[highlight];
137
137
  if (opt)
138
138
  toggle(opt.value);
139
- }
140
- else if (e.key === "Escape") {
141
- e.preventDefault();
142
- close();
143
- }
144
- };
139
+ },
140
+ close,
141
+ // Tab is already moving focus out; `close` would drag it back to the trigger
142
+ dismiss: () => setOpen(false),
143
+ });
145
144
  React.useEffect(() => {
146
145
  if (!open)
147
146
  return;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.selectRecipe = exports.Select = void 0;
3
+ exports.selectRecipe = exports.Select = exports.useListboxKeyboard = void 0;
4
+ var listbox_keyboard_1 = require("./listbox-keyboard");
5
+ Object.defineProperty(exports, "useListboxKeyboard", { enumerable: true, get: function () { return listbox_keyboard_1.useListboxKeyboard; } });
4
6
  var select_1 = require("./select");
5
7
  Object.defineProperty(exports, "Select", { enumerable: true, get: function () { return select_1.Select; } });
6
8
  var select_recipe_1 = require("./select.recipe");
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useListboxKeyboard = useListboxKeyboard;
4
+ /**
5
+ * The WAI-ARIA listbox keyboard model for a POPUP listbox: ↑/↓ to move the
6
+ * highlight (wrapping, skipping disabled options), Home/End to jump, Enter to
7
+ * choose, Escape to close, Tab to dismiss.
8
+ *
9
+ * Extracted from `Select`, whose set was the complete one, so `MultiSelect` —
10
+ * the same widget with multiple selection — stops being a second partial copy
11
+ * that had no Home/End and stayed open on Tab (#198). The same reason
12
+ * `useMenuKeyboard` and `useTablistKeyboard` exist.
13
+ *
14
+ * NOT for `WheelPicker`: that is an always-visible multi-column wheel that
15
+ * commits on move, so Enter, Escape and Tab have no meaning in it.
16
+ */
17
+ function useListboxKeyboard({ enabledIndexes, highlight, setHighlight, commit, close, dismiss = close, }) {
18
+ const move = (dir) => {
19
+ if (enabledIndexes.length === 0)
20
+ return;
21
+ const pos = enabledIndexes.indexOf(highlight);
22
+ // nothing highlighted yet: ↓ enters at the top, ↑ at the bottom
23
+ const nextPos = pos < 0
24
+ ? dir > 0
25
+ ? 0
26
+ : enabledIndexes.length - 1
27
+ : (pos + dir + enabledIndexes.length) % enabledIndexes.length;
28
+ setHighlight(enabledIndexes[nextPos] ?? 0);
29
+ };
30
+ const onKeyDown = (e) => {
31
+ switch (e.key) {
32
+ case "ArrowDown":
33
+ e.preventDefault();
34
+ move(1);
35
+ break;
36
+ case "ArrowUp":
37
+ e.preventDefault();
38
+ move(-1);
39
+ break;
40
+ case "Home":
41
+ e.preventDefault();
42
+ // the `?? 0` these used to end in armed option 0 on a list with no
43
+ // options — the arrows guarded against that and Home/End did not
44
+ if (enabledIndexes.length)
45
+ setHighlight(enabledIndexes[0]);
46
+ break;
47
+ case "End":
48
+ e.preventDefault();
49
+ if (enabledIndexes.length)
50
+ setHighlight(enabledIndexes[enabledIndexes.length - 1]);
51
+ break;
52
+ case "Enter":
53
+ e.preventDefault();
54
+ commit();
55
+ break;
56
+ case "Escape":
57
+ e.preventDefault();
58
+ close();
59
+ break;
60
+ case "Tab":
61
+ // not preventDefault: Tab must still move focus, it just must not
62
+ // leave an open popup floating behind it
63
+ dismiss();
64
+ break;
65
+ }
66
+ };
67
+ return { onKeyDown };
68
+ }
@@ -37,6 +37,7 @@ exports.Select = Select;
37
37
  const jsx_runtime_1 = require("react/jsx-runtime");
38
38
  const lucide_react_1 = require("lucide-react");
39
39
  const React = __importStar(require("react"));
40
+ const listbox_keyboard_1 = require("./listbox-keyboard");
40
41
  const select_recipe_1 = require("./select.recipe");
41
42
  function optionText(o) {
42
43
  if (o.keywords)
@@ -116,48 +117,14 @@ function Select({ options, value, defaultValue, onValueChange, placeholder = "Se
116
117
  setOpen(false);
117
118
  triggerRef.current?.focus({ preventScroll: true });
118
119
  };
119
- const moveHighlight = (dir) => {
120
- if (enabledIndexes.length === 0)
121
- return;
122
- const pos = enabledIndexes.indexOf(highlight);
123
- const nextPos = pos < 0
124
- ? dir > 0
125
- ? 0
126
- : enabledIndexes.length - 1
127
- : (pos + dir + enabledIndexes.length) % enabledIndexes.length;
128
- setHighlight(enabledIndexes[nextPos] ?? 0);
129
- };
130
- const onNavKeys = (e) => {
131
- switch (e.key) {
132
- case "ArrowDown":
133
- e.preventDefault();
134
- moveHighlight(1);
135
- break;
136
- case "ArrowUp":
137
- e.preventDefault();
138
- moveHighlight(-1);
139
- break;
140
- case "Home":
141
- e.preventDefault();
142
- setHighlight(enabledIndexes[0] ?? 0);
143
- break;
144
- case "End":
145
- e.preventDefault();
146
- setHighlight(enabledIndexes[enabledIndexes.length - 1] ?? 0);
147
- break;
148
- case "Enter":
149
- e.preventDefault();
150
- pick(filtered[highlight]);
151
- break;
152
- case "Escape":
153
- e.preventDefault();
154
- close();
155
- break;
156
- case "Tab":
157
- setOpen(false);
158
- break;
159
- }
160
- };
120
+ const { onKeyDown: onNavKeys } = (0, listbox_keyboard_1.useListboxKeyboard)({
121
+ enabledIndexes,
122
+ highlight,
123
+ setHighlight,
124
+ commit: () => pick(filtered[highlight]),
125
+ close,
126
+ dismiss: () => setOpen(false),
127
+ });
161
128
  const onTriggerKeyDown = (e) => {
162
129
  if (!open) {
163
130
  if (e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") {
@@ -1,6 +1,7 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Check, ChevronDown, Search } from "lucide-react";
3
3
  import * as React from "react";
4
+ import { useListboxKeyboard } from "../select/index.js";
4
5
  import { multiselectRecipe } from "./multiselect.recipe.js";
5
6
  const TONES = [
6
7
  "brand",
@@ -86,26 +87,24 @@ export function MultiSelect({ options, value: valueProp, defaultValue, onValueCh
86
87
  setInternalValue(next);
87
88
  onValueChange?.(next);
88
89
  };
89
- const onNavKeys = (e) => {
90
- if (e.key === "ArrowDown") {
91
- e.preventDefault();
92
- setHighlight((h) => Math.min(h + 1, filtered.length - 1));
93
- }
94
- else if (e.key === "ArrowUp") {
95
- e.preventDefault();
96
- setHighlight((h) => Math.max(h - 1, 0));
97
- }
98
- else if (e.key === "Enter") {
99
- e.preventDefault();
90
+ // crisp's shared popup-listbox model (#198). Hand-rolling a second one is
91
+ // how MultiSelect ended up with no Home/End, arrows that dead-end instead of
92
+ // wrapping, and a popup left open behind a Tab — while Select, the same
93
+ // widget with single selection, had all three.
94
+ const { onKeyDown: onNavKeys } = useListboxKeyboard({
95
+ // every rendered option is selectable: MultiSelect has no disabled state
96
+ enabledIndexes: filtered.map((_, i) => i),
97
+ highlight,
98
+ setHighlight,
99
+ commit: () => {
100
100
  const opt = filtered[highlight];
101
101
  if (opt)
102
102
  toggle(opt.value);
103
- }
104
- else if (e.key === "Escape") {
105
- e.preventDefault();
106
- close();
107
- }
108
- };
103
+ },
104
+ close,
105
+ // Tab is already moving focus out; `close` would drag it back to the trigger
106
+ dismiss: () => setOpen(false),
107
+ });
109
108
  React.useEffect(() => {
110
109
  if (!open)
111
110
  return;
@@ -1,2 +1,3 @@
1
+ export { useListboxKeyboard, } from "./listbox-keyboard.js";
1
2
  export { Select } from "./select.js";
2
3
  export { selectRecipe } from "./select.recipe.js";
@@ -0,0 +1,65 @@
1
+ /**
2
+ * The WAI-ARIA listbox keyboard model for a POPUP listbox: ↑/↓ to move the
3
+ * highlight (wrapping, skipping disabled options), Home/End to jump, Enter to
4
+ * choose, Escape to close, Tab to dismiss.
5
+ *
6
+ * Extracted from `Select`, whose set was the complete one, so `MultiSelect` —
7
+ * the same widget with multiple selection — stops being a second partial copy
8
+ * that had no Home/End and stayed open on Tab (#198). The same reason
9
+ * `useMenuKeyboard` and `useTablistKeyboard` exist.
10
+ *
11
+ * NOT for `WheelPicker`: that is an always-visible multi-column wheel that
12
+ * commits on move, so Enter, Escape and Tab have no meaning in it.
13
+ */
14
+ export function useListboxKeyboard({ enabledIndexes, highlight, setHighlight, commit, close, dismiss = close, }) {
15
+ const move = (dir) => {
16
+ if (enabledIndexes.length === 0)
17
+ return;
18
+ const pos = enabledIndexes.indexOf(highlight);
19
+ // nothing highlighted yet: ↓ enters at the top, ↑ at the bottom
20
+ const nextPos = pos < 0
21
+ ? dir > 0
22
+ ? 0
23
+ : enabledIndexes.length - 1
24
+ : (pos + dir + enabledIndexes.length) % enabledIndexes.length;
25
+ setHighlight(enabledIndexes[nextPos] ?? 0);
26
+ };
27
+ const onKeyDown = (e) => {
28
+ switch (e.key) {
29
+ case "ArrowDown":
30
+ e.preventDefault();
31
+ move(1);
32
+ break;
33
+ case "ArrowUp":
34
+ e.preventDefault();
35
+ move(-1);
36
+ break;
37
+ case "Home":
38
+ e.preventDefault();
39
+ // the `?? 0` these used to end in armed option 0 on a list with no
40
+ // options — the arrows guarded against that and Home/End did not
41
+ if (enabledIndexes.length)
42
+ setHighlight(enabledIndexes[0]);
43
+ break;
44
+ case "End":
45
+ e.preventDefault();
46
+ if (enabledIndexes.length)
47
+ setHighlight(enabledIndexes[enabledIndexes.length - 1]);
48
+ break;
49
+ case "Enter":
50
+ e.preventDefault();
51
+ commit();
52
+ break;
53
+ case "Escape":
54
+ e.preventDefault();
55
+ close();
56
+ break;
57
+ case "Tab":
58
+ // not preventDefault: Tab must still move focus, it just must not
59
+ // leave an open popup floating behind it
60
+ dismiss();
61
+ break;
62
+ }
63
+ };
64
+ return { onKeyDown };
65
+ }
@@ -1,6 +1,7 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Check, ChevronDown, Search } from "lucide-react";
3
3
  import * as React from "react";
4
+ import { useListboxKeyboard } from "./listbox-keyboard.js";
4
5
  import { selectRecipe } from "./select.recipe.js";
5
6
  function optionText(o) {
6
7
  if (o.keywords)
@@ -80,48 +81,14 @@ export function Select({ options, value, defaultValue, onValueChange, placeholde
80
81
  setOpen(false);
81
82
  triggerRef.current?.focus({ preventScroll: true });
82
83
  };
83
- const moveHighlight = (dir) => {
84
- if (enabledIndexes.length === 0)
85
- return;
86
- const pos = enabledIndexes.indexOf(highlight);
87
- const nextPos = pos < 0
88
- ? dir > 0
89
- ? 0
90
- : enabledIndexes.length - 1
91
- : (pos + dir + enabledIndexes.length) % enabledIndexes.length;
92
- setHighlight(enabledIndexes[nextPos] ?? 0);
93
- };
94
- const onNavKeys = (e) => {
95
- switch (e.key) {
96
- case "ArrowDown":
97
- e.preventDefault();
98
- moveHighlight(1);
99
- break;
100
- case "ArrowUp":
101
- e.preventDefault();
102
- moveHighlight(-1);
103
- break;
104
- case "Home":
105
- e.preventDefault();
106
- setHighlight(enabledIndexes[0] ?? 0);
107
- break;
108
- case "End":
109
- e.preventDefault();
110
- setHighlight(enabledIndexes[enabledIndexes.length - 1] ?? 0);
111
- break;
112
- case "Enter":
113
- e.preventDefault();
114
- pick(filtered[highlight]);
115
- break;
116
- case "Escape":
117
- e.preventDefault();
118
- close();
119
- break;
120
- case "Tab":
121
- setOpen(false);
122
- break;
123
- }
124
- };
84
+ const { onKeyDown: onNavKeys } = useListboxKeyboard({
85
+ enabledIndexes,
86
+ highlight,
87
+ setHighlight,
88
+ commit: () => pick(filtered[highlight]),
89
+ close,
90
+ dismiss: () => setOpen(false),
91
+ });
125
92
  const onTriggerKeyDown = (e) => {
126
93
  if (!open) {
127
94
  if (e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") {
@@ -1,2 +1,3 @@
1
+ export { type UseListboxKeyboardOptions, useListboxKeyboard, } from "./listbox-keyboard";
1
2
  export { Select, type SelectOption, type SelectProps } from "./select";
2
3
  export { selectRecipe } from "./select.recipe";
@@ -0,0 +1,38 @@
1
+ import type * as React from "react";
2
+ export type UseListboxKeyboardOptions = {
3
+ /**
4
+ * Indexes into the rendered option list that may be highlighted, in order.
5
+ * Disabled options are left out, so the arrows step over them rather than
6
+ * landing on something that cannot be chosen.
7
+ */
8
+ enabledIndexes: number[];
9
+ /** The highlighted index, or -1 when nothing is highlighted yet. */
10
+ highlight: number;
11
+ setHighlight: (index: number) => void;
12
+ /** Choose the highlighted option. Enter. */
13
+ commit: () => void;
14
+ /** Close the popup. Escape. */
15
+ close: () => void;
16
+ /**
17
+ * Close WITHOUT the focus handling `close` does, for Tab — the browser is
18
+ * already moving focus to the next element, so anything that pulls it back
19
+ * lands the user in the wrong place. Defaults to `close`.
20
+ */
21
+ dismiss?: () => void;
22
+ };
23
+ /**
24
+ * The WAI-ARIA listbox keyboard model for a POPUP listbox: ↑/↓ to move the
25
+ * highlight (wrapping, skipping disabled options), Home/End to jump, Enter to
26
+ * choose, Escape to close, Tab to dismiss.
27
+ *
28
+ * Extracted from `Select`, whose set was the complete one, so `MultiSelect` —
29
+ * the same widget with multiple selection — stops being a second partial copy
30
+ * that had no Home/End and stayed open on Tab (#198). The same reason
31
+ * `useMenuKeyboard` and `useTablistKeyboard` exist.
32
+ *
33
+ * NOT for `WheelPicker`: that is an always-visible multi-column wheel that
34
+ * commits on move, so Enter, Escape and Tab have no meaning in it.
35
+ */
36
+ export declare function useListboxKeyboard({ enabledIndexes, highlight, setHighlight, commit, close, dismiss, }: UseListboxKeyboardOptions): {
37
+ onKeyDown: (e: React.KeyboardEvent) => void;
38
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crisp-ui-kit/crisp",
3
- "version": "0.16.0",
3
+ "version": "0.16.1",
4
4
  "description": "A lean React design system, measured pixel-for-pixel from Attio.",
5
5
  "license": "MIT",
6
6
  "type": "module",