@ariakit/core 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +6 -0
- package/CHANGELOG.md +24 -0
- package/checkbox/checkbox-store/package.json +7 -0
- package/cjs/__chunks/2YFRPUZP.cjs +30 -0
- package/cjs/__chunks/3BBA3Z5G.cjs +143 -0
- package/cjs/__chunks/3KP2MDG6.cjs +1491 -0
- package/cjs/__chunks/5D5Y5EI4.cjs +146 -0
- package/cjs/__chunks/7ZXWQTAY.cjs +71 -0
- package/cjs/__chunks/AZVDLKO3.cjs +286 -0
- package/cjs/__chunks/EFEGT32M.cjs +12 -0
- package/cjs/__chunks/EIDN2CWH.cjs +57 -0
- package/cjs/__chunks/GDZQUFNP.cjs +30 -0
- package/cjs/__chunks/NX5OHIMM.cjs +169 -0
- package/cjs/__chunks/OFNGELMA.cjs +182 -0
- package/cjs/checkbox/checkbox-store.cjs +27 -0
- package/cjs/checkbox/checkbox-store.d.ts +34 -0
- package/cjs/collection/collection-store.cjs +9 -0
- package/cjs/collection/collection-store.d.ts +65 -0
- package/cjs/combobox/combobox-store.cjs +163 -0
- package/cjs/combobox/combobox-store.d.ts +67 -0
- package/cjs/composite/composite-overflow-store.cjs +15 -0
- package/cjs/composite/composite-overflow-store.d.ts +10 -0
- package/cjs/composite/composite-store.cjs +11 -0
- package/cjs/composite/composite-store.d.ts +202 -0
- package/cjs/dialog/dialog-store.cjs +9 -0
- package/cjs/dialog/dialog-store.d.ts +10 -0
- package/cjs/disclosure/disclosure-store.cjs +8 -0
- package/cjs/disclosure/disclosure-store.d.ts +87 -0
- package/cjs/form/form-store.cjs +241 -0
- package/cjs/form/form-store.d.ts +236 -0
- package/cjs/form/types.cjs +1 -0
- package/cjs/form/types.d.ts +38 -0
- package/cjs/hovercard/hovercard-store.cjs +11 -0
- package/cjs/hovercard/hovercard-store.d.ts +45 -0
- package/cjs/index.cjs +5 -0
- package/cjs/index.d.ts +2 -0
- package/cjs/menu/menu-bar-store.cjs +27 -0
- package/cjs/menu/menu-bar-store.d.ts +10 -0
- package/cjs/menu/menu-store.cjs +108 -0
- package/cjs/menu/menu-store.d.ts +70 -0
- package/cjs/popover/popover-store.cjs +10 -0
- package/cjs/popover/popover-store.d.ts +150 -0
- package/cjs/radio/radio-store.cjs +41 -0
- package/cjs/radio/radio-store.d.ts +36 -0
- package/cjs/select/select-store.cjs +164 -0
- package/cjs/select/select-store.d.ts +87 -0
- package/cjs/tab/tab-store.cjs +126 -0
- package/cjs/tab/tab-store.d.ts +78 -0
- package/cjs/toolbar/toolbar-store.cjs +27 -0
- package/cjs/toolbar/toolbar-store.d.ts +21 -0
- package/cjs/tooltip/tooltip-store.cjs +98 -0
- package/cjs/tooltip/tooltip-store.d.ts +28 -0
- package/cjs/tsconfig.build.tsbuildinfo +1 -0
- package/cjs/utils/array.cjs +12 -0
- package/cjs/utils/array.d.ts +29 -0
- package/cjs/utils/dom.cjs +38 -0
- package/cjs/utils/dom.d.ts +105 -0
- package/cjs/utils/events.cjs +132 -0
- package/cjs/utils/events.d.ts +73 -0
- package/cjs/utils/focus.cjs +222 -0
- package/cjs/utils/focus.d.ts +117 -0
- package/cjs/utils/misc.cjs +40 -0
- package/cjs/utils/misc.d.ts +111 -0
- package/cjs/utils/platform.cjs +15 -0
- package/cjs/utils/platform.d.ts +20 -0
- package/cjs/utils/store.cjs +9 -0
- package/cjs/utils/store.d.ts +99 -0
- package/cjs/utils/types.cjs +1 -0
- package/cjs/utils/types.d.ts +72 -0
- package/collection/collection-store/package.json +7 -0
- package/combobox/combobox-store/package.json +7 -0
- package/composite/composite-overflow-store/package.json +7 -0
- package/composite/composite-store/package.json +7 -0
- package/dialog/dialog-store/package.json +7 -0
- package/disclosure/disclosure-store/package.json +7 -0
- package/esm/__chunks/5XEKIOCW.js +30 -0
- package/esm/__chunks/6U25WEDX.js +286 -0
- package/esm/__chunks/6UPCMUXT.js +1491 -0
- package/esm/__chunks/ADRUFBEO.js +12 -0
- package/esm/__chunks/DXA3K2FY.js +30 -0
- package/esm/__chunks/HCKYJLMC.js +71 -0
- package/esm/__chunks/KLKI3AIB.js +146 -0
- package/esm/__chunks/L7KN5AYP.js +169 -0
- package/esm/__chunks/RX3ZUQ6U.js +57 -0
- package/esm/__chunks/UOJSZ35L.js +143 -0
- package/esm/__chunks/UVCATTRC.js +182 -0
- package/esm/checkbox/checkbox-store.d.ts +34 -0
- package/esm/checkbox/checkbox-store.js +27 -0
- package/esm/collection/collection-store.d.ts +65 -0
- package/esm/collection/collection-store.js +9 -0
- package/esm/combobox/combobox-store.d.ts +67 -0
- package/esm/combobox/combobox-store.js +163 -0
- package/esm/composite/composite-overflow-store.d.ts +10 -0
- package/esm/composite/composite-overflow-store.js +15 -0
- package/esm/composite/composite-store.d.ts +202 -0
- package/esm/composite/composite-store.js +11 -0
- package/esm/dialog/dialog-store.d.ts +10 -0
- package/esm/dialog/dialog-store.js +9 -0
- package/esm/disclosure/disclosure-store.d.ts +87 -0
- package/esm/disclosure/disclosure-store.js +8 -0
- package/esm/form/form-store.d.ts +236 -0
- package/esm/form/form-store.js +241 -0
- package/esm/form/types.d.ts +38 -0
- package/esm/form/types.js +0 -0
- package/esm/hovercard/hovercard-store.d.ts +45 -0
- package/esm/hovercard/hovercard-store.js +11 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.js +5 -0
- package/esm/menu/menu-bar-store.d.ts +10 -0
- package/esm/menu/menu-bar-store.js +27 -0
- package/esm/menu/menu-store.d.ts +70 -0
- package/esm/menu/menu-store.js +108 -0
- package/esm/popover/popover-store.d.ts +150 -0
- package/esm/popover/popover-store.js +10 -0
- package/esm/radio/radio-store.d.ts +36 -0
- package/esm/radio/radio-store.js +41 -0
- package/esm/select/select-store.d.ts +87 -0
- package/esm/select/select-store.js +164 -0
- package/esm/tab/tab-store.d.ts +78 -0
- package/esm/tab/tab-store.js +126 -0
- package/esm/toolbar/toolbar-store.d.ts +21 -0
- package/esm/toolbar/toolbar-store.js +27 -0
- package/esm/tooltip/tooltip-store.d.ts +28 -0
- package/esm/tooltip/tooltip-store.js +98 -0
- package/esm/tsconfig.build.tsbuildinfo +1 -0
- package/esm/utils/array.d.ts +29 -0
- package/esm/utils/array.js +12 -0
- package/esm/utils/dom.d.ts +105 -0
- package/esm/utils/dom.js +38 -0
- package/esm/utils/events.d.ts +73 -0
- package/esm/utils/events.js +132 -0
- package/esm/utils/focus.d.ts +117 -0
- package/esm/utils/focus.js +222 -0
- package/esm/utils/misc.d.ts +111 -0
- package/esm/utils/misc.js +40 -0
- package/esm/utils/platform.d.ts +20 -0
- package/esm/utils/platform.js +15 -0
- package/esm/utils/store.d.ts +99 -0
- package/esm/utils/store.js +9 -0
- package/esm/utils/types.d.ts +72 -0
- package/esm/utils/types.js +0 -0
- package/form/form-store/package.json +7 -0
- package/form/types/package.json +7 -0
- package/hovercard/hovercard-store/package.json +7 -0
- package/license +21 -0
- package/menu/menu-bar-store/package.json +7 -0
- package/menu/menu-store/package.json +7 -0
- package/package.json +137 -6
- package/popover/popover-store/package.json +7 -0
- package/radio/radio-store/package.json +7 -0
- package/select/select-store/package.json +7 -0
- package/tab/tab-store/package.json +7 -0
- package/toolbar/toolbar-store/package.json +7 -0
- package/tooltip/tooltip-store/package.json +7 -0
- package/tsconfig.build.json +4 -0
- package/utils/array/package.json +7 -0
- package/utils/dom/package.json +7 -0
- package/utils/events/package.json +7 -0
- package/utils/focus/package.json +7 -0
- package/utils/misc/package.json +7 -0
- package/utils/platform/package.json +7 -0
- package/utils/store/package.json +7 -0
- package/utils/types/package.json +7 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCollectionStore
|
|
3
|
+
} from "../__chunks/L7KN5AYP.js";
|
|
4
|
+
import {
|
|
5
|
+
createStore
|
|
6
|
+
} from "../__chunks/UOJSZ35L.js";
|
|
7
|
+
import {
|
|
8
|
+
applyState,
|
|
9
|
+
defaultValue,
|
|
10
|
+
isInteger,
|
|
11
|
+
isObject
|
|
12
|
+
} from "../__chunks/KLKI3AIB.js";
|
|
13
|
+
import "../__chunks/UVCATTRC.js";
|
|
14
|
+
|
|
15
|
+
// src/form/form-store.ts
|
|
16
|
+
function nextFrame() {
|
|
17
|
+
return new Promise((resolve) => requestAnimationFrame(() => resolve()));
|
|
18
|
+
}
|
|
19
|
+
function hasMessages(object) {
|
|
20
|
+
return Object.keys(object).some((key) => {
|
|
21
|
+
if (isObject(object[key])) {
|
|
22
|
+
return hasMessages(object[key]);
|
|
23
|
+
}
|
|
24
|
+
return !!object[key];
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function get(values, path, defaultValue2) {
|
|
28
|
+
const [key, ...rest] = Array.isArray(path) ? path : `${path}`.split(".");
|
|
29
|
+
if (key == null || !values) {
|
|
30
|
+
return defaultValue2;
|
|
31
|
+
}
|
|
32
|
+
if (!rest.length) {
|
|
33
|
+
return values[key] ?? defaultValue2;
|
|
34
|
+
}
|
|
35
|
+
return get(values[key], rest, defaultValue2);
|
|
36
|
+
}
|
|
37
|
+
function set(values, path, value) {
|
|
38
|
+
const [k, ...rest] = Array.isArray(path) ? path : `${path}`.split(".");
|
|
39
|
+
if (k == null)
|
|
40
|
+
return values;
|
|
41
|
+
const key = k;
|
|
42
|
+
const isIntegerKey = isInteger(key);
|
|
43
|
+
const nextValues = isIntegerKey ? values || [] : values || {};
|
|
44
|
+
const nestedValues = nextValues[key];
|
|
45
|
+
const result = rest.length && (Array.isArray(nestedValues) || isObject(nestedValues)) ? set(nestedValues, rest, value) : value;
|
|
46
|
+
if (isIntegerKey) {
|
|
47
|
+
const index = Number(key);
|
|
48
|
+
if (values) {
|
|
49
|
+
return [
|
|
50
|
+
...values.slice(0, index),
|
|
51
|
+
result,
|
|
52
|
+
...values.slice(index + 1)
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
const nextValues2 = [];
|
|
56
|
+
nextValues2[index] = result;
|
|
57
|
+
return nextValues2;
|
|
58
|
+
}
|
|
59
|
+
return { ...values, [key]: result };
|
|
60
|
+
}
|
|
61
|
+
function setAll(values, value) {
|
|
62
|
+
const result = {};
|
|
63
|
+
const keys = Object.keys(values);
|
|
64
|
+
for (const key of keys) {
|
|
65
|
+
const currentValue = values[key];
|
|
66
|
+
if (Array.isArray(currentValue)) {
|
|
67
|
+
result[key] = currentValue.map((v) => {
|
|
68
|
+
if (isObject(v)) {
|
|
69
|
+
return setAll(v, value);
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
});
|
|
73
|
+
} else if (isObject(currentValue)) {
|
|
74
|
+
result[key] = setAll(currentValue, value);
|
|
75
|
+
} else {
|
|
76
|
+
result[key] = value;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
function getNameHandler(cache, prevKeys = []) {
|
|
82
|
+
const handler = {
|
|
83
|
+
get(target, key) {
|
|
84
|
+
if (["toString", "valueOf", Symbol.toPrimitive].includes(key)) {
|
|
85
|
+
return () => prevKeys.join(".");
|
|
86
|
+
}
|
|
87
|
+
const nextKeys = [...prevKeys, key];
|
|
88
|
+
const nextKey = nextKeys.join(".");
|
|
89
|
+
if (cache[nextKey]) {
|
|
90
|
+
return cache[nextKey];
|
|
91
|
+
}
|
|
92
|
+
const nextProxy = new Proxy(target, getNameHandler(cache, nextKeys));
|
|
93
|
+
cache[nextKey] = nextProxy;
|
|
94
|
+
return nextProxy;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
return handler;
|
|
98
|
+
}
|
|
99
|
+
function createNames() {
|
|
100
|
+
const cache = /* @__PURE__ */ Object.create(null);
|
|
101
|
+
return new Proxy(/* @__PURE__ */ Object.create(null), getNameHandler(cache));
|
|
102
|
+
}
|
|
103
|
+
function createFormStore(props = {}) {
|
|
104
|
+
const syncState = props.store?.getState();
|
|
105
|
+
const collection = createCollectionStore(props);
|
|
106
|
+
const values = defaultValue(
|
|
107
|
+
props.values,
|
|
108
|
+
syncState?.values,
|
|
109
|
+
props.defaultValues,
|
|
110
|
+
{}
|
|
111
|
+
);
|
|
112
|
+
const errors = defaultValue(
|
|
113
|
+
props.errors,
|
|
114
|
+
syncState?.errors,
|
|
115
|
+
props.defaultErrors,
|
|
116
|
+
{}
|
|
117
|
+
);
|
|
118
|
+
const touched = defaultValue(
|
|
119
|
+
props.touched,
|
|
120
|
+
syncState?.touched,
|
|
121
|
+
props.defaultTouched,
|
|
122
|
+
{}
|
|
123
|
+
);
|
|
124
|
+
const initialState = {
|
|
125
|
+
...collection.getState(),
|
|
126
|
+
values,
|
|
127
|
+
errors,
|
|
128
|
+
touched,
|
|
129
|
+
validating: defaultValue(syncState?.validating, false),
|
|
130
|
+
submitting: defaultValue(syncState?.submitting, false),
|
|
131
|
+
submitSucceed: defaultValue(syncState?.submitSucceed, 0),
|
|
132
|
+
submitFailed: defaultValue(syncState?.submitFailed, 0),
|
|
133
|
+
valid: !hasMessages(errors)
|
|
134
|
+
};
|
|
135
|
+
const form = createStore(initialState, collection, props.store);
|
|
136
|
+
const validateCallbacks = /* @__PURE__ */ new Set();
|
|
137
|
+
const submitCallbacks = /* @__PURE__ */ new Set();
|
|
138
|
+
const validate = async () => {
|
|
139
|
+
form.setState("validating", true);
|
|
140
|
+
form.setState("errors", {});
|
|
141
|
+
try {
|
|
142
|
+
const callbacks = [...validateCallbacks];
|
|
143
|
+
const results = callbacks.map((callback) => callback(form.getState()));
|
|
144
|
+
await Promise.all(results).then(nextFrame);
|
|
145
|
+
return !hasMessages(form.getState().errors);
|
|
146
|
+
} finally {
|
|
147
|
+
form.setState("validating", false);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
return {
|
|
151
|
+
...collection,
|
|
152
|
+
...form,
|
|
153
|
+
names: createNames(),
|
|
154
|
+
setValues: (values2) => form.setState("values", values2),
|
|
155
|
+
getValue: (name) => get(form.getState().values, name),
|
|
156
|
+
setValue: (name, value) => form.setState("values", (values2) => {
|
|
157
|
+
const prevValue = get(values2, name);
|
|
158
|
+
const nextValue = applyState(value, prevValue);
|
|
159
|
+
if (nextValue === prevValue)
|
|
160
|
+
return values2;
|
|
161
|
+
return set(values2, name, nextValue);
|
|
162
|
+
}),
|
|
163
|
+
pushValue: (name, value) => form.setState("values", (values2) => {
|
|
164
|
+
const array = get(values2, name, []);
|
|
165
|
+
return set(values2, name, [...array, value]);
|
|
166
|
+
}),
|
|
167
|
+
removeValue: (name, index) => form.setState("values", (values2) => {
|
|
168
|
+
const array = get(values2, name, []);
|
|
169
|
+
return set(values2, name, [
|
|
170
|
+
...array.slice(0, index),
|
|
171
|
+
null,
|
|
172
|
+
...array.slice(index + 1)
|
|
173
|
+
]);
|
|
174
|
+
}),
|
|
175
|
+
setErrors: (errors2) => form.setState("errors", errors2),
|
|
176
|
+
getError: (name) => get(form.getState().errors, name),
|
|
177
|
+
setError: (name, error) => form.setState("errors", (errors2) => {
|
|
178
|
+
const prevError = get(errors2, name);
|
|
179
|
+
const nextError = applyState(error, prevError);
|
|
180
|
+
if (nextError === prevError)
|
|
181
|
+
return errors2;
|
|
182
|
+
return set(errors2, name, nextError);
|
|
183
|
+
}),
|
|
184
|
+
setTouched: (touched2) => form.setState("touched", touched2),
|
|
185
|
+
getFieldTouched: (name) => !!get(form.getState().touched, name),
|
|
186
|
+
setFieldTouched: (name, value) => form.setState("touched", (touched2) => {
|
|
187
|
+
const prevValue = get(touched2, name);
|
|
188
|
+
const nextValue = applyState(value, prevValue);
|
|
189
|
+
if (nextValue === prevValue)
|
|
190
|
+
return touched2;
|
|
191
|
+
return set(touched2, name, nextValue);
|
|
192
|
+
}),
|
|
193
|
+
onValidate: (callback) => {
|
|
194
|
+
validateCallbacks.add(callback);
|
|
195
|
+
return () => validateCallbacks.delete(callback);
|
|
196
|
+
},
|
|
197
|
+
validate,
|
|
198
|
+
onSubmit: (callback) => {
|
|
199
|
+
submitCallbacks.add(callback);
|
|
200
|
+
return () => submitCallbacks.delete(callback);
|
|
201
|
+
},
|
|
202
|
+
submit: async () => {
|
|
203
|
+
form.setState("submitting", true);
|
|
204
|
+
form.setState("touched", setAll(form.getState().values, true));
|
|
205
|
+
try {
|
|
206
|
+
if (await validate()) {
|
|
207
|
+
const callbacks = [...submitCallbacks];
|
|
208
|
+
const results = callbacks.map(
|
|
209
|
+
(callback) => callback(form.getState())
|
|
210
|
+
);
|
|
211
|
+
await Promise.all(results).then(nextFrame);
|
|
212
|
+
if (!hasMessages(form.getState().errors)) {
|
|
213
|
+
form.setState("submitSucceed", (count) => count + 1);
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
form.setState("submitFailed", (count) => count + 1);
|
|
218
|
+
return false;
|
|
219
|
+
} catch (error) {
|
|
220
|
+
form.setState("submitFailed", (count) => count + 1);
|
|
221
|
+
throw error;
|
|
222
|
+
} finally {
|
|
223
|
+
form.setState("submitting", false);
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
reset: () => {
|
|
227
|
+
form.setState("values", values);
|
|
228
|
+
form.setState("errors", errors);
|
|
229
|
+
form.setState("touched", touched);
|
|
230
|
+
form.setState("validating", false);
|
|
231
|
+
form.setState("submitting", false);
|
|
232
|
+
form.setState("submitSucceed", 0);
|
|
233
|
+
form.setState("submitFailed", 0);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
export {
|
|
238
|
+
createFormStore,
|
|
239
|
+
get,
|
|
240
|
+
hasMessages
|
|
241
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { AnyObject } from "../utils/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* An object or primitive value that can be converted to a string.
|
|
4
|
+
*/
|
|
5
|
+
export type StringLike = {
|
|
6
|
+
toString: () => string;
|
|
7
|
+
valueOf: () => string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Maps through an object `T` or array and defines the leaf values to the given
|
|
11
|
+
* type `V`.
|
|
12
|
+
* @template T Object
|
|
13
|
+
* @template V Value
|
|
14
|
+
*/
|
|
15
|
+
export type DeepMap<T, V> = {
|
|
16
|
+
[K in keyof T]: T[K] extends AnyObject ? DeepMap<T[K], V> : V;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Similar to `Partial<T>`, but recursively maps through the object and makes
|
|
20
|
+
* nested object properties optional.
|
|
21
|
+
* @template T Object
|
|
22
|
+
*/
|
|
23
|
+
export type DeepPartial<T> = {
|
|
24
|
+
[K in keyof T]?: T[K] extends AnyObject ? DeepPartial<T[K]> : T[K];
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Maps through the values object `T` and defines all properties into a string
|
|
28
|
+
* like type. That is, a type that is an object that can contain other
|
|
29
|
+
* properties but can also be converted into a string with the path name.
|
|
30
|
+
* @template T Values object
|
|
31
|
+
*/
|
|
32
|
+
export type Names<T> = {
|
|
33
|
+
[K in keyof T]: T[K] extends Array<infer U> ? U extends AnyObject ? {
|
|
34
|
+
[key: number]: Names<U>;
|
|
35
|
+
} & StringLike : {
|
|
36
|
+
[key: number]: U & StringLike;
|
|
37
|
+
} & StringLike : T[K] extends AnyObject ? Names<T[K]> : StringLike;
|
|
38
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { PopoverStoreFunctions, PopoverStoreOptions, PopoverStoreState } from "../popover/popover-store.js";
|
|
2
|
+
import type { Store, StoreOptions, StoreProps } from "../utils/store.js";
|
|
3
|
+
import type { SetState } from "../utils/types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a hovercard store.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createHovercardStore(props?: HovercardStoreProps): HovercardStore;
|
|
8
|
+
export interface HovercardStoreState extends PopoverStoreState {
|
|
9
|
+
/**
|
|
10
|
+
* @default "bottom"
|
|
11
|
+
*/
|
|
12
|
+
placement: PopoverStoreState["placement"];
|
|
13
|
+
/**
|
|
14
|
+
* The amount of time in milliseconds to wait before showing or hiding the
|
|
15
|
+
* popover.
|
|
16
|
+
* @default 500
|
|
17
|
+
*/
|
|
18
|
+
timeout: number;
|
|
19
|
+
/**
|
|
20
|
+
* The amount of time in milliseconds to wait before **showing** the popover.
|
|
21
|
+
* It defaults to the value passed to `timeout`.
|
|
22
|
+
*/
|
|
23
|
+
showTimeout: number;
|
|
24
|
+
/**
|
|
25
|
+
* The amount of time in milliseconds to wait before **hiding** the popover.
|
|
26
|
+
* It defaults to the value passed to `timeout`.
|
|
27
|
+
*/
|
|
28
|
+
hideTimeout: number;
|
|
29
|
+
/**
|
|
30
|
+
* Whether the popover or an element inside it should be focused when it is
|
|
31
|
+
* shown.
|
|
32
|
+
* @default false
|
|
33
|
+
*/
|
|
34
|
+
autoFocusOnShow: boolean;
|
|
35
|
+
}
|
|
36
|
+
export interface HovercardStoreFunctions extends PopoverStoreFunctions {
|
|
37
|
+
/**
|
|
38
|
+
* Sets the `autoFocusOnShow` state.
|
|
39
|
+
*/
|
|
40
|
+
setAutoFocusOnShow: SetState<HovercardStoreState["autoFocusOnShow"]>;
|
|
41
|
+
}
|
|
42
|
+
export interface HovercardStoreOptions extends StoreOptions<HovercardStoreState, "placement" | "timeout" | "showTimeout" | "hideTimeout">, PopoverStoreOptions {
|
|
43
|
+
}
|
|
44
|
+
export type HovercardStoreProps = HovercardStoreOptions & StoreProps<HovercardStoreState>;
|
|
45
|
+
export type HovercardStore = HovercardStoreFunctions & Store<HovercardStoreState>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createHovercardStore
|
|
3
|
+
} from "../__chunks/RX3ZUQ6U.js";
|
|
4
|
+
import "../__chunks/6UPCMUXT.js";
|
|
5
|
+
import "../__chunks/ADRUFBEO.js";
|
|
6
|
+
import "../__chunks/HCKYJLMC.js";
|
|
7
|
+
import "../__chunks/UOJSZ35L.js";
|
|
8
|
+
import "../__chunks/KLKI3AIB.js";
|
|
9
|
+
export {
|
|
10
|
+
createHovercardStore
|
|
11
|
+
};
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CompositeStore, CompositeStoreFunctions, CompositeStoreOptions, CompositeStoreProps, CompositeStoreState } from "../composite/composite-store.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a menu bar store.
|
|
4
|
+
*/
|
|
5
|
+
export declare function createMenuBarStore(props?: MenuBarStoreProps): MenuBarStore;
|
|
6
|
+
export type MenuBarStoreState = CompositeStoreState;
|
|
7
|
+
export type MenuBarStoreFunctions = CompositeStoreFunctions;
|
|
8
|
+
export type MenuBarStoreOptions = CompositeStoreOptions;
|
|
9
|
+
export type MenuBarStoreProps = CompositeStoreProps;
|
|
10
|
+
export type MenuBarStore = CompositeStore;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCompositeStore
|
|
3
|
+
} from "../__chunks/6U25WEDX.js";
|
|
4
|
+
import "../__chunks/L7KN5AYP.js";
|
|
5
|
+
import "../__chunks/UOJSZ35L.js";
|
|
6
|
+
import {
|
|
7
|
+
defaultValue
|
|
8
|
+
} from "../__chunks/KLKI3AIB.js";
|
|
9
|
+
import "../__chunks/UVCATTRC.js";
|
|
10
|
+
import "../__chunks/5XEKIOCW.js";
|
|
11
|
+
|
|
12
|
+
// src/menu/menu-bar-store.ts
|
|
13
|
+
function createMenuBarStore(props = {}) {
|
|
14
|
+
const syncState = props.store?.getState();
|
|
15
|
+
return createCompositeStore({
|
|
16
|
+
...props,
|
|
17
|
+
orientation: defaultValue(
|
|
18
|
+
props.orientation,
|
|
19
|
+
syncState?.orientation,
|
|
20
|
+
"horizontal"
|
|
21
|
+
),
|
|
22
|
+
focusLoop: defaultValue(props.focusLoop, syncState?.focusLoop, true)
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
createMenuBarStore
|
|
27
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { ComboboxStore } from "../combobox/combobox-store.js";
|
|
2
|
+
import type { CompositeStoreFunctions, CompositeStoreOptions, CompositeStoreState } from "../composite/composite-store.js";
|
|
3
|
+
import type { HovercardStoreFunctions, HovercardStoreOptions, HovercardStoreState } from "../hovercard/hovercard-store.js";
|
|
4
|
+
import type { Store, StoreOptions, StoreProps } from "../utils/store.js";
|
|
5
|
+
import type { BivariantCallback, PickRequired, SetState, SetStateAction } from "../utils/types.js";
|
|
6
|
+
type Values = Record<string, string | boolean | number | Array<string | number>>;
|
|
7
|
+
export declare function createMenuStore<T extends Values = Values>(props: PickRequired<MenuStoreProps<T>, "values" | "defaultValues">): MenuStore<T>;
|
|
8
|
+
export declare function createMenuStore(props?: MenuStoreProps): MenuStore;
|
|
9
|
+
export type MenuStoreValues = Record<string, string | boolean | number | Array<string | number>>;
|
|
10
|
+
export interface MenuStoreState<T extends Values = Values> extends CompositeStoreState, HovercardStoreState {
|
|
11
|
+
/**
|
|
12
|
+
* @default "vertical"
|
|
13
|
+
*/
|
|
14
|
+
orientation: CompositeStoreState["orientation"];
|
|
15
|
+
/**
|
|
16
|
+
* @default "bottom-start"
|
|
17
|
+
*/
|
|
18
|
+
placement: HovercardStoreState["placement"];
|
|
19
|
+
/**
|
|
20
|
+
* @default 0
|
|
21
|
+
*/
|
|
22
|
+
hideTimeout: HovercardStoreState["hideTimeout"];
|
|
23
|
+
/**
|
|
24
|
+
* Determines the element that should be focused when the menu is opened.
|
|
25
|
+
*/
|
|
26
|
+
initialFocus: "container" | "first" | "last";
|
|
27
|
+
/**
|
|
28
|
+
* A map of names and values that will be used by the `MenuItemCheckbox` and
|
|
29
|
+
* `MenuItemRadio` components.
|
|
30
|
+
*/
|
|
31
|
+
values: T;
|
|
32
|
+
}
|
|
33
|
+
export interface MenuStoreFunctions<T extends Values = Values> extends CompositeStoreFunctions, HovercardStoreFunctions {
|
|
34
|
+
/**
|
|
35
|
+
* Sets the `initialFocus` state.
|
|
36
|
+
*/
|
|
37
|
+
setInitialFocus: SetState<MenuStoreState<T>["initialFocus"]>;
|
|
38
|
+
/**
|
|
39
|
+
* Sets the `values` state.
|
|
40
|
+
* @example
|
|
41
|
+
* store.setValues({ watching: ["issues"] });
|
|
42
|
+
* store.setValues((values) => ({ ...values, watching: ["issues"] }));
|
|
43
|
+
*/
|
|
44
|
+
setValues: SetState<MenuStoreState<T>["values"]>;
|
|
45
|
+
/**
|
|
46
|
+
* Sets a specific menu value.
|
|
47
|
+
* @param name The name.
|
|
48
|
+
* @param value The value.
|
|
49
|
+
* @example
|
|
50
|
+
* store.setValue("watching", ["issues"]);
|
|
51
|
+
* store.setValue("watching", (value) => [...value, "issues"]);
|
|
52
|
+
*/
|
|
53
|
+
setValue: BivariantCallback<(name: string, value: SetStateAction<MenuStoreState<T>["values"][string]>) => void>;
|
|
54
|
+
}
|
|
55
|
+
export interface MenuStoreOptions<T extends Values = Values> extends StoreOptions<MenuStoreState<T>, "orientation" | "placement" | "hideTimeout" | "values">, CompositeStoreOptions, HovercardStoreOptions {
|
|
56
|
+
/**
|
|
57
|
+
* A reference to a combobox store. This is used when combining the combobox
|
|
58
|
+
* with a menu (e.g., dropdown menu with a search input). The stores will
|
|
59
|
+
* share the same state.
|
|
60
|
+
*/
|
|
61
|
+
combobox?: ComboboxStore;
|
|
62
|
+
/**
|
|
63
|
+
* The default values for the `values` state.
|
|
64
|
+
* @default {}
|
|
65
|
+
*/
|
|
66
|
+
defaultValues?: MenuStoreState<T>["values"];
|
|
67
|
+
}
|
|
68
|
+
export type MenuStoreProps<T extends Values = Values> = MenuStoreOptions<T> & StoreProps<MenuStoreState<T>>;
|
|
69
|
+
export type MenuStore<T extends Values = Values> = MenuStoreFunctions<T> & Store<MenuStoreState<T>>;
|
|
70
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createHovercardStore
|
|
3
|
+
} from "../__chunks/RX3ZUQ6U.js";
|
|
4
|
+
import "../__chunks/6UPCMUXT.js";
|
|
5
|
+
import "../__chunks/ADRUFBEO.js";
|
|
6
|
+
import "../__chunks/HCKYJLMC.js";
|
|
7
|
+
import {
|
|
8
|
+
createCompositeStore
|
|
9
|
+
} from "../__chunks/6U25WEDX.js";
|
|
10
|
+
import "../__chunks/L7KN5AYP.js";
|
|
11
|
+
import {
|
|
12
|
+
createStore,
|
|
13
|
+
mergeStore
|
|
14
|
+
} from "../__chunks/UOJSZ35L.js";
|
|
15
|
+
import {
|
|
16
|
+
applyState,
|
|
17
|
+
defaultValue
|
|
18
|
+
} from "../__chunks/KLKI3AIB.js";
|
|
19
|
+
import "../__chunks/UVCATTRC.js";
|
|
20
|
+
import "../__chunks/5XEKIOCW.js";
|
|
21
|
+
|
|
22
|
+
// src/menu/menu-store.ts
|
|
23
|
+
function createMenuStore({
|
|
24
|
+
combobox,
|
|
25
|
+
...props
|
|
26
|
+
} = {}) {
|
|
27
|
+
const store = mergeStore(
|
|
28
|
+
props.store,
|
|
29
|
+
combobox?.omit(
|
|
30
|
+
"baseElement",
|
|
31
|
+
"arrowElement",
|
|
32
|
+
"anchorElement",
|
|
33
|
+
"contentElement",
|
|
34
|
+
"popoverElement",
|
|
35
|
+
"disclosureElement"
|
|
36
|
+
)
|
|
37
|
+
);
|
|
38
|
+
const syncState = store.getState();
|
|
39
|
+
const composite = createCompositeStore({
|
|
40
|
+
...props,
|
|
41
|
+
store,
|
|
42
|
+
orientation: defaultValue(
|
|
43
|
+
props.orientation,
|
|
44
|
+
syncState.orientation,
|
|
45
|
+
"vertical"
|
|
46
|
+
)
|
|
47
|
+
});
|
|
48
|
+
const hovercard = createHovercardStore({
|
|
49
|
+
...props,
|
|
50
|
+
store,
|
|
51
|
+
placement: defaultValue(
|
|
52
|
+
props.placement,
|
|
53
|
+
syncState.placement,
|
|
54
|
+
"bottom-start"
|
|
55
|
+
),
|
|
56
|
+
hideTimeout: defaultValue(props.hideTimeout, syncState.hideTimeout, 0)
|
|
57
|
+
});
|
|
58
|
+
const initialState = {
|
|
59
|
+
...composite.getState(),
|
|
60
|
+
...hovercard.getState(),
|
|
61
|
+
initialFocus: defaultValue(syncState.initialFocus, "container"),
|
|
62
|
+
values: defaultValue(
|
|
63
|
+
props.values,
|
|
64
|
+
syncState.values,
|
|
65
|
+
props.defaultValues,
|
|
66
|
+
{}
|
|
67
|
+
)
|
|
68
|
+
};
|
|
69
|
+
const menu = createStore(initialState, composite, hovercard, store);
|
|
70
|
+
menu.setup(
|
|
71
|
+
() => menu.sync(
|
|
72
|
+
(state) => {
|
|
73
|
+
if (state.mounted)
|
|
74
|
+
return;
|
|
75
|
+
menu.setState("activeId", null);
|
|
76
|
+
},
|
|
77
|
+
["mounted"]
|
|
78
|
+
)
|
|
79
|
+
);
|
|
80
|
+
return {
|
|
81
|
+
...composite,
|
|
82
|
+
...hovercard,
|
|
83
|
+
...menu,
|
|
84
|
+
setInitialFocus: (value) => menu.setState("initialFocus", value),
|
|
85
|
+
setValues: (values) => menu.setState("values", values),
|
|
86
|
+
setValue: (name, value) => {
|
|
87
|
+
if (name === "__proto__")
|
|
88
|
+
return;
|
|
89
|
+
if (name === "constructor")
|
|
90
|
+
return;
|
|
91
|
+
if (Array.isArray(name))
|
|
92
|
+
return;
|
|
93
|
+
menu.setState("values", (values) => {
|
|
94
|
+
const prevValue = values[name];
|
|
95
|
+
const nextValue = applyState(value, prevValue);
|
|
96
|
+
if (nextValue === prevValue)
|
|
97
|
+
return values;
|
|
98
|
+
return {
|
|
99
|
+
...values,
|
|
100
|
+
[name]: nextValue !== void 0 && nextValue
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export {
|
|
107
|
+
createMenuStore
|
|
108
|
+
};
|