@zag-js/radio-group 1.34.1 → 1.35.0

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.js CHANGED
@@ -1,434 +1,39 @@
1
- 'use strict';
2
-
3
- var anatomy$1 = require('@zag-js/anatomy');
4
- var domQuery = require('@zag-js/dom-query');
5
- var focusVisible = require('@zag-js/focus-visible');
6
- var utils = require('@zag-js/utils');
7
- var core = require('@zag-js/core');
8
- var types = require('@zag-js/types');
9
-
10
- // src/radio-group.anatomy.ts
11
- var anatomy = anatomy$1.createAnatomy("radio-group").parts(
12
- "root",
13
- "label",
14
- "item",
15
- "itemText",
16
- "itemControl",
17
- "indicator"
18
- );
19
- var parts = anatomy.build();
20
- var getRootId = (ctx) => ctx.ids?.root ?? `radio-group:${ctx.id}`;
21
- var getLabelId = (ctx) => ctx.ids?.label ?? `radio-group:${ctx.id}:label`;
22
- var getItemId = (ctx, value) => ctx.ids?.item?.(value) ?? `radio-group:${ctx.id}:radio:${value}`;
23
- var getItemHiddenInputId = (ctx, value) => ctx.ids?.itemHiddenInput?.(value) ?? `radio-group:${ctx.id}:radio:input:${value}`;
24
- var getItemControlId = (ctx, value) => ctx.ids?.itemControl?.(value) ?? `radio-group:${ctx.id}:radio:control:${value}`;
25
- var getItemLabelId = (ctx, value) => ctx.ids?.itemLabel?.(value) ?? `radio-group:${ctx.id}:radio:label:${value}`;
26
- var getIndicatorId = (ctx) => ctx.ids?.indicator ?? `radio-group:${ctx.id}:indicator`;
27
- var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
28
- var getItemHiddenInputEl = (ctx, value) => ctx.getById(getItemHiddenInputId(ctx, value));
29
- var getIndicatorEl = (ctx) => ctx.getById(getIndicatorId(ctx));
30
- var getFirstEnabledInputEl = (ctx) => getRootEl(ctx)?.querySelector("input:not(:disabled)");
31
- var getFirstEnabledAndCheckedInputEl = (ctx) => getRootEl(ctx)?.querySelector("input:not(:disabled):checked");
32
- var getInputEls = (ctx) => {
33
- const ownerId = CSS.escape(getRootId(ctx));
34
- const selector = `input[type=radio][data-ownedby='${ownerId}']:not([disabled])`;
35
- return domQuery.queryAll(getRootEl(ctx), selector);
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
36
9
  };
37
- var getRadioEl = (ctx, value) => {
38
- if (!value) return;
39
- return ctx.getById(getItemId(ctx, value));
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
40
17
  };
41
- var getOffsetRect = (el) => ({
42
- x: el?.offsetLeft ?? 0,
43
- y: el?.offsetTop ?? 0,
44
- width: el?.offsetWidth ?? 0,
45
- height: el?.offsetHeight ?? 0
46
- });
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
47
20
 
