@zag-js/radio-group 0.10.4 → 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.
@@ -1,128 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const core = require('@zag-js/core');
6
- const domQuery = require('@zag-js/dom-query');
7
- const elementRect = require('@zag-js/element-rect');
8
- const formUtils = require('@zag-js/form-utils');
9
- const utils = require('@zag-js/utils');
10
- const radioGroup_dom = require('./radio-group.dom.js');
11
-
12
- function machine(userContext) {
13
- const ctx = utils.compact(userContext);
14
- return core.createMachine(
15
- {
16
- id: "radio",
17
- initial: "idle",
18
- context: {
19
- value: null,
20
- activeId: null,
21
- focusedId: null,
22
- hoveredId: null,
23
- indicatorRect: {},
24
- canIndicatorTransition: false,
25
- ...ctx
26
- },
27
- entry: ["syncIndicatorRect"],
28
- exit: ["cleanupObserver"],
29
- activities: ["trackFormControlState"],
30
- watch: {
31
- value: [
32
- "setIndicatorTransition",
33
- // important to set this after `setIndicatorTransition`
34
- "setPreviousValue",
35
- "invokeOnChange",
36
- "syncIndicatorRect",
37
- "syncInputElements"
38
- ]
39
- },
40
- on: {
41
- SET_VALUE: {
42
- actions: ["setValue"]
43
- },
44
- SET_HOVERED: {
45
- actions: "setHovered"
46
- },
47
- SET_ACTIVE: {
48
- actions: "setActive"
49
- },
50
- SET_FOCUSED: {
51
- actions: "setFocused"
52
- }
53
- },
54
- states: {
55
- idle: {}
56
- }
57
- },
58
- {
59
- activities: {
60
- trackFormControlState(ctx2, _evt, { send, initialContext }) {
61
- return formUtils.trackFormControl(radioGroup_dom.dom.getRootEl(ctx2), {
62
- onFieldsetDisabled() {
63
- ctx2.disabled = true;
64
- },
65
- onFormReset() {
66
- send({ type: "SET_VALUE", value: initialContext.value });
67
- }
68
- });
69
- }
70
- },
71
- actions: {
72
- setValue(ctx2, evt) {
73
- ctx2.value = evt.value;
74
- },
75
- setHovered(ctx2, evt) {
76
- ctx2.hoveredId = evt.value;
77
- },
78
- setActive(ctx2, evt) {
79
- ctx2.activeId = evt.value;
80
- },
81
- setFocused(ctx2, evt) {
82
- ctx2.focusedId = evt.value;
83
- },
84
- invokeOnChange(ctx2, evt) {
85
- ctx2.onChange?.({ value: evt.value });
86
- },
87
- syncInputElements(ctx2) {
88
- const inputs = radioGroup_dom.dom.getInputEls(ctx2);
89
- inputs.forEach((input) => {
90
- input.checked = input.value === ctx2.value;
91
- });
92
- },
93
- setIndicatorTransition(ctx2) {
94
- ctx2.canIndicatorTransition = utils.isString(ctx2.value);
95
- },
96
- cleanupObserver(ctx2) {
97
- ctx2.indicatorCleanup?.();
98
- },
99
- syncIndicatorRect(ctx2) {
100
- ctx2.indicatorCleanup?.();
101
- if (!radioGroup_dom.dom.getIndicatorEl(ctx2))
102
- return;
103
- const value = ctx2.value;
104
- if (value == null) {
105
- ctx2.indicatorRect = {};
106
- return;
107
- }
108
- const radioEl = radioGroup_dom.dom.getActiveRadioEl(ctx2);
109
- if (!radioEl)
110
- return;
111
- ctx2.indicatorCleanup = elementRect.trackElementRect(radioEl, {
112
- getRect(el) {
113
- return radioGroup_dom.dom.getOffsetRect(el);
114
- },
115
- onChange(rect) {
116
- ctx2.indicatorRect = radioGroup_dom.dom.resolveRect(rect);
117
- domQuery.nextTick(() => {
118
- ctx2.canIndicatorTransition = false;
119
- });
120
- }
121
- });
122
- }
123
- }
124
- }
125
- );
126
- }
127
-
128
- exports.machine = machine;
@@ -1,124 +0,0 @@
1
- import { createMachine } from '@zag-js/core';
2
- import { nextTick } from '@zag-js/dom-query';
3
- import { trackElementRect } from '@zag-js/element-rect';
4
- import { trackFormControl } from '@zag-js/form-utils';
5
- import { compact, isString } from '@zag-js/utils';
6
- import { dom } from './radio-group.dom.mjs';
7
-
8
- function machine(userContext) {
9
- const ctx = compact(userContext);
10
- return createMachine(
11
- {
12
- id: "radio",
13
- initial: "idle",
14
- context: {
15
- value: null,
16
- activeId: null,
17
- focusedId: null,
18
- hoveredId: null,
19
- indicatorRect: {},
20
- canIndicatorTransition: false,
21
- ...ctx
22
- },
23
- entry: ["syncIndicatorRect"],
24
- exit: ["cleanupObserver"],
25
- activities: ["trackFormControlState"],
26
- watch: {
27
- value: [
28
- "setIndicatorTransition",
29
- // important to set this after `setIndicatorTransition`
30
- "setPreviousValue",
31
- "invokeOnChange",
32
- "syncIndicatorRect",
33
- "syncInputElements"
34
- ]
35
- },
36
- on: {
37
- SET_VALUE: {
38
- actions: ["setValue"]
39
- },
40
- SET_HOVERED: {
41
- actions: "setHovered"
42
- },
43
- SET_ACTIVE: {
44
- actions: "setActive"
45
- },
46
- SET_FOCUSED: {
47
- actions: "setFocused"
48
- }
49
- },
50
- states: {
51
- idle: {}
52
- }
53
- },
54
- {
55
- activities: {
56
- trackFormControlState(ctx2, _evt, { send, initialContext }) {
57
- return trackFormControl(dom.getRootEl(ctx2), {
58
- onFieldsetDisabled() {
59
- ctx2.disabled = true;
60
- },
61
- onFormReset() {
62
- send({ type: "SET_VALUE", value: initialContext.value });
63
- }
64
- });
65
- }
66
- },
67
- actions: {
68
- setValue(ctx2, evt) {
69
- ctx2.value = evt.value;
70
- },
71
- setHovered(ctx2, evt) {
72
- ctx2.hoveredId = evt.value;
73
- },
74
- setActive(ctx2, evt) {
75
- ctx2.activeId = evt.value;
76
- },
77
- setFocused(ctx2, evt) {
78
- ctx2.focusedId = evt.value;
79
- },
80
- invokeOnChange(ctx2, evt) {
81
- ctx2.onChange?.({ value: evt.value });
82
- },
83
- syncInputElements(ctx2) {
84
- const inputs = dom.getInputEls(ctx2);
85
- inputs.forEach((input) => {
86
- input.checked = input.value === ctx2.value;
87
- });
88
- },
89
- setIndicatorTransition(ctx2) {
90
- ctx2.canIndicatorTransition = isString(ctx2.value);
91
- },
92
- cleanupObserver(ctx2) {
93
- ctx2.indicatorCleanup?.();
94
- },
95
- syncIndicatorRect(ctx2) {
96
- ctx2.indicatorCleanup?.();
97
- if (!dom.getIndicatorEl(ctx2))
98
- return;
99
- const value = ctx2.value;
100
- if (value == null) {
101
- ctx2.indicatorRect = {};
102
- return;
103
- }
104
- const radioEl = dom.getActiveRadioEl(ctx2);
105
- if (!radioEl)
106
- return;
107
- ctx2.indicatorCleanup = trackElementRect(radioEl, {
108
- getRect(el) {
109
- return dom.getOffsetRect(el);
110
- },
111
- onChange(rect) {
112
- ctx2.indicatorRect = dom.resolveRect(rect);
113
- nextTick(() => {
114
- ctx2.canIndicatorTransition = false;
115
- });
116
- }
117
- });
118
- }
119
- }
120
- }
121
- );
122
- }
123
-
124
- export { machine };
@@ -1,85 +0,0 @@
1
- import type { StateMachine as S } from "@zag-js/core";
2
- import type { CommonProperties, Context, DirectionProperty, RequiredBy } from "@zag-js/types";
3
- type ElementIds = Partial<{
4
- root: string;
5
- label: string;
6
- indicator: string;
7
- radio(value: string): string;
8
- radioLabel(value: string): string;
9
- radioControl(value: string): string;
10
- radioInput(value: string): string;
11
- }>;
12
- type PublicContext = DirectionProperty & CommonProperties & {
13
- /**
14
- * The ids of the elements in the radio. Useful for composition.
15
- */
16
- ids?: ElementIds;
17
- /**
18
- * The value of the checked radio
19
- */
20
- value: string | null;
21
- /**
22
- * The name of the input fields in the radio
23
- * (Useful for form submission).
24
- */
25
- name?: string;
26
- /**
27
- * The associate form of the underlying input.
28
- */
29
- form?: string;
30
- /**
31
- * If `true`, the radio group will be disabled
32
- */
33
- disabled?: boolean;
34
- /**
35
- * If `true`, the radio group will be readonly
36
- */
37
- readOnly?: boolean;
38
- /**
39
- * Function called once a radio is checked
40
- * @param value the value of the checked radio
41
- */
42
- onChange?(details: {
43
- value: string;
44
- }): void;
45
- /**
46
- * Orientation of the radio group
47
- */
48
- orientation?: "horizontal" | "vertical";
49
- };
50
- type PrivateContext = Context<{}>;
51
- export type UserDefinedContext = RequiredBy<PublicContext, "id">;
52
- type ComputedContext = Readonly<{}>;
53
- export type MachineContext = PublicContext & PrivateContext & ComputedContext;
54
- export type MachineState = {
55
- value: "idle";
56
- };
57
- export type State = S.State<MachineContext, MachineState>;
58
- export type Send = S.Send<S.AnyEventObject>;
59
- export type RadioProps = {
60
- value: string;
61
- /**
62
- * If `true`, the radio will be disabled
63
- */
64
- disabled?: boolean;
65
- /**
66
- * If `true`, the radio will be readonly
67
- */
68
- readOnly?: boolean;
69
- /**
70
- * If `true`, the radio is marked as invalid.
71
- */
72
- invalid?: boolean;
73
- };
74
- export type InputProps = RadioProps & {
75
- /**
76
- * If `true` and `disabled` is passed, the radio will
77
- * remain tabbable but not interactive
78
- */
79
- focusable?: boolean;
80
- /**
81
- * If `true`, the radio input is marked as required,
82
- */
83
- required?: boolean;
84
- };
85
- export {};