menhir 0.1.5 → 0.1.6

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 (93) hide show
  1. package/README.md +2 -2
  2. package/dist/combobox/clear/ComboboxClear.js +39 -0
  3. package/dist/combobox/filter/includesText.js +24 -0
  4. package/dist/combobox/group/ComboboxGroup.js +32 -0
  5. package/dist/combobox/group/ComboboxGroupContext.js +10 -0
  6. package/dist/combobox/group-label/ComboboxGroupLabel.js +26 -0
  7. package/dist/combobox/index.js +3 -0
  8. package/dist/combobox/index.parts.js +12 -0
  9. package/dist/combobox/input/ComboboxInput.js +160 -0
  10. package/dist/combobox/item/ComboboxItem.js +98 -0
  11. package/dist/combobox/item/ComboboxItemContext.js +10 -0
  12. package/dist/combobox/item-indicator/ComboboxItemIndicator.js +22 -0
  13. package/dist/combobox/list/ComboboxList.js +26 -0
  14. package/dist/combobox/popup/ComboboxPopup.js +78 -0
  15. package/dist/combobox/root/ComboboxContext.js +137 -0
  16. package/dist/combobox/root/ComboboxRoot.js +21 -0
  17. package/dist/combobox/status/ComboboxStatus.js +31 -0
  18. package/dist/combobox/trigger/ComboboxTrigger.js +47 -0
  19. package/dist/select/group/SelectGroup.js +32 -0
  20. package/dist/select/group/SelectGroupContext.js +10 -0
  21. package/dist/select/group-label/SelectGroupLabel.js +26 -0
  22. package/dist/select/index.js +2 -0
  23. package/dist/select/index.parts.js +9 -0
  24. package/dist/select/item/SelectItem.js +97 -0
  25. package/dist/select/item/SelectItemContext.js +10 -0
  26. package/dist/select/item-indicator/SelectItemIndicator.js +22 -0
  27. package/dist/select/popup/SelectPopup.js +92 -0
  28. package/dist/select/root/SelectContext.js +99 -0
  29. package/dist/select/root/SelectRoot.js +21 -0
  30. package/dist/select/trigger/SelectTrigger.js +163 -0
  31. package/dist/select/value/SelectValue.js +32 -0
  32. package/dist/source/combobox/clear/ComboboxClear.d.ts +12 -0
  33. package/dist/source/combobox/clear/ComboboxClear.jsx +36 -0
  34. package/dist/source/combobox/filter/includesText.d.ts +12 -0
  35. package/dist/source/combobox/filter/includesText.js +26 -0
  36. package/dist/source/combobox/group/ComboboxGroup.d.ts +10 -0
  37. package/dist/source/combobox/group/ComboboxGroup.jsx +23 -0
  38. package/dist/source/combobox/group/ComboboxGroupContext.d.ts +10 -0
  39. package/dist/source/combobox/group/ComboboxGroupContext.js +12 -0
  40. package/dist/source/combobox/group-label/ComboboxGroupLabel.d.ts +10 -0
  41. package/dist/source/combobox/group-label/ComboboxGroupLabel.jsx +24 -0
  42. package/dist/source/combobox/index.d.ts +2 -0
  43. package/dist/source/combobox/index.js +4 -0
  44. package/dist/source/combobox/index.parts.d.ts +11 -0
  45. package/dist/source/combobox/index.parts.js +11 -0
  46. package/dist/source/combobox/input/ComboboxInput.d.ts +19 -0
  47. package/dist/source/combobox/input/ComboboxInput.jsx +174 -0
  48. package/dist/source/combobox/item/ComboboxItem.d.ts +25 -0
  49. package/dist/source/combobox/item/ComboboxItem.jsx +93 -0
  50. package/dist/source/combobox/item/ComboboxItemContext.d.ts +11 -0
  51. package/dist/source/combobox/item/ComboboxItemContext.js +12 -0
  52. package/dist/source/combobox/item-indicator/ComboboxItemIndicator.d.ts +11 -0
  53. package/dist/source/combobox/item-indicator/ComboboxItemIndicator.jsx +16 -0
  54. package/dist/source/combobox/list/ComboboxList.d.ts +12 -0
  55. package/dist/source/combobox/list/ComboboxList.jsx +17 -0
  56. package/dist/source/combobox/popup/ComboboxPopup.d.ts +34 -0
  57. package/dist/source/combobox/popup/ComboboxPopup.jsx +76 -0
  58. package/dist/source/combobox/root/ComboboxContext.d.ts +132 -0
  59. package/dist/source/combobox/root/ComboboxContext.js +156 -0
  60. package/dist/source/combobox/root/ComboboxRoot.d.ts +13 -0
  61. package/dist/source/combobox/root/ComboboxRoot.jsx +13 -0
  62. package/dist/source/combobox/status/ComboboxStatus.d.ts +19 -0
  63. package/dist/source/combobox/status/ComboboxStatus.jsx +27 -0
  64. package/dist/source/combobox/trigger/ComboboxTrigger.d.ts +13 -0
  65. package/dist/source/combobox/trigger/ComboboxTrigger.jsx +40 -0
  66. package/dist/source/floating/dismissal/reason.d.ts +7 -2
  67. package/dist/source/select/group/SelectGroup.d.ts +10 -0
  68. package/dist/source/select/group/SelectGroup.jsx +23 -0
  69. package/dist/source/select/group/SelectGroupContext.d.ts +10 -0
  70. package/dist/source/select/group/SelectGroupContext.js +12 -0
  71. package/dist/source/select/group-label/SelectGroupLabel.d.ts +10 -0
  72. package/dist/source/select/group-label/SelectGroupLabel.jsx +24 -0
  73. package/dist/source/select/index.d.ts +1 -0
  74. package/dist/source/select/index.js +1 -0
  75. package/dist/source/select/index.parts.d.ts +8 -0
  76. package/dist/source/select/index.parts.js +8 -0
  77. package/dist/source/select/item/SelectItem.d.ts +24 -0
  78. package/dist/source/select/item/SelectItem.jsx +92 -0
  79. package/dist/source/select/item/SelectItemContext.d.ts +11 -0
  80. package/dist/source/select/item/SelectItemContext.js +12 -0
  81. package/dist/source/select/item-indicator/SelectItemIndicator.d.ts +11 -0
  82. package/dist/source/select/item-indicator/SelectItemIndicator.jsx +16 -0
  83. package/dist/source/select/popup/SelectPopup.d.ts +32 -0
  84. package/dist/source/select/popup/SelectPopup.jsx +97 -0
  85. package/dist/source/select/root/SelectContext.d.ts +106 -0
  86. package/dist/source/select/root/SelectContext.js +119 -0
  87. package/dist/source/select/root/SelectRoot.d.ts +13 -0
  88. package/dist/source/select/root/SelectRoot.jsx +11 -0
  89. package/dist/source/select/trigger/SelectTrigger.d.ts +15 -0
  90. package/dist/source/select/trigger/SelectTrigger.jsx +181 -0
  91. package/dist/source/select/value/SelectValue.d.ts +20 -0
  92. package/dist/source/select/value/SelectValue.jsx +27 -0
  93. package/package.json +11 -1