48
- // src/radio-group.connect.ts
49
- function connect(service, normalize) {
50
- const { context, send, computed, prop, scope } = service;
51
- const groupDisabled = computed("isDisabled");
52
- const groupInvalid = prop("invalid");
53
- const readOnly = prop("readOnly");
54
- function getItemState(props2) {
55
- return {
56
- value: props2.value,
57
- invalid: !!props2.invalid || !!groupInvalid,
58
- disabled: !!props2.disabled || groupDisabled,
59
- checked: context.get("value") === props2.value,
60
- focused: context.get("focusedValue") === props2.value,
61
- focusVisible: context.get("focusVisibleValue") === props2.value,
62
- hovered: context.get("hoveredValue") === props2.value,
63
- active: context.get("activeValue") === props2.value
64
- };
65
- }
66
- function getItemDataAttrs(props2) {
67
- const itemState = getItemState(props2);
68
- return {
69
- "data-focus": domQuery.dataAttr(itemState.focused),
70
- "data-focus-visible": domQuery.dataAttr(itemState.focusVisible),
71
- "data-disabled": domQuery.dataAttr(itemState.disabled),
72
- "data-readonly": domQuery.dataAttr(readOnly),
73
- "data-state": itemState.checked ? "checked" : "unchecked",
74
- "data-hover": domQuery.dataAttr(itemState.hovered),
75
- "data-invalid": domQuery.dataAttr(itemState.invalid),
76
- "data-orientation": prop("orientation"),
77
- "data-ssr": domQuery.dataAttr(context.get("ssr"))
78
- };
79
- }
80
- const focus = () => {
81
- const nodeToFocus = getFirstEnabledAndCheckedInputEl(scope) ?? getFirstEnabledInputEl(scope);
82
- nodeToFocus?.focus();
83
- };
84
- return {
85
- focus,
86
- value: context.get("value"),
87
- setValue(value) {
88
- send({ type: "SET_VALUE", value, isTrusted: false });
89
- },
90
- clearValue() {
91
- send({ type: "SET_VALUE", value: null, isTrusted: false });
92
- },
93
- getRootProps() {
94
- return normalize.element({
95
- ...parts.root.attrs,
96
- role: "radiogroup",
97
- id: getRootId(scope),
98
- "aria-labelledby": getLabelId(scope),
99
- "aria-required": prop("required") || void 0,
100
- "aria-disabled": groupDisabled || void 0,
101
- "aria-readonly": readOnly || void 0,
102
- "data-orientation": prop("orientation"),
103
- "data-disabled": domQuery.dataAttr(groupDisabled),
104
- "data-invalid": domQuery.dataAttr(groupInvalid),
105
- "data-required": domQuery.dataAttr(prop("required")),
106
- "aria-orientation": prop("orientation"),
107
- dir: prop("dir"),
108
- style: {
109
- position: "relative"
110
- }
111
- });
112
- },
113
- getLabelProps() {
114
- return normalize.element({
115
- ...parts.label.attrs,
116
- dir: prop("dir"),
117
- "data-orientation": prop("orientation"),
118
- "data-disabled": domQuery.dataAttr(groupDisabled),
119
- "data-invalid": domQuery.dataAttr(groupInvalid),
120
- "data-required": domQuery.dataAttr(prop("required")),
121
- id: getLabelId(scope),
122
- onClick: focus
123
- });
124
- },
125
- getItemState,
126
- getItemProps(props2) {
127
- const itemState = getItemState(props2);
128
- return normalize.label({
129
- ...parts.item.attrs,
130
- dir: prop("dir"),
131
- id: getItemId(scope, props2.value),
132
- htmlFor: getItemHiddenInputId(scope, props2.value),
133
- ...getItemDataAttrs(props2),
134
- onPointerMove() {
135
- if (itemState.disabled) return;
136
- if (itemState.hovered) return;
137
- send({ type: "SET_HOVERED", value: props2.value, hovered: true });
138
- },
139
- onPointerLeave() {
140
- if (itemState.disabled) return;
141
- send({ type: "SET_HOVERED", value: null });
142
- },
143
- onPointerDown(event) {
144
- if (itemState.disabled) return;
145
- if (!domQuery.isLeftClick(event)) return;
146
- if (itemState.focused && event.pointerType === "mouse") {
147
- event.preventDefault();
148
- }
149
- send({ type: "SET_ACTIVE", value: props2.value, active: true });
150
- },
151
- onPointerUp() {
152
- if (itemState.disabled) return;
153
- send({ type: "SET_ACTIVE", value: null });
154
- },
155
- onClick() {
156
- if (!itemState.disabled && domQuery.isSafari()) {
157
- getItemHiddenInputEl(scope, props2.value)?.focus();
158
- }
159
- }
160
- });
161
- },
162
- getItemTextProps(props2) {
163
- return normalize.element({
164
- ...parts.itemText.attrs,
165
- dir: prop("dir"),
166
- id: getItemLabelId(scope, props2.value),
167
- ...getItemDataAttrs(props2)
168
- });
169
- },
170
- getItemControlProps(props2) {
171
- const itemState = getItemState(props2);
172
- return normalize.element({
173
- ...parts.itemControl.attrs,
174
- dir: prop("dir"),
175
- id: getItemControlId(scope, props2.value),
176
- "data-active": domQuery.dataAttr(itemState.active),
177
- "aria-hidden": true,
178
- ...getItemDataAttrs(props2)
179
- });
180
- },
181
- getItemHiddenInputProps(props2) {
182
- const itemState = getItemState(props2);
183
- return normalize.input({
184
- "data-ownedby": getRootId(scope),
185
- id: getItemHiddenInputId(scope, props2.value),
186
- type: "radio",
187
- name: prop("name") || prop("id"),
188
- form: prop("form"),
189
- value: props2.value,
190
- required: prop("required"),
191
- "aria-invalid": itemState.invalid || void 0,
192
- onClick(event) {
193
- if (readOnly) {
194
- event.preventDefault();
195
- return;
196
- }
197
- if (event.currentTarget.checked) {
198
- send({ type: "SET_VALUE", value: props2.value, isTrusted: true });
199
- }
200
- },
201
- onBlur() {
202
- send({ type: "SET_FOCUSED", value: null, focused: false, focusVisible: false });
203
- },
204
- onFocus() {
205
- const focusVisible$1 = focusVisible.isFocusVisible();
206
- send({ type: "SET_FOCUSED", value: props2.value, focused: true, focusVisible: focusVisible$1 });
207
- },
208
- onKeyDown(event) {
209
- if (event.defaultPrevented) return;
210
- if (event.key === " ") {
211
- send({ type: "SET_ACTIVE", value: props2.value, active: true });
212
- }
213
- },
214
- onKeyUp(event) {
215
- if (event.defaultPrevented) return;
216
- if (event.key === " ") {
217
- send({ type: "SET_ACTIVE", value: null });
218
- }
219
- },
220
- disabled: itemState.disabled || readOnly,
221
- defaultChecked: itemState.checked,
222
- style: domQuery.visuallyHiddenStyle
223
- });
224
- },
225
- getIndicatorProps() {
226
- const rect = context.get("indicatorRect");
227
- const rectIsEmpty = rect == null || rect.width === 0 && rect.height === 0 && rect.x === 0 && rect.y === 0;
228
- return normalize.element({
229
- id: getIndicatorId(scope),
230
- ...parts.indicator.attrs,
231
- dir: prop("dir"),
232
- hidden: context.get("value") == null || rectIsEmpty,
233
- "data-disabled": domQuery.dataAttr(groupDisabled),
234
- "data-orientation": prop("orientation"),
235
- style: {
236
- "--transition-property": "left, top, width, height",
237
- "--left": utils.toPx(rect?.x),
238
- "--top": utils.toPx(rect?.y),
239
- "--width": utils.toPx(rect?.width),
240
- "--height": utils.toPx(rect?.height),
241
- position: "absolute",
242
- willChange: "var(--transition-property)",
243
- transitionProperty: "var(--transition-property)",
244
- transitionDuration: "var(--transition-duration, 150ms)",
245
- transitionTimingFunction: "var(--transition-timing-function)",
246
- [prop("orientation") === "horizontal" ? "left" : "top"]: prop("orientation") === "horizontal" ? "var(--left)" : "var(--top)"
247
- }
248
- });
249
- }
250
- };
251
- }
252
- var { not } = core.createGuards();
253
- var machine = core.createMachine({
254
- props({ props: props2 }) {
255
- return {
256
- orientation: "vertical",
257
- ...props2
258
- };
259
- },
260
- initialState() {
261
- return "idle";
262
- },
263
- context({ prop, bindable }) {
264
- return {
265
- value: bindable(() => ({
266
- defaultValue: prop("defaultValue"),
267
- value: prop("value"),
268
- onChange(value) {
269
- prop("onValueChange")?.({ value });
270
- }
271
- })),
272
- activeValue: bindable(() => ({
273
- defaultValue: null
274
- })),
275
- focusedValue: bindable(() => ({
276
- defaultValue: null
277
- })),
278
- focusVisibleValue: bindable(() => ({
279
- defaultValue: null
280
- })),
281
- hoveredValue: bindable(() => ({
282
- defaultValue: null
283
- })),
284
- indicatorRect: bindable(() => ({
285
- defaultValue: null
286
- })),
287
- fieldsetDisabled: bindable(() => ({
288
- defaultValue: false
289
- })),
290
- ssr: bindable(() => ({
291
- defaultValue: true
292
- }))
293
- };
294
- },
295
- refs() {
296
- return {
297
- indicatorCleanup: null,
298
- focusVisibleValue: null
299
- };
300
- },
301
- computed: {
302
- isDisabled: ({ prop, context }) => !!prop("disabled") || context.get("fieldsetDisabled")
303
- },
304
- entry: ["syncIndicatorRect", "syncSsr"],
305
- exit: ["cleanupObserver"],
306
- effects: ["trackFormControlState", "trackFocusVisible"],
307
- watch({ track, action, context }) {
308
- track([() => context.get("value")], () => {
309
- action(["syncIndicatorRect", "syncInputElements"]);
310
- });
311
- },
312
- on: {
313
- SET_VALUE: [
314
- {
315
- guard: not("isTrusted"),
316
- actions: ["setValue", "dispatchChangeEvent"]
317
- },
318
- {
319
- actions: ["setValue"]
320
- }
321
- ],
322
- SET_HOVERED: {
323
- actions: ["setHovered"]
324
- },
325
- SET_ACTIVE: {
326
- actions: ["setActive"]
327
- },
328
- SET_FOCUSED: {
329
- actions: ["setFocused"]
330
- }
331
- },
332
- states: {
333
- idle: {}
334
- },
335
- implementations: {
336
- guards: {
337
- isTrusted: ({ event }) => !!event.isTrusted
338
- },
339
- effects: {
340
- trackFormControlState({ context, scope }) {
341
- return domQuery.trackFormControl(getRootEl(scope), {
342
- onFieldsetDisabledChange(disabled) {
343
- context.set("fieldsetDisabled", disabled);
344
- },
345
- onFormReset() {
346
- context.set("value", context.initial("value"));
347
- }
348
- });
349
- },
350
- trackFocusVisible({ scope }) {
351
- return focusVisible.trackFocusVisible({ root: scope.getRootNode?.() });
352
- }
353
- },
354
- actions: {
355
- setValue({ context, event }) {
356
- context.set("value", event.value);
357
- },
358
- setHovered({ context, event }) {
359
- context.set("hoveredValue", event.value);
360
- },
361
- setActive({ context, event }) {
362
- context.set("activeValue", event.value);
363
- },
364
- setFocused({ context, event }) {
365
- context.set("focusedValue", event.value);
366
- const focusVisibleValue = event.value != null && event.focusVisible ? event.value : null;
367
- context.set("focusVisibleValue", focusVisibleValue);
368
- },
369
- syncInputElements({ context, scope }) {
370
- const inputs = getInputEls(scope);
371
- inputs.forEach((input) => {
372
- input.checked = input.value === context.get("value");
373
- });
374
- },
375
- cleanupObserver({ refs }) {
376
- refs.get("indicatorCleanup")?.();
377
- },
378
- syncSsr({ context }) {
379
- context.set("ssr", false);
380
- },
381
- syncIndicatorRect({ context, scope, refs }) {
382
- refs.get("indicatorCleanup")?.();
383
- if (!getIndicatorEl(scope)) return;
384
- const value = context.get("value");
385
- const radioEl = getRadioEl(scope, value);
386
- if (value == null || !radioEl) {
387
- context.set("indicatorRect", null);
388
- return;
389
- }
390
- const exec = () => {
391
- context.set("indicatorRect", getOffsetRect(radioEl));
392
- };
393
- exec();
394
- const indicatorCleanup = domQuery.resizeObserverBorderBox.observe(radioEl, exec);
395
- refs.set("indicatorCleanup", indicatorCleanup);
396
- },
397
- dispatchChangeEvent({ context, scope }) {
398
- const inputEls = getInputEls(scope);
399
- inputEls.forEach((inputEl) => {
400
- const checked = inputEl.value === context.get("value");
401
- if (checked === inputEl.checked) return;
402
- domQuery.dispatchInputCheckedEvent(inputEl, { checked });
403
- });
404
- }
405
- }
406
- }
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ anatomy: () => import_radio_group.anatomy,
25
+ connect: () => import_radio_group2.connect,
26
+ machine: () => import_radio_group3.machine
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+ var import_radio_group = require("./radio-group.anatomy.cjs");
30
+ var import_radio_group2 = require("./radio-group.connect.cjs");
31
+ var import_radio_group3 = require("./radio-group.machine.cjs");
32
+ __reExport(index_exports, require("./radio-group.props.cjs"), module.exports);
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ anatomy,
36
+ connect,
37
+ machine,
38
+ ...require("./radio-group.props.cjs")
407
39
  });
408
- var props = types.createProps()([
409
- "dir",
410
- "disabled",
411
- "form",
412
- "getRootNode",
413
- "id",
414
- "ids",
415
- "invalid",
416
- "name",
417
- "onValueChange",
418
- "orientation",
419
- "readOnly",
420
- "required",
421
- "value",
422
- "defaultValue"
423
- ]);
424
- var splitProps = utils.createSplitProps(props);
425
- var itemProps = types.createProps()(["value", "disabled", "invalid"]);
426
- var splitItemProps = utils.createSplitProps(itemProps);
427
-
428
- exports.anatomy = anatomy;
429
- exports.connect = connect;
430
- exports.itemProps = itemProps;
431
- exports.machine = machine;
432
- exports.props = props;
433
- exports.splitItemProps = splitItemProps;
434
- exports.splitProps = splitProps;