@destyler/switch 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.cjs DELETED
@@ -1,286 +0,0 @@
1
- 'use strict';
2
-
3
- const anatomy$1 = require('@zag-js/anatomy');
4
- const domQuery = require('@zag-js/dom-query');
5
- const focusVisible = require('@zag-js/focus-visible');
6
- const core = require('@zag-js/core');
7
- const domEvent = require('@zag-js/dom-event');
8
- const formUtils = require('@zag-js/form-utils');
9
- const utils = require('@zag-js/utils');
10
- const types = require('@zag-js/types');
11
-
12
- const anatomy = anatomy$1.createAnatomy("switch").parts("root", "label", "control", "thumb");
13
- const parts = anatomy.build();
14
-
15
- const dom = domQuery.createScope({
16
- getRootId: (ctx) => ctx.ids?.root ?? `switch:${ctx.id}`,
17
- getLabelId: (ctx) => ctx.ids?.label ?? `switch:${ctx.id}:label`,
18
- getThumbId: (ctx) => ctx.ids?.thumb ?? `switch:${ctx.id}:thumb`,
19
- getControlId: (ctx) => ctx.ids?.control ?? `switch:${ctx.id}:control`,
20
- getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `switch:${ctx.id}:input`,
21
- getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
22
- getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
23
- });
24
-
25
- function connect(state, send, normalize) {
26
- const disabled = state.context.isDisabled;
27
- const readOnly = state.context.readOnly;
28
- const checked = state.context.checked;
29
- const focused = !disabled && state.context.focused;
30
- const focusVisible$1 = !disabled && state.context.focusVisible;
31
- const dataAttrs = {
32
- "data-active": domQuery.dataAttr(state.context.active),
33
- "data-focus": domQuery.dataAttr(focused),
34
- "data-focus-visible": domQuery.dataAttr(focusVisible$1),
35
- "data-readonly": domQuery.dataAttr(readOnly),
36
- "data-hover": domQuery.dataAttr(state.context.hovered),
37
- "data-disabled": domQuery.dataAttr(disabled),
38
- "data-state": state.context.checked ? "checked" : "unchecked",
39
- "data-invalid": domQuery.dataAttr(state.context.invalid)
40
- };
41
- return {
42
- checked,
43
- disabled,
44
- focused,
45
- setChecked(checked2) {
46
- send({ type: "CHECKED.SET", checked: checked2, isTrusted: false });
47
- },
48
- toggleChecked() {
49
- send({ type: "CHECKED.TOGGLE", checked, isTrusted: false });
50
- },
51
- getRootProps() {
52
- return normalize.label({
53
- ...parts.root.attrs,
54
- ...dataAttrs,
55
- dir: state.context.dir,
56
- id: dom.getRootId(state.context),
57
- htmlFor: dom.getHiddenInputId(state.context),
58
- onPointerMove() {
59
- if (disabled)
60
- return;
61
- send({ type: "CONTEXT.SET", context: { hovered: true } });
62
- },
63
- onPointerLeave() {
64
- if (disabled)
65
- return;
66
- send({ type: "CONTEXT.SET", context: { hovered: false } });
67
- },
68
- onClick(event) {
69
- if (disabled)
70
- return;
71
- const target = domQuery.getEventTarget(event);
72
- if (target === dom.getHiddenInputEl(state.context)) {
73
- event.stopPropagation();
74
- }
75
- }
76
- });
77
- },
78
- getLabelProps() {
79
- return normalize.element({
80
- ...parts.label.attrs,
81
- ...dataAttrs,
82
- dir: state.context.dir,
83
- id: dom.getLabelId(state.context)
84
- });
85
- },
86
- getThumbProps() {
87
- return normalize.element({
88
- ...parts.thumb.attrs,
89
- ...dataAttrs,
90
- "dir": state.context.dir,
91
- "id": dom.getThumbId(state.context),
92
- "aria-hidden": true
93
- });
94
- },
95
- getControlProps() {
96
- return normalize.element({
97
- ...parts.control.attrs,
98
- ...dataAttrs,
99
- "dir": state.context.dir,
100
- "id": dom.getControlId(state.context),
101
- "aria-hidden": true
102
- });
103
- },
104
- getHiddenInputProps() {
105
- return normalize.input({
106
- "id": dom.getHiddenInputId(state.context),
107
- "type": "checkbox",
108
- "required": state.context.required,
109
- "defaultChecked": checked,
110
- "disabled": disabled,
111
- "aria-labelledby": dom.getLabelId(state.context),
112
- "aria-invalid": state.context.invalid,
113
- "name": state.context.name,
114
- "form": state.context.form,
115
- "value": state.context.value,
116
- "style": domQuery.visuallyHiddenStyle,
117
- onFocus() {
118
- const focusVisible2 = focusVisible.isFocusVisible();
119
- send({ type: "CONTEXT.SET", context: { focused: true, focusVisible: focusVisible2 } });
120
- },
121
- onBlur() {
122
- send({ type: "CONTEXT.SET", context: { focused: false, focusVisible: false } });
123
- },
124
- onClick(event) {
125
- if (readOnly) {
126
- event.preventDefault();
127
- return;
128
- }
129
- const checked2 = event.currentTarget.checked;
130
- send({ type: "CHECKED.SET", checked: checked2, isTrusted: true });
131
- }
132
- });
133
- }
134
- };
135
- }
136
-
137
- const { not } = core.guards;
138
- const invoke = {
139
- change: (ctx) => {
140
- ctx.onCheckedChange?.({ checked: ctx.checked });
141
- }
142
- };
143
- const set = {
144
- checked: (ctx, checked) => {
145
- if (utils.isEqual(ctx.checked, checked))
146
- return;
147
- ctx.checked = checked;
148
- invoke.change(ctx);
149
- }
150
- };
151
- function machine(userContext) {
152
- const ctx = utils.compact(userContext);
153
- return core.createMachine(
154
- {
155
- id: "switch",
156
- initial: "ready",
157
- context: {
158
- checked: false,
159
- label: "switch",
160
- value: "on",
161
- disabled: false,
162
- ...ctx,
163
- fieldsetDisabled: false,
164
- focusVisible: false
165
- },
166
- computed: {
167
- isDisabled: (ctx2) => ctx2.disabled || ctx2.fieldsetDisabled
168
- },
169
- watch: {
170
- disabled: "removeFocusIfNeeded",
171
- checked: "syncInputElement"
172
- },
173
- activities: ["trackFormControlState", "trackPressEvent", "trackFocusVisible"],
174
- on: {
175
- "CHECKED.TOGGLE": [
176
- {
177
- guard: not("isTrusted"),
178
- actions: ["toggleChecked", "dispatchChangeEvent"]
179
- },
180
- {
181
- actions: ["toggleChecked"]
182
- }
183
- ],
184
- "CHECKED.SET": [
185
- {
186
- guard: not("isTrusted"),
187
- actions: ["setChecked", "dispatchChangeEvent"]
188
- },
189
- {
190
- actions: ["setChecked"]
191
- }
192
- ],
193
- "CONTEXT.SET": {
194
- actions: ["setContext"]
195
- }
196
- },
197
- states: {
198
- ready: {}
199
- }
200
- },
201
- {
202
- guards: {
203
- isTrusted: (_ctx, evt) => !!evt.isTrusted
204
- },
205
- activities: {
206
- trackPressEvent(ctx2) {
207
- if (ctx2.isDisabled)
208
- return;
209
- return domEvent.trackPress({
210
- pointerNode: dom.getRootEl(ctx2),
211
- keyboardNode: dom.getHiddenInputEl(ctx2),
212
- isValidKey: (event) => event.key === " ",
213
- onPress: () => ctx2.active = false,
214
- onPressStart: () => ctx2.active = true,
215
- onPressEnd: () => ctx2.active = false
216
- });
217
- },
218
- trackFocusVisible(ctx2) {
219
- if (ctx2.isDisabled)
220
- return;
221
- return focusVisible.trackFocusVisible({ root: dom.getRootNode(ctx2) });
222
- },
223
- trackFormControlState(ctx2, _evt, { send, initialContext }) {
224
- return formUtils.trackFormControl(dom.getHiddenInputEl(ctx2), {
225
- onFieldsetDisabledChange(disabled) {
226
- ctx2.fieldsetDisabled = disabled;
227
- },
228
- onFormReset() {
229
- send({ type: "CHECKED.SET", checked: !!initialContext.checked, src: "form-reset" });
230
- }
231
- });
232
- }
233
- },
234
- actions: {
235
- setContext(ctx2, evt) {
236
- Object.assign(ctx2, evt.context);
237
- },
238
- syncInputElement(ctx2) {
239
- const inputEl = dom.getHiddenInputEl(ctx2);
240
- if (!inputEl)
241
- return;
242
- inputEl.checked = !!ctx2.checked;
243
- },
244
- removeFocusIfNeeded(ctx2) {
245
- if (ctx2.disabled && ctx2.focused) {
246
- ctx2.focused = false;
247
- }
248
- },
249
- setChecked(ctx2, evt) {
250
- set.checked(ctx2, evt.checked);
251
- },
252
- toggleChecked(ctx2, _evt) {
253
- set.checked(ctx2, !ctx2.checked);
254
- },
255
- dispatchChangeEvent(ctx2) {
256
- const inputEl = dom.getHiddenInputEl(ctx2);
257
- formUtils.dispatchInputCheckedEvent(inputEl, { checked: ctx2.checked });
258
- }
259
- }
260
- }
261
- );
262
- }
263
-
264
- const props = types.createProps()([
265
- "checked",
266
- "dir",
267
- "disabled",
268
- "form",
269
- "getRootNode",
270
- "id",
271
- "ids",
272
- "invalid",
273
- "label",
274
- "name",
275
- "onCheckedChange",
276
- "readOnly",
277
- "required",
278
- "value"
279
- ]);
280
- const splitProps = utils.createSplitProps(props);
281
-
282
- exports.anatomy = anatomy;
283
- exports.connect = connect;
284
- exports.machine = machine;
285
- exports.props = props;
286
- exports.splitProps = splitProps;
package/dist/index.d.cts DELETED
@@ -1,143 +0,0 @@
1
- import * as _zag_js_anatomy from '@zag-js/anatomy';
2
- import { DirectionProperty, CommonProperties, PropTypes, RequiredBy, NormalizeProps } from '@zag-js/types';
3
- import * as _zag_js_core from '@zag-js/core';
4
- import { StateMachine, Machine } from '@zag-js/core';
5
-
6
- declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "label" | "control" | "thumb">;
7
-
8
- interface CheckedChangeDetails {
9
- checked: boolean;
10
- }
11
- type ElementIds = Partial<{
12
- root: string;
13
- hiddenInput: string;
14
- control: string;
15
- label: string;
16
- thumb: string;
17
- }>;
18
- interface PublicContext extends DirectionProperty, CommonProperties {
19
- /**
20
- * The ids of the elements in the switch. Useful for composition.
21
- */
22
- ids?: ElementIds | undefined;
23
- /**
24
- * Specifies the localized strings that identifies the accessibility elements and their states
25
- */
26
- label: string;
27
- /**
28
- * Whether the switch is disabled.
29
- */
30
- disabled?: boolean | undefined;
31
- /**
32
- * If `true`, the switch is marked as invalid.
33
- */
34
- invalid?: boolean | undefined;
35
- /**
36
- * If `true`, the switch input is marked as required,
37
- */
38
- required?: boolean | undefined;
39
- /**
40
- * Whether the switch is read-only
41
- */
42
- readOnly?: boolean | undefined;
43
- /**
44
- * Function to call when the switch is clicked.
45
- */
46
- onCheckedChange?: ((details: CheckedChangeDetails) => void) | undefined;
47
- /**
48
- * Whether the switch is checked.
49
- */
50
- checked: boolean;
51
- /**
52
- * The name of the input field in a switch
53
- * (Useful for form submission).
54
- */
55
- name?: string | undefined;
56
- /**
57
- * The id of the form that the switch belongs to
58
- */
59
- form?: string | undefined;
60
- /**
61
- * The value of switch input. Useful for form submission.
62
- * @default "on"
63
- */
64
- value?: string | number | undefined;
65
- }
66
- type UserDefinedContext = RequiredBy<PublicContext, 'id'>;
67
- type ComputedContext = Readonly<{
68
- /**
69
- * Whether the switch is disabled
70
- */
71
- isDisabled: boolean;
72
- }>;
73
- interface PrivateContext {
74
- /**
75
- * @internal
76
- * Whether the checkbox is pressed
77
- */
78
- active?: boolean | undefined;
79
- /**
80
- * @internal
81
- * Whether the checkbox has focus
82
- */
83
- focused?: boolean | undefined;
84
- /**
85
- * @internal
86
- * Whether the checkbox has focus visible
87
- */
88
- focusVisible?: boolean | undefined;
89
- /**
90
- * @internal
91
- * Whether the checkbox is hovered
92
- */
93
- hovered?: boolean | undefined;
94
- /**
95
- * @internal
96
- * Whether the checkbox fieldset is disabled
97
- */
98
- fieldsetDisabled: boolean;
99
- }
100
- interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
101
- }
102
- interface MachineState {
103
- value: 'ready';
104
- }
105
- type State = StateMachine.State<MachineContext, MachineState>;
106
- type Send = StateMachine.Send<StateMachine.AnyEventObject>;
107
- type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
108
- interface MachineApi<T extends PropTypes = PropTypes> {
109
- /**
110
- * Whether the checkbox is checked
111
- */
112
- checked: boolean;
113
- /**
114
- * Whether the checkbox is disabled
115
- */
116
- disabled: boolean | undefined;
117
- /**
118
- * Whether the checkbox is focused
119
- */
120
- focused: boolean | undefined;
121
- /**
122
- * Function to set the checked state of the switch.
123
- */
124
- setChecked: (checked: boolean) => void;
125
- /**
126
- * Function to toggle the checked state of the checkbox
127
- */
128
- toggleChecked: () => void;
129
- getRootProps: () => T['label'];
130
- getLabelProps: () => T['element'];
131
- getThumbProps: () => T['element'];
132
- getControlProps: () => T['element'];
133
- getHiddenInputProps: () => T['input'];
134
- }
135
-
136
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
137
-
138
- declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
139
-
140
- declare const props: ("label" | "ids" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "name" | "form" | "value" | "id" | "dir" | "getRootNode")[];
141
- declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "label" | "ids" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "name" | "form" | "value" | "id" | "dir" | "getRootNode">];
142
-
143
- export { type MachineApi as Api, type CheckedChangeDetails, type UserDefinedContext as Context, type ElementIds, type Service, anatomy, connect, machine, props, splitProps };
package/dist/index.d.mts DELETED
@@ -1,143 +0,0 @@
1
- import * as _zag_js_anatomy from '@zag-js/anatomy';
2
- import { DirectionProperty, CommonProperties, PropTypes, RequiredBy, NormalizeProps } from '@zag-js/types';
3
- import * as _zag_js_core from '@zag-js/core';
4
- import { StateMachine, Machine } from '@zag-js/core';
5
-
6
- declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "label" | "control" | "thumb">;
7
-
8
- interface CheckedChangeDetails {
9
- checked: boolean;
10
- }
11
- type ElementIds = Partial<{
12
- root: string;
13
- hiddenInput: string;
14
- control: string;
15
- label: string;
16
- thumb: string;
17
- }>;
18
- interface PublicContext extends DirectionProperty, CommonProperties {
19
- /**
20
- * The ids of the elements in the switch. Useful for composition.
21
- */
22
- ids?: ElementIds | undefined;
23
- /**
24
- * Specifies the localized strings that identifies the accessibility elements and their states
25
- */
26
- label: string;
27
- /**
28
- * Whether the switch is disabled.
29
- */
30
- disabled?: boolean | undefined;
31
- /**
32
- * If `true`, the switch is marked as invalid.
33
- */
34
- invalid?: boolean | undefined;
35
- /**
36
- * If `true`, the switch input is marked as required,
37
- */
38
- required?: boolean | undefined;
39
- /**
40
- * Whether the switch is read-only
41
- */
42
- readOnly?: boolean | undefined;
43
- /**
44
- * Function to call when the switch is clicked.
45
- */
46
- onCheckedChange?: ((details: CheckedChangeDetails) => void) | undefined;
47
- /**
48
- * Whether the switch is checked.
49
- */
50
- checked: boolean;
51
- /**
52
- * The name of the input field in a switch
53
- * (Useful for form submission).
54
- */
55
- name?: string | undefined;
56
- /**
57
- * The id of the form that the switch belongs to
58
- */
59
- form?: string | undefined;
60
- /**
61
- * The value of switch input. Useful for form submission.
62
- * @default "on"
63
- */
64
- value?: string | number | undefined;
65
- }
66
- type UserDefinedContext = RequiredBy<PublicContext, 'id'>;
67
- type ComputedContext = Readonly<{
68
- /**
69
- * Whether the switch is disabled
70
- */
71
- isDisabled: boolean;
72
- }>;
73
- interface PrivateContext {
74
- /**
75
- * @internal
76
- * Whether the checkbox is pressed
77
- */
78
- active?: boolean | undefined;
79
- /**
80
- * @internal
81
- * Whether the checkbox has focus
82
- */
83
- focused?: boolean | undefined;
84
- /**
85
- * @internal
86
- * Whether the checkbox has focus visible
87
- */
88
- focusVisible?: boolean | undefined;
89
- /**
90
- * @internal
91
- * Whether the checkbox is hovered
92
- */
93
- hovered?: boolean | undefined;
94
- /**
95
- * @internal
96
- * Whether the checkbox fieldset is disabled
97
- */
98
- fieldsetDisabled: boolean;
99
- }
100
- interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
101
- }
102
- interface MachineState {
103
- value: 'ready';
104
- }
105
- type State = StateMachine.State<MachineContext, MachineState>;
106
- type Send = StateMachine.Send<StateMachine.AnyEventObject>;
107
- type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
108
- interface MachineApi<T extends PropTypes = PropTypes> {
109
- /**
110
- * Whether the checkbox is checked
111
- */
112
- checked: boolean;
113
- /**
114
- * Whether the checkbox is disabled
115
- */
116
- disabled: boolean | undefined;
117
- /**
118
- * Whether the checkbox is focused
119
- */
120
- focused: boolean | undefined;
121
- /**
122
- * Function to set the checked state of the switch.
123
- */
124
- setChecked: (checked: boolean) => void;
125
- /**
126
- * Function to toggle the checked state of the checkbox
127
- */
128
- toggleChecked: () => void;
129
- getRootProps: () => T['label'];
130
- getLabelProps: () => T['element'];
131
- getThumbProps: () => T['element'];
132
- getControlProps: () => T['element'];
133
- getHiddenInputProps: () => T['input'];
134
- }
135
-
136
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
137
-
138
- declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
139
-
140
- declare const props: ("label" | "ids" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "name" | "form" | "value" | "id" | "dir" | "getRootNode")[];
141
- declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "label" | "ids" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "name" | "form" | "value" | "id" | "dir" | "getRootNode">];
142
-
143
- export { type MachineApi as Api, type CheckedChangeDetails, type UserDefinedContext as Context, type ElementIds, type Service, anatomy, connect, machine, props, splitProps };