@ariakit/components 0.1.0
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/CHANGELOG.md +19 -0
- package/dist/checkbox/checkbox-store.d.ts +47 -0
- package/dist/checkbox/checkbox-store.d.ts.map +1 -0
- package/dist/checkbox/checkbox-store.js +16 -0
- package/dist/checkbox/checkbox-store.js.map +1 -0
- package/dist/collection/collection-store.d.ts +2 -0
- package/dist/collection/collection-store.js +132 -0
- package/dist/collection/collection-store.js.map +1 -0
- package/dist/collection-store-yNe83BiS.d.ts +81 -0
- package/dist/collection-store-yNe83BiS.d.ts.map +1 -0
- package/dist/combobox/combobox-store.d.ts +150 -0
- package/dist/combobox/combobox-store.d.ts.map +1 -0
- package/dist/combobox/combobox-store.js +83 -0
- package/dist/combobox/combobox-store.js.map +1 -0
- package/dist/composite/composite-overflow-store.d.ts +16 -0
- package/dist/composite/composite-overflow-store.d.ts.map +1 -0
- package/dist/composite/composite-overflow-store.js +12 -0
- package/dist/composite/composite-overflow-store.js.map +1 -0
- package/dist/composite/composite-store.d.ts +2 -0
- package/dist/composite/composite-store.js +167 -0
- package/dist/composite/composite-store.js.map +1 -0
- package/dist/composite-store-B-iDEtZZ.d.ts +331 -0
- package/dist/composite-store-B-iDEtZZ.d.ts.map +1 -0
- package/dist/dialog/dialog-store.d.ts +2 -0
- package/dist/dialog/dialog-store.js +12 -0
- package/dist/dialog/dialog-store.js.map +1 -0
- package/dist/dialog-store-BOLvw2IX.d.ts +16 -0
- package/dist/dialog-store-BOLvw2IX.d.ts.map +1 -0
- package/dist/disclosure/disclosure-store.d.ts +2 -0
- package/dist/disclosure/disclosure-store.js +47 -0
- package/dist/disclosure/disclosure-store.js.map +1 -0
- package/dist/disclosure-store-xKlQffR0.d.ts +142 -0
- package/dist/disclosure-store-xKlQffR0.d.ts.map +1 -0
- package/dist/form/form-store.d.ts +247 -0
- package/dist/form/form-store.d.ts.map +1 -0
- package/dist/form/form-store.js +211 -0
- package/dist/form/form-store.js.map +1 -0
- package/dist/form/types.d.ts +37 -0
- package/dist/form/types.d.ts.map +1 -0
- package/dist/form/types.js +0 -0
- package/dist/hovercard/hovercard-store.d.ts +65 -0
- package/dist/hovercard/hovercard-store.d.ts.map +1 -0
- package/dist/hovercard/hovercard-store.js +31 -0
- package/dist/hovercard/hovercard-store.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/menu/menu-bar-store.d.ts +16 -0
- package/dist/menu/menu-bar-store.d.ts.map +1 -0
- package/dist/menu/menu-bar-store.js +12 -0
- package/dist/menu/menu-bar-store.js.map +1 -0
- package/dist/menu/menu-store.d.ts +100 -0
- package/dist/menu/menu-store.d.ts.map +1 -0
- package/dist/menu/menu-store.js +74 -0
- package/dist/menu/menu-store.js.map +1 -0
- package/dist/menubar/menubar-store.d.ts +2 -0
- package/dist/menubar/menubar-store.js +24 -0
- package/dist/menubar/menubar-store.js.map +1 -0
- package/dist/menubar-store-CD3YDYfW.d.ts +16 -0
- package/dist/menubar-store-CD3YDYfW.d.ts.map +1 -0
- package/dist/popover/popover-store.d.ts +2 -0
- package/dist/popover/popover-store.js +44 -0
- package/dist/popover/popover-store.js.map +1 -0
- package/dist/popover-store-DoCiTmUQ.d.ts +106 -0
- package/dist/popover-store-DoCiTmUQ.d.ts.map +1 -0
- package/dist/radio/radio-store.d.ts +42 -0
- package/dist/radio/radio-store.d.ts.map +1 -0
- package/dist/radio/radio-store.js +27 -0
- package/dist/radio/radio-store.js.map +1 -0
- package/dist/select/select-store.d.ts +116 -0
- package/dist/select/select-store.d.ts.map +1 -0
- package/dist/select/select-store.js +93 -0
- package/dist/select/select-store.js.map +1 -0
- package/dist/tab/tab-store.d.ts +127 -0
- package/dist/tab/tab-store.d.ts.map +1 -0
- package/dist/tab/tab-store.js +107 -0
- package/dist/tab/tab-store.js.map +1 -0
- package/dist/tag/tag-store.d.ts +2 -0
- package/dist/tag/tag-store.js +60 -0
- package/dist/tag/tag-store.js.map +1 -0
- package/dist/tag-store-D47X5_zA.d.ts +83 -0
- package/dist/tag-store-D47X5_zA.d.ts.map +1 -0
- package/dist/toolbar/toolbar-store.d.ts +21 -0
- package/dist/toolbar/toolbar-store.d.ts.map +1 -0
- package/dist/toolbar/toolbar-store.js +18 -0
- package/dist/toolbar/toolbar-store.js.map +1 -0
- package/dist/tooltip/tooltip-store.d.ts +35 -0
- package/dist/tooltip/tooltip-store.d.ts.map +1 -0
- package/dist/tooltip/tooltip-store.js +29 -0
- package/dist/tooltip/tooltip-store.js.map +1 -0
- package/license +21 -0
- package/package.json +121 -0
- package/readme.md +19 -0
- package/src/checkbox/checkbox-store.ts +93 -0
- package/src/collection/collection-store.ts +301 -0
- package/src/combobox/combobox-store.ts +382 -0
- package/src/composite/composite-overflow-store.ts +30 -0
- package/src/composite/composite-store.ts +711 -0
- package/src/dialog/dialog-store.ts +26 -0
- package/src/disclosure/disclosure-store.ts +226 -0
- package/src/form/form-store.ts +608 -0
- package/src/form/types.ts +44 -0
- package/src/hovercard/hovercard-store.ts +112 -0
- package/src/index.ts +1 -0
- package/src/menu/menu-bar-store.ts +28 -0
- package/src/menu/menu-store.ts +263 -0
- package/src/menubar/menubar-store.ts +51 -0
- package/src/popover/popover-store.ts +170 -0
- package/src/radio/radio-store.ts +80 -0
- package/src/select/select-store.ts +323 -0
- package/src/tab/tab-store.ts +330 -0
- package/src/tag/tag-store.ts +170 -0
- package/src/toolbar/toolbar-store.ts +47 -0
- package/src/tooltip/tooltip-store.ts +93 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @ariakit/components
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Added component packages
|
|
6
|
+
|
|
7
|
+
The internal component packages are now available under these names:
|
|
8
|
+
|
|
9
|
+
- `@ariakit/components`
|
|
10
|
+
- `@ariakit/react-components`
|
|
11
|
+
- `@ariakit/solid-components`
|
|
12
|
+
|
|
13
|
+
### Other updates
|
|
14
|
+
|
|
15
|
+
- Updated dependencies: `@ariakit/utils@0.1.0`, `@ariakit/store@0.1.0`
|
|
16
|
+
|
|
17
|
+
## 0.0.0
|
|
18
|
+
|
|
19
|
+
Initial release.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Store, StoreOptions, StoreProps } from "@ariakit/store";
|
|
2
|
+
import { PickRequired, SetState, ToPrimitive } from "@ariakit/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/checkbox/checkbox-store.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a checkbox store.
|
|
7
|
+
*/
|
|
8
|
+
declare function createCheckboxStore<T extends CheckboxStoreValue = CheckboxStoreValue>(props: PickRequired<CheckboxStoreProps<T>, "value" | "defaultValue">): CheckboxStore<T>;
|
|
9
|
+
declare function createCheckboxStore(props?: CheckboxStoreProps): CheckboxStore;
|
|
10
|
+
type CheckboxStoreValue = boolean | string | number | ReadonlyArray<string | number>;
|
|
11
|
+
interface CheckboxStoreState<T extends CheckboxStoreValue = CheckboxStoreValue> {
|
|
12
|
+
/**
|
|
13
|
+
* The checked state of the checkbox.
|
|
14
|
+
*
|
|
15
|
+
* Live examples:
|
|
16
|
+
* - [Custom Checkbox](https://ariakit.com/examples/checkbox-custom)
|
|
17
|
+
*/
|
|
18
|
+
value: ToPrimitive<T>;
|
|
19
|
+
}
|
|
20
|
+
interface CheckboxStoreFunctions<T extends CheckboxStoreValue = CheckboxStoreValue> {
|
|
21
|
+
/**
|
|
22
|
+
* Sets the [`value`](https://ariakit.com/reference/checkbox-provider#value)
|
|
23
|
+
* state.
|
|
24
|
+
* @example
|
|
25
|
+
* store.setValue(true);
|
|
26
|
+
* store.setValue((value) => !value);
|
|
27
|
+
*/
|
|
28
|
+
setValue: SetState<CheckboxStoreState<T>["value"]>;
|
|
29
|
+
}
|
|
30
|
+
interface CheckboxStoreOptions<T extends CheckboxStoreValue = CheckboxStoreValue> extends StoreOptions<CheckboxStoreState<T>, "value"> {
|
|
31
|
+
/**
|
|
32
|
+
* The default
|
|
33
|
+
* [`value`](https://ariakit.com/reference/checkbox-provider#value) state of
|
|
34
|
+
* the checkbox.
|
|
35
|
+
*
|
|
36
|
+
* Live examples:
|
|
37
|
+
* - [Custom Checkbox](https://ariakit.com/examples/checkbox-custom)
|
|
38
|
+
* - [Checkbox group](https://ariakit.com/examples/checkbox-group)
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
defaultValue?: CheckboxStoreState<T>["value"];
|
|
42
|
+
}
|
|
43
|
+
interface CheckboxStoreProps<T extends CheckboxStoreValue = CheckboxStoreValue> extends CheckboxStoreOptions<T>, StoreProps<CheckboxStoreState<T>> {}
|
|
44
|
+
interface CheckboxStore<T extends CheckboxStoreValue = CheckboxStoreValue> extends CheckboxStoreFunctions<T>, Store<CheckboxStoreState<T>> {}
|
|
45
|
+
//#endregion
|
|
46
|
+
export { CheckboxStore, CheckboxStoreFunctions, CheckboxStoreOptions, CheckboxStoreProps, CheckboxStoreState, CheckboxStoreValue, createCheckboxStore };
|
|
47
|
+
//# sourceMappingURL=checkbox-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkbox-store.d.ts","names":["Store","StoreOptions","StoreProps","PickRequired","SetState","ToPrimitive","createCheckboxStore","T","CheckboxStoreValue","CheckboxStoreProps","CheckboxStore","props","ReadonlyArray","CheckboxStoreState","value","CheckboxStoreFunctions","setValue","CheckboxStoreOptions","defaultValue"],"sources":["../../src/checkbox/checkbox-store.d.ts"],"mappings":";;;;;;AAKA;iBAAwBM,mBAAAA,WAA8BE,kBAAAA,GAAqBA,kBAAAA,CAAAA,CAAoBG,KAAAA,EAAOR,YAAAA,CAAaM,kBAAAA,CAAmBF,CAAAA,+BAAgCG,aAAAA,CAAcH,CAAAA;AAAAA,iBAC5JD,mBAAAA,CAAoBK,KAAAA,GAAQF,kBAAAA,GAAqBC,aAAa;AAAA,KAC1EF,kBAAAA,+BAAiDI,aAAa;AAAA,UACzDC,kBAAAA,WAA6BL,kBAAAA,GAAqBA,kBAAAA;EAHmED;;;;;;EAUlIO,KAAAA,EAAOT,WAAAA,CAAYE,CAAAA;AAAAA;AAAAA,UAENQ,sBAAAA,WAAiCP,kBAAAA,GAAqBA,kBAAAA;EAZIA;;;;;;;EAoBvEQ,QAAAA,EAAUZ,QAAAA,CAASS,kBAAAA,CAAmBN,CAAAA;AAAAA;AAAAA,UAEzBU,oBAAAA,WAA+BT,kBAAAA,GAAqBA,kBAAAA,UAA4BP,YAAAA,CAAaY,kBAAAA,CAAmBN,CAAAA;EArBtF;;;;;;;AAA2C;AACtF;;EA+BIW,YAAAA,GAAeL,kBAAAA,CAAmBN,CAAAA;AAAAA;AAAAA,UAErBE,kBAAAA,WAA6BD,kBAAAA,GAAqBA,kBAAAA,UAA4BS,oBAAAA,CAAqBV,CAAAA,GAAIL,UAAAA,CAAWW,kBAAAA,CAAmBN,CAAAA;AAAAA,UAErIG,aAAAA,WAAwBF,kBAAAA,GAAqBA,kBAAAA,UAA4BO,sBAAAA,CAAuBR,CAAAA,GAAIP,KAAAA,CAAMa,kBAAAA,CAAmBN,CAAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createStore, throwOnConflictingProps } from "@ariakit/store";
|
|
2
|
+
import { defaultValue } from "@ariakit/utils";
|
|
3
|
+
//#region src/checkbox/checkbox-store.ts
|
|
4
|
+
function createCheckboxStore(props = {}) {
|
|
5
|
+
throwOnConflictingProps(props, props.store);
|
|
6
|
+
const syncState = props.store?.getState();
|
|
7
|
+
const checkbox = createStore({ value: defaultValue(props.value, syncState?.value, props.defaultValue, false) }, props.store);
|
|
8
|
+
return {
|
|
9
|
+
...checkbox,
|
|
10
|
+
setValue: (value) => checkbox.setState("value", value)
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { createCheckboxStore };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=checkbox-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkbox-store.js","names":[],"sources":["../../src/checkbox/checkbox-store.ts"],"sourcesContent":["import { createStore, throwOnConflictingProps } from \"@ariakit/store\";\nimport type { Store, StoreOptions, StoreProps } from \"@ariakit/store\";\nimport { defaultValue } from \"@ariakit/utils\";\nimport type { PickRequired, SetState, ToPrimitive } from \"@ariakit/utils\";\n\n/**\n * Creates a checkbox store.\n */\nexport function createCheckboxStore<\n T extends CheckboxStoreValue = CheckboxStoreValue,\n>(\n props: PickRequired<CheckboxStoreProps<T>, \"value\" | \"defaultValue\">,\n): CheckboxStore<T>;\n\nexport function createCheckboxStore(props?: CheckboxStoreProps): CheckboxStore;\n\nexport function createCheckboxStore(\n props: CheckboxStoreProps = {},\n): CheckboxStore {\n throwOnConflictingProps(props, props.store);\n\n const syncState = props.store?.getState();\n const initialState: CheckboxStoreState = {\n value: defaultValue(\n props.value,\n syncState?.value,\n props.defaultValue,\n false,\n ),\n };\n const checkbox = createStore(initialState, props.store);\n return {\n ...checkbox,\n setValue: (value) => checkbox.setState(\"value\", value),\n };\n}\n\nexport type CheckboxStoreValue =\n | boolean\n | string\n | number\n | ReadonlyArray<string | number>;\n\nexport interface CheckboxStoreState<\n T extends CheckboxStoreValue = CheckboxStoreValue,\n> {\n /**\n * The checked state of the checkbox.\n *\n * Live examples:\n * - [Custom Checkbox](https://ariakit.com/examples/checkbox-custom)\n */\n value: ToPrimitive<T>;\n}\n\nexport interface CheckboxStoreFunctions<\n T extends CheckboxStoreValue = CheckboxStoreValue,\n> {\n /**\n * Sets the [`value`](https://ariakit.com/reference/checkbox-provider#value)\n * state.\n * @example\n * store.setValue(true);\n * store.setValue((value) => !value);\n */\n setValue: SetState<CheckboxStoreState<T>[\"value\"]>;\n}\n\nexport interface CheckboxStoreOptions<\n T extends CheckboxStoreValue = CheckboxStoreValue,\n> extends StoreOptions<CheckboxStoreState<T>, \"value\"> {\n /**\n * The default\n * [`value`](https://ariakit.com/reference/checkbox-provider#value) state of\n * the checkbox.\n *\n * Live examples:\n * - [Custom Checkbox](https://ariakit.com/examples/checkbox-custom)\n * - [Checkbox group](https://ariakit.com/examples/checkbox-group)\n * @default false\n */\n defaultValue?: CheckboxStoreState<T>[\"value\"];\n}\n\nexport interface CheckboxStoreProps<\n T extends CheckboxStoreValue = CheckboxStoreValue,\n>\n extends CheckboxStoreOptions<T>, StoreProps<CheckboxStoreState<T>> {}\n\nexport interface CheckboxStore<\n T extends CheckboxStoreValue = CheckboxStoreValue,\n>\n extends CheckboxStoreFunctions<T>, Store<CheckboxStoreState<T>> {}\n"],"mappings":";;;AAgBA,SAAgB,oBACd,QAA4B,CAAC,GACd;CACf,wBAAwB,OAAO,MAAM,KAAK;CAE1C,MAAM,YAAY,MAAM,OAAO,SAAS;CASxC,MAAM,WAAW,YAAY,EAP3B,OAAO,aACL,MAAM,OACN,WAAW,OACX,MAAM,cACN,KACF,EAEsC,GAAG,MAAM,KAAK;CACtD,OAAO;EACL,GAAG;EACH,WAAW,UAAU,SAAS,SAAS,SAAS,KAAK;CACvD;AACF"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as CollectionStoreProps, i as CollectionStoreOptions, n as CollectionStoreFunctions, o as CollectionStoreState, r as CollectionStoreItem, s as createCollectionStore, t as CollectionStore } from "../collection-store-yNe83BiS.js";
|
|
2
|
+
export { CollectionStore, CollectionStoreFunctions, CollectionStoreItem, CollectionStoreOptions, CollectionStoreProps, CollectionStoreState, createCollectionStore };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { batch, createStore, init, setup, throwOnConflictingProps } from "@ariakit/store";
|
|
2
|
+
import { chain, defaultValue, getDocument, sortBasedOnDOMPosition } from "@ariakit/utils";
|
|
3
|
+
//#region src/collection/collection-store.ts
|
|
4
|
+
function getCommonParent(items) {
|
|
5
|
+
const firstItem = items.find((item) => !!item.element);
|
|
6
|
+
const lastElement = [...items].reverse().find((item) => !!item.element)?.element;
|
|
7
|
+
let parentElement = firstItem?.element?.parentElement;
|
|
8
|
+
if (!lastElement) return getDocument(parentElement).body;
|
|
9
|
+
while (parentElement) {
|
|
10
|
+
if (parentElement.contains(lastElement)) return parentElement;
|
|
11
|
+
parentElement = parentElement.parentElement;
|
|
12
|
+
}
|
|
13
|
+
return getDocument(parentElement).body;
|
|
14
|
+
}
|
|
15
|
+
function getPrivateStore(store) {
|
|
16
|
+
return store?.__unstablePrivateStore;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a collection store.
|
|
20
|
+
*/
|
|
21
|
+
function createCollectionStore(props = {}) {
|
|
22
|
+
throwOnConflictingProps(props, props.store);
|
|
23
|
+
const syncState = props.store?.getState();
|
|
24
|
+
const items = defaultValue(props.items, syncState?.items, props.defaultItems, []);
|
|
25
|
+
const itemsMap = new Map(items.map((item) => [item.id, item]));
|
|
26
|
+
const initialState = {
|
|
27
|
+
items,
|
|
28
|
+
renderedItems: defaultValue(syncState?.renderedItems, [])
|
|
29
|
+
};
|
|
30
|
+
const syncPrivateStore = getPrivateStore(props.store);
|
|
31
|
+
const privateStore = createStore({
|
|
32
|
+
items,
|
|
33
|
+
renderedItems: initialState.renderedItems
|
|
34
|
+
}, syncPrivateStore);
|
|
35
|
+
const collection = createStore(initialState, props.store);
|
|
36
|
+
const sortItems = (renderedItems) => {
|
|
37
|
+
const sortedItems = sortBasedOnDOMPosition(renderedItems, (i) => i.element);
|
|
38
|
+
privateStore.setState("renderedItems", sortedItems);
|
|
39
|
+
collection.setState("renderedItems", sortedItems);
|
|
40
|
+
};
|
|
41
|
+
setup(collection, () => init(privateStore));
|
|
42
|
+
setup(privateStore, () => {
|
|
43
|
+
return batch(privateStore, ["items"], (state) => {
|
|
44
|
+
collection.setState("items", state.items);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
setup(privateStore, () => {
|
|
48
|
+
return batch(privateStore, ["renderedItems"], (state) => {
|
|
49
|
+
let firstRun = true;
|
|
50
|
+
let raf = requestAnimationFrame(() => {
|
|
51
|
+
const { renderedItems } = collection.getState();
|
|
52
|
+
if (state.renderedItems === renderedItems) return;
|
|
53
|
+
sortItems(state.renderedItems);
|
|
54
|
+
});
|
|
55
|
+
if (typeof IntersectionObserver !== "function") return () => cancelAnimationFrame(raf);
|
|
56
|
+
const ioCallback = () => {
|
|
57
|
+
if (firstRun) {
|
|
58
|
+
firstRun = false;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
cancelAnimationFrame(raf);
|
|
62
|
+
raf = requestAnimationFrame(() => sortItems(state.renderedItems));
|
|
63
|
+
};
|
|
64
|
+
const root = getCommonParent(state.renderedItems);
|
|
65
|
+
const observer = new IntersectionObserver(ioCallback, { root });
|
|
66
|
+
for (const item of state.renderedItems) {
|
|
67
|
+
if (!item.element) continue;
|
|
68
|
+
observer.observe(item.element);
|
|
69
|
+
}
|
|
70
|
+
return () => {
|
|
71
|
+
cancelAnimationFrame(raf);
|
|
72
|
+
observer.disconnect();
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
const mergeItem = (item, setItems, canDeleteFromMap = false) => {
|
|
77
|
+
let prevItem;
|
|
78
|
+
setItems((items) => {
|
|
79
|
+
const index = items.findIndex(({ id }) => id === item.id);
|
|
80
|
+
const nextItems = items.slice();
|
|
81
|
+
if (index !== -1) {
|
|
82
|
+
prevItem = items[index];
|
|
83
|
+
const nextItem = {
|
|
84
|
+
...prevItem,
|
|
85
|
+
...item
|
|
86
|
+
};
|
|
87
|
+
nextItems[index] = nextItem;
|
|
88
|
+
itemsMap.set(item.id, nextItem);
|
|
89
|
+
} else {
|
|
90
|
+
nextItems.push(item);
|
|
91
|
+
itemsMap.set(item.id, item);
|
|
92
|
+
}
|
|
93
|
+
return nextItems;
|
|
94
|
+
});
|
|
95
|
+
const unmergeItem = () => {
|
|
96
|
+
setItems((items) => {
|
|
97
|
+
if (!prevItem) {
|
|
98
|
+
if (canDeleteFromMap) itemsMap.delete(item.id);
|
|
99
|
+
return items.filter(({ id }) => id !== item.id);
|
|
100
|
+
}
|
|
101
|
+
const index = items.findIndex(({ id }) => id === item.id);
|
|
102
|
+
if (index === -1) return items;
|
|
103
|
+
const nextItems = items.slice();
|
|
104
|
+
nextItems[index] = prevItem;
|
|
105
|
+
itemsMap.set(item.id, prevItem);
|
|
106
|
+
return nextItems;
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
return unmergeItem;
|
|
110
|
+
};
|
|
111
|
+
const registerItem = (item) => mergeItem(item, (getItems) => privateStore.setState("items", getItems), true);
|
|
112
|
+
return {
|
|
113
|
+
...collection,
|
|
114
|
+
registerItem,
|
|
115
|
+
renderItem: (item) => chain(registerItem(item), mergeItem(item, (getItems) => privateStore.setState("renderedItems", getItems))),
|
|
116
|
+
item: (id) => {
|
|
117
|
+
if (!id) return null;
|
|
118
|
+
let item = itemsMap.get(id);
|
|
119
|
+
if (!item) {
|
|
120
|
+
const { items } = privateStore.getState();
|
|
121
|
+
item = items.find((item) => item.id === id);
|
|
122
|
+
if (item) itemsMap.set(id, item);
|
|
123
|
+
}
|
|
124
|
+
return item || null;
|
|
125
|
+
},
|
|
126
|
+
__unstablePrivateStore: privateStore
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
export { createCollectionStore };
|
|
131
|
+
|
|
132
|
+
//# sourceMappingURL=collection-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection-store.js","names":["parent"],"sources":["../../src/collection/collection-store.ts"],"sourcesContent":["import {\n batch,\n createStore,\n init,\n setup,\n throwOnConflictingProps,\n} from \"@ariakit/store\";\nimport type { Store, StoreOptions, StoreProps } from \"@ariakit/store\";\nimport {\n getDocument,\n sortBasedOnDOMPosition,\n chain,\n defaultValue,\n} from \"@ariakit/utils\";\nimport type { BivariantCallback } from \"@ariakit/utils\";\n\nfunction getCommonParent(items: CollectionStoreItem[]) {\n const firstItem = items.find((item) => !!item.element);\n const lastElement = [...items]\n .reverse()\n .find((item) => !!item.element)?.element;\n let parentElement = firstItem?.element?.parentElement;\n if (!lastElement) {\n return getDocument(parentElement).body;\n }\n while (parentElement) {\n const parent = parentElement;\n if (parent.contains(lastElement)) {\n return parentElement;\n }\n parentElement = parentElement.parentElement;\n }\n return getDocument(parentElement).body;\n}\n\nfunction getPrivateStore<T extends CollectionStoreItem>(\n store?: Store & {\n __unstablePrivateStore?: Store<{\n renderedItems: T[];\n }>;\n },\n) {\n return store?.__unstablePrivateStore;\n}\n\n/**\n * Creates a collection store.\n */\nexport function createCollectionStore<\n T extends CollectionStoreItem = CollectionStoreItem,\n>(props: CollectionStoreProps<T> = {}): CollectionStore<T> {\n throwOnConflictingProps(props, props.store);\n\n const syncState = props.store?.getState();\n\n const items = defaultValue(\n props.items,\n syncState?.items,\n props.defaultItems,\n [],\n );\n\n const itemsMap = new Map<string, T>(items.map((item) => [item.id, item]));\n\n const initialState: CollectionStoreState<T> = {\n items,\n renderedItems: defaultValue(syncState?.renderedItems, []),\n };\n\n const syncPrivateStore = getPrivateStore<T>(props.store);\n\n const privateStore = createStore(\n { items, renderedItems: initialState.renderedItems },\n syncPrivateStore,\n );\n\n const collection = createStore(initialState, props.store);\n\n const sortItems = (renderedItems: T[]) => {\n const sortedItems = sortBasedOnDOMPosition(renderedItems, (i) => i.element);\n privateStore.setState(\"renderedItems\", sortedItems);\n collection.setState(\"renderedItems\", sortedItems);\n };\n\n setup(collection, () => init(privateStore));\n\n // Use the private store to register items and then batch the changes to the\n // public store so we don't trigger multiple updates on the store when adding\n // multiple items.\n setup(privateStore, () => {\n return batch(privateStore, [\"items\"], (state) => {\n collection.setState(\"items\", state.items);\n });\n });\n\n setup(privateStore, () => {\n return batch(privateStore, [\"renderedItems\"], (state) => {\n let firstRun = true;\n\n let raf = requestAnimationFrame(() => {\n const { renderedItems } = collection.getState();\n // Bail out if the rendered items haven't changed. This is important\n // because the following lines can cause this function to be called\n // again.\n if (state.renderedItems === renderedItems) return;\n sortItems(state.renderedItems);\n });\n\n if (typeof IntersectionObserver !== \"function\") {\n return () => cancelAnimationFrame(raf);\n }\n\n const ioCallback: IntersectionObserverCallback = () => {\n if (firstRun) {\n // The IntersectionObserver callback is called synchronously the first\n // time. We just ignore it.\n firstRun = false;\n return;\n }\n cancelAnimationFrame(raf);\n raf = requestAnimationFrame(() => sortItems(state.renderedItems));\n };\n\n const root = getCommonParent(state.renderedItems);\n const observer = new IntersectionObserver(ioCallback, { root });\n\n for (const item of state.renderedItems) {\n if (!item.element) continue;\n observer.observe(item.element);\n }\n\n return () => {\n cancelAnimationFrame(raf);\n observer.disconnect();\n };\n });\n });\n\n const mergeItem = (\n item: T,\n setItems: (getItems: (items: T[]) => T[]) => void,\n canDeleteFromMap = false,\n ) => {\n let prevItem: T | undefined;\n setItems((items) => {\n const index = items.findIndex(({ id }) => id === item.id);\n const nextItems = items.slice();\n if (index !== -1) {\n prevItem = items[index];\n const nextItem = { ...prevItem, ...item };\n nextItems[index] = nextItem;\n itemsMap.set(item.id, nextItem);\n } else {\n nextItems.push(item);\n itemsMap.set(item.id, item);\n }\n return nextItems;\n });\n const unmergeItem = () => {\n setItems((items) => {\n if (!prevItem) {\n if (canDeleteFromMap) {\n itemsMap.delete(item.id);\n }\n return items.filter(({ id }) => id !== item.id);\n }\n const index = items.findIndex(({ id }) => id === item.id);\n if (index === -1) return items;\n const nextItems = items.slice();\n nextItems[index] = prevItem;\n itemsMap.set(item.id, prevItem);\n return nextItems;\n });\n };\n return unmergeItem;\n };\n\n const registerItem: CollectionStore<T>[\"registerItem\"] = (item) =>\n mergeItem(\n item,\n (getItems) => privateStore.setState(\"items\", getItems),\n true,\n );\n\n return {\n ...collection,\n\n registerItem,\n renderItem: (item) =>\n chain(\n registerItem(item),\n mergeItem(item, (getItems) =>\n privateStore.setState(\"renderedItems\", getItems),\n ),\n ),\n\n item: (id) => {\n if (!id) return null;\n let item = itemsMap.get(id);\n if (!item) {\n const { items } = privateStore.getState();\n item = items.find((item) => item.id === id);\n if (item) {\n itemsMap.set(id, item);\n }\n }\n return item || null;\n },\n\n // @ts-expect-error Internal\n __unstablePrivateStore: privateStore,\n };\n}\n\nexport interface CollectionStoreItem {\n /**\n * The id of the item.\n */\n id: string;\n /**\n * The item HTML element. This is automatically set when the item is rendered.\n */\n element?: HTMLElement | null;\n}\n\nexport interface CollectionStoreState<\n T extends CollectionStoreItem = CollectionStoreItem,\n> {\n /**\n * Lists all items along with their metadata. This state is automatically\n * updated when an item is registered or unregistered using the\n * [`registerItem`](https://ariakit.com/reference/use-collection-store#registeritem)\n * function.\n *\n * Live examples:\n * - [Command Menu with\n * Tabs](https://ariakit.com/examples/dialog-combobox-tab-command-menu)\n */\n items: T[];\n /**\n * Lists all items, along with their metadata, in the exact order they appear in\n * the DOM. This state is automatically updated when an item is rendered or\n * unmounted using the\n * [`renderItem`](https://ariakit.com/reference/use-collection-store#renderitem)\n * function.\n */\n renderedItems: T[];\n}\n\nexport interface CollectionStoreFunctions<\n T extends CollectionStoreItem = CollectionStoreItem,\n> {\n /**\n * Registers an item in the collection. This function returns a cleanup\n * function that unregisters the item.\n * @example\n * const unregisterItem = store.registerItem({ id: \"item-1\" });\n * // on cleanup\n * unregisterItem();\n */\n registerItem: BivariantCallback<(item: T) => () => void>;\n /**\n * Renders an item in the collection. This function returns a cleanup function\n * that unmounts the item.\n * @example\n * const unrenderItem = store.renderItem({ id: \"item-1\" });\n * // on cleanup\n * unrenderItem();\n */\n renderItem: BivariantCallback<(item: T) => () => void>;\n /**\n * Gets an item by its id.\n *\n * Live examples:\n * - [Animated TabPanel](https://ariakit.com/examples/tab-panel-animated)\n * @example\n * const item = store.item(\"item-1\");\n */\n item: (id: string | null | undefined) => T | null;\n}\n\nexport interface CollectionStoreOptions<\n T extends CollectionStoreItem = CollectionStoreItem,\n> extends StoreOptions<CollectionStoreState<T>, \"items\"> {\n /**\n * The defaut value for the\n * [`items`](https://ariakit.com/reference/collection-provider#items) state.\n * @default []\n */\n defaultItems?: CollectionStoreState<T>[\"items\"];\n}\n\nexport interface CollectionStoreProps<\n T extends CollectionStoreItem = CollectionStoreItem,\n>\n extends CollectionStoreOptions<T>, StoreProps<CollectionStoreState<T>> {}\n\nexport interface CollectionStore<\n T extends CollectionStoreItem = CollectionStoreItem,\n>\n extends CollectionStoreFunctions<T>, Store<CollectionStoreState<T>> {}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Store, StoreOptions, StoreProps } from "@ariakit/store";
|
|
2
|
+
import { BivariantCallback } from "@ariakit/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/collection/collection-store.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a collection store.
|
|
7
|
+
*/
|
|
8
|
+
declare function createCollectionStore<T extends CollectionStoreItem = CollectionStoreItem>(props?: CollectionStoreProps<T>): CollectionStore<T>;
|
|
9
|
+
interface CollectionStoreItem {
|
|
10
|
+
/**
|
|
11
|
+
* The id of the item.
|
|
12
|
+
*/
|
|
13
|
+
id: string;
|
|
14
|
+
/**
|
|
15
|
+
* The item HTML element. This is automatically set when the item is rendered.
|
|
16
|
+
*/
|
|
17
|
+
element?: HTMLElement | null;
|
|
18
|
+
}
|
|
19
|
+
interface CollectionStoreState<T extends CollectionStoreItem = CollectionStoreItem> {
|
|
20
|
+
/**
|
|
21
|
+
* Lists all items along with their metadata. This state is automatically
|
|
22
|
+
* updated when an item is registered or unregistered using the
|
|
23
|
+
* [`registerItem`](https://ariakit.com/reference/use-collection-store#registeritem)
|
|
24
|
+
* function.
|
|
25
|
+
*
|
|
26
|
+
* Live examples:
|
|
27
|
+
* - [Command Menu with
|
|
28
|
+
* Tabs](https://ariakit.com/examples/dialog-combobox-tab-command-menu)
|
|
29
|
+
*/
|
|
30
|
+
items: T[];
|
|
31
|
+
/**
|
|
32
|
+
* Lists all items, along with their metadata, in the exact order they appear in
|
|
33
|
+
* the DOM. This state is automatically updated when an item is rendered or
|
|
34
|
+
* unmounted using the
|
|
35
|
+
* [`renderItem`](https://ariakit.com/reference/use-collection-store#renderitem)
|
|
36
|
+
* function.
|
|
37
|
+
*/
|
|
38
|
+
renderedItems: T[];
|
|
39
|
+
}
|
|
40
|
+
interface CollectionStoreFunctions<T extends CollectionStoreItem = CollectionStoreItem> {
|
|
41
|
+
/**
|
|
42
|
+
* Registers an item in the collection. This function returns a cleanup
|
|
43
|
+
* function that unregisters the item.
|
|
44
|
+
* @example
|
|
45
|
+
* const unregisterItem = store.registerItem({ id: "item-1" });
|
|
46
|
+
* // on cleanup
|
|
47
|
+
* unregisterItem();
|
|
48
|
+
*/
|
|
49
|
+
registerItem: BivariantCallback<(item: T) => () => void>;
|
|
50
|
+
/**
|
|
51
|
+
* Renders an item in the collection. This function returns a cleanup function
|
|
52
|
+
* that unmounts the item.
|
|
53
|
+
* @example
|
|
54
|
+
* const unrenderItem = store.renderItem({ id: "item-1" });
|
|
55
|
+
* // on cleanup
|
|
56
|
+
* unrenderItem();
|
|
57
|
+
*/
|
|
58
|
+
renderItem: BivariantCallback<(item: T) => () => void>;
|
|
59
|
+
/**
|
|
60
|
+
* Gets an item by its id.
|
|
61
|
+
*
|
|
62
|
+
* Live examples:
|
|
63
|
+
* - [Animated TabPanel](https://ariakit.com/examples/tab-panel-animated)
|
|
64
|
+
* @example
|
|
65
|
+
* const item = store.item("item-1");
|
|
66
|
+
*/
|
|
67
|
+
item: (id: string | null | undefined) => T | null;
|
|
68
|
+
}
|
|
69
|
+
interface CollectionStoreOptions<T extends CollectionStoreItem = CollectionStoreItem> extends StoreOptions<CollectionStoreState<T>, "items"> {
|
|
70
|
+
/**
|
|
71
|
+
* The defaut value for the
|
|
72
|
+
* [`items`](https://ariakit.com/reference/collection-provider#items) state.
|
|
73
|
+
* @default []
|
|
74
|
+
*/
|
|
75
|
+
defaultItems?: CollectionStoreState<T>["items"];
|
|
76
|
+
}
|
|
77
|
+
interface CollectionStoreProps<T extends CollectionStoreItem = CollectionStoreItem> extends CollectionStoreOptions<T>, StoreProps<CollectionStoreState<T>> {}
|
|
78
|
+
interface CollectionStore<T extends CollectionStoreItem = CollectionStoreItem> extends CollectionStoreFunctions<T>, Store<CollectionStoreState<T>> {}
|
|
79
|
+
//#endregion
|
|
80
|
+
export { CollectionStoreProps as a, CollectionStoreOptions as i, CollectionStoreFunctions as n, CollectionStoreState as o, CollectionStoreItem as r, createCollectionStore as s, CollectionStore as t };
|
|
81
|
+
//# sourceMappingURL=collection-store-yNe83BiS.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection-store-yNe83BiS.d.ts","names":["Store","StoreOptions","StoreProps","BivariantCallback","createCollectionStore","T","CollectionStoreItem","CollectionStoreProps","CollectionStore","props","HTMLElement","id","element","CollectionStoreState","items","renderedItems","CollectionStoreFunctions","registerItem","item","renderItem","CollectionStoreOptions","defaultItems"],"sources":["../src/collection/collection-store.d.ts"],"mappings":";;;;;;AAKA;iBAAwBI,qBAAAA,WAAgCE,mBAAAA,GAAsBA,mBAAAA,CAAAA,CAAqBG,KAAAA,GAAQF,oBAAAA,CAAqBF,CAAAA,IAAKG,eAAAA,CAAgBH,CAAAA;AAAAA,UACpIC,mBAAAA;EADuCA;;;EAKpDK,EAAAA;EALiJN;;;EASjJO,OAAAA,GAAUF,WAAW;AAAA;AAAA,UAERG,oBAAAA,WAA+BP,mBAAAA,GAAsBA,mBAAAA;EAXQA;;;;;;;AAAwE;AACtJ;;EAqBIQ,KAAAA,EAAOT,CAAAA;EAbc;;;;;AAAA;AAEzB;EAmBIU,aAAAA,EAAeV,CAAAA;AAAAA;AAAAA,UAEFW,wBAAAA,WAAmCV,mBAAAA,GAAsBA,mBAAAA;EArBJA;;;;;;;;EA8BlEW,YAAAA,EAAcd,iBAAAA,EAAmBe,IAAAA,EAAMb,CAAAA;EAnBhCA;;;;AAQS;AAEpB;;;EAkBIc,UAAAA,EAAYhB,iBAAAA,EAAmBe,IAAAA,EAAMb,CAAAA;EAlBiCC;;;;;;;;EA2BtEY,IAAAA,GAAOP,EAAAA,gCAAkCN,CAAAA;AAAAA;AAAAA,UAE5Be,sBAAAA,WAAiCd,mBAAAA,GAAsBA,mBAAAA,UAA6BL,YAAAA,CAAaY,oBAAAA,CAAqBR,CAAAA;EApBnIY;;;;;EA0BAI,YAAAA,GAAeR,oBAAAA,CAAqBR,CAAAA;AAAAA;AAAAA,UAEvBE,oBAAAA,WAA+BD,mBAAAA,GAAsBA,mBAAAA,UAA6Bc,sBAAAA,CAAuBf,CAAAA,GAAIH,UAAAA,CAAWW,oBAAAA,CAAqBR,CAAAA;AAAAA,UAE7IG,eAAAA,WAA0BF,mBAAAA,GAAsBA,mBAAAA,UAA6BU,wBAAAA,CAAyBX,CAAAA,GAAIL,KAAAA,CAAMa,oBAAAA,CAAqBR,CAAAA"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { i as CompositeStoreOptions, n as CompositeStoreFunctions, r as CompositeStoreItem, s as CompositeStoreState } from "../composite-store-B-iDEtZZ.js";
|
|
2
|
+
import { a as PopoverStoreState, n as PopoverStoreFunctions, r as PopoverStoreOptions } from "../popover-store-DoCiTmUQ.js";
|
|
3
|
+
import { t as TagStore } from "../tag-store-D47X5_zA.js";
|
|
4
|
+
import { Store, StoreOptions, StoreProps } from "@ariakit/store";
|
|
5
|
+
import { PickRequired, SetState } from "@ariakit/utils";
|
|
6
|
+
|
|
7
|
+
//#region src/combobox/combobox-store.d.ts
|
|
8
|
+
type MutableValue<T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue> = T extends string ? string : T;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a combobox store.
|
|
11
|
+
*/
|
|
12
|
+
declare function createComboboxStore<T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue>(props: PickRequired<ComboboxStoreProps<T>, "selectedValue" | "defaultSelectedValue">): ComboboxStore<T>;
|
|
13
|
+
declare function createComboboxStore(props?: ComboboxStoreProps): ComboboxStore;
|
|
14
|
+
type ComboboxStoreSelectedValue = string | readonly string[];
|
|
15
|
+
interface ComboboxStoreItem extends CompositeStoreItem {
|
|
16
|
+
value?: string;
|
|
17
|
+
}
|
|
18
|
+
interface ComboboxStoreState<T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue> extends CompositeStoreState<ComboboxStoreItem>, PopoverStoreState {
|
|
19
|
+
/** @default true */
|
|
20
|
+
includesBaseElement: CompositeStoreState<ComboboxStoreItem>["includesBaseElement"];
|
|
21
|
+
/** @default true */
|
|
22
|
+
focusLoop: CompositeStoreState<ComboboxStoreItem>["focusLoop"];
|
|
23
|
+
/** @default true */
|
|
24
|
+
focusWrap: CompositeStoreState<ComboboxStoreItem>["focusWrap"];
|
|
25
|
+
/** @default "vertical" */
|
|
26
|
+
orientation: CompositeStoreState<ComboboxStoreItem>["orientation"];
|
|
27
|
+
/** @default true */
|
|
28
|
+
virtualFocus: CompositeStoreState<ComboboxStoreItem>["virtualFocus"];
|
|
29
|
+
/**
|
|
30
|
+
* The combobox input value.
|
|
31
|
+
*
|
|
32
|
+
* Live examples:
|
|
33
|
+
* - [Combobox with integrated
|
|
34
|
+
* filter](https://ariakit.com/examples/combobox-filtering-integrated)
|
|
35
|
+
* - [Combobox with links](https://ariakit.com/examples/combobox-links)
|
|
36
|
+
* - [Combobox filtering](https://ariakit.com/examples/combobox-filtering)
|
|
37
|
+
* - [Multi-selectable
|
|
38
|
+
* Combobox](https://ariakit.com/examples/combobox-multiple)
|
|
39
|
+
* - [Textarea with inline
|
|
40
|
+
* Combobox](https://ariakit.com/examples/combobox-textarea)
|
|
41
|
+
* - [Command Menu with
|
|
42
|
+
* Tabs](https://ariakit.com/examples/dialog-combobox-tab-command-menu)
|
|
43
|
+
*/
|
|
44
|
+
value: string;
|
|
45
|
+
/**
|
|
46
|
+
* The value of the currently active item. This state automatically updates as
|
|
47
|
+
* the user navigates the combobox items either by keyboard or mouse click.
|
|
48
|
+
* Note that it doesn't update when the user simply hovers over the items.
|
|
49
|
+
*/
|
|
50
|
+
activeValue: string | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* The value(s) of the currently selected item(s). This can be a string or an
|
|
53
|
+
* array of strings. If it's an array, the combobox is considered
|
|
54
|
+
* [multi-selectable](https://ariakit.com/examples/combobox-multiple).
|
|
55
|
+
*
|
|
56
|
+
* Live examples:
|
|
57
|
+
* - [Multi-selectable
|
|
58
|
+
* Combobox](https://ariakit.com/examples/combobox-multiple)
|
|
59
|
+
*/
|
|
60
|
+
selectedValue: MutableValue<T>;
|
|
61
|
+
/**
|
|
62
|
+
* Whether to reset the value when the combobox popover closes. This prop is
|
|
63
|
+
* automatically set to `true` by default if the combobox supports multiple
|
|
64
|
+
* selections. In other words, if the
|
|
65
|
+
* [`selectedValue`](https://ariakit.com/reference/combobox-provider#selectedvalue)
|
|
66
|
+
* or
|
|
67
|
+
* [`defaultSelectedValue`](https://ariakit.com/reference/combobox-provider#defaultselectedvalue)
|
|
68
|
+
* props are arrays.
|
|
69
|
+
*
|
|
70
|
+
* Live examples:
|
|
71
|
+
* - [Multi-selectable
|
|
72
|
+
* Combobox](https://ariakit.com/examples/combobox-multiple)
|
|
73
|
+
* - [Menu with Combobox](https://ariakit.com/examples/menu-combobox)
|
|
74
|
+
* - [Select with Combobox](https://ariakit.com/examples/select-combobox)
|
|
75
|
+
* - [Submenu with
|
|
76
|
+
* Combobox](https://ariakit.com/examples/menu-nested-combobox)
|
|
77
|
+
* - [Command Menu with
|
|
78
|
+
* Tabs](https://ariakit.com/examples/dialog-combobox-tab-command-menu)
|
|
79
|
+
* - [Select with Combobox and
|
|
80
|
+
* Tabs](https://ariakit.com/examples/select-combobox-tab)
|
|
81
|
+
*/
|
|
82
|
+
resetValueOnHide: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Whether to reset the value when an item is selected. This prop is
|
|
85
|
+
* automatically set to `true` by default if the combobox supports multiple
|
|
86
|
+
* selections. In other words, if the
|
|
87
|
+
* [`selectedValue`](https://ariakit.com/reference/combobox-provider#selectedvalue)
|
|
88
|
+
* or
|
|
89
|
+
* [`defaultSelectedValue`](https://ariakit.com/reference/combobox-provider#defaultselectedvalue)
|
|
90
|
+
* props are arrays.
|
|
91
|
+
* @deprecated Use the
|
|
92
|
+
* [`resetValueOnSelect`](https://ariakit.com/reference/combobox-item#resetvalueonselect)
|
|
93
|
+
* prop on [`ComboboxItem`](https://ariakit.com/reference/combobox-item)
|
|
94
|
+
* instead.
|
|
95
|
+
*/
|
|
96
|
+
resetValueOnSelect: boolean;
|
|
97
|
+
}
|
|
98
|
+
interface ComboboxStoreFunctions<T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue> extends Pick<ComboboxStoreOptions<T>, "tag">, CompositeStoreFunctions<ComboboxStoreItem>, PopoverStoreFunctions {
|
|
99
|
+
/**
|
|
100
|
+
* Sets the [`value`](https://ariakit.com/reference/combobox-provider#value)
|
|
101
|
+
* state.
|
|
102
|
+
*
|
|
103
|
+
* Live examples:
|
|
104
|
+
* - [Textarea with inline
|
|
105
|
+
* Combobox](https://ariakit.com/examples/combobox-textarea)
|
|
106
|
+
* @example
|
|
107
|
+
* store.setValue("Hello world");
|
|
108
|
+
* store.setValue((value) => value + "!");
|
|
109
|
+
*/
|
|
110
|
+
setValue: SetState<ComboboxStoreState<T>["value"]>;
|
|
111
|
+
/**
|
|
112
|
+
* Resets the [`value`](https://ariakit.com/reference/combobox-provider#value)
|
|
113
|
+
* state to its initial value.
|
|
114
|
+
*/
|
|
115
|
+
resetValue: () => void;
|
|
116
|
+
/**
|
|
117
|
+
* Sets the
|
|
118
|
+
* [`selectedValue`](https://ariakit.com/reference/combobox-provider#selectedvalue)
|
|
119
|
+
* state.
|
|
120
|
+
*/
|
|
121
|
+
setSelectedValue: SetState<ComboboxStoreState<T>["selectedValue"]>;
|
|
122
|
+
}
|
|
123
|
+
interface ComboboxStoreOptions<T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue> extends StoreOptions<ComboboxStoreState<T>, "includesBaseElement" | "focusLoop" | "focusWrap" | "orientation" | "virtualFocus" | "value" | "selectedValue" | "resetValueOnHide" | "resetValueOnSelect">, CompositeStoreOptions<ComboboxStoreItem>, PopoverStoreOptions {
|
|
124
|
+
/** @default null */
|
|
125
|
+
defaultActiveId?: CompositeStoreOptions<ComboboxStoreItem>["activeId"];
|
|
126
|
+
/**
|
|
127
|
+
* The initial value of the combobox input.
|
|
128
|
+
* @default ""
|
|
129
|
+
*/
|
|
130
|
+
defaultValue?: ComboboxStoreState<T>["value"];
|
|
131
|
+
/**
|
|
132
|
+
* The initial value of the
|
|
133
|
+
* [`selectedValue`](https://ariakit.com/reference/combobox-provider#selectedvalue)
|
|
134
|
+
* state. This can be a string or an array of strings. If it's an array, the
|
|
135
|
+
* combobox is considered
|
|
136
|
+
* [multi-selectable](https://ariakit.com/examples/combobox-multiple).
|
|
137
|
+
* @default ""
|
|
138
|
+
*/
|
|
139
|
+
defaultSelectedValue?: ComboboxStoreState<T>["selectedValue"];
|
|
140
|
+
/**
|
|
141
|
+
* A reference to a tag store. This is used when rendering a combobox within a
|
|
142
|
+
* tag list. The stores will share the same state.
|
|
143
|
+
*/
|
|
144
|
+
tag?: TagStore | null;
|
|
145
|
+
}
|
|
146
|
+
interface ComboboxStoreProps<T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue> extends ComboboxStoreOptions<T>, StoreProps<ComboboxStoreState<T>> {}
|
|
147
|
+
interface ComboboxStore<T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue> extends ComboboxStoreFunctions<T>, Store<ComboboxStoreState<T>> {}
|
|
148
|
+
//#endregion
|
|
149
|
+
export { ComboboxStore, ComboboxStoreFunctions, ComboboxStoreItem, ComboboxStoreOptions, ComboboxStoreProps, ComboboxStoreSelectedValue, ComboboxStoreState, createComboboxStore };
|
|
150
|
+
//# sourceMappingURL=combobox-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"combobox-store.d.ts","names":["Store","StoreOptions","StoreProps","PickRequired","SetState","CompositeStoreFunctions","CompositeStoreItem","CompositeStoreOptions","CompositeStoreState","PopoverStoreFunctions","PopoverStoreOptions","PopoverStoreState","TagStore","MutableValue","T","ComboboxStoreSelectedValue","createComboboxStore","ComboboxStoreProps","ComboboxStore","props","ComboboxStoreItem","value","ComboboxStoreState","includesBaseElement","focusLoop","focusWrap","orientation","virtualFocus","activeValue","selectedValue","resetValueOnHide","resetValueOnSelect","ComboboxStoreFunctions","ComboboxStoreOptions","Pick","setValue","resetValue","setSelectedValue","defaultActiveId","defaultValue","defaultSelectedValue","tag"],"sources":["../../src/combobox/combobox-store.d.ts"],"mappings":";;;;;;;KAKKa,YAAAA,WAAuBE,0BAAAA,GAA6BA,0BAAAA,IAA8BD,CAAAA,2BAA4BA,CAAAA;;AAD/D;;iBAK5BE,mBAAAA,WAA8BD,0BAAAA,GAA6BA,0BAAAA,CAAAA,CAA4BI,KAAAA,EAAOhB,YAAAA,CAAac,kBAAAA,CAAmBH,CAAAA,+CAAgDI,aAAAA,CAAcJ,CAAAA;AAAAA,iBAC5LE,mBAAAA,CAAoBG,KAAAA,GAAQF,kBAAAA,GAAqBC,aAAa;AAAA,KAC1EH,0BAAAA;AAAAA,UACKK,iBAAAA,SAA0Bd,kBAAkB;EACzDe,KAAK;AAAA;AAAA,UAEQC,kBAAAA,WAA6BP,0BAAAA,GAA6BA,0BAAAA,UAAoCP,mBAAAA,CAAoBY,iBAAAA,GAAoBT,iBAAAA;EAVrIG;EAYdS,mBAAAA,EAAqBf,mBAAAA,CAAoBY,iBAAAA;EAZYL;EAcrDS,SAAAA,EAAWhB,mBAAAA,CAAoBY,iBAAAA;EAdgFN;EAgB/GW,SAAAA,EAAWjB,mBAAAA,CAAoBY,iBAAAA;EAhBiF;EAkBhHM,WAAAA,EAAalB,mBAAAA,CAAoBY,iBAAAA;EAdM;EAgBvCO,YAAAA,EAAcnB,mBAAAA,CAAoBY,iBAAAA;EAhBgBL;;;;;;;;;;;;;;;EAgClDM,KAAAA;EAhCkMH;;;AAAe;AACrN;EAqCIU,WAAAA;;;;;;;AArCkF;AACtF;;EA8CIC,aAAAA,EAAehB,YAAAA,CAAaC,CAAAA;EA9CM;AAAA;AACtC;;;;AACS;AAET;;;;;;;;;;;;;;EAgEIgB,gBAAAA;EAtDctB;;;;;;;;;;;;;EAoEduB,kBAAAA;AAAAA;AAAAA,UAEaC,sBAAAA,WAAiCjB,0BAAAA,GAA6BA,0BAAAA,UAAoCmB,IAAAA,CAAKD,oBAAAA,CAAqBnB,CAAAA,WAAYT,uBAAAA,CAAwBe,iBAAAA,GAAoBX,qBAAAA;EA5EjMe;;;;;;;;;;;EAwFAW,QAAAA,EAAU/B,QAAAA,CAASkB,kBAAAA,CAAmBR,CAAAA;EAlEtCO;;;;EAuEAe,UAAAA;EAjCAN;;;AAckB;AAEtB;EAuBIO,gBAAAA,EAAkBjC,QAAAA,CAASkB,kBAAAA,CAAmBR,CAAAA;AAAAA;AAAAA,UAEjCmB,oBAAAA,WAA+BlB,0BAAAA,GAA6BA,0BAAAA,UAAoCd,YAAAA,CAAaqB,kBAAAA,CAAmBR,CAAAA,gKAAiKP,qBAAAA,CAAsBa,iBAAAA,GAAoBV,mBAAAA;EAzB7QK;EA2B3EuB,eAAAA,GAAkB/B,qBAAAA,CAAsBa,iBAAAA;EA3B4Ea;;;;EAgCpHM,YAAAA,GAAejB,kBAAAA,CAAmBR,CAAAA;EATYA;;;;;;;;EAkB9C0B,oBAAAA,GAAuBlB,kBAAAA,CAAmBR,CAAAA;EAzCIC;;;;EA8C9C0B,GAAAA,GAAM7B,QAAAA;AAAAA;AAAAA,UAEOK,kBAAAA,WAA6BF,0BAAAA,GAA6BA,0BAAAA,UAAoCkB,oBAAAA,CAAqBnB,CAAAA,GAAIZ,UAAAA,CAAWoB,kBAAAA,CAAmBR,CAAAA;AAAAA,UAErJI,aAAAA,WAAwBH,0BAAAA,GAA6BA,0BAAAA,UAAoCiB,sBAAAA,CAAuBlB,CAAAA,GAAId,KAAAA,CAAMsB,kBAAAA,CAAmBR,CAAAA"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { createCompositeStore } from "../composite/composite-store.js";
|
|
2
|
+
import { createPopoverStore } from "../popover/popover-store.js";
|
|
3
|
+
import { batch, createStore, mergeStore, pick, setup, sync, throwOnConflictingProps } from "@ariakit/store";
|
|
4
|
+
import { chain, defaultValue, isSafari, isTouchDevice } from "@ariakit/utils";
|
|
5
|
+
//#region src/combobox/combobox-store.ts
|
|
6
|
+
const isTouchSafari = isSafari() && isTouchDevice();
|
|
7
|
+
function createComboboxStore({ tag, ...props } = {}) {
|
|
8
|
+
const store = mergeStore(props.store, pick(tag, ["value", "rtl"]));
|
|
9
|
+
throwOnConflictingProps(props, store);
|
|
10
|
+
const tagState = tag?.getState();
|
|
11
|
+
const syncState = store?.getState();
|
|
12
|
+
const activeId = defaultValue(props.activeId, syncState?.activeId, props.defaultActiveId, null);
|
|
13
|
+
const composite = createCompositeStore({
|
|
14
|
+
...props,
|
|
15
|
+
activeId,
|
|
16
|
+
includesBaseElement: defaultValue(props.includesBaseElement, syncState?.includesBaseElement, true),
|
|
17
|
+
orientation: defaultValue(props.orientation, syncState?.orientation, "vertical"),
|
|
18
|
+
focusLoop: defaultValue(props.focusLoop, syncState?.focusLoop, true),
|
|
19
|
+
focusWrap: defaultValue(props.focusWrap, syncState?.focusWrap, true),
|
|
20
|
+
virtualFocus: defaultValue(props.virtualFocus, syncState?.virtualFocus, true)
|
|
21
|
+
});
|
|
22
|
+
const popover = createPopoverStore({
|
|
23
|
+
...props,
|
|
24
|
+
placement: defaultValue(props.placement, syncState?.placement, "bottom-start")
|
|
25
|
+
});
|
|
26
|
+
const value = defaultValue(props.value, syncState?.value, props.defaultValue, "");
|
|
27
|
+
const selectedValue = defaultValue(props.selectedValue, syncState?.selectedValue, tagState?.values, props.defaultSelectedValue, "");
|
|
28
|
+
const multiSelectable = Array.isArray(selectedValue);
|
|
29
|
+
const initialState = {
|
|
30
|
+
...composite.getState(),
|
|
31
|
+
...popover.getState(),
|
|
32
|
+
value,
|
|
33
|
+
selectedValue,
|
|
34
|
+
resetValueOnSelect: defaultValue(props.resetValueOnSelect, syncState?.resetValueOnSelect, multiSelectable),
|
|
35
|
+
resetValueOnHide: defaultValue(props.resetValueOnHide, syncState?.resetValueOnHide, multiSelectable && !tag),
|
|
36
|
+
activeValue: syncState?.activeValue
|
|
37
|
+
};
|
|
38
|
+
const combobox = createStore(initialState, composite, popover, store);
|
|
39
|
+
if (isTouchSafari) setup(combobox, () => sync(combobox, ["virtualFocus"], () => {
|
|
40
|
+
combobox.setState("virtualFocus", false);
|
|
41
|
+
}));
|
|
42
|
+
setup(combobox, () => {
|
|
43
|
+
if (!tag) return;
|
|
44
|
+
return chain(sync(combobox, ["selectedValue"], (state) => {
|
|
45
|
+
if (!Array.isArray(state.selectedValue)) return;
|
|
46
|
+
tag.setValues(state.selectedValue);
|
|
47
|
+
}), sync(tag, ["values"], (state) => {
|
|
48
|
+
combobox.setState("selectedValue", state.values);
|
|
49
|
+
}));
|
|
50
|
+
});
|
|
51
|
+
setup(combobox, () => sync(combobox, ["resetValueOnHide", "mounted"], (state) => {
|
|
52
|
+
if (!state.resetValueOnHide) return;
|
|
53
|
+
if (state.mounted) return;
|
|
54
|
+
combobox.setState("value", value);
|
|
55
|
+
}));
|
|
56
|
+
setup(combobox, () => sync(combobox, ["open"], (state) => {
|
|
57
|
+
if (state.open) return;
|
|
58
|
+
combobox.setState("activeId", activeId);
|
|
59
|
+
combobox.setState("moves", 0);
|
|
60
|
+
}));
|
|
61
|
+
setup(combobox, () => sync(combobox, ["moves", "activeId"], (state, prevState) => {
|
|
62
|
+
if (state.moves === prevState.moves) combobox.setState("activeValue", void 0);
|
|
63
|
+
}));
|
|
64
|
+
setup(combobox, () => batch(combobox, ["moves", "renderedItems"], (state, prev) => {
|
|
65
|
+
if (state.moves === prev.moves) return;
|
|
66
|
+
const { activeId } = combobox.getState();
|
|
67
|
+
const activeItem = composite.item(activeId);
|
|
68
|
+
combobox.setState("activeValue", activeItem?.value);
|
|
69
|
+
}));
|
|
70
|
+
return {
|
|
71
|
+
...popover,
|
|
72
|
+
...composite,
|
|
73
|
+
...combobox,
|
|
74
|
+
tag,
|
|
75
|
+
setValue: (value) => combobox.setState("value", value),
|
|
76
|
+
resetValue: () => combobox.setState("value", initialState.value),
|
|
77
|
+
setSelectedValue: (selectedValue) => combobox.setState("selectedValue", selectedValue)
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { createComboboxStore };
|
|
82
|
+
|
|
83
|
+
//# sourceMappingURL=combobox-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"combobox-store.js","names":[],"sources":["../../src/combobox/combobox-store.ts"],"sourcesContent":["import {\n batch,\n createStore,\n mergeStore,\n pick,\n setup,\n sync,\n throwOnConflictingProps,\n} from \"@ariakit/store\";\nimport type { Store, StoreOptions, StoreProps } from \"@ariakit/store\";\nimport { chain, defaultValue, isSafari, isTouchDevice } from \"@ariakit/utils\";\nimport type { PickRequired, SetState } from \"@ariakit/utils\";\nimport type {\n CompositeStoreFunctions,\n CompositeStoreItem,\n CompositeStoreOptions,\n CompositeStoreState,\n} from \"../composite/composite-store.ts\";\nimport { createCompositeStore } from \"../composite/composite-store.ts\";\nimport type {\n PopoverStoreFunctions,\n PopoverStoreOptions,\n PopoverStoreState,\n} from \"../popover/popover-store.ts\";\nimport { createPopoverStore } from \"../popover/popover-store.ts\";\nimport type { TagStore } from \"../tag/tag-store.ts\";\n\ntype MutableValue<\n T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue,\n> = T extends string ? string : T;\n\nconst isTouchSafari = isSafari() && isTouchDevice();\n\n/**\n * Creates a combobox store.\n */\nexport function createComboboxStore<\n T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue,\n>(\n props: PickRequired<\n ComboboxStoreProps<T>,\n \"selectedValue\" | \"defaultSelectedValue\"\n >,\n): ComboboxStore<T>;\n\nexport function createComboboxStore(props?: ComboboxStoreProps): ComboboxStore;\n\nexport function createComboboxStore({\n tag,\n ...props\n}: ComboboxStoreProps = {}): ComboboxStore {\n const store = mergeStore(props.store, pick(tag, [\"value\", \"rtl\"]));\n\n throwOnConflictingProps(props, store);\n\n const tagState = tag?.getState();\n const syncState = store?.getState();\n\n const activeId = defaultValue(\n props.activeId,\n syncState?.activeId,\n props.defaultActiveId,\n null,\n );\n\n const composite = createCompositeStore({\n ...props,\n activeId,\n includesBaseElement: defaultValue(\n props.includesBaseElement,\n syncState?.includesBaseElement,\n true,\n ),\n orientation: defaultValue(\n props.orientation,\n syncState?.orientation,\n \"vertical\" as const,\n ),\n focusLoop: defaultValue(props.focusLoop, syncState?.focusLoop, true),\n focusWrap: defaultValue(props.focusWrap, syncState?.focusWrap, true),\n virtualFocus: defaultValue(\n props.virtualFocus,\n syncState?.virtualFocus,\n true,\n ),\n });\n\n const popover = createPopoverStore({\n ...props,\n placement: defaultValue(\n props.placement,\n syncState?.placement,\n \"bottom-start\" as const,\n ),\n });\n\n const value = defaultValue(\n props.value,\n syncState?.value,\n props.defaultValue,\n \"\",\n );\n\n const selectedValue = defaultValue(\n props.selectedValue,\n syncState?.selectedValue,\n tagState?.values,\n props.defaultSelectedValue,\n \"\",\n );\n\n const multiSelectable = Array.isArray(selectedValue);\n\n const initialState: ComboboxStoreState = {\n ...composite.getState(),\n ...popover.getState(),\n value,\n selectedValue,\n resetValueOnSelect: defaultValue(\n props.resetValueOnSelect,\n syncState?.resetValueOnSelect,\n multiSelectable,\n ),\n resetValueOnHide: defaultValue(\n props.resetValueOnHide,\n syncState?.resetValueOnHide,\n multiSelectable && !tag,\n ),\n activeValue: syncState?.activeValue,\n };\n\n const combobox = createStore(initialState, composite, popover, store);\n\n // Safari doesn't support aria-activedescendant on combobox elements. This is\n // particularly problematic when using touch devices as moving the VoiceOver\n // virtual cursor through the combobox items will always move the focus to the\n // input element. To work around this, we disable virtual focus on touch\n // devices when using Safari.\n if (isTouchSafari) {\n setup(combobox, () =>\n sync(combobox, [\"virtualFocus\"], () => {\n combobox.setState(\"virtualFocus\", false);\n }),\n );\n }\n\n // Sync tag values with the combobox selectedValue state.\n setup(combobox, () => {\n if (!tag) return;\n return chain(\n sync(combobox, [\"selectedValue\"], (state) => {\n if (!Array.isArray(state.selectedValue)) return;\n tag.setValues(state.selectedValue);\n }),\n sync(tag, [\"values\"], (state) => {\n combobox.setState(\"selectedValue\", state.values);\n }),\n );\n });\n\n setup(combobox, () =>\n sync(combobox, [\"resetValueOnHide\", \"mounted\"], (state) => {\n if (!state.resetValueOnHide) return;\n if (state.mounted) return;\n combobox.setState(\"value\", value);\n }),\n );\n\n // Resets the state when the combobox popover is hidden.\n setup(combobox, () =>\n sync(combobox, [\"open\"], (state) => {\n if (state.open) return;\n combobox.setState(\"activeId\", activeId);\n combobox.setState(\"moves\", 0);\n }),\n );\n\n // When the activeId changes, but the moves count doesn't, we reset the\n // activeValue state. This is useful when the activeId changes because of\n // a mouse move interaction.\n setup(combobox, () =>\n sync(combobox, [\"moves\", \"activeId\"], (state, prevState) => {\n if (state.moves === prevState.moves) {\n combobox.setState(\"activeValue\", undefined);\n }\n }),\n );\n\n // Otherwise, if the moves count changes, we update the activeValue state.\n setup(combobox, () =>\n batch(combobox, [\"moves\", \"renderedItems\"], (state, prev) => {\n if (state.moves === prev.moves) return;\n const { activeId } = combobox.getState();\n const activeItem = composite.item(activeId);\n combobox.setState(\"activeValue\", activeItem?.value);\n }),\n );\n\n return {\n ...popover,\n ...composite,\n ...combobox,\n tag,\n setValue: (value) => combobox.setState(\"value\", value),\n resetValue: () => combobox.setState(\"value\", initialState.value),\n setSelectedValue: (selectedValue) =>\n combobox.setState(\"selectedValue\", selectedValue),\n };\n}\n\nexport type ComboboxStoreSelectedValue = string | readonly string[];\n\nexport interface ComboboxStoreItem extends CompositeStoreItem {\n value?: string;\n}\n\nexport interface ComboboxStoreState<\n T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue,\n>\n extends CompositeStoreState<ComboboxStoreItem>, PopoverStoreState {\n /** @default true */\n includesBaseElement: CompositeStoreState<ComboboxStoreItem>[\"includesBaseElement\"];\n /** @default true */\n focusLoop: CompositeStoreState<ComboboxStoreItem>[\"focusLoop\"];\n /** @default true */\n focusWrap: CompositeStoreState<ComboboxStoreItem>[\"focusWrap\"];\n /** @default \"vertical\" */\n orientation: CompositeStoreState<ComboboxStoreItem>[\"orientation\"];\n /** @default true */\n virtualFocus: CompositeStoreState<ComboboxStoreItem>[\"virtualFocus\"];\n /**\n * The combobox input value.\n *\n * Live examples:\n * - [Combobox with integrated\n * filter](https://ariakit.com/examples/combobox-filtering-integrated)\n * - [Combobox with links](https://ariakit.com/examples/combobox-links)\n * - [Combobox filtering](https://ariakit.com/examples/combobox-filtering)\n * - [Multi-selectable\n * Combobox](https://ariakit.com/examples/combobox-multiple)\n * - [Textarea with inline\n * Combobox](https://ariakit.com/examples/combobox-textarea)\n * - [Command Menu with\n * Tabs](https://ariakit.com/examples/dialog-combobox-tab-command-menu)\n */\n value: string;\n /**\n * The value of the currently active item. This state automatically updates as\n * the user navigates the combobox items either by keyboard or mouse click.\n * Note that it doesn't update when the user simply hovers over the items.\n */\n activeValue: string | undefined;\n /**\n * The value(s) of the currently selected item(s). This can be a string or an\n * array of strings. If it's an array, the combobox is considered\n * [multi-selectable](https://ariakit.com/examples/combobox-multiple).\n *\n * Live examples:\n * - [Multi-selectable\n * Combobox](https://ariakit.com/examples/combobox-multiple)\n */\n selectedValue: MutableValue<T>;\n /**\n * Whether to reset the value when the combobox popover closes. This prop is\n * automatically set to `true` by default if the combobox supports multiple\n * selections. In other words, if the\n * [`selectedValue`](https://ariakit.com/reference/combobox-provider#selectedvalue)\n * or\n * [`defaultSelectedValue`](https://ariakit.com/reference/combobox-provider#defaultselectedvalue)\n * props are arrays.\n *\n * Live examples:\n * - [Multi-selectable\n * Combobox](https://ariakit.com/examples/combobox-multiple)\n * - [Menu with Combobox](https://ariakit.com/examples/menu-combobox)\n * - [Select with Combobox](https://ariakit.com/examples/select-combobox)\n * - [Submenu with\n * Combobox](https://ariakit.com/examples/menu-nested-combobox)\n * - [Command Menu with\n * Tabs](https://ariakit.com/examples/dialog-combobox-tab-command-menu)\n * - [Select with Combobox and\n * Tabs](https://ariakit.com/examples/select-combobox-tab)\n */\n resetValueOnHide: boolean;\n /**\n * Whether to reset the value when an item is selected. This prop is\n * automatically set to `true` by default if the combobox supports multiple\n * selections. In other words, if the\n * [`selectedValue`](https://ariakit.com/reference/combobox-provider#selectedvalue)\n * or\n * [`defaultSelectedValue`](https://ariakit.com/reference/combobox-provider#defaultselectedvalue)\n * props are arrays.\n * @deprecated Use the\n * [`resetValueOnSelect`](https://ariakit.com/reference/combobox-item#resetvalueonselect)\n * prop on [`ComboboxItem`](https://ariakit.com/reference/combobox-item)\n * instead.\n */\n resetValueOnSelect: boolean;\n}\n\nexport interface ComboboxStoreFunctions<\n T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue,\n>\n extends\n Pick<ComboboxStoreOptions<T>, \"tag\">,\n CompositeStoreFunctions<ComboboxStoreItem>,\n PopoverStoreFunctions {\n /**\n * Sets the [`value`](https://ariakit.com/reference/combobox-provider#value)\n * state.\n *\n * Live examples:\n * - [Textarea with inline\n * Combobox](https://ariakit.com/examples/combobox-textarea)\n * @example\n * store.setValue(\"Hello world\");\n * store.setValue((value) => value + \"!\");\n */\n setValue: SetState<ComboboxStoreState<T>[\"value\"]>;\n /**\n * Resets the [`value`](https://ariakit.com/reference/combobox-provider#value)\n * state to its initial value.\n */\n resetValue: () => void;\n /**\n * Sets the\n * [`selectedValue`](https://ariakit.com/reference/combobox-provider#selectedvalue)\n * state.\n */\n setSelectedValue: SetState<ComboboxStoreState<T>[\"selectedValue\"]>;\n}\n\nexport interface ComboboxStoreOptions<\n T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue,\n>\n extends\n StoreOptions<\n ComboboxStoreState<T>,\n | \"includesBaseElement\"\n | \"focusLoop\"\n | \"focusWrap\"\n | \"orientation\"\n | \"virtualFocus\"\n | \"value\"\n | \"selectedValue\"\n | \"resetValueOnHide\"\n | \"resetValueOnSelect\"\n >,\n CompositeStoreOptions<ComboboxStoreItem>,\n PopoverStoreOptions {\n /** @default null */\n defaultActiveId?: CompositeStoreOptions<ComboboxStoreItem>[\"activeId\"];\n /**\n * The initial value of the combobox input.\n * @default \"\"\n */\n defaultValue?: ComboboxStoreState<T>[\"value\"];\n /**\n * The initial value of the\n * [`selectedValue`](https://ariakit.com/reference/combobox-provider#selectedvalue)\n * state. This can be a string or an array of strings. If it's an array, the\n * combobox is considered\n * [multi-selectable](https://ariakit.com/examples/combobox-multiple).\n * @default \"\"\n */\n defaultSelectedValue?: ComboboxStoreState<T>[\"selectedValue\"];\n /**\n * A reference to a tag store. This is used when rendering a combobox within a\n * tag list. The stores will share the same state.\n */\n tag?: TagStore | null;\n}\n\nexport interface ComboboxStoreProps<\n T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue,\n>\n extends ComboboxStoreOptions<T>, StoreProps<ComboboxStoreState<T>> {}\n\nexport interface ComboboxStore<\n T extends ComboboxStoreSelectedValue = ComboboxStoreSelectedValue,\n>\n extends ComboboxStoreFunctions<T>, Store<ComboboxStoreState<T>> {}\n"],"mappings":";;;;;AA+BA,MAAM,gBAAgB,SAAS,KAAK,cAAc;AAgBlD,SAAgB,oBAAoB,EAClC,KACA,GAAG,UACmB,CAAC,GAAkB;CACzC,MAAM,QAAQ,WAAW,MAAM,OAAO,KAAK,KAAK,CAAC,SAAS,KAAK,CAAC,CAAC;CAEjE,wBAAwB,OAAO,KAAK;CAEpC,MAAM,WAAW,KAAK,SAAS;CAC/B,MAAM,YAAY,OAAO,SAAS;CAElC,MAAM,WAAW,aACf,MAAM,UACN,WAAW,UACX,MAAM,iBACN,IACF;CAEA,MAAM,YAAY,qBAAqB;EACrC,GAAG;EACH;EACA,qBAAqB,aACnB,MAAM,qBACN,WAAW,qBACX,IACF;EACA,aAAa,aACX,MAAM,aACN,WAAW,aACX,UACF;EACA,WAAW,aAAa,MAAM,WAAW,WAAW,WAAW,IAAI;EACnE,WAAW,aAAa,MAAM,WAAW,WAAW,WAAW,IAAI;EACnE,cAAc,aACZ,MAAM,cACN,WAAW,cACX,IACF;CACF,CAAC;CAED,MAAM,UAAU,mBAAmB;EACjC,GAAG;EACH,WAAW,aACT,MAAM,WACN,WAAW,WACX,cACF;CACF,CAAC;CAED,MAAM,QAAQ,aACZ,MAAM,OACN,WAAW,OACX,MAAM,cACN,EACF;CAEA,MAAM,gBAAgB,aACpB,MAAM,eACN,WAAW,eACX,UAAU,QACV,MAAM,sBACN,EACF;CAEA,MAAM,kBAAkB,MAAM,QAAQ,aAAa;CAEnD,MAAM,eAAmC;EACvC,GAAG,UAAU,SAAS;EACtB,GAAG,QAAQ,SAAS;EACpB;EACA;EACA,oBAAoB,aAClB,MAAM,oBACN,WAAW,oBACX,eACF;EACA,kBAAkB,aAChB,MAAM,kBACN,WAAW,kBACX,mBAAmB,CAAC,GACtB;EACA,aAAa,WAAW;CAC1B;CAEA,MAAM,WAAW,YAAY,cAAc,WAAW,SAAS,KAAK;CAOpE,IAAI,eACF,MAAM,gBACJ,KAAK,UAAU,CAAC,cAAc,SAAS;EACrC,SAAS,SAAS,gBAAgB,KAAK;CACzC,CAAC,CACH;CAIF,MAAM,gBAAgB;EACpB,IAAI,CAAC,KAAK;EACV,OAAO,MACL,KAAK,UAAU,CAAC,eAAe,IAAI,UAAU;GAC3C,IAAI,CAAC,MAAM,QAAQ,MAAM,aAAa,GAAG;GACzC,IAAI,UAAU,MAAM,aAAa;EACnC,CAAC,GACD,KAAK,KAAK,CAAC,QAAQ,IAAI,UAAU;GAC/B,SAAS,SAAS,iBAAiB,MAAM,MAAM;EACjD,CAAC,CACH;CACF,CAAC;CAED,MAAM,gBACJ,KAAK,UAAU,CAAC,oBAAoB,SAAS,IAAI,UAAU;EACzD,IAAI,CAAC,MAAM,kBAAkB;EAC7B,IAAI,MAAM,SAAS;EACnB,SAAS,SAAS,SAAS,KAAK;CAClC,CAAC,CACH;CAGA,MAAM,gBACJ,KAAK,UAAU,CAAC,MAAM,IAAI,UAAU;EAClC,IAAI,MAAM,MAAM;EAChB,SAAS,SAAS,YAAY,QAAQ;EACtC,SAAS,SAAS,SAAS,CAAC;CAC9B,CAAC,CACH;CAKA,MAAM,gBACJ,KAAK,UAAU,CAAC,SAAS,UAAU,IAAI,OAAO,cAAc;EAC1D,IAAI,MAAM,UAAU,UAAU,OAC5B,SAAS,SAAS,eAAe,KAAA,CAAS;CAE9C,CAAC,CACH;CAGA,MAAM,gBACJ,MAAM,UAAU,CAAC,SAAS,eAAe,IAAI,OAAO,SAAS;EAC3D,IAAI,MAAM,UAAU,KAAK,OAAO;EAChC,MAAM,EAAE,aAAa,SAAS,SAAS;EACvC,MAAM,aAAa,UAAU,KAAK,QAAQ;EAC1C,SAAS,SAAS,eAAe,YAAY,KAAK;CACpD,CAAC,CACH;CAEA,OAAO;EACL,GAAG;EACH,GAAG;EACH,GAAG;EACH;EACA,WAAW,UAAU,SAAS,SAAS,SAAS,KAAK;EACrD,kBAAkB,SAAS,SAAS,SAAS,aAAa,KAAK;EAC/D,mBAAmB,kBACjB,SAAS,SAAS,iBAAiB,aAAa;CACpD;AACF"}
|