@destyler/checkbox 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,288 +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, setElementChecked, 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("checkbox").parts("root", "label", "control", "indicator");
11
- const parts = anatomy.build();
12
-
13
- const dom = createScope({
14
- getRootId: (ctx) => ctx.ids?.root ?? `checkbox:${ctx.id}`,
15
- getLabelId: (ctx) => ctx.ids?.label ?? `checkbox:${ctx.id}:label`,
16
- getControlId: (ctx) => ctx.ids?.control ?? `checkbox:${ctx.id}:control`,
17
- getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `checkbox:${ctx.id}:input`,
18
- getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
19
- getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
20
- });
21
-
22
- function connect(state, send, normalize) {
23
- const disabled = state.context.isDisabled;
24
- const readOnly = state.context.readOnly;
25
- const focused = !disabled && state.context.focused;
26
- const focusVisible = !disabled && state.context.focusVisible;
27
- const checked = state.context.isChecked;
28
- const indeterminate = state.context.isIndeterminate;
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": indeterminate ? "indeterminate" : state.context.checked ? "checked" : "unchecked",
37
- "data-invalid": dataAttr(state.context.invalid)
38
- };
39
- return {
40
- checked,
41
- disabled,
42
- indeterminate,
43
- focused,
44
- checkedState: state.context.checked,
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
- 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
- getControlProps() {
85
- return normalize.element({
86
- ...parts.control.attrs,
87
- ...dataAttrs,
88
- "dir": state.context.dir,
89
- "id": dom.getControlId(state.context),
90
- "aria-hidden": true
91
- });
92
- },
93
- getIndicatorProps() {
94
- return normalize.element({
95
- ...parts.indicator.attrs,
96
- ...dataAttrs,
97
- dir: state.context.dir,
98
- hidden: !indeterminate && !state.context.checked
99
- });
100
- },
101
- getHiddenInputProps() {
102
- return normalize.input({
103
- "id": dom.getHiddenInputId(state.context),
104
- "type": "checkbox",
105
- "required": state.context.required,
106
- "defaultChecked": checked,
107
- "disabled": disabled,
108
- "aria-labelledby": dom.getLabelId(state.context),
109
- "aria-invalid": state.context.invalid,
110
- "name": state.context.name,
111
- "form": state.context.form,
112
- "value": state.context.value,
113
- "style": visuallyHiddenStyle,
114
- onFocus() {
115
- const focusVisible2 = isFocusVisible();
116
- send({ type: "CONTEXT.SET", context: { focused: true, focusVisible: focusVisible2 } });
117
- },
118
- onBlur() {
119
- send({ type: "CONTEXT.SET", context: { focused: false, focusVisible: false } });
120
- },
121
- onClick(event) {
122
- if (readOnly) {
123
- event.preventDefault();
124
- return;
125
- }
126
- const checked2 = event.currentTarget.checked;
127
- send({ type: "CHECKED.SET", checked: checked2, isTrusted: true });
128
- }
129
- });
130
- }
131
- };
132
- }
133
-
134
- const { not } = guards;
135
- function isIndeterminate(checked) {
136
- return checked === "indeterminate";
137
- }
138
- function isChecked(checked) {
139
- return isIndeterminate(checked) ? false : !!checked;
140
- }
141
- const invoke = {
142
- change: (ctx) => {
143
- ctx.onCheckedChange?.({ checked: ctx.checked });
144
- }
145
- };
146
- const set = {
147
- checked: (ctx, checked) => {
148
- if (isEqual(ctx.checked, checked))
149
- return;
150
- ctx.checked = checked;
151
- invoke.change(ctx);
152
- }
153
- };
154
- function machine(userContext) {
155
- const ctx = compact(userContext);
156
- return createMachine(
157
- {
158
- id: "checkbox",
159
- initial: "ready",
160
- context: {
161
- checked: false,
162
- value: "on",
163
- disabled: false,
164
- ...ctx,
165
- fieldsetDisabled: false,
166
- focusVisible: false
167
- },
168
- watch: {
169
- disabled: "removeFocusIfNeeded",
170
- checked: "syncInputElement"
171
- },
172
- activities: ["trackFormControlState", "trackPressEvent", "trackFocusVisible"],
173
- on: {
174
- "CHECKED.TOGGLE": [
175
- {
176
- guard: not("isTrusted"),
177
- actions: ["toggleChecked", "dispatchChangeEvent"]
178
- },
179
- {
180
- actions: ["toggleChecked"]
181
- }
182
- ],
183
- "CHECKED.SET": [
184
- {
185
- guard: not("isTrusted"),
186
- actions: ["setChecked", "dispatchChangeEvent"]
187
- },
188
- {
189
- actions: ["setChecked"]
190
- }
191
- ],
192
- "CONTEXT.SET": {
193
- actions: ["setContext"]
194
- }
195
- },
196
- computed: {
197
- isIndeterminate: (ctx2) => isIndeterminate(ctx2.checked),
198
- isChecked: (ctx2) => isChecked(ctx2.checked),
199
- isDisabled: (ctx2) => !!ctx2.disabled || ctx2.fieldsetDisabled
200
- },
201
- states: {
202
- ready: {}
203
- }
204
- },
205
- {
206
- guards: {
207
- isTrusted: (_ctx, evt) => !!evt.isTrusted
208
- },
209
- activities: {
210
- trackPressEvent(ctx2) {
211
- if (ctx2.isDisabled)
212
- return;
213
- return trackPress({
214
- pointerNode: dom.getRootEl(ctx2),
215
- keyboardNode: dom.getHiddenInputEl(ctx2),
216
- isValidKey: (event) => event.key === " ",
217
- onPress: () => ctx2.active = false,
218
- onPressStart: () => ctx2.active = true,
219
- onPressEnd: () => ctx2.active = false
220
- });
221
- },
222
- trackFocusVisible(ctx2) {
223
- if (ctx2.isDisabled)
224
- return;
225
- return trackFocusVisible({ root: dom.getRootNode(ctx2) });
226
- },
227
- trackFormControlState(ctx2, _evt, { send, initialContext }) {
228
- return trackFormControl(dom.getHiddenInputEl(ctx2), {
229
- onFieldsetDisabledChange(disabled) {
230
- ctx2.fieldsetDisabled = disabled;
231
- },
232
- onFormReset() {
233
- send({ type: "CHECKED.SET", checked: !!initialContext.checked });
234
- }
235
- });
236
- }
237
- },
238
- actions: {
239
- setContext(ctx2, evt) {
240
- Object.assign(ctx2, evt.context);
241
- },
242
- syncInputElement(ctx2) {
243
- const inputEl = dom.getHiddenInputEl(ctx2);
244
- if (!inputEl)
245
- return;
246
- setElementChecked(inputEl, ctx2.isChecked);
247
- inputEl.indeterminate = ctx2.isIndeterminate;
248
- },
249
- removeFocusIfNeeded(ctx2) {
250
- if (ctx2.disabled && ctx2.focused) {
251
- ctx2.focused = false;
252
- ctx2.focusVisible = false;
253
- }
254
- },
255
- setChecked(ctx2, evt) {
256
- set.checked(ctx2, evt.checked);
257
- },
258
- toggleChecked(ctx2) {
259
- const checked = isIndeterminate(ctx2.checked) ? true : !ctx2.checked;
260
- set.checked(ctx2, checked);
261
- },
262
- dispatchChangeEvent(ctx2) {
263
- const inputEl = dom.getHiddenInputEl(ctx2);
264
- dispatchInputCheckedEvent(inputEl, { checked: isChecked(ctx2.checked) });
265
- }
266
- }
267
- }
268
- );
269
- }
270
-
271
- const props = createProps()([
272
- "checked",
273
- "dir",
274
- "disabled",
275
- "form",
276
- "getRootNode",
277
- "id",
278
- "ids",
279
- "invalid",
280
- "name",
281
- "onCheckedChange",
282
- "readOnly",
283
- "required",
284
- "value"
285
- ]);
286
- const splitProps = createSplitProps(props);
287
-
288
- export { anatomy, connect, machine, props, splitProps };