@@ -0,0 +1,137 @@
1
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
2
+ import { createActiveDescendantList } from "../../floating/list-navigation/activeDescendant.js";
3
+ import { isDev } from "@solidjs/web";
4
+ import { createContext, createMemo, createUniqueId, untrack, useContext } from "solid-js";
5
+ // src/combobox/root/ComboboxContext.ts
6
+ /** Creates a combobox's state and its list navigation. */
7
+ function createCombobox(props) {
8
+ const [open, setOpen] = createControllableSignal({
9
+ get value() {
10
+ return props.open;
11
+ },
12
+ get defaultValue() {
13
+ return props.defaultOpen ?? false;
14
+ },
15
+ onChange: (value, details) => props.onOpenChange?.(value, details)
16
+ });
17
+ const [value, setValue, requestedValue] = createControllableSignal({
18
+ get value() {
19
+ return props.value;
20
+ },
21
+ get defaultValue() {
22
+ return props.defaultValue ?? null;
23
+ },
24
+ onChange: (value) => props.onValueChange?.(value)
25
+ });
26
+ const [text, setText, requestedText] = createControllableSignal({
27
+ get value() {
28
+ return props.inputValue;
29
+ },
30
+ get defaultValue() {
31
+ return props.defaultInputValue ?? null;
32
+ },
33
+ onChange: (text, details) => props.onInputValueChange?.(text, details)
34
+ });
35
+ const list = createActiveDescendantList({
36
+ get open() {
37
+ return open();
38
+ },
39
+ wrap: true
40
+ });
41
+ const labelOfValue = (selected) => {
42
+ return list.items().find((item) => item.value === selected)?.text.trim();
43
+ };
44
+ let labelled = null;
45
+ const selectedLabel = createMemo((previous) => {
46
+ const selected = value();
47
+ const label = labelOfValue(selected) ?? (selected === labelled ? previous : void 0);
48
+ labelled = selected;
49
+ return label;
50
+ });
51
+ const inputValue = createMemo(() => text() ?? selectedLabel() ?? "");
52
+ const requestedInputValue = () => requestedText() ?? untrack(selectedLabel) ?? "";
53
+ let edited = false;
54
+ const setInputValue = (next, details) => {
55
+ edited = details.reason === "input-change";
56
+ if (next !== requestedInputValue()) setText(next, details);
57
+ };
58
+ const commit = (next) => {
59
+ if (next === requestedValue()) return;
60
+ const input = combobox.hiddenInput;
61
+ if (!input) return setValue(next);
62
+ input.value = next ?? "";
63
+ input.dispatchEvent(new Event("input", { bubbles: true }));
64
+ input.dispatchEvent(new Event("change", { bubbles: true }));
65
+ };
66
+ const combobox = {
67
+ open,
68
+ setOpen,
69
+ value,
70
+ setValue,
71
+ inputValue,
72
+ setInputValue,
73
+ select: (item, event) => {
74
+ if (!item || item.disabled) return false;
75
+ commit(item.value);
76
+ setInputValue(item.text.trim(), {
77
+ reason: "item-press",
78
+ event
79
+ });
80
+ return true;
81
+ },
82
+ clear: (event, reason) => {
83
+ commit(null);
84
+ setInputValue("", {
85
+ reason,
86
+ event
87
+ });
88
+ },
89
+ revert: (event) => {
90
+ if (!edited) return;
91
+ if (!requestedInputValue().trim()) {
92
+ commit(null);
93
+ setInputValue("", {
94
+ reason: "focus-out",
95
+ event
96
+ });
97
+ return;
98
+ }
99
+ const selected = requestedValue();
100
+ const label = selected === null ? "" : labelOfValue(selected) ?? selectedLabel() ?? "";
101
+ setInputValue(label, {
102
+ reason: "focus-out",
103
+ event
104
+ });
105
+ },
106
+ labelOfValue,
107
+ isActive: (item) => open() && list.isActive(item),
108
+ count: () => list.items().length,
109
+ list,
110
+ listId: createUniqueId(),
111
+ input: void 0,
112
+ hiddenInput: void 0,
113
+ attributes: {
114
+ get "data-open"() {
115
+ return open() ? "" : void 0;
116
+ },
117
+ get "data-closed"() {
118
+ return open() ? void 0 : "";
119
+ },
120
+ get "data-disabled"() {
121
+ return props.disabled ? "" : void 0;
122
+ },
123
+ get "data-required"() {
124
+ return props.required ? "" : void 0;
125
+ }
126
+ },
127
+ props
128
+ };
129
+ return combobox;
130
+ }
131
+ var ComboboxContext = createContext(null);
132
+ function useComboboxContext() {
133
+ const context = useContext(ComboboxContext);
134
+ if (isDev && !context) throw new Error("[menhir] Combobox parts must be placed within <Combobox.Root>.");
135
+ return context;
136
+ }
137
+ export { ComboboxContext, createCombobox, useComboboxContext };
@@ -0,0 +1,21 @@
1
+ import { ComboboxContext, createCombobox } from "./ComboboxContext.js";
2
+ import { createComponent } from "@solidjs/web";
3
+ // src/combobox/root/ComboboxRoot.tsx
4
+ /**
5
+ * An input that picks a value from a list: holds the value, the text in
6
+ * the input and whether the list is open, and shares them with
7
+ * `Combobox.Input`, `Combobox.Trigger`, `Combobox.Clear`, `Combobox.Popup`
8
+ * and the parts inside it. Its form props go on the hidden input the Input
9
+ * renders. Renders no element of its own.
10
+ */
11
+ function ComboboxRoot(props) {
12
+ return createComponent(ComboboxContext, {
13
+ get value() {
14
+ return createCombobox(props);
15
+ },
16
+ get children() {
17
+ return props.children;
18
+ }
19
+ });
20
+ }
21
+ export { ComboboxRoot };
@@ -0,0 +1,31 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useComboboxContext } from "../root/ComboboxContext.js";
4
+ import { spread, template } from "@solidjs/web";
5
+ import { omit } from "solid-js";
6
+ // src/combobox/status/ComboboxStatus.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<div>`);
8
+ /**
9
+ * The number of items, said in the app's words: a `<div role="status">`, a
10
+ * polite live region, with what its function returns while the list is
11
+ * open, and nothing while it is closed. In `Combobox.Popup`, beside the
12
+ * List, it is the message for an empty list; visually hidden beside the
13
+ * input, it tells a screen reader how many items match as the user types.
14
+ * Place it inside `Combobox.Root`.
15
+ */
16
+ function ComboboxStatus(props) {
17
+ const combobox = useComboboxContext();
18
+ const elementProps = mergeElementProps({
19
+ role: "status",
20
+ get children() {
21
+ const said = props.children(combobox.count());
22
+ return combobox.open() ? said : void 0;
23
+ }
24
+ }, omit(props, "render", "children"));
25
+ return renderElement(props, elementProps, (p) => (() => {
26
+ var _el$ = _tmpl$();
27
+ spread(_el$, p, false);
28
+ return _el$;
29
+ })());
30
+ }
31
+ export { ComboboxStatus };
@@ -0,0 +1,47 @@
1
+ import { warnUnlessButton } from "../../core/button.js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.js";
4
+ import { useComboboxContext } from "../root/ComboboxContext.js";
5
+ import { isDev, spread, template } from "@solidjs/web";
6
+ import { merge, omit } from "solid-js";
7
+ // src/combobox/trigger/ComboboxTrigger.tsx
8
+ var _tmpl$ = /* @__PURE__ */ template(`<button>`);
9
+ /**
10
+ * The button that opens and closes the list, for the pointer and touch: a
11
+ * `<button type="button" tabindex="-1">` with `aria-expanded` and
12
+ * `aria-controls` naming the List. Out of the tab order, since the input
13
+ * takes the keys, and a press leaves focus in the input. Name it with
14
+ * `aria-label`. Place it inside `Combobox.Root`.
15
+ */
16
+ function ComboboxTrigger(props) {
17
+ const combobox = useComboboxContext();
18
+ const ownProps = merge(combobox.attributes, {
19
+ type: "button",
20
+ tabindex: -1,
21
+ get disabled() {
22
+ return combobox.props.disabled;
23
+ },
24
+ get "aria-expanded"() {
25
+ return combobox.open() ? "true" : "false";
26
+ },
27
+ "aria-controls": combobox.listId,
28
+ onMouseDown: (event) => event.preventDefault(),
29
+ onClick: (event) => {
30
+ combobox.setOpen(!combobox.open(), {
31
+ reason: "trigger-press",
32
+ event
33
+ });
34
+ combobox.input?.focus();
35
+ },
36
+ ref: (element) => {
37
+ if (isDev) warnUnlessButton(element, "Combobox.Trigger");
38
+ }
39
+ });
40
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
41
+ return renderElement(props, elementProps, (p) => (() => {
42
+ var _el$ = _tmpl$();
43
+ spread(_el$, p, false);
44
+ return _el$;
45
+ })());
46
+ }
47
+ export { ComboboxTrigger };
@@ -0,0 +1,32 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { createIdSlot } from "../../core/slot.js";
4
+ import { SelectGroupContext } from "./SelectGroupContext.js";
5
+ import { createComponent, spread, template } from "@solidjs/web";
6
+ import { omit } from "solid-js";
7
+ // src/select/group/SelectGroup.tsx
8
+ var _tmpl$ = /* @__PURE__ */ template(`<div>`);
9
+ /**
10
+ * Items that belong together: a `<div role="group">` named by its
11
+ * `Select.GroupLabel`. Place it inside `Select.Popup`.
12
+ */
13
+ function SelectGroup(props) {
14
+ const [labelId, registerLabelId] = createIdSlot();
15
+ const elementProps = mergeElementProps({
16
+ role: "group",
17
+ get "aria-labelledby"() {
18
+ return props["aria-labelledby"] ?? labelId();
19
+ }
20
+ }, omit(props, "render", "aria-labelledby"));
21
+ return createComponent(SelectGroupContext, {
22
+ value: { registerLabelId },
23
+ get children() {
24
+ return renderElement(props, elementProps, (p) => (() => {
25
+ var _el$ = _tmpl$();
26
+ spread(_el$, p, false);
27
+ return _el$;
28
+ })());
29
+ }
30
+ });
31
+ }
32
+ export { SelectGroup };
@@ -0,0 +1,10 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createContext, useContext } from "solid-js";
3
+ // src/select/group/SelectGroupContext.ts
4
+ var SelectGroupContext = createContext(null);
5
+ function useSelectGroupContext() {
6
+ const context = useContext(SelectGroupContext);
7
+ if (isDev && !context) throw new Error("[menhir] Select.GroupLabel must be placed within <Select.Group>.");
8
+ return context;
9
+ }
10
+ export { SelectGroupContext, useSelectGroupContext };
@@ -0,0 +1,26 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useSelectGroupContext } from "../group/SelectGroupContext.js";
4
+ import { spread, template } from "@solidjs/web";
5
+ import { createUniqueId, omit, onSettled } from "solid-js";
6
+ // src/select/group-label/SelectGroupLabel.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<div>`);
8
+ /**
9
+ * The label of a group, which names it in `aria-labelledby`: a `<div>`,
10
+ * not reached by the keys. Place it inside `Select.Group`.
11
+ */
12
+ function SelectGroupLabel(props) {
13
+ const group = useSelectGroupContext();
14
+ const generated = createUniqueId();
15
+ const id = () => props.id ?? generated;
16
+ onSettled(() => group.registerLabelId(id));
17
+ const elementProps = mergeElementProps({ get id() {
18
+ return id();
19
+ } }, omit(props, "render", "id"));
20
+ return renderElement(props, elementProps, (p) => (() => {
21
+ var _el$ = _tmpl$();
22
+ spread(_el$, p, false);
23
+ return _el$;
24
+ })());
25
+ }
26
+ export { SelectGroupLabel };
@@ -0,0 +1,2 @@
1
+ import * as Select from "./index.parts.js";
2
+ export { Select };
@@ -0,0 +1,9 @@
1
+ import { SelectRoot } from "./root/SelectRoot.js";
2
+ import { SelectTrigger } from "./trigger/SelectTrigger.js";
3
+ import { SelectValue } from "./value/SelectValue.js";
4
+ import { SelectPopup } from "./popup/SelectPopup.js";
5
+ import { SelectItem } from "./item/SelectItem.js";
6
+ import { SelectItemIndicator } from "./item-indicator/SelectItemIndicator.js";
7
+ import { SelectGroup } from "./group/SelectGroup.js";
8
+ import { SelectGroupLabel } from "./group-label/SelectGroupLabel.js";
9
+ export { SelectGroup as Group, SelectGroupLabel as GroupLabel, SelectItem as Item, SelectItemIndicator as ItemIndicator, SelectPopup as Popup, SelectRoot as Root, SelectTrigger as Trigger, SelectValue as Value };
@@ -0,0 +1,97 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useSelectContext } from "../root/SelectContext.js";
4
+ import { SelectItemContext } from "./SelectItemContext.js";
5
+ import { createComponent, spread, template } from "@solidjs/web";
6
+ import { createUniqueId, merge, omit, onSettled } from "solid-js";
7
+ // src/select/item/SelectItem.tsx
8
+ var _tmpl$ = /* @__PURE__ */ template(`<div>`);
9
+ /**
10
+ * A value to select: a `<div role="option">` with `aria-selected`. A click
11
+ * selects it and closes the list, and so do Enter, Space and Tab while it
12
+ * is the active item; a disabled item is not selected. Place it inside
13
+ * `Select.Popup`.
14
+ */
15
+ function SelectItem(props) {
16
+ const select = useSelectContext();
17
+ let element;
18
+ const generated = createUniqueId();
19
+ const id = () => props.id || generated;
20
+ const item = {
21
+ get element() {
22
+ return element;
23
+ },
24
+ get id() {
25
+ return id();
26
+ },
27
+ get text() {
28
+ return props.label ?? labelOf(element);
29
+ },
30
+ get value() {
31
+ return props.value;
32
+ },
33
+ get disabled() {
34
+ return props.disabled;
35
+ }
36
+ };
37
+ onSettled(() => select.list.register(item));
38
+ const selected = () => select.value() === props.value;
39
+ const attributes = {
40
+ get "data-selected"() {
41
+ return selected() ? "" : void 0;
42
+ },
43
+ get "data-unselected"() {
44
+ return selected() ? void 0 : "";
45
+ }
46
+ };
47
+ const ownProps = merge(attributes, {
48
+ role: "option",
49
+ get id() {
50
+ return id();
51
+ },
52
+ get "aria-selected"() {
53
+ return selected() ? "true" : "false";
54
+ },
55
+ get "data-active"() {
56
+ return select.isActive(item) ? "" : void 0;
57
+ },
58
+ get "aria-disabled"() {
59
+ return props.disabled ? "true" : void 0;
60
+ },
61
+ get "data-disabled"() {
62
+ return props.disabled ? "" : void 0;
63
+ },
64
+ onPointerMove: (event) => select.list.onPointerMove(item, event),
65
+ onClick: (event) => {
66
+ if (select.select(item)) select.setOpen(false, {
67
+ reason: "item-press",
68
+ event
69
+ });
70
+ },
71
+ ref: (el) => {
72
+ element = el;
73
+ }
74
+ });
75
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "disabled", "label", "id"));
76
+ return createComponent(SelectItemContext, {
77
+ value: attributes,
78
+ get children() {
79
+ return renderElement(props, elementProps, (p) => (() => {
80
+ var _el$ = _tmpl$();
81
+ spread(_el$, p, false);
82
+ return _el$;
83
+ })());
84
+ }
85
+ });
86
+ }
87
+ /**
88
+ * An item's own text: its text content without what is `aria-hidden`, such
89
+ * as its ItemIndicator's check mark, which is no part of its name either.
90
+ */
91
+ function labelOf(node) {
92
+ let text = "";
93
+ for (const child of node.childNodes) if (child.nodeType === 3) text += child.nodeValue;
94
+ else if (child.getAttribute?.("aria-hidden") !== "true") text += labelOf(child);
95
+ return text;
96
+ }
97
+ export { SelectItem };
@@ -0,0 +1,10 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createContext, useContext } from "solid-js";
3
+ // src/select/item/SelectItemContext.ts
4
+ var SelectItemContext = createContext(null);
5
+ function useSelectItemContext() {
6
+ const context = useContext(SelectItemContext);
7
+ if (isDev && !context) throw new Error("[menhir] Select.ItemIndicator must be placed within <Select.Item>.");
8
+ return context;
9
+ }
10
+ export { SelectItemContext, useSelectItemContext };
@@ -0,0 +1,22 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useSelectItemContext } from "../item/SelectItemContext.js";
4
+ import { spread, template } from "@solidjs/web";
5
+ import { merge, omit } from "solid-js";
6
+ // src/select/item-indicator/SelectItemIndicator.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<span>`);
8
+ /**
9
+ * The check mark of an item: an `aria-hidden` `<span>` with its item's
10
+ * `data-selected` or `data-unselected`, always rendered, so that CSS shows
11
+ * it by state. Place it inside `Select.Item`.
12
+ */
13
+ function SelectItemIndicator(props) {
14
+ const ownProps = merge(useSelectItemContext(), { "aria-hidden": "true" });
15
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
16
+ return renderElement(props, elementProps, (p) => (() => {
17
+ var _el$ = _tmpl$();
18
+ spread(_el$, p, false);
19
+ return _el$;
20
+ })());
21
+ }
22
+ export { SelectItemIndicator };
@@ -0,0 +1,92 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { createPopoverClose } from "../../floating/dismissal/popoverClose.js";
4
+ import { createPosition } from "../../floating/positioning/createPosition.js";
5
+ import { flip } from "../../floating/positioning/flip.js";
6
+ import { offset } from "../../floating/positioning/offset.js";
7
+ import { shift } from "../../floating/positioning/shift.js";
8
+ import { size } from "../../floating/positioning/size.js";
9
+ import { createPresence } from "../../floating/presence/presence.js";
10
+ import { useSelectContext } from "../root/SelectContext.js";
11
+ import { spread, template } from "@solidjs/web";
12
+ import { createEffect, merge, omit } from "solid-js";
13
+ // src/select/popup/SelectPopup.tsx
14
+ var _tmpl$ = /* @__PURE__ */ template(`<div>`);
15
+ /**
16
+ * The list itself: a `<div popover="auto" role="listbox">` in the top
17
+ * layer, placed against the Trigger and labelled by it. It takes no focus:
18
+ * the Trigger keeps it. Escape, a press outside, Tab and selecting an item
19
+ * close it. Its CSS variables `--anchor-width` and `--available-height`
20
+ * are for its `min-width` and `max-height`. Place it inside `Select.Root`,
21
+ * right after `Select.Trigger`.
22
+ */
23
+ function SelectPopup(props) {
24
+ const select = useSelectContext();
25
+ let popup;
26
+ const options = {
27
+ get popup() {
28
+ return popup;
29
+ },
30
+ get trigger() {
31
+ return select.trigger;
32
+ },
33
+ get open() {
34
+ return select.open();
35
+ },
36
+ onDismiss: (details) => select.setOpen(false, details)
37
+ };
38
+ createPresence(options);
39
+ createPopoverClose(options);
40
+ createEffect(() => select.initial(), (item) => {
41
+ if (item) queueMicrotask(() => item.element.scrollIntoView({ block: "nearest" }));
42
+ });
43
+ createPosition({
44
+ get anchor() {
45
+ const anchor = props.anchor;
46
+ return (typeof anchor === "function" ? anchor() : anchor) ?? select.trigger;
47
+ },
48
+ get popup() {
49
+ return popup;
50
+ },
51
+ get open() {
52
+ return select.open();
53
+ },
54
+ get side() {
55
+ return props.side ?? "bottom";
56
+ },
57
+ get align() {
58
+ return props.align ?? "start";
59
+ },
60
+ get middleware() {
61
+ const padding = props.collisionPadding ?? 5;
62
+ return [
63
+ offset({
64
+ mainAxis: props.sideOffset ?? 0,
65
+ alignmentAxis: props.alignOffset ?? 0
66
+ }),
67
+ flip({ padding }),
68
+ shift({ padding }),
69
+ size({ padding })
70
+ ];
71
+ }
72
+ });
73
+ const ownProps = merge(select.attributes, {
74
+ id: select.popupId,
75
+ popover: "auto",
76
+ role: "listbox",
77
+ get "aria-labelledby"() {
78
+ return props["aria-labelledby"] ?? select.triggerId();
79
+ },
80
+ onMouseDown: (event) => event.preventDefault(),
81
+ ref: (element) => {
82
+ popup = element;
83
+ }
84
+ });
85
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "side", "align", "sideOffset", "alignOffset", "collisionPadding", "anchor", "aria-labelledby", "id"));
86
+ return renderElement(props, elementProps, (p) => (() => {
87
+ var _el$ = _tmpl$();
88
+ spread(_el$, p, false);
89
+ return _el$;
90
+ })());
91
+ }
92
+ export { SelectPopup };
@@ -0,0 +1,99 @@
1
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
2
+ import { createActiveDescendantList } from "../../floating/list-navigation/activeDescendant.js";
3
+ import { createTypeahead } from "../../floating/typeahead/typeahead.js";
4
+ import { isDev } from "@solidjs/web";
5
+ import { createContext, createMemo, createUniqueId, useContext } from "solid-js";
6
+ // src/select/root/SelectContext.ts
7
+ /** Creates a select's state, its list navigation and its typeahead. */
8
+ function createSelect(props) {
9
+ const [open, setOpen] = createControllableSignal({
10
+ get value() {
11
+ return props.open;
12
+ },
13
+ get defaultValue() {
14
+ return props.defaultOpen ?? false;
15
+ },
16
+ onChange: (value, details) => props.onOpenChange?.(value, details)
17
+ });
18
+ const [value, setValue, requested] = createControllableSignal({
19
+ get value() {
20
+ return props.value;
21
+ },
22
+ get defaultValue() {
23
+ return props.defaultValue ?? null;
24
+ },
25
+ onChange: (value) => props.onValueChange?.(value)
26
+ });
27
+ const list = createActiveDescendantList({ get open() {
28
+ return open();
29
+ } });
30
+ const typeahead = createTypeahead({
31
+ get open() {
32
+ return open();
33
+ },
34
+ get items() {
35
+ return list.items();
36
+ },
37
+ get active() {
38
+ return active();
39
+ },
40
+ move: (item) => list.move(item)
41
+ });
42
+ const selectedItem = createMemo(() => {
43
+ const selected = value();
44
+ return list.items().find((item) => item.value === selected);
45
+ });
46
+ const initial = createMemo(() => open() && !list.active() ? selectedItem() ?? list.items()[0] : void 0);
47
+ const active = () => list.active() ?? initial();
48
+ const select = {
49
+ open,
50
+ setOpen,
51
+ value,
52
+ setValue,
53
+ select: (item) => {
54
+ if (!item || item.disabled) return false;
55
+ const input = select.input;
56
+ if (item.value !== requested()) {
57
+ if (input) {
58
+ input.value = item.value;
59
+ input.dispatchEvent(new Event("input", { bubbles: true }));
60
+ input.dispatchEvent(new Event("change", { bubbles: true }));
61
+ } else setValue(item.value);
62
+ }
63
+ return true;
64
+ },
65
+ selectedItem,
66
+ active,
67
+ isActive: (item) => open() && (list.isActive(item) || initial() === item),
68
+ initial,
69
+ list,
70
+ typeahead,
71
+ popupId: createUniqueId(),
72
+ triggerId: () => void 0,
73
+ trigger: void 0,
74
+ input: void 0,
75
+ attributes: {
76
+ get "data-open"() {
77
+ return open() ? "" : void 0;
78
+ },
79
+ get "data-closed"() {
80
+ return open() ? void 0 : "";
81
+ },
82
+ get "data-disabled"() {
83
+ return props.disabled ? "" : void 0;
84
+ },
85
+ get "data-required"() {
86
+ return props.required ? "" : void 0;
87
+ }
88
+ },
89
+ props
90
+ };
91
+ return select;
92
+ }
93
+ var SelectContext = createContext(null);
94
+ function useSelectContext() {
95
+ const context = useContext(SelectContext);
96
+ if (isDev && !context) throw new Error("[menhir] Select parts must be placed within <Select.Root>.");
97
+ return context;
98
+ }
99
+ export { SelectContext, createSelect, useSelectContext };
@@ -0,0 +1,21 @@
1
+ import { SelectContext, createSelect } from "./SelectContext.js";
2
+ import { createComponent } from "@solidjs/web";
3
+ // src/select/root/SelectRoot.tsx
4
+ /**
5
+ * A value selected from a list that a button opens: holds the value and
6
+ * whether the list is open, and shares them with `Select.Trigger`,
7
+ * `Select.Value`, `Select.Popup` and the items inside it. Its form props
8
+ * go on the hidden input the Trigger renders. Renders no element of its
9
+ * own.
10
+ */
11
+ function SelectRoot(props) {
12
+ return createComponent(SelectContext, {
13
+ get value() {
14
+ return createSelect(props);
15
+ },
16
+ get children() {
17
+ return props.children;
18
+ }
19
+ });
20
+ }
21
+ export { SelectRoot };