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