@destyler/toggle 0.0.7-beta.7 → 0.0.7-beta.9

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/dist/index.mjs DELETED
@@ -1,341 +0,0 @@
1
- import { createAnatomy } from '@zag-js/anatomy';
2
- import { getEventKey } from '@zag-js/dom-event';
3
- import { createScope, prevById, nextById, queryAll, dataAttr, isSelfTarget, isSafari, raf } from '@zag-js/dom-query';
4
- import { last, first, compact, remove, add, isEqual, createSplitProps } from '@zag-js/utils';
5
- import { guards, createMachine } from '@zag-js/core';
6
- import { createProps } from '@zag-js/types';
7
-
8
- const anatomy = createAnatomy("toggle").parts("root", "item");
9
- const parts = anatomy.build();
10
-
11
- const dom = createScope({
12
- getRootId: (ctx) => ctx.ids?.root ?? `toggle:${ctx.id}`,
13
- getItemId: (ctx, value) => ctx.ids?.item?.(value) ?? `toggle:${ctx.id}:${value}`,
14
- getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
15
- getElements: (ctx) => {
16
- const ownerId = CSS.escape(dom.getRootId(ctx));
17
- const selector = `[data-ownedby='${ownerId}']:not([data-disabled])`;
18
- return queryAll(dom.getRootEl(ctx), selector);
19
- },
20
- getFirstEl: (ctx) => first(dom.getElements(ctx)),
21
- getLastEl: (ctx) => last(dom.getElements(ctx)),
22
- getNextEl: (ctx, id) => nextById(dom.getElements(ctx), id, ctx.currentLoopFocus),
23
- getPrevEl: (ctx, id) => prevById(dom.getElements(ctx), id, ctx.currentLoopFocus)
24
- });
25
-
26
- function connect(state, send, normalize) {
27
- const value = state.context.value;
28
- const disabled = state.context.disabled;
29
- const isSingle = !state.context.multiple;
30
- const rovingFocus = state.context.rovingFocus;
31
- const isHorizontal = state.context.orientation === "horizontal";
32
- function getItemState(props) {
33
- const id = dom.getItemId(state.context, props.value);
34
- return {
35
- id,
36
- disabled: Boolean(props.disabled || disabled),
37
- pressed: !!value.includes(props.value),
38
- focused: state.context.focusedId === id
39
- };
40
- }
41
- return {
42
- value,
43
- setValue(value2) {
44
- send({ type: "VALUE.SET", value: value2 });
45
- },
46
- getRootProps() {
47
- return normalize.element({
48
- ...parts.root.attrs,
49
- "id": dom.getRootId(state.context),
50
- "dir": state.context.dir,
51
- "role": isSingle ? "radiogroup" : "group",
52
- "tabIndex": state.context.isTabbingBackward ? -1 : 0,
53
- "data-disabled": dataAttr(disabled),
54
- "data-orientation": state.context.orientation,
55
- "data-focus": dataAttr(state.context.focusedId != null),
56
- "style": { outline: "none" },
57
- onMouseDown() {
58
- if (disabled)
59
- return;
60
- send("ROOT.MOUSE_DOWN");
61
- },
62
- onFocus(event) {
63
- if (disabled)
64
- return;
65
- const evt = event.nativeEvent || event;
66
- if (!isSelfTarget(evt) || !!state.context.isClickFocus || state.context.isTabbingBackward)
67
- return;
68
- send("ROOT.FOCUS");
69
- },
70
- onBlur() {
71
- if (disabled)
72
- return;
73
- send("ROOT.BLUR");
74
- }
75
- });
76
- },
77
- getItemState,
78
- getItemProps(props) {
79
- const itemState = getItemState(props);
80
- const rovingTabIndex = itemState.focused ? 0 : -1;
81
- return normalize.button({
82
- ...parts.item.attrs,
83
- "id": itemState.id,
84
- "type": "button",
85
- "data-ownedby": dom.getRootId(state.context),
86
- "data-focus": dataAttr(itemState.focused),
87
- "disabled": itemState.disabled,
88
- "tabIndex": rovingFocus ? rovingTabIndex : void 0,
89
- // radio
90
- "role": isSingle ? "radio" : void 0,
91
- "aria-checked": isSingle ? itemState.pressed : void 0,
92
- "aria-pressed": isSingle ? void 0 : itemState.pressed,
93
- //
94
- "data-disabled": dataAttr(itemState.disabled),
95
- "data-orientation": state.context.orientation,
96
- "dir": state.context.dir,
97
- "data-state": itemState.pressed ? "on" : "off",
98
- onFocus() {
99
- if (itemState.disabled)
100
- return;
101
- send({ type: "TOGGLE.FOCUS", id: itemState.id });
102
- },
103
- onClick(event) {
104
- if (itemState.disabled)
105
- return;
106
- send({ type: "TOGGLE.CLICK", id: itemState.id, value: props.value });
107
- if (isSafari()) {
108
- event.currentTarget.focus({ preventScroll: true });
109
- }
110
- },
111
- onKeyDown(event) {
112
- if (event.defaultPrevented)
113
- return;
114
- if (!isSelfTarget(event))
115
- return;
116
- if (itemState.disabled)
117
- return;
118
- const keyMap = {
119
- Tab(event2) {
120
- const isShiftTab = event2.shiftKey;
121
- send({ type: "TOGGLE.SHIFT_TAB", isShiftTab });
122
- },
123
- ArrowLeft() {
124
- if (!rovingFocus || !isHorizontal)
125
- return;
126
- send("TOGGLE.FOCUS_PREV");
127
- },
128
- ArrowRight() {
129
- if (!rovingFocus || !isHorizontal)
130
- return;
131
- send("TOGGLE.FOCUS_NEXT");
132
- },
133
- ArrowUp() {
134
- if (!rovingFocus || isHorizontal)
135
- return;
136
- send("TOGGLE.FOCUS_PREV");
137
- },
138
- ArrowDown() {
139
- if (!rovingFocus || isHorizontal)
140
- return;
141
- send("TOGGLE.FOCUS_NEXT");
142
- },
143
- Home() {
144
- if (!rovingFocus)
145
- return;
146
- send("TOGGLE.FOCUS_FIRST");
147
- },
148
- End() {
149
- if (!rovingFocus)
150
- return;
151
- send("TOGGLE.FOCUS_LAST");
152
- }
153
- };
154
- const exec = keyMap[getEventKey(event)];
155
- if (exec) {
156
- exec(event);
157
- if (event.key !== "Tab")
158
- event.preventDefault();
159
- }
160
- }
161
- });
162
- }
163
- };
164
- }
165
-
166
- const { not, and } = guards;
167
- const invoke = {
168
- change(ctx) {
169
- ctx.onValueChange?.({ value: Array.from(ctx.value) });
170
- }
171
- };
172
- const set = {
173
- value(ctx, value) {
174
- if (isEqual(ctx.value, value))
175
- return;
176
- ctx.value = value;
177
- invoke.change(ctx);
178
- }
179
- };
180
- function machine(userContext) {
181
- const ctx = compact(userContext);
182
- return createMachine(
183
- {
184
- id: "toggle",
185
- initial: "idle",
186
- context: {
187
- value: [],
188
- disabled: false,
189
- orientation: "horizontal",
190
- rovingFocus: true,
191
- loopFocus: true,
192
- ...ctx,
193
- focusedId: null,
194
- isTabbingBackward: false,
195
- isClickFocus: false,
196
- isWithinToolbar: false
197
- },
198
- computed: {
199
- currentLoopFocus: (ctx2) => ctx2.loopFocus && !ctx2.isWithinToolbar
200
- },
201
- entry: ["checkIfWithinToolbar"],
202
- on: {
203
- "VALUE.SET": {
204
- actions: ["setValue"]
205
- },
206
- "TOGGLE.CLICK": {
207
- actions: ["setValue"]
208
- },
209
- "ROOT.MOUSE_DOWN": {
210
- actions: ["setClickFocus"]
211
- }
212
- },
213
- states: {
214
- idle: {
215
- on: {
216
- "ROOT.FOCUS": {
217
- target: "focused",
218
- guard: not(and("isClickFocus", "isTabbingBackward")),
219
- actions: ["focusFirstToggle", "clearClickFocus"]
220
- },
221
- "TOGGLE.FOCUS": {
222
- target: "focused",
223
- actions: ["setFocusedId"]
224
- }
225
- }
226
- },
227
- focused: {
228
- on: {
229
- "ROOT.BLUR": {
230
- target: "idle",
231
- actions: ["clearIsTabbingBackward"]
232
- },
233
- "TOGGLE.FOCUS": {
234
- actions: ["setFocusedId"]
235
- },
236
- "TOGGLE.FOCUS_NEXT": {
237
- actions: ["focusNextToggle"]
238
- },
239
- "TOGGLE.FOCUS_PREV": {
240
- actions: ["focusPrevToggle"]
241
- },
242
- "TOGGLE.FOCUS_FIRST": {
243
- actions: ["focusFirstToggle"]
244
- },
245
- "TOGGLE.FOCUS_LAST": {
246
- actions: ["focusLastToggle"]
247
- },
248
- "TOGGLE.SHIFT_TAB": {
249
- target: "idle",
250
- actions: ["setIsTabbingBackward"]
251
- }
252
- }
253
- }
254
- }
255
- },
256
- {
257
- guards: {
258
- isClickFocus: (ctx2) => ctx2.isClickFocus,
259
- isTabbingBackward: (ctx2) => ctx2.isTabbingBackward
260
- },
261
- actions: {
262
- setIsTabbingBackward(ctx2) {
263
- ctx2.isTabbingBackward = true;
264
- },
265
- clearIsTabbingBackward(ctx2) {
266
- ctx2.isTabbingBackward = false;
267
- },
268
- setClickFocus(ctx2) {
269
- ctx2.isClickFocus = true;
270
- },
271
- clearClickFocus(ctx2) {
272
- ctx2.isClickFocus = false;
273
- },
274
- checkIfWithinToolbar(ctx2) {
275
- const closestToolbar = dom.getRootEl(ctx2)?.closest("[role=toolbar]");
276
- ctx2.isWithinToolbar = !!closestToolbar;
277
- },
278
- setFocusedId(ctx2, evt) {
279
- ctx2.focusedId = evt.id;
280
- },
281
- clearFocusedId(ctx2) {
282
- ctx2.focusedId = null;
283
- },
284
- setValue(ctx2, evt) {
285
- if (!evt.value)
286
- return;
287
- let next = Array.from(ctx2.value);
288
- if (ctx2.multiple) {
289
- next = next.includes(evt.value) ? remove(next, evt.value) : add(next, evt.value);
290
- } else {
291
- next = isEqual(ctx2.value, [evt.value]) ? [] : [evt.value];
292
- }
293
- set.value(ctx2, next);
294
- },
295
- focusNextToggle(ctx2) {
296
- raf(() => {
297
- if (!ctx2.focusedId)
298
- return;
299
- dom.getNextEl(ctx2, ctx2.focusedId)?.focus({ preventScroll: true });
300
- });
301
- },
302
- focusPrevToggle(ctx2) {
303
- raf(() => {
304
- if (!ctx2.focusedId)
305
- return;
306
- dom.getPrevEl(ctx2, ctx2.focusedId)?.focus({ preventScroll: true });
307
- });
308
- },
309
- focusFirstToggle(ctx2) {
310
- raf(() => {
311
- dom.getFirstEl(ctx2)?.focus({ preventScroll: true });
312
- });
313
- },
314
- focusLastToggle(ctx2) {
315
- raf(() => {
316
- dom.getLastEl(ctx2)?.focus({ preventScroll: true });
317
- });
318
- }
319
- }
320
- }
321
- );
322
- }
323
-
324
- const props = createProps()([
325
- "dir",
326
- "disabled",
327
- "getRootNode",
328
- "id",
329
- "ids",
330
- "loopFocus",
331
- "multiple",
332
- "onValueChange",
333
- "orientation",
334
- "rovingFocus",
335
- "value"
336
- ]);
337
- const splitProps = createSplitProps(props);
338
- const itemProps = createProps()(["value", "disabled"]);
339
- const splitItemProps = createSplitProps(itemProps);
340
-
341
- export { anatomy, connect, itemProps, machine, props, splitItemProps, splitProps };