@zag-js/radio-group 0.10.5 → 0.11.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.d.mts +136 -0
- package/dist/index.d.ts +136 -4
- package/dist/index.js +419 -8
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +395 -3
- package/dist/index.mjs.map +1 -0
- package/package.json +10 -10
- package/dist/radio-group.anatomy.d.ts +0 -3
- package/dist/radio-group.anatomy.js +0 -19
- package/dist/radio-group.anatomy.mjs +0 -14
- package/dist/radio-group.connect.d.ts +0 -44
- package/dist/radio-group.connect.js +0 -213
- package/dist/radio-group.connect.mjs +0 -209
- package/dist/radio-group.dom.d.ts +0 -54
- package/dist/radio-group.dom.js +0 -52
- package/dist/radio-group.dom.mjs +0 -48
- package/dist/radio-group.machine.d.ts +0 -3
- package/dist/radio-group.machine.js +0 -128
- package/dist/radio-group.machine.mjs +0 -124
- package/dist/radio-group.types.d.ts +0 -85
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
+
import { RequiredBy, DirectionProperty, CommonProperties, Context, PropTypes, NormalizeProps } from '@zag-js/types';
|
|
3
|
+
import * as _zag_js_core from '@zag-js/core';
|
|
4
|
+
import { StateMachine } from '@zag-js/core';
|
|
5
|
+
|
|
6
|
+
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "label" | "radio" | "radioLabel" | "radioControl" | "radioInput" | "indicator">;
|
|
7
|
+
|
|
8
|
+
type ElementIds = Partial<{
|
|
9
|
+
root: string;
|
|
10
|
+
label: string;
|
|
11
|
+
indicator: string;
|
|
12
|
+
radio(value: string): string;
|
|
13
|
+
radioLabel(value: string): string;
|
|
14
|
+
radioControl(value: string): string;
|
|
15
|
+
radioInput(value: string): string;
|
|
16
|
+
}>;
|
|
17
|
+
type PublicContext = DirectionProperty & CommonProperties & {
|
|
18
|
+
/**
|
|
19
|
+
* The ids of the elements in the radio. Useful for composition.
|
|
20
|
+
*/
|
|
21
|
+
ids?: ElementIds;
|
|
22
|
+
/**
|
|
23
|
+
* The value of the checked radio
|
|
24
|
+
*/
|
|
25
|
+
value: string | null;
|
|
26
|
+
/**
|
|
27
|
+
* The name of the input fields in the radio
|
|
28
|
+
* (Useful for form submission).
|
|
29
|
+
*/
|
|
30
|
+
name?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The associate form of the underlying input.
|
|
33
|
+
*/
|
|
34
|
+
form?: string;
|
|
35
|
+
/**
|
|
36
|
+
* If `true`, the radio group will be disabled
|
|
37
|
+
*/
|
|
38
|
+
disabled?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* If `true`, the radio group will be readonly
|
|
41
|
+
*/
|
|
42
|
+
readOnly?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Function called once a radio is checked
|
|
45
|
+
* @param value the value of the checked radio
|
|
46
|
+
*/
|
|
47
|
+
onChange?(details: {
|
|
48
|
+
value: string;
|
|
49
|
+
}): void;
|
|
50
|
+
/**
|
|
51
|
+
* Orientation of the radio group
|
|
52
|
+
*/
|
|
53
|
+
orientation?: "horizontal" | "vertical";
|
|
54
|
+
};
|
|
55
|
+
type PrivateContext = Context<{}>;
|
|
56
|
+
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
57
|
+
type ComputedContext = Readonly<{}>;
|
|
58
|
+
type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
59
|
+
type MachineState = {
|
|
60
|
+
value: "idle";
|
|
61
|
+
};
|
|
62
|
+
type State = StateMachine.State<MachineContext, MachineState>;
|
|
63
|
+
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
64
|
+
type RadioProps = {
|
|
65
|
+
value: string;
|
|
66
|
+
/**
|
|
67
|
+
* If `true`, the radio will be disabled
|
|
68
|
+
*/
|
|
69
|
+
disabled?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* If `true`, the radio will be readonly
|
|
72
|
+
*/
|
|
73
|
+
readOnly?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* If `true`, the radio is marked as invalid.
|
|
76
|
+
*/
|
|
77
|
+
invalid?: boolean;
|
|
78
|
+
};
|
|
79
|
+
type InputProps = RadioProps & {
|
|
80
|
+
/**
|
|
81
|
+
* If `true` and `disabled` is passed, the radio will
|
|
82
|
+
* remain tabbable but not interactive
|
|
83
|
+
*/
|
|
84
|
+
focusable?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* If `true`, the radio input is marked as required,
|
|
87
|
+
*/
|
|
88
|
+
required?: boolean;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
|
|
92
|
+
/**
|
|
93
|
+
* The current value of the radio group
|
|
94
|
+
*/
|
|
95
|
+
value: string | null;
|
|
96
|
+
/**
|
|
97
|
+
* Function to set the value of the radio group
|
|
98
|
+
*/
|
|
99
|
+
setValue(value: string): void;
|
|
100
|
+
/**
|
|
101
|
+
* Function to clear the value of the radio group
|
|
102
|
+
*/
|
|
103
|
+
clearValue(): void;
|
|
104
|
+
/**
|
|
105
|
+
* Function to focus the radio group
|
|
106
|
+
*/
|
|
107
|
+
focus: () => void;
|
|
108
|
+
/**
|
|
109
|
+
* Function to blur the currently focused radio input in the radio group
|
|
110
|
+
*/
|
|
111
|
+
blur(): void;
|
|
112
|
+
/**
|
|
113
|
+
* Returns the state details of a radio input
|
|
114
|
+
*/
|
|
115
|
+
getRadioState: <T_1 extends RadioProps>(props: T_1) => {
|
|
116
|
+
isInteractive: boolean;
|
|
117
|
+
isReadOnly: boolean | undefined;
|
|
118
|
+
isInvalid: boolean | undefined;
|
|
119
|
+
isDisabled: boolean | undefined;
|
|
120
|
+
isChecked: boolean;
|
|
121
|
+
isFocused: boolean;
|
|
122
|
+
isHovered: boolean;
|
|
123
|
+
isActive: boolean;
|
|
124
|
+
};
|
|
125
|
+
rootProps: T["element"];
|
|
126
|
+
labelProps: T["element"];
|
|
127
|
+
getRadioProps(props: RadioProps): T["label"];
|
|
128
|
+
getRadioLabelProps(props: RadioProps): T["element"];
|
|
129
|
+
getRadioControlProps(props: RadioProps): T["element"];
|
|
130
|
+
getRadioInputProps(props: InputProps): T["input"];
|
|
131
|
+
indicatorProps: T["element"];
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
135
|
+
|
|
136
|
+
export { UserDefinedContext as Context, anatomy, connect, machine };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,136 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
+
import { RequiredBy, DirectionProperty, CommonProperties, Context, PropTypes, NormalizeProps } from '@zag-js/types';
|
|
3
|
+
import * as _zag_js_core from '@zag-js/core';
|
|
4
|
+
import { StateMachine } from '@zag-js/core';
|
|
5
|
+
|
|
6
|
+
declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "label" | "radio" | "radioLabel" | "radioControl" | "radioInput" | "indicator">;
|
|
7
|
+
|
|
8
|
+
type ElementIds = Partial<{
|
|
9
|
+
root: string;
|
|
10
|
+
label: string;
|
|
11
|
+
indicator: string;
|
|
12
|
+
radio(value: string): string;
|
|
13
|
+
radioLabel(value: string): string;
|
|
14
|
+
radioControl(value: string): string;
|
|
15
|
+
radioInput(value: string): string;
|
|
16
|
+
}>;
|
|
17
|
+
type PublicContext = DirectionProperty & CommonProperties & {
|
|
18
|
+
/**
|
|
19
|
+
* The ids of the elements in the radio. Useful for composition.
|
|
20
|
+
*/
|
|
21
|
+
ids?: ElementIds;
|
|
22
|
+
/**
|
|
23
|
+
* The value of the checked radio
|
|
24
|
+
*/
|
|
25
|
+
value: string | null;
|
|
26
|
+
/**
|
|
27
|
+
* The name of the input fields in the radio
|
|
28
|
+
* (Useful for form submission).
|
|
29
|
+
*/
|
|
30
|
+
name?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The associate form of the underlying input.
|
|
33
|
+
*/
|
|
34
|
+
form?: string;
|
|
35
|
+
/**
|
|
36
|
+
* If `true`, the radio group will be disabled
|
|
37
|
+
*/
|
|
38
|
+
disabled?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* If `true`, the radio group will be readonly
|
|
41
|
+
*/
|
|
42
|
+
readOnly?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Function called once a radio is checked
|
|
45
|
+
* @param value the value of the checked radio
|
|
46
|
+
*/
|
|
47
|
+
onChange?(details: {
|
|
48
|
+
value: string;
|
|
49
|
+
}): void;
|
|
50
|
+
/**
|
|
51
|
+
* Orientation of the radio group
|
|
52
|
+
*/
|
|
53
|
+
orientation?: "horizontal" | "vertical";
|
|
54
|
+
};
|
|
55
|
+
type PrivateContext = Context<{}>;
|
|
56
|
+
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
57
|
+
type ComputedContext = Readonly<{}>;
|
|
58
|
+
type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
59
|
+
type MachineState = {
|
|
60
|
+
value: "idle";
|
|
61
|
+
};
|
|
62
|
+
type State = StateMachine.State<MachineContext, MachineState>;
|
|
63
|
+
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
64
|
+
type RadioProps = {
|
|
65
|
+
value: string;
|
|
66
|
+
/**
|
|
67
|
+
* If `true`, the radio will be disabled
|
|
68
|
+
*/
|
|
69
|
+
disabled?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* If `true`, the radio will be readonly
|
|
72
|
+
*/
|
|
73
|
+
readOnly?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* If `true`, the radio is marked as invalid.
|
|
76
|
+
*/
|
|
77
|
+
invalid?: boolean;
|
|
78
|
+
};
|
|
79
|
+
type InputProps = RadioProps & {
|
|
80
|
+
/**
|
|
81
|
+
* If `true` and `disabled` is passed, the radio will
|
|
82
|
+
* remain tabbable but not interactive
|
|
83
|
+
*/
|
|
84
|
+
focusable?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* If `true`, the radio input is marked as required,
|
|
87
|
+
*/
|
|
88
|
+
required?: boolean;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
|
|
92
|
+
/**
|
|
93
|
+
* The current value of the radio group
|
|
94
|
+
*/
|
|
95
|
+
value: string | null;
|
|
96
|
+
/**
|
|
97
|
+
* Function to set the value of the radio group
|
|
98
|
+
*/
|
|
99
|
+
setValue(value: string): void;
|
|
100
|
+
/**
|
|
101
|
+
* Function to clear the value of the radio group
|
|
102
|
+
*/
|
|
103
|
+
clearValue(): void;
|
|
104
|
+
/**
|
|
105
|
+
* Function to focus the radio group
|
|
106
|
+
*/
|
|
107
|
+
focus: () => void;
|
|
108
|
+
/**
|
|
109
|
+
* Function to blur the currently focused radio input in the radio group
|
|
110
|
+
*/
|
|
111
|
+
blur(): void;
|
|
112
|
+
/**
|
|
113
|
+
* Returns the state details of a radio input
|
|
114
|
+
*/
|
|
115
|
+
getRadioState: <T_1 extends RadioProps>(props: T_1) => {
|
|
116
|
+
isInteractive: boolean;
|
|
117
|
+
isReadOnly: boolean | undefined;
|
|
118
|
+
isInvalid: boolean | undefined;
|
|
119
|
+
isDisabled: boolean | undefined;
|
|
120
|
+
isChecked: boolean;
|
|
121
|
+
isFocused: boolean;
|
|
122
|
+
isHovered: boolean;
|
|
123
|
+
isActive: boolean;
|
|
124
|
+
};
|
|
125
|
+
rootProps: T["element"];
|
|
126
|
+
labelProps: T["element"];
|
|
127
|
+
getRadioProps(props: RadioProps): T["label"];
|
|
128
|
+
getRadioLabelProps(props: RadioProps): T["element"];
|
|
129
|
+
getRadioControlProps(props: RadioProps): T["element"];
|
|
130
|
+
getRadioInputProps(props: InputProps): T["input"];
|
|
131
|
+
indicatorProps: T["element"];
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
135
|
+
|
|
136
|
+
export { UserDefinedContext as Context, anatomy, connect, machine };
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,424 @@
|
|
|
1
|
-
|
|
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);
|
|
2
19
|
|
|
3
|
-
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
anatomy: () => anatomy,
|
|
24
|
+
connect: () => connect,
|
|
25
|
+
machine: () => machine
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
4
28
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
29
|
+
// src/radio-group.anatomy.ts
|
|
30
|
+
var import_anatomy = require("@zag-js/anatomy");
|
|
31
|
+
var anatomy = (0, import_anatomy.createAnatomy)("radio-group").parts(
|
|
32
|
+
"root",
|
|
33
|
+
"label",
|
|
34
|
+
"radio",
|
|
35
|
+
"radioLabel",
|
|
36
|
+
"radioControl",
|
|
37
|
+
"radioInput",
|
|
38
|
+
"indicator"
|
|
39
|
+
);
|
|
40
|
+
var parts = anatomy.build();
|
|
8
41
|
|
|
42
|
+
// src/radio-group.connect.ts
|
|
43
|
+
var import_dom_query2 = require("@zag-js/dom-query");
|
|
44
|
+
var import_visually_hidden = require("@zag-js/visually-hidden");
|
|
9
45
|
|
|
46
|
+
// src/radio-group.dom.ts
|
|
47
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
48
|
+
var dom = (0, import_dom_query.createScope)({
|
|
49
|
+
getRootId: (ctx) => ctx.ids?.root ?? `radio-group:${ctx.id}`,
|
|
50
|
+
getLabelId: (ctx) => ctx.ids?.label ?? `radio-group:${ctx.id}:label`,
|
|
51
|
+
getRadioId: (ctx, value) => ctx.ids?.radio?.(value) ?? `radio-group:${ctx.id}:radio:${value}`,
|
|
52
|
+
getRadioInputId: (ctx, value) => ctx.ids?.radioInput?.(value) ?? `radio-group:${ctx.id}:radio:input:${value}`,
|
|
53
|
+
getRadioControlId: (ctx, value) => ctx.ids?.radioControl?.(value) ?? `radio-group:${ctx.id}:radio:control:${value}`,
|
|
54
|
+
getRadioLabelId: (ctx, value) => ctx.ids?.radioLabel?.(value) ?? `radio-group:${ctx.id}:radio:label:${value}`,
|
|
55
|
+
getIndicatorId: (ctx) => ctx.ids?.indicator ?? `tabs:${ctx.id}:indicator`,
|
|
56
|
+
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
57
|
+
getRadioInputEl: (ctx, value) => dom.getById(ctx, dom.getRadioInputId(ctx, value)),
|
|
58
|
+
getIndicatorEl: (ctx) => dom.getById(ctx, dom.getIndicatorId(ctx)),
|
|
59
|
+
getFirstEnabledInputEl: (ctx) => dom.getRootEl(ctx)?.querySelector("input:not(:disabled)"),
|
|
60
|
+
getFirstEnabledAndCheckedInputEl: (ctx) => dom.getRootEl(ctx)?.querySelector("input:not(:disabled):checked"),
|
|
61
|
+
getInputEls: (ctx) => {
|
|
62
|
+
const ownerId = CSS.escape(dom.getRootId(ctx));
|
|
63
|
+
const selector = `input[type=radio][data-ownedby='${ownerId}']:not([disabled])`;
|
|
64
|
+
return (0, import_dom_query.queryAll)(dom.getRootEl(ctx), selector);
|
|
65
|
+
},
|
|
66
|
+
getActiveRadioEl: (ctx) => {
|
|
67
|
+
if (!ctx.value)
|
|
68
|
+
return;
|
|
69
|
+
return dom.getById(ctx, dom.getRadioId(ctx, ctx.value));
|
|
70
|
+
},
|
|
71
|
+
getOffsetRect: (el) => {
|
|
72
|
+
return {
|
|
73
|
+
left: el?.offsetLeft ?? 0,
|
|
74
|
+
top: el?.offsetTop ?? 0,
|
|
75
|
+
width: el?.offsetWidth ?? 0,
|
|
76
|
+
height: el?.offsetHeight ?? 0
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
getRectById: (ctx, id) => {
|
|
80
|
+
const tab = dom.queryById(ctx, dom.getRadioId(ctx, id));
|
|
81
|
+
return dom.resolveRect(dom.getOffsetRect(tab));
|
|
82
|
+
},
|
|
83
|
+
resolveRect(rect) {
|
|
84
|
+
return {
|
|
85
|
+
width: `${rect.width}px`,
|
|
86
|
+
height: `${rect.height}px`,
|
|
87
|
+
left: `${rect.left}px`,
|
|
88
|
+
top: `${rect.top}px`
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
});
|
|
10
92
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
93
|
+
// src/radio-group.connect.ts
|
|
94
|
+
function connect(state, send, normalize) {
|
|
95
|
+
const isGroupDisabled = state.context.disabled;
|
|
96
|
+
const isGroupReadOnly = state.context.readOnly;
|
|
97
|
+
function getRadioState(props) {
|
|
98
|
+
const radioState = {
|
|
99
|
+
isReadOnly: props.readOnly || isGroupReadOnly,
|
|
100
|
+
isInvalid: props.invalid,
|
|
101
|
+
isDisabled: props.disabled || isGroupDisabled,
|
|
102
|
+
isChecked: state.context.value === props.value,
|
|
103
|
+
isFocused: state.context.focusedId === props.value,
|
|
104
|
+
isHovered: state.context.hoveredId === props.value,
|
|
105
|
+
isActive: state.context.activeId === props.value
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
...radioState,
|
|
109
|
+
isInteractive: !(radioState.isReadOnly || radioState.isDisabled)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function getRadioDataAttrs(props) {
|
|
113
|
+
const radioState = getRadioState(props);
|
|
114
|
+
return {
|
|
115
|
+
"data-focus": (0, import_dom_query2.dataAttr)(radioState.isFocused),
|
|
116
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(radioState.isDisabled),
|
|
117
|
+
"data-checked": (0, import_dom_query2.dataAttr)(radioState.isChecked),
|
|
118
|
+
"data-hover": (0, import_dom_query2.dataAttr)(radioState.isHovered),
|
|
119
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(radioState.isInvalid),
|
|
120
|
+
"data-readonly": (0, import_dom_query2.dataAttr)(radioState.isReadOnly)
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const focus = () => {
|
|
124
|
+
const firstEnabledAndCheckedInput = dom.getFirstEnabledAndCheckedInputEl(state.context);
|
|
125
|
+
if (firstEnabledAndCheckedInput) {
|
|
126
|
+
firstEnabledAndCheckedInput.focus();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const firstEnabledInput = dom.getFirstEnabledInputEl(state.context);
|
|
130
|
+
firstEnabledInput?.focus();
|
|
131
|
+
};
|
|
132
|
+
return {
|
|
133
|
+
/**
|
|
134
|
+
* The current value of the radio group
|
|
135
|
+
*/
|
|
136
|
+
value: state.context.value,
|
|
137
|
+
/**
|
|
138
|
+
* Function to set the value of the radio group
|
|
139
|
+
*/
|
|
140
|
+
setValue(value) {
|
|
141
|
+
send({ type: "SET_VALUE", value, manual: true });
|
|
142
|
+
},
|
|
143
|
+
/**
|
|
144
|
+
* Function to clear the value of the radio group
|
|
145
|
+
*/
|
|
146
|
+
clearValue() {
|
|
147
|
+
send({ type: "SET_VALUE", value: null, manual: true });
|
|
148
|
+
},
|
|
149
|
+
/**
|
|
150
|
+
* Function to focus the radio group
|
|
151
|
+
*/
|
|
152
|
+
focus,
|
|
153
|
+
/**
|
|
154
|
+
* Function to blur the currently focused radio input in the radio group
|
|
155
|
+
*/
|
|
156
|
+
blur() {
|
|
157
|
+
const focusedElement = dom.getActiveElement(state.context);
|
|
158
|
+
const inputEls = dom.getInputEls(state.context);
|
|
159
|
+
const radioInputIsFocused = inputEls.some((el) => el === focusedElement);
|
|
160
|
+
if (radioInputIsFocused)
|
|
161
|
+
focusedElement?.blur();
|
|
162
|
+
},
|
|
163
|
+
/**
|
|
164
|
+
* Returns the state details of a radio input
|
|
165
|
+
*/
|
|
166
|
+
getRadioState,
|
|
167
|
+
rootProps: normalize.element({
|
|
168
|
+
...parts.root.attrs,
|
|
169
|
+
role: "radiogroup",
|
|
170
|
+
id: dom.getRootId(state.context),
|
|
171
|
+
"aria-labelledby": dom.getLabelId(state.context),
|
|
172
|
+
"data-orientation": state.context.orientation,
|
|
173
|
+
"aria-orientation": state.context.orientation,
|
|
174
|
+
dir: state.context.dir
|
|
175
|
+
}),
|
|
176
|
+
labelProps: normalize.element({
|
|
177
|
+
...parts.label.attrs,
|
|
178
|
+
id: dom.getLabelId(state.context),
|
|
179
|
+
onClick: focus
|
|
180
|
+
}),
|
|
181
|
+
getRadioProps(props) {
|
|
182
|
+
const rootState = getRadioState(props);
|
|
183
|
+
return normalize.label({
|
|
184
|
+
...parts.radio.attrs,
|
|
185
|
+
id: dom.getRadioId(state.context, props.value),
|
|
186
|
+
htmlFor: dom.getRadioInputId(state.context, props.value),
|
|
187
|
+
...getRadioDataAttrs(props),
|
|
188
|
+
onPointerMove() {
|
|
189
|
+
if (!rootState.isInteractive)
|
|
190
|
+
return;
|
|
191
|
+
send({ type: "SET_HOVERED", value: props.value, hovered: true });
|
|
192
|
+
},
|
|
193
|
+
onPointerLeave() {
|
|
194
|
+
if (!rootState.isInteractive)
|
|
195
|
+
return;
|
|
196
|
+
send({ type: "SET_HOVERED", value: null });
|
|
197
|
+
},
|
|
198
|
+
onPointerDown(event) {
|
|
199
|
+
if (!rootState.isInteractive)
|
|
200
|
+
return;
|
|
201
|
+
if (rootState.isFocused && event.pointerType === "mouse") {
|
|
202
|
+
event.preventDefault();
|
|
203
|
+
}
|
|
204
|
+
send({ type: "SET_ACTIVE", value: props.value, active: true });
|
|
205
|
+
},
|
|
206
|
+
onPointerUp() {
|
|
207
|
+
if (!rootState.isInteractive)
|
|
208
|
+
return;
|
|
209
|
+
send({ type: "SET_ACTIVE", value: null });
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
getRadioLabelProps(props) {
|
|
214
|
+
return normalize.element({
|
|
215
|
+
...parts.radioLabel.attrs,
|
|
216
|
+
id: dom.getRadioLabelId(state.context, props.value),
|
|
217
|
+
...getRadioDataAttrs(props)
|
|
218
|
+
});
|
|
219
|
+
},
|
|
220
|
+
getRadioControlProps(props) {
|
|
221
|
+
const controlState = getRadioState(props);
|
|
222
|
+
return normalize.element({
|
|
223
|
+
...parts.radioControl.attrs,
|
|
224
|
+
id: dom.getRadioControlId(state.context, props.value),
|
|
225
|
+
"data-active": (0, import_dom_query2.dataAttr)(controlState.isActive),
|
|
226
|
+
"aria-hidden": true,
|
|
227
|
+
...getRadioDataAttrs(props)
|
|
228
|
+
});
|
|
229
|
+
},
|
|
230
|
+
getRadioInputProps(props) {
|
|
231
|
+
const inputState = getRadioState(props);
|
|
232
|
+
const isRequired = props.required;
|
|
233
|
+
const trulyDisabled = inputState.isDisabled && !props.focusable;
|
|
234
|
+
return normalize.input({
|
|
235
|
+
...parts.radioInput.attrs,
|
|
236
|
+
"data-ownedby": dom.getRootId(state.context),
|
|
237
|
+
id: dom.getRadioInputId(state.context, props.value),
|
|
238
|
+
type: "radio",
|
|
239
|
+
name: state.context.name || state.context.id,
|
|
240
|
+
form: state.context.form,
|
|
241
|
+
value: props.value,
|
|
242
|
+
onChange(event) {
|
|
243
|
+
if (inputState.isReadOnly || inputState.isDisabled) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (event.target.checked) {
|
|
247
|
+
send({ type: "SET_VALUE", value: props.value });
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
onBlur() {
|
|
251
|
+
send({ type: "SET_FOCUSED", value: null });
|
|
252
|
+
},
|
|
253
|
+
onFocus() {
|
|
254
|
+
send({ type: "SET_FOCUSED", value: props.value, focused: true });
|
|
255
|
+
},
|
|
256
|
+
onKeyDown(event) {
|
|
257
|
+
if (event.key === " ") {
|
|
258
|
+
send({ type: "SET_ACTIVE", value: props.value, active: true });
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
onKeyUp(event) {
|
|
262
|
+
if (event.key === " ") {
|
|
263
|
+
send({ type: "SET_ACTIVE", value: null });
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
disabled: trulyDisabled,
|
|
267
|
+
required: isRequired,
|
|
268
|
+
defaultChecked: inputState.isChecked,
|
|
269
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(inputState.isDisabled),
|
|
270
|
+
"aria-required": (0, import_dom_query2.ariaAttr)(isRequired),
|
|
271
|
+
"aria-invalid": (0, import_dom_query2.ariaAttr)(inputState.isInvalid),
|
|
272
|
+
readOnly: inputState.isReadOnly,
|
|
273
|
+
"data-readonly": (0, import_dom_query2.dataAttr)(inputState.isReadOnly),
|
|
274
|
+
"aria-disabled": (0, import_dom_query2.ariaAttr)(trulyDisabled),
|
|
275
|
+
"aria-checked": (0, import_dom_query2.ariaAttr)(inputState.isChecked),
|
|
276
|
+
style: import_visually_hidden.visuallyHiddenStyle
|
|
277
|
+
});
|
|
278
|
+
},
|
|
279
|
+
indicatorProps: normalize.element({
|
|
280
|
+
id: dom.getIndicatorId(state.context),
|
|
281
|
+
...parts.indicator.attrs,
|
|
282
|
+
"data-orientation": state.context.orientation,
|
|
283
|
+
style: {
|
|
284
|
+
"--transition-duration": "150ms",
|
|
285
|
+
"--transition-property": "left, top, width, height",
|
|
286
|
+
position: "absolute",
|
|
287
|
+
willChange: "var(--transition-property)",
|
|
288
|
+
transitionProperty: "var(--transition-property)",
|
|
289
|
+
transitionDuration: state.context.canIndicatorTransition ? "var(--transition-duration)" : "0ms",
|
|
290
|
+
transitionTimingFunction: "var(--transition-timing-function)",
|
|
291
|
+
...state.context.indicatorRect
|
|
292
|
+
}
|
|
293
|
+
})
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/radio-group.machine.ts
|
|
298
|
+
var import_core = require("@zag-js/core");
|
|
299
|
+
var import_dom_query3 = require("@zag-js/dom-query");
|
|
300
|
+
var import_element_rect = require("@zag-js/element-rect");
|
|
301
|
+
var import_form_utils = require("@zag-js/form-utils");
|
|
302
|
+
var import_utils = require("@zag-js/utils");
|
|
303
|
+
function machine(userContext) {
|
|
304
|
+
const ctx = (0, import_utils.compact)(userContext);
|
|
305
|
+
return (0, import_core.createMachine)(
|
|
306
|
+
{
|
|
307
|
+
id: "radio",
|
|
308
|
+
initial: "idle",
|
|
309
|
+
context: {
|
|
310
|
+
value: null,
|
|
311
|
+
activeId: null,
|
|
312
|
+
focusedId: null,
|
|
313
|
+
hoveredId: null,
|
|
314
|
+
indicatorRect: {},
|
|
315
|
+
canIndicatorTransition: false,
|
|
316
|
+
...ctx
|
|
317
|
+
},
|
|
318
|
+
entry: ["syncIndicatorRect"],
|
|
319
|
+
exit: ["cleanupObserver"],
|
|
320
|
+
activities: ["trackFormControlState"],
|
|
321
|
+
watch: {
|
|
322
|
+
value: [
|
|
323
|
+
"setIndicatorTransition",
|
|
324
|
+
// important to set this after `setIndicatorTransition`
|
|
325
|
+
"setPreviousValue",
|
|
326
|
+
"invokeOnChange",
|
|
327
|
+
"syncIndicatorRect",
|
|
328
|
+
"syncInputElements"
|
|
329
|
+
]
|
|
330
|
+
},
|
|
331
|
+
on: {
|
|
332
|
+
SET_VALUE: {
|
|
333
|
+
actions: ["setValue"]
|
|
334
|
+
},
|
|
335
|
+
SET_HOVERED: {
|
|
336
|
+
actions: "setHovered"
|
|
337
|
+
},
|
|
338
|
+
SET_ACTIVE: {
|
|
339
|
+
actions: "setActive"
|
|
340
|
+
},
|
|
341
|
+
SET_FOCUSED: {
|
|
342
|
+
actions: "setFocused"
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
states: {
|
|
346
|
+
idle: {}
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
activities: {
|
|
351
|
+
trackFormControlState(ctx2, _evt, { send, initialContext }) {
|
|
352
|
+
return (0, import_form_utils.trackFormControl)(dom.getRootEl(ctx2), {
|
|
353
|
+
onFieldsetDisabled() {
|
|
354
|
+
ctx2.disabled = true;
|
|
355
|
+
},
|
|
356
|
+
onFormReset() {
|
|
357
|
+
send({ type: "SET_VALUE", value: initialContext.value });
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
actions: {
|
|
363
|
+
setValue(ctx2, evt) {
|
|
364
|
+
ctx2.value = evt.value;
|
|
365
|
+
},
|
|
366
|
+
setHovered(ctx2, evt) {
|
|
367
|
+
ctx2.hoveredId = evt.value;
|
|
368
|
+
},
|
|
369
|
+
setActive(ctx2, evt) {
|
|
370
|
+
ctx2.activeId = evt.value;
|
|
371
|
+
},
|
|
372
|
+
setFocused(ctx2, evt) {
|
|
373
|
+
ctx2.focusedId = evt.value;
|
|
374
|
+
},
|
|
375
|
+
invokeOnChange(ctx2, evt) {
|
|
376
|
+
ctx2.onChange?.({ value: evt.value });
|
|
377
|
+
},
|
|
378
|
+
syncInputElements(ctx2) {
|
|
379
|
+
const inputs = dom.getInputEls(ctx2);
|
|
380
|
+
inputs.forEach((input) => {
|
|
381
|
+
input.checked = input.value === ctx2.value;
|
|
382
|
+
});
|
|
383
|
+
},
|
|
384
|
+
setIndicatorTransition(ctx2) {
|
|
385
|
+
ctx2.canIndicatorTransition = (0, import_utils.isString)(ctx2.value);
|
|
386
|
+
},
|
|
387
|
+
cleanupObserver(ctx2) {
|
|
388
|
+
ctx2.indicatorCleanup?.();
|
|
389
|
+
},
|
|
390
|
+
syncIndicatorRect(ctx2) {
|
|
391
|
+
ctx2.indicatorCleanup?.();
|
|
392
|
+
if (!dom.getIndicatorEl(ctx2))
|
|
393
|
+
return;
|
|
394
|
+
const value = ctx2.value;
|
|
395
|
+
if (value == null) {
|
|
396
|
+
ctx2.indicatorRect = {};
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const radioEl = dom.getActiveRadioEl(ctx2);
|
|
400
|
+
if (!radioEl)
|
|
401
|
+
return;
|
|
402
|
+
ctx2.indicatorCleanup = (0, import_element_rect.trackElementRect)(radioEl, {
|
|
403
|
+
getRect(el) {
|
|
404
|
+
return dom.getOffsetRect(el);
|
|
405
|
+
},
|
|
406
|
+
onChange(rect) {
|
|
407
|
+
ctx2.indicatorRect = dom.resolveRect(rect);
|
|
408
|
+
(0, import_dom_query3.nextTick)(() => {
|
|
409
|
+
ctx2.canIndicatorTransition = false;
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
419
|
+
0 && (module.exports = {
|
|
420
|
+
anatomy,
|
|
421
|
+
connect,
|
|
422
|
+
machine
|
|
423
|
+
});
|
|
424
|
+
//# sourceMappingURL=index.js.map
|