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