@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.
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/radio-group.machine.ts
31
+ var radio_group_machine_exports = {};
32
+ __export(radio_group_machine_exports, {
33
+ machine: () => machine
34
+ });
35
+ module.exports = __toCommonJS(radio_group_machine_exports);
36
+ var import_core = require("@zag-js/core");
37
+ var import_dom_query = require("@zag-js/dom-query");
38
+ var import_focus_visible = require("@zag-js/focus-visible");
39
+ var dom = __toESM(require("./radio-group.dom.cjs"));
40
+ var { not } = (0, import_core.createGuards)();
41
+ var machine = (0, import_core.createMachine)({
42
+ props({ props }) {
43
+ return {
44
+ orientation: "vertical",
45
+ ...props
46
+ };
47
+ },
48
+ initialState() {
49
+ return "idle";
50
+ },
51
+ context({ prop, bindable }) {
52
+ return {
53
+ value: bindable(() => ({
54
+ defaultValue: prop("defaultValue"),
55
+ value: prop("value"),
56
+ onChange(value) {
57
+ prop("onValueChange")?.({ value });
58
+ }
59
+ })),
60
+ activeValue: bindable(() => ({
61
+ defaultValue: null
62
+ })),
63
+ focusedValue: bindable(() => ({
64
+ defaultValue: null
65
+ })),
66
+ focusVisibleValue: bindable(() => ({
67
+ defaultValue: null
68
+ })),
69
+ hoveredValue: bindable(() => ({
70
+ defaultValue: null
71
+ })),
72
+ indicatorRect: bindable(() => ({
73
+ defaultValue: null
74
+ })),
75
+ fieldsetDisabled: bindable(() => ({
76
+ defaultValue: false
77
+ })),
78
+ ssr: bindable(() => ({
79
+ defaultValue: true
80
+ }))
81
+ };
82
+ },
83
+ refs() {
84
+ return {
85
+ indicatorCleanup: null,
86
+ focusVisibleValue: null
87
+ };
88
+ },
89
+ computed: {
90
+ isDisabled: ({ prop, context }) => !!prop("disabled") || context.get("fieldsetDisabled")
91
+ },
92
+ entry: ["syncIndicatorRect", "syncSsr"],
93
+ exit: ["cleanupObserver"],
94
+ effects: ["trackFormControlState", "trackFocusVisible"],
95
+ watch({ track, action, context }) {
96
+ track([() => context.get("value")], () => {
97
+ action(["syncIndicatorRect", "syncInputElements"]);
98
+ });
99
+ },
100
+ on: {
101
+ SET_VALUE: [
102
+ {
103
+ guard: not("isTrusted"),
104
+ actions: ["setValue", "dispatchChangeEvent"]
105
+ },
106
+ {
107
+ actions: ["setValue"]
108
+ }
109
+ ],
110
+ SET_HOVERED: {
111
+ actions: ["setHovered"]
112
+ },
113
+ SET_ACTIVE: {
114
+ actions: ["setActive"]
115
+ },
116
+ SET_FOCUSED: {
117
+ actions: ["setFocused"]
118
+ }
119
+ },
120
+ states: {
121
+ idle: {}
122
+ },
123
+ implementations: {
124
+ guards: {
125
+ isTrusted: ({ event }) => !!event.isTrusted
126
+ },
127
+ effects: {
128
+ trackFormControlState({ context, scope }) {
129
+ return (0, import_dom_query.trackFormControl)(dom.getRootEl(scope), {
130
+ onFieldsetDisabledChange(disabled) {
131
+ context.set("fieldsetDisabled", disabled);
132
+ },
133
+ onFormReset() {
134
+ context.set("value", context.initial("value"));
135
+ }
136
+ });
137
+ },
138
+ trackFocusVisible({ scope }) {
139
+ return (0, import_focus_visible.trackFocusVisible)({ root: scope.getRootNode?.() });
140
+ }
141
+ },
142
+ actions: {
143
+ setValue({ context, event }) {
144
+ context.set("value", event.value);
145
+ },
146
+ setHovered({ context, event }) {
147
+ context.set("hoveredValue", event.value);
148
+ },
149
+ setActive({ context, event }) {
150
+ context.set("activeValue", event.value);
151
+ },
152
+ setFocused({ context, event }) {
153
+ context.set("focusedValue", event.value);
154
+ const focusVisibleValue = event.value != null && event.focusVisible ? event.value : null;
155
+ context.set("focusVisibleValue", focusVisibleValue);
156
+ },
157
+ syncInputElements({ context, scope }) {
158
+ const inputs = dom.getInputEls(scope);
159
+ inputs.forEach((input) => {
160
+ input.checked = input.value === context.get("value");
161
+ });
162
+ },
163
+ cleanupObserver({ refs }) {
164
+ refs.get("indicatorCleanup")?.();
165
+ },
166
+ syncSsr({ context }) {
167
+ context.set("ssr", false);
168
+ },
169
+ syncIndicatorRect({ context, scope, refs }) {
170
+ refs.get("indicatorCleanup")?.();
171
+ if (!dom.getIndicatorEl(scope)) return;
172
+ const value = context.get("value");
173
+ const radioEl = dom.getRadioEl(scope, value);
174
+ if (value == null || !radioEl) {
175
+ context.set("indicatorRect", null);
176
+ return;
177
+ }
178
+ const exec = () => {
179
+ context.set("indicatorRect", dom.getOffsetRect(radioEl));
180
+ };
181
+ exec();
182
+ const indicatorCleanup = import_dom_query.resizeObserverBorderBox.observe(radioEl, exec);
183
+ refs.set("indicatorCleanup", indicatorCleanup);
184
+ },
185
+ dispatchChangeEvent({ context, scope }) {
186
+ const inputEls = dom.getInputEls(scope);
187
+ inputEls.forEach((inputEl) => {
188
+ const checked = inputEl.value === context.get("value");
189
+ if (checked === inputEl.checked) return;
190
+ (0, import_dom_query.dispatchInputCheckedEvent)(inputEl, { checked });
191
+ });
192
+ }
193
+ }
194
+ }
195
+ });
196
+ // Annotate the CommonJS export names for ESM import in node:
197
+ 0 && (module.exports = {
198
+ machine
199
+ });
@@ -0,0 +1,164 @@
1
+ // src/radio-group.machine.ts
2
+ import { createGuards, createMachine } from "@zag-js/core";
3
+ import { dispatchInputCheckedEvent, resizeObserverBorderBox, trackFormControl } from "@zag-js/dom-query";
4
+ import { trackFocusVisible } from "@zag-js/focus-visible";
5
+ import * as dom from "./radio-group.dom.mjs";
6
+ var { not } = createGuards();
7
+ var machine = createMachine({
8
+ props({ props }) {
9
+ return {
10
+ orientation: "vertical",
11
+ ...props
12
+ };
13
+ },
14
+ initialState() {
15
+ return "idle";
16
+ },
17
+ context({ prop, bindable }) {
18
+ return {
19
+ value: bindable(() => ({
20
+ defaultValue: prop("defaultValue"),
21
+ value: prop("value"),
22
+ onChange(value) {
23
+ prop("onValueChange")?.({ value });
24
+ }
25
+ })),
26
+ activeValue: bindable(() => ({
27
+ defaultValue: null
28
+ })),
29
+ focusedValue: bindable(() => ({
30
+ defaultValue: null
31
+ })),
32
+ focusVisibleValue: bindable(() => ({
33
+ defaultValue: null
34
+ })),
35
+ hoveredValue: bindable(() => ({
36
+ defaultValue: null
37
+ })),
38
+ indicatorRect: bindable(() => ({
39
+ defaultValue: null
40
+ })),
41
+ fieldsetDisabled: bindable(() => ({
42
+ defaultValue: false
43
+ })),
44
+ ssr: bindable(() => ({
45
+ defaultValue: true
46
+ }))
47
+ };
48
+ },
49
+ refs() {
50
+ return {
51
+ indicatorCleanup: null,
52
+ focusVisibleValue: null
53
+ };
54
+ },
55
+ computed: {
56
+ isDisabled: ({ prop, context }) => !!prop("disabled") || context.get("fieldsetDisabled")
57
+ },
58
+ entry: ["syncIndicatorRect", "syncSsr"],
59
+ exit: ["cleanupObserver"],
60
+ effects: ["trackFormControlState", "trackFocusVisible"],
61
+ watch({ track, action, context }) {
62
+ track([() => context.get("value")], () => {
63
+ action(["syncIndicatorRect", "syncInputElements"]);
64
+ });
65
+ },
66
+ on: {
67
+ SET_VALUE: [
68
+ {
69
+ guard: not("isTrusted"),
70
+ actions: ["setValue", "dispatchChangeEvent"]
71
+ },
72
+ {
73
+ actions: ["setValue"]
74
+ }
75
+ ],
76
+ SET_HOVERED: {
77
+ actions: ["setHovered"]
78
+ },
79
+ SET_ACTIVE: {
80
+ actions: ["setActive"]
81
+ },
82
+ SET_FOCUSED: {
83
+ actions: ["setFocused"]
84
+ }
85
+ },
86
+ states: {
87
+ idle: {}
88
+ },
89
+ implementations: {
90
+ guards: {
91
+ isTrusted: ({ event }) => !!event.isTrusted
92
+ },
93
+ effects: {
94
+ trackFormControlState({ context, scope }) {
95
+ return trackFormControl(dom.getRootEl(scope), {
96
+ onFieldsetDisabledChange(disabled) {
97
+ context.set("fieldsetDisabled", disabled);
98
+ },
99
+ onFormReset() {
100
+ context.set("value", context.initial("value"));
101
+ }
102
+ });
103
+ },
104
+ trackFocusVisible({ scope }) {
105
+ return trackFocusVisible({ root: scope.getRootNode?.() });
106
+ }
107
+ },
108
+ actions: {
109
+ setValue({ context, event }) {
110
+ context.set("value", event.value);
111
+ },
112
+ setHovered({ context, event }) {
113
+ context.set("hoveredValue", event.value);
114
+ },
115
+ setActive({ context, event }) {
116
+ context.set("activeValue", event.value);
117
+ },
118
+ setFocused({ context, event }) {
119
+ context.set("focusedValue", event.value);
120
+ const focusVisibleValue = event.value != null && event.focusVisible ? event.value : null;
121
+ context.set("focusVisibleValue", focusVisibleValue);
122
+ },
123
+ syncInputElements({ context, scope }) {
124
+ const inputs = dom.getInputEls(scope);
125
+ inputs.forEach((input) => {
126
+ input.checked = input.value === context.get("value");
127
+ });
128
+ },
129
+ cleanupObserver({ refs }) {
130
+ refs.get("indicatorCleanup")?.();
131
+ },
132
+ syncSsr({ context }) {
133
+ context.set("ssr", false);
134
+ },
135
+ syncIndicatorRect({ context, scope, refs }) {
136
+ refs.get("indicatorCleanup")?.();
137
+ if (!dom.getIndicatorEl(scope)) return;
138
+ const value = context.get("value");
139
+ const radioEl = dom.getRadioEl(scope, value);
140
+ if (value == null || !radioEl) {
141
+ context.set("indicatorRect", null);
142
+ return;
143
+ }
144
+ const exec = () => {
145
+ context.set("indicatorRect", dom.getOffsetRect(radioEl));
146
+ };
147
+ exec();
148
+ const indicatorCleanup = resizeObserverBorderBox.observe(radioEl, exec);
149
+ refs.set("indicatorCleanup", indicatorCleanup);
150
+ },
151
+ dispatchChangeEvent({ context, scope }) {
152
+ const inputEls = dom.getInputEls(scope);
153
+ inputEls.forEach((inputEl) => {
154
+ const checked = inputEl.value === context.get("value");
155
+ if (checked === inputEl.checked) return;
156
+ dispatchInputCheckedEvent(inputEl, { checked });
157
+ });
158
+ }
159
+ }
160
+ }
161
+ });
162
+ export {
163
+ machine
164
+ };
@@ -0,0 +1,10 @@
1
+ import { ItemProps, RadioGroupProps } from './radio-group.types.mjs';
2
+ import '@zag-js/core';
3
+ import '@zag-js/types';
4
+
5
+ declare const props: (keyof RadioGroupProps)[];
6
+ declare const splitProps: <Props extends Partial<RadioGroupProps>>(props: Props) => [Partial<RadioGroupProps>, Omit<Props, keyof RadioGroupProps>];
7
+ declare const itemProps: (keyof ItemProps)[];
8
+ declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, keyof ItemProps>];
9
+
10
+ export { itemProps, props, splitItemProps, splitProps };
@@ -0,0 +1,10 @@
1
+ import { ItemProps, RadioGroupProps } from './radio-group.types.js';
2
+ import '@zag-js/core';
3
+ import '@zag-js/types';
4
+
5
+ declare const props: (keyof RadioGroupProps)[];
6
+ declare const splitProps: <Props extends Partial<RadioGroupProps>>(props: Props) => [Partial<RadioGroupProps>, Omit<Props, keyof RadioGroupProps>];
7
+ declare const itemProps: (keyof ItemProps)[];
8
+ declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, keyof ItemProps>];
9
+
10
+ export { itemProps, props, splitItemProps, splitProps };
@@ -0,0 +1,56 @@
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.props.ts
21
+ var radio_group_props_exports = {};
22
+ __export(radio_group_props_exports, {
23
+ itemProps: () => itemProps,
24
+ props: () => props,
25
+ splitItemProps: () => splitItemProps,
26
+ splitProps: () => splitProps
27
+ });
28
+ module.exports = __toCommonJS(radio_group_props_exports);
29
+ var import_types = require("@zag-js/types");
30
+ var import_utils = require("@zag-js/utils");
31
+ var props = (0, import_types.createProps)()([
32
+ "dir",
33
+ "disabled",
34
+ "form",
35
+ "getRootNode",
36
+ "id",
37
+ "ids",
38
+ "invalid",
39
+ "name",
40
+ "onValueChange",
41
+ "orientation",
42
+ "readOnly",
43
+ "required",
44
+ "value",
45
+ "defaultValue"
46
+ ]);
47
+ var splitProps = (0, import_utils.createSplitProps)(props);
48
+ var itemProps = (0, import_types.createProps)()(["value", "disabled", "invalid"]);
49
+ var splitItemProps = (0, import_utils.createSplitProps)(itemProps);
50
+ // Annotate the CommonJS export names for ESM import in node:
51
+ 0 && (module.exports = {
52
+ itemProps,
53
+ props,
54
+ splitItemProps,
55
+ splitProps
56
+ });
@@ -0,0 +1,28 @@
1
+ // src/radio-group.props.ts
2
+ import { createProps } from "@zag-js/types";
3
+ import { createSplitProps } from "@zag-js/utils";
4
+ var props = createProps()([
5
+ "dir",
6
+ "disabled",
7
+ "form",
8
+ "getRootNode",
9
+ "id",
10
+ "ids",
11
+ "invalid",
12
+ "name",
13
+ "onValueChange",
14
+ "orientation",
15
+ "readOnly",
16
+ "required",
17
+ "value",
18
+ "defaultValue"
19
+ ]);
20
+ var splitProps = createSplitProps(props);
21
+ var itemProps = createProps()(["value", "disabled", "invalid"]);
22
+ var splitItemProps = createSplitProps(itemProps);
23
+ export {
24
+ itemProps,
25
+ props,
26
+ splitItemProps,
27
+ splitProps
28
+ };
@@ -0,0 +1,193 @@
1
+ import { Machine, EventObject, Service } from '@zag-js/core';
2
+ import { PropTypes, RequiredBy, DirectionProperty, CommonProperties, Rect } from '@zag-js/types';
3
+
4
+ interface ValueChangeDetails {
5
+ value: string | null;
6
+ }
7
+ type ElementIds = Partial<{
8
+ root: string;
9
+ label: string;
10
+ indicator: string;
11
+ item: (value: string) => string;
12
+ itemLabel: (value: string) => string;
13
+ itemControl: (value: string) => string;
14
+ itemHiddenInput: (value: string) => string;
15
+ }>;
16
+ interface RadioGroupProps extends DirectionProperty, CommonProperties {
17
+ /**
18
+ * The ids of the elements in the radio. Useful for composition.
19
+ */
20
+ ids?: ElementIds | undefined;
21
+ /**
22
+ * The controlled value of the radio group
23
+ */
24
+ value?: string | null | undefined;
25
+ /**
26
+ * The initial value of the checked radio when rendered.
27
+ * Use when you don't need to control the value of the radio group.
28
+ */
29
+ defaultValue?: string | null | undefined;
30
+ /**
31
+ * The name of the input fields in the radio
32
+ * (Useful for form submission).
33
+ */
34
+ name?: string | undefined;
35
+ /**
36
+ * The associate form of the underlying input.
37
+ */
38
+ form?: string | undefined;
39
+ /**
40
+ * If `true`, the radio group will be disabled
41
+ */
42
+ disabled?: boolean | undefined;
43
+ /**
44
+ * If `true`, the radio group is marked as invalid.
45
+ */
46
+ invalid?: boolean | undefined;
47
+ /**
48
+ * If `true`, the radio group is marked as required.
49
+ */
50
+ required?: boolean | undefined;
51
+ /**
52
+ * Whether the radio group is read-only
53
+ */
54
+ readOnly?: boolean | undefined;
55
+ /**
56
+ * Function called once a radio is checked
57
+ */
58
+ onValueChange?: ((details: ValueChangeDetails) => void) | undefined;
59
+ /**
60
+ * Orientation of the radio group
61
+ */
62
+ orientation?: "horizontal" | "vertical" | undefined;
63
+ }
64
+ interface PrivateContext {
65
+ /**
66
+ * The value of the checked radio
67
+ */
68
+ value: string | null;
69
+ /**
70
+ * The id of the active radio
71
+ */
72
+ activeValue: string | null;
73
+ /**
74
+ * The id of the focused radio
75
+ */
76
+ focusedValue: string | null;
77
+ /**
78
+ * The id of the radio that has focus-visible
79
+ */
80
+ focusVisibleValue: string | null;
81
+ /**
82
+ * The id of the hovered radio
83
+ */
84
+ hoveredValue: string | null;
85
+ /**
86
+ * The active tab indicator's dom rect
87
+ */
88
+ indicatorRect: Rect | null;
89
+ /**
90
+ * Whether the radio group's fieldset is disabled
91
+ */
92
+ fieldsetDisabled: boolean;
93
+ /**
94
+ * Whether the radio group is in server-side rendering
95
+ */
96
+ ssr: boolean;
97
+ }
98
+ type PropsWithDefault = "orientation";
99
+ type ComputedContext = Readonly<{
100
+ /**
101
+ * Whether the radio group is disabled
102
+ */
103
+ isDisabled: boolean;
104
+ }>;
105
+ interface Refs {
106
+ /**
107
+ * Function to clean up the observer for the active tab's rect
108
+ */
109
+ indicatorCleanup: VoidFunction | null;
110
+ }
111
+ interface RadioGroupSchema {
112
+ state: "idle";
113
+ props: RequiredBy<RadioGroupProps, PropsWithDefault>;
114
+ context: PrivateContext;
115
+ computed: ComputedContext;
116
+ refs: Refs;
117
+ event: EventObject;
118
+ action: string;
119
+ effect: string;
120
+ guard: string;
121
+ }
122
+ type RadioGroupService = Service<RadioGroupSchema>;
123
+ type RadioGroupMachine = Machine<RadioGroupSchema>;
124
+ interface ItemProps {
125
+ value: string;
126
+ disabled?: boolean | undefined;
127
+ invalid?: boolean | undefined;
128
+ }
129
+ interface ItemState {
130
+ /**
131
+ * The value of the item
132
+ */
133
+ value: string;
134
+ /**
135
+ * Whether the item is invalid
136
+ */
137
+ invalid: boolean;
138
+ /**
139
+ * Whether the item is disabled
140
+ */
141
+ disabled: boolean;
142
+ /**
143
+ * Whether the item is checked
144
+ */
145
+ checked: boolean;
146
+ /**
147
+ * Whether the item is focused
148
+ */
149
+ focused: boolean;
150
+ /**
151
+ * Whether the item is focused and the focus is visible
152
+ */
153
+ focusVisible: boolean;
154
+ /**
155
+ * Whether the item is hovered
156
+ */
157
+ hovered: boolean;
158
+ /**
159
+ * Whether the item is active or pressed
160
+ */
161
+ active: boolean;
162
+ }
163
+ interface RadioGroupApi<T extends PropTypes = PropTypes> {
164
+ /**
165
+ * The current value of the radio group
166
+ */
167
+ value: string | null;
168
+ /**
169
+ * Function to set the value of the radio group
170
+ */
171
+ setValue: (value: string) => void;
172
+ /**
173
+ * Function to clear the value of the radio group
174
+ */
175
+ clearValue: VoidFunction;
176
+ /**
177
+ * Function to focus the radio group
178
+ */
179
+ focus: VoidFunction;
180
+ /**
181
+ * Returns the state details of a radio input
182
+ */
183
+ getItemState: (props: ItemProps) => ItemState;
184
+ getRootProps: () => T["element"];
185
+ getLabelProps: () => T["element"];
186
+ getItemProps: (props: ItemProps) => T["label"];
187
+ getItemTextProps: (props: ItemProps) => T["element"];
188
+ getItemControlProps: (props: ItemProps) => T["element"];
189
+ getItemHiddenInputProps: (props: ItemProps) => T["input"];
190
+ getIndicatorProps: () => T["element"];
191
+ }
192
+
193
+ export type { ElementIds, ItemProps, ItemState, RadioGroupApi, RadioGroupMachine, RadioGroupProps, RadioGroupSchema, RadioGroupService, ValueChangeDetails };