@zag-js/radio-group 1.34.1 → 1.35.1

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