@radix-ui/react-switch 1.2.7-rc.1780436093918 → 1.3.0-rc.1780442461555
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 +27 -4
- package/dist/index.d.ts +27 -4
- package/dist/index.js +161 -69
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +162 -70
- package/dist/index.mjs.map +2 -2
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
1
2
|
import * as _radix_ui_react_context from '@radix-ui/react-context';
|
|
3
|
+
import { Scope } from '@radix-ui/react-context';
|
|
2
4
|
import * as React from 'react';
|
|
3
5
|
import { Primitive } from '@radix-ui/react-primitive';
|
|
4
6
|
|
|
7
|
+
type ScopedProps<P> = P & {
|
|
8
|
+
__scopeSwitch?: Scope;
|
|
9
|
+
};
|
|
5
10
|
declare const createSwitchScope: _radix_ui_react_context.CreateScope;
|
|
11
|
+
interface SwitchProviderProps {
|
|
12
|
+
checked?: boolean;
|
|
13
|
+
defaultChecked?: boolean;
|
|
14
|
+
required?: boolean;
|
|
15
|
+
onCheckedChange?(checked: boolean): void;
|
|
16
|
+
name?: string;
|
|
17
|
+
form?: string;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
value?: string | number | readonly string[];
|
|
20
|
+
children?: React.ReactNode;
|
|
21
|
+
}
|
|
22
|
+
declare function SwitchProvider(props: ScopedProps<SwitchProviderProps>): react_jsx_runtime.JSX.Element;
|
|
23
|
+
interface SwitchTriggerProps extends Omit<React.ComponentPropsWithoutRef<typeof Primitive.button>, keyof SwitchProviderProps> {
|
|
24
|
+
children?: React.ReactNode;
|
|
25
|
+
}
|
|
26
|
+
declare const SwitchTrigger: React.ForwardRefExoticComponent<SwitchTriggerProps & React.RefAttributes<HTMLButtonElement>>;
|
|
6
27
|
type PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;
|
|
7
|
-
interface SwitchProps extends PrimitiveButtonProps {
|
|
28
|
+
interface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {
|
|
8
29
|
checked?: boolean;
|
|
9
30
|
defaultChecked?: boolean;
|
|
10
31
|
required?: boolean;
|
|
@@ -15,7 +36,9 @@ type PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;
|
|
|
15
36
|
interface SwitchThumbProps extends PrimitiveSpanProps {
|
|
16
37
|
}
|
|
17
38
|
declare const SwitchThumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
|
|
18
|
-
|
|
19
|
-
|
|
39
|
+
type InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;
|
|
40
|
+
interface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {
|
|
41
|
+
}
|
|
42
|
+
declare const SwitchBubbleInput: React.ForwardRefExoticComponent<SwitchBubbleInputProps & React.RefAttributes<HTMLInputElement>>;
|
|
20
43
|
|
|
21
|
-
export { Root, Switch, type SwitchProps, SwitchThumb, type SwitchThumbProps, Thumb, createSwitchScope };
|
|
44
|
+
export { Switch as Root, Switch, type SwitchProps, SwitchThumb, type SwitchThumbProps, SwitchThumb as Thumb, createSwitchScope, SwitchBubbleInput as unstable_BubbleInput, SwitchProvider as unstable_Provider, SwitchBubbleInput as unstable_SwitchBubbleInput, type SwitchBubbleInputProps as unstable_SwitchBubbleInputProps, SwitchProvider as unstable_SwitchProvider, type SwitchProviderProps as unstable_SwitchProviderProps, SwitchTrigger as unstable_SwitchTrigger, type SwitchTriggerProps as unstable_SwitchTriggerProps, SwitchTrigger as unstable_Trigger };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
1
2
|
import * as _radix_ui_react_context from '@radix-ui/react-context';
|
|
3
|
+
import { Scope } from '@radix-ui/react-context';
|
|
2
4
|
import * as React from 'react';
|
|
3
5
|
import { Primitive } from '@radix-ui/react-primitive';
|
|
4
6
|
|
|
7
|
+
type ScopedProps<P> = P & {
|
|
8
|
+
__scopeSwitch?: Scope;
|
|
9
|
+
};
|
|
5
10
|
declare const createSwitchScope: _radix_ui_react_context.CreateScope;
|
|
11
|
+
interface SwitchProviderProps {
|
|
12
|
+
checked?: boolean;
|
|
13
|
+
defaultChecked?: boolean;
|
|
14
|
+
required?: boolean;
|
|
15
|
+
onCheckedChange?(checked: boolean): void;
|
|
16
|
+
name?: string;
|
|
17
|
+
form?: string;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
value?: string | number | readonly string[];
|
|
20
|
+
children?: React.ReactNode;
|
|
21
|
+
}
|
|
22
|
+
declare function SwitchProvider(props: ScopedProps<SwitchProviderProps>): react_jsx_runtime.JSX.Element;
|
|
23
|
+
interface SwitchTriggerProps extends Omit<React.ComponentPropsWithoutRef<typeof Primitive.button>, keyof SwitchProviderProps> {
|
|
24
|
+
children?: React.ReactNode;
|
|
25
|
+
}
|
|
26
|
+
declare const SwitchTrigger: React.ForwardRefExoticComponent<SwitchTriggerProps & React.RefAttributes<HTMLButtonElement>>;
|
|
6
27
|
type PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;
|
|
7
|
-
interface SwitchProps extends PrimitiveButtonProps {
|
|
28
|
+
interface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {
|
|
8
29
|
checked?: boolean;
|
|
9
30
|
defaultChecked?: boolean;
|
|
10
31
|
required?: boolean;
|
|
@@ -15,7 +36,9 @@ type PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;
|
|
|
15
36
|
interface SwitchThumbProps extends PrimitiveSpanProps {
|
|
16
37
|
}
|
|
17
38
|
declare const SwitchThumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
|
|
18
|
-
|
|
19
|
-
|
|
39
|
+
type InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;
|
|
40
|
+
interface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {
|
|
41
|
+
}
|
|
42
|
+
declare const SwitchBubbleInput: React.ForwardRefExoticComponent<SwitchBubbleInputProps & React.RefAttributes<HTMLInputElement>>;
|
|
20
43
|
|
|
21
|
-
export { Root, Switch, type SwitchProps, SwitchThumb, type SwitchThumbProps, Thumb, createSwitchScope };
|
|
44
|
+
export { Switch as Root, Switch, type SwitchProps, SwitchThumb, type SwitchThumbProps, SwitchThumb as Thumb, createSwitchScope, SwitchBubbleInput as unstable_BubbleInput, SwitchProvider as unstable_Provider, SwitchBubbleInput as unstable_SwitchBubbleInput, type SwitchBubbleInputProps as unstable_SwitchBubbleInputProps, SwitchProvider as unstable_SwitchProvider, type SwitchProviderProps as unstable_SwitchProviderProps, SwitchTrigger as unstable_SwitchTrigger, type SwitchTriggerProps as unstable_SwitchTriggerProps, SwitchTrigger as unstable_Trigger };
|
package/dist/index.js
CHANGED
|
@@ -31,11 +31,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
// src/index.ts
|
|
32
32
|
var index_exports = {};
|
|
33
33
|
__export(index_exports, {
|
|
34
|
-
Root: () =>
|
|
34
|
+
Root: () => Switch,
|
|
35
35
|
Switch: () => Switch,
|
|
36
36
|
SwitchThumb: () => SwitchThumb,
|
|
37
|
-
Thumb: () =>
|
|
38
|
-
createSwitchScope: () => createSwitchScope
|
|
37
|
+
Thumb: () => SwitchThumb,
|
|
38
|
+
createSwitchScope: () => createSwitchScope,
|
|
39
|
+
unstable_BubbleInput: () => SwitchBubbleInput,
|
|
40
|
+
unstable_Provider: () => SwitchProvider,
|
|
41
|
+
unstable_SwitchBubbleInput: () => SwitchBubbleInput,
|
|
42
|
+
unstable_SwitchProvider: () => SwitchProvider,
|
|
43
|
+
unstable_SwitchTrigger: () => SwitchTrigger,
|
|
44
|
+
unstable_Trigger: () => SwitchTrigger
|
|
39
45
|
});
|
|
40
46
|
module.exports = __toCommonJS(index_exports);
|
|
41
47
|
|
|
@@ -51,69 +57,137 @@ var import_react_primitive = require("@radix-ui/react-primitive");
|
|
|
51
57
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
52
58
|
var SWITCH_NAME = "Switch";
|
|
53
59
|
var [createSwitchContext, createSwitchScope] = (0, import_react_context.createContextScope)(SWITCH_NAME);
|
|
54
|
-
var [
|
|
60
|
+
var [SwitchProviderImpl, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
|
61
|
+
function SwitchProvider(props) {
|
|
62
|
+
const {
|
|
63
|
+
__scopeSwitch,
|
|
64
|
+
checked: checkedProp,
|
|
65
|
+
children,
|
|
66
|
+
defaultChecked,
|
|
67
|
+
disabled,
|
|
68
|
+
form,
|
|
69
|
+
name,
|
|
70
|
+
onCheckedChange,
|
|
71
|
+
required,
|
|
72
|
+
value = "on",
|
|
73
|
+
// @ts-expect-error
|
|
74
|
+
internal_do_not_use_render
|
|
75
|
+
} = props;
|
|
76
|
+
const [checked, setChecked] = (0, import_react_use_controllable_state.useControllableState)({
|
|
77
|
+
prop: checkedProp,
|
|
78
|
+
defaultProp: defaultChecked ?? false,
|
|
79
|
+
onChange: onCheckedChange,
|
|
80
|
+
caller: SWITCH_NAME
|
|
81
|
+
});
|
|
82
|
+
const [control, setControl] = React.useState(null);
|
|
83
|
+
const [bubbleInput, setBubbleInput] = React.useState(null);
|
|
84
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
85
|
+
const isFormControl = control ? !!form || !!control.closest("form") : (
|
|
86
|
+
// We set this to true by default so that events bubble to forms without JS (SSR)
|
|
87
|
+
true
|
|
88
|
+
);
|
|
89
|
+
const context = {
|
|
90
|
+
checked,
|
|
91
|
+
setChecked,
|
|
92
|
+
disabled,
|
|
93
|
+
control,
|
|
94
|
+
setControl,
|
|
95
|
+
name,
|
|
96
|
+
form,
|
|
97
|
+
value,
|
|
98
|
+
hasConsumerStoppedPropagationRef,
|
|
99
|
+
required,
|
|
100
|
+
defaultChecked,
|
|
101
|
+
isFormControl,
|
|
102
|
+
bubbleInput,
|
|
103
|
+
setBubbleInput
|
|
104
|
+
};
|
|
105
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SwitchProviderImpl, { scope: __scopeSwitch, ...context, children: isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children });
|
|
106
|
+
}
|
|
107
|
+
var TRIGGER_NAME = "SwitchTrigger";
|
|
108
|
+
var SwitchTrigger = React.forwardRef(
|
|
109
|
+
({ __scopeSwitch, onClick, ...switchProps }, forwardedRef) => {
|
|
110
|
+
const {
|
|
111
|
+
value,
|
|
112
|
+
disabled,
|
|
113
|
+
checked,
|
|
114
|
+
required,
|
|
115
|
+
setControl,
|
|
116
|
+
setChecked,
|
|
117
|
+
hasConsumerStoppedPropagationRef,
|
|
118
|
+
isFormControl,
|
|
119
|
+
bubbleInput
|
|
120
|
+
} = useSwitchContext(TRIGGER_NAME, __scopeSwitch);
|
|
121
|
+
const composedRefs = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, setControl);
|
|
122
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
123
|
+
import_react_primitive.Primitive.button,
|
|
124
|
+
{
|
|
125
|
+
type: "button",
|
|
126
|
+
role: "switch",
|
|
127
|
+
"aria-checked": checked,
|
|
128
|
+
"aria-required": required,
|
|
129
|
+
"data-state": getState(checked),
|
|
130
|
+
"data-disabled": disabled ? "" : void 0,
|
|
131
|
+
disabled,
|
|
132
|
+
value,
|
|
133
|
+
...switchProps,
|
|
134
|
+
ref: composedRefs,
|
|
135
|
+
onClick: (0, import_primitive.composeEventHandlers)(onClick, (event) => {
|
|
136
|
+
setChecked((prevChecked) => !prevChecked);
|
|
137
|
+
if (bubbleInput && isFormControl) {
|
|
138
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
139
|
+
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
SwitchTrigger.displayName = TRIGGER_NAME;
|
|
55
147
|
var Switch = React.forwardRef(
|
|
56
148
|
(props, forwardedRef) => {
|
|
57
149
|
const {
|
|
58
150
|
__scopeSwitch,
|
|
59
151
|
name,
|
|
60
|
-
checked
|
|
152
|
+
checked,
|
|
61
153
|
defaultChecked,
|
|
62
154
|
required,
|
|
63
155
|
disabled,
|
|
64
|
-
value
|
|
156
|
+
value,
|
|
65
157
|
onCheckedChange,
|
|
66
158
|
form,
|
|
67
159
|
...switchProps
|
|
68
160
|
} = props;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"data-disabled": disabled ? "" : void 0,
|
|
89
|
-
disabled,
|
|
90
|
-
value,
|
|
91
|
-
...switchProps,
|
|
92
|
-
ref: composedRefs,
|
|
93
|
-
onClick: (0, import_primitive.composeEventHandlers)(props.onClick, (event) => {
|
|
94
|
-
setChecked((prevChecked) => !prevChecked);
|
|
95
|
-
if (isFormControl) {
|
|
96
|
-
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
97
|
-
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
|
161
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
162
|
+
SwitchProvider,
|
|
163
|
+
{
|
|
164
|
+
__scopeSwitch,
|
|
165
|
+
checked,
|
|
166
|
+
defaultChecked,
|
|
167
|
+
disabled,
|
|
168
|
+
required,
|
|
169
|
+
onCheckedChange,
|
|
170
|
+
name,
|
|
171
|
+
form,
|
|
172
|
+
value,
|
|
173
|
+
internal_do_not_use_render: ({ isFormControl }) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
174
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
175
|
+
SwitchTrigger,
|
|
176
|
+
{
|
|
177
|
+
...switchProps,
|
|
178
|
+
ref: forwardedRef,
|
|
179
|
+
__scopeSwitch
|
|
98
180
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
checked,
|
|
110
|
-
required,
|
|
111
|
-
disabled,
|
|
112
|
-
form,
|
|
113
|
-
style: { transform: "translateX(-100%)" }
|
|
114
|
-
}
|
|
115
|
-
)
|
|
116
|
-
] });
|
|
181
|
+
),
|
|
182
|
+
isFormControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
183
|
+
SwitchBubbleInput,
|
|
184
|
+
{
|
|
185
|
+
__scopeSwitch
|
|
186
|
+
}
|
|
187
|
+
)
|
|
188
|
+
] })
|
|
189
|
+
}
|
|
190
|
+
);
|
|
117
191
|
}
|
|
118
192
|
);
|
|
119
193
|
Switch.displayName = SWITCH_NAME;
|
|
@@ -136,19 +210,25 @@ var SwitchThumb = React.forwardRef(
|
|
|
136
210
|
SwitchThumb.displayName = THUMB_NAME;
|
|
137
211
|
var BUBBLE_INPUT_NAME = "SwitchBubbleInput";
|
|
138
212
|
var SwitchBubbleInput = React.forwardRef(
|
|
139
|
-
({
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
213
|
+
({ __scopeSwitch, ...props }, forwardedRef) => {
|
|
214
|
+
const {
|
|
215
|
+
control,
|
|
216
|
+
hasConsumerStoppedPropagationRef,
|
|
217
|
+
checked,
|
|
218
|
+
defaultChecked,
|
|
219
|
+
required,
|
|
220
|
+
disabled,
|
|
221
|
+
name,
|
|
222
|
+
value,
|
|
223
|
+
form,
|
|
224
|
+
bubbleInput,
|
|
225
|
+
setBubbleInput
|
|
226
|
+
} = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);
|
|
227
|
+
const composedRefs = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, setBubbleInput);
|
|
148
228
|
const prevChecked = (0, import_react_use_previous.usePrevious)(checked);
|
|
149
229
|
const controlSize = (0, import_react_use_size.useSize)(control);
|
|
150
230
|
React.useEffect(() => {
|
|
151
|
-
const input =
|
|
231
|
+
const input = bubbleInput;
|
|
152
232
|
if (!input) return;
|
|
153
233
|
const inputProto = window.HTMLInputElement.prototype;
|
|
154
234
|
const descriptor = Object.getOwnPropertyDescriptor(
|
|
@@ -156,18 +236,25 @@ var SwitchBubbleInput = React.forwardRef(
|
|
|
156
236
|
"checked"
|
|
157
237
|
);
|
|
158
238
|
const setChecked = descriptor.set;
|
|
239
|
+
const bubbles = !hasConsumerStoppedPropagationRef.current;
|
|
159
240
|
if (prevChecked !== checked && setChecked) {
|
|
160
241
|
const event = new Event("click", { bubbles });
|
|
161
242
|
setChecked.call(input, checked);
|
|
162
243
|
input.dispatchEvent(event);
|
|
163
244
|
}
|
|
164
|
-
}, [prevChecked, checked,
|
|
245
|
+
}, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);
|
|
246
|
+
const defaultCheckedRef = React.useRef(checked);
|
|
165
247
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
166
|
-
|
|
248
|
+
import_react_primitive.Primitive.input,
|
|
167
249
|
{
|
|
168
250
|
type: "checkbox",
|
|
169
251
|
"aria-hidden": true,
|
|
170
|
-
defaultChecked:
|
|
252
|
+
defaultChecked: defaultChecked ?? defaultCheckedRef.current,
|
|
253
|
+
required,
|
|
254
|
+
disabled,
|
|
255
|
+
name,
|
|
256
|
+
value,
|
|
257
|
+
form,
|
|
171
258
|
...props,
|
|
172
259
|
tabIndex: -1,
|
|
173
260
|
ref: composedRefs,
|
|
@@ -177,16 +264,21 @@ var SwitchBubbleInput = React.forwardRef(
|
|
|
177
264
|
position: "absolute",
|
|
178
265
|
pointerEvents: "none",
|
|
179
266
|
opacity: 0,
|
|
180
|
-
margin: 0
|
|
267
|
+
margin: 0,
|
|
268
|
+
// We transform because the input is absolutely positioned but we have
|
|
269
|
+
// rendered it **after** the button. This pulls it back to sit on top
|
|
270
|
+
// of the button.
|
|
271
|
+
transform: "translateX(-100%)"
|
|
181
272
|
}
|
|
182
273
|
}
|
|
183
274
|
);
|
|
184
275
|
}
|
|
185
276
|
);
|
|
186
277
|
SwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;
|
|
278
|
+
function isFunction(value) {
|
|
279
|
+
return typeof value === "function";
|
|
280
|
+
}
|
|
187
281
|
function getState(checked) {
|
|
188
282
|
return checked ? "checked" : "unchecked";
|
|
189
283
|
}
|
|
190
|
-
var Root = Switch;
|
|
191
|
-
var Thumb = SwitchThumb;
|
|
192
284
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts", "../src/switch.tsx"],
|
|
4
|
-
"sourcesContent": ["'use client';\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n} from './switch';\nexport type { SwitchProps, SwitchThumbProps } from './switch';\n", "import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = { checked: boolean; disabled?: boolean };\nconst [SwitchProvider, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\ntype SwitchElement = React.ComponentRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends PrimitiveButtonProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = React.forwardRef<SwitchElement, SwitchProps>(\n (props: ScopedProps<SwitchProps>, forwardedRef) => {\n const {\n __scopeSwitch,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n form,\n ...switchProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = button ? form || !!button.closest('form') : true;\n const [checked, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked ?? false,\n onChange: onCheckedChange,\n caller: SWITCH_NAME,\n });\n\n return (\n <SwitchProvider scope={__scopeSwitch} checked={checked} disabled={disabled}>\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(props.onClick, (event) => {\n setChecked((prevChecked) => !prevChecked);\n if (isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect switch updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n {isFormControl && (\n <SwitchBubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n form={form}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </SwitchProvider>\n );\n },\n);\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ComponentRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME, __scopeSwitch);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n ref={forwardedRef}\n />\n );\n },\n);\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchBubbleInput\n * -----------------------------------------------------------------------------------------------*/\n\nconst BUBBLE_INPUT_NAME = 'SwitchBubbleInput';\n\ntype InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;\ninterface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst SwitchBubbleInput = React.forwardRef<HTMLInputElement, SwitchBubbleInputProps>(\n (\n {\n __scopeSwitch,\n control,\n checked,\n bubbles = true,\n ...props\n }: ScopedProps<SwitchBubbleInputProps>,\n forwardedRef,\n ) => {\n const ref = React.useRef<HTMLInputElement>(null);\n const composedRefs = useComposedRefs(ref, forwardedRef);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current;\n if (!input) return;\n\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked',\n ) as PropertyDescriptor;\n const setChecked = descriptor.set;\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n }\n }, [prevChecked, checked, bubbles]);\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={checked}\n {...props}\n tabIndex={-1}\n ref={composedRefs}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n );\n },\n);\n\nSwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nconst Root = Switch;\nconst Thumb = SwitchThumb;\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n};\nexport type { SwitchProps, SwitchThumbProps };\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,uBAAqC;AACrC,gCAAgC;AAChC,2BAAmC;AACnC,0CAAqC;AACrC,gCAA4B;AAC5B,4BAAwB;AACxB,6BAA0B;
|
|
4
|
+
"sourcesContent": ["'use client';\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchProvider as unstable_SwitchProvider,\n SwitchTrigger as unstable_SwitchTrigger,\n SwitchThumb,\n SwitchBubbleInput as unstable_SwitchBubbleInput,\n //\n Root,\n Provider as unstable_Provider,\n Trigger as unstable_Trigger,\n Thumb,\n BubbleInput as unstable_BubbleInput,\n} from './switch';\nexport type {\n SwitchProps,\n SwitchProviderProps as unstable_SwitchProviderProps,\n SwitchTriggerProps as unstable_SwitchTriggerProps,\n SwitchThumbProps,\n SwitchBubbleInputProps as unstable_SwitchBubbleInputProps,\n} from './switch';\n", "import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = {\n checked: boolean;\n setChecked: React.Dispatch<React.SetStateAction<boolean>>;\n disabled: boolean | undefined;\n control: HTMLButtonElement | null;\n setControl: React.Dispatch<React.SetStateAction<HTMLButtonElement | null>>;\n name: string | undefined;\n form: string | undefined;\n value: string | number | readonly string[];\n hasConsumerStoppedPropagationRef: React.RefObject<boolean>;\n required: boolean | undefined;\n defaultChecked: boolean | undefined;\n isFormControl: boolean;\n bubbleInput: HTMLInputElement | null;\n setBubbleInput: React.Dispatch<React.SetStateAction<HTMLInputElement | null>>;\n};\n\nconst [SwitchProviderImpl, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchProvider\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SwitchProviderProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n name?: string;\n form?: string;\n disabled?: boolean;\n value?: string | number | readonly string[];\n children?: React.ReactNode;\n}\n\nfunction SwitchProvider(props: ScopedProps<SwitchProviderProps>) {\n const {\n __scopeSwitch,\n checked: checkedProp,\n children,\n defaultChecked,\n disabled,\n form,\n name,\n onCheckedChange,\n required,\n value = 'on',\n // @ts-expect-error\n internal_do_not_use_render,\n } = props;\n\n const [checked, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked ?? false,\n onChange: onCheckedChange,\n caller: SWITCH_NAME,\n });\n const [control, setControl] = React.useState<HTMLButtonElement | null>(null);\n const [bubbleInput, setBubbleInput] = React.useState<HTMLInputElement | null>(null);\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n const isFormControl = control\n ? !!form || !!control.closest('form')\n : // We set this to true by default so that events bubble to forms without JS (SSR)\n true;\n\n const context: SwitchContextValue = {\n checked,\n setChecked,\n disabled,\n control,\n setControl,\n name,\n form,\n value,\n hasConsumerStoppedPropagationRef,\n required,\n defaultChecked,\n isFormControl,\n bubbleInput,\n setBubbleInput,\n };\n\n return (\n <SwitchProviderImpl scope={__scopeSwitch} {...context}>\n {isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children}\n </SwitchProviderImpl>\n );\n}\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchTrigger\n * -----------------------------------------------------------------------------------------------*/\n\nconst TRIGGER_NAME = 'SwitchTrigger';\n\ninterface SwitchTriggerProps extends Omit<\n React.ComponentPropsWithoutRef<typeof Primitive.button>,\n keyof SwitchProviderProps\n> {\n children?: React.ReactNode;\n}\n\nconst SwitchTrigger = React.forwardRef<HTMLButtonElement, SwitchTriggerProps>(\n ({ __scopeSwitch, onClick, ...switchProps }: ScopedProps<SwitchTriggerProps>, forwardedRef) => {\n const {\n value,\n disabled,\n checked,\n required,\n setControl,\n setChecked,\n hasConsumerStoppedPropagationRef,\n isFormControl,\n bubbleInput,\n } = useSwitchContext(TRIGGER_NAME, __scopeSwitch);\n const composedRefs = useComposedRefs(forwardedRef, setControl);\n\n return (\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(onClick, (event) => {\n setChecked((prevChecked) => !prevChecked);\n if (bubbleInput && isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch has a bubble input and is a form control, stop\n // propagation from the button so that we only propagate one click\n // event (from the input). We propagate changes from an input so\n // that native form validation works and form events reflect switch\n // updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n );\n },\n);\n\nSwitchTrigger.displayName = TRIGGER_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\ntype SwitchElement = React.ComponentRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = React.forwardRef<SwitchElement, SwitchProps>(\n (props: ScopedProps<SwitchProps>, forwardedRef) => {\n const {\n __scopeSwitch,\n name,\n checked,\n defaultChecked,\n required,\n disabled,\n value,\n onCheckedChange,\n form,\n ...switchProps\n } = props;\n\n return (\n <SwitchProvider\n __scopeSwitch={__scopeSwitch}\n checked={checked}\n defaultChecked={defaultChecked}\n disabled={disabled}\n required={required}\n onCheckedChange={onCheckedChange}\n name={name}\n form={form}\n value={value}\n // @ts-expect-error\n internal_do_not_use_render={({ isFormControl }: SwitchContextValue) => (\n <>\n <SwitchTrigger\n {...switchProps}\n ref={forwardedRef}\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n {isFormControl && (\n <SwitchBubbleInput\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n )}\n </>\n )}\n />\n );\n },\n);\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ComponentRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME, __scopeSwitch);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n ref={forwardedRef}\n />\n );\n },\n);\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchBubbleInput\n * -----------------------------------------------------------------------------------------------*/\n\nconst BUBBLE_INPUT_NAME = 'SwitchBubbleInput';\n\ntype InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;\ninterface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {}\n\nconst SwitchBubbleInput = React.forwardRef<HTMLInputElement, SwitchBubbleInputProps>(\n ({ __scopeSwitch, ...props }: ScopedProps<SwitchBubbleInputProps>, forwardedRef) => {\n const {\n control,\n hasConsumerStoppedPropagationRef,\n checked,\n defaultChecked,\n required,\n disabled,\n name,\n value,\n form,\n bubbleInput,\n setBubbleInput,\n } = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);\n\n const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = bubbleInput;\n if (!input) return;\n\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked',\n ) as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n const bubbles = !hasConsumerStoppedPropagationRef.current;\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n }\n }, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);\n\n const defaultCheckedRef = React.useRef(checked);\n return (\n <Primitive.input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={defaultChecked ?? defaultCheckedRef.current}\n required={required}\n disabled={disabled}\n name={name}\n value={value}\n form={form}\n {...props}\n tabIndex={-1}\n ref={composedRefs}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n transform: 'translateX(-100%)',\n }}\n />\n );\n },\n);\n\nSwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction isFunction(value: unknown): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchProvider,\n SwitchTrigger,\n SwitchThumb,\n SwitchBubbleInput,\n //\n Switch as Root,\n SwitchProvider as Provider,\n SwitchTrigger as Trigger,\n SwitchThumb as Thumb,\n SwitchBubbleInput as BubbleInput,\n};\nexport type {\n SwitchProps,\n SwitchProviderProps,\n SwitchTriggerProps,\n SwitchThumbProps,\n SwitchBubbleInputProps,\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,uBAAq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,OAAO;AAC9C,WACE;AAAA,MAAC,iCAAU;AAAA,MAAV;AAAA,QACC,MAAK;AAAA,QACL,eAAW;AAAA,QACX,gBAAgB,kBAAkB,kBAAkB;AAAA,QACpD;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACC,GAAG;AAAA,QACJ,UAAU;AAAA,QACV,KAAK;AAAA,QACL,OAAO;AAAA,UACL,GAAG,MAAM;AAAA,UACT,GAAG;AAAA,UACH,UAAU;AAAA,UACV,eAAe;AAAA,UACf,SAAS;AAAA,UACT,QAAQ;AAAA;AAAA;AAAA;AAAA,UAIR,WAAW;AAAA,QACb;AAAA;AAAA,IACF;AAAA,EAEJ;AACF;AAEA,kBAAkB,cAAc;AAIhC,SAAS,WAAW,OAAkD;AACpE,SAAO,OAAO,UAAU;AAC1B;AAEA,SAAS,SAAS,SAAkB;AAClC,SAAO,UAAU,YAAY;AAC/B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -9,72 +9,140 @@ import { useControllableState } from "@radix-ui/react-use-controllable-state";
|
|
|
9
9
|
import { usePrevious } from "@radix-ui/react-use-previous";
|
|
10
10
|
import { useSize } from "@radix-ui/react-use-size";
|
|
11
11
|
import { Primitive } from "@radix-ui/react-primitive";
|
|
12
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
12
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
13
13
|
var SWITCH_NAME = "Switch";
|
|
14
14
|
var [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);
|
|
15
|
-
var [
|
|
15
|
+
var [SwitchProviderImpl, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
|
16
|
+
function SwitchProvider(props) {
|
|
17
|
+
const {
|
|
18
|
+
__scopeSwitch,
|
|
19
|
+
checked: checkedProp,
|
|
20
|
+
children,
|
|
21
|
+
defaultChecked,
|
|
22
|
+
disabled,
|
|
23
|
+
form,
|
|
24
|
+
name,
|
|
25
|
+
onCheckedChange,
|
|
26
|
+
required,
|
|
27
|
+
value = "on",
|
|
28
|
+
// @ts-expect-error
|
|
29
|
+
internal_do_not_use_render
|
|
30
|
+
} = props;
|
|
31
|
+
const [checked, setChecked] = useControllableState({
|
|
32
|
+
prop: checkedProp,
|
|
33
|
+
defaultProp: defaultChecked ?? false,
|
|
34
|
+
onChange: onCheckedChange,
|
|
35
|
+
caller: SWITCH_NAME
|
|
36
|
+
});
|
|
37
|
+
const [control, setControl] = React.useState(null);
|
|
38
|
+
const [bubbleInput, setBubbleInput] = React.useState(null);
|
|
39
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
40
|
+
const isFormControl = control ? !!form || !!control.closest("form") : (
|
|
41
|
+
// We set this to true by default so that events bubble to forms without JS (SSR)
|
|
42
|
+
true
|
|
43
|
+
);
|
|
44
|
+
const context = {
|
|
45
|
+
checked,
|
|
46
|
+
setChecked,
|
|
47
|
+
disabled,
|
|
48
|
+
control,
|
|
49
|
+
setControl,
|
|
50
|
+
name,
|
|
51
|
+
form,
|
|
52
|
+
value,
|
|
53
|
+
hasConsumerStoppedPropagationRef,
|
|
54
|
+
required,
|
|
55
|
+
defaultChecked,
|
|
56
|
+
isFormControl,
|
|
57
|
+
bubbleInput,
|
|
58
|
+
setBubbleInput
|
|
59
|
+
};
|
|
60
|
+
return /* @__PURE__ */ jsx(SwitchProviderImpl, { scope: __scopeSwitch, ...context, children: isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children });
|
|
61
|
+
}
|
|
62
|
+
var TRIGGER_NAME = "SwitchTrigger";
|
|
63
|
+
var SwitchTrigger = React.forwardRef(
|
|
64
|
+
({ __scopeSwitch, onClick, ...switchProps }, forwardedRef) => {
|
|
65
|
+
const {
|
|
66
|
+
value,
|
|
67
|
+
disabled,
|
|
68
|
+
checked,
|
|
69
|
+
required,
|
|
70
|
+
setControl,
|
|
71
|
+
setChecked,
|
|
72
|
+
hasConsumerStoppedPropagationRef,
|
|
73
|
+
isFormControl,
|
|
74
|
+
bubbleInput
|
|
75
|
+
} = useSwitchContext(TRIGGER_NAME, __scopeSwitch);
|
|
76
|
+
const composedRefs = useComposedRefs(forwardedRef, setControl);
|
|
77
|
+
return /* @__PURE__ */ jsx(
|
|
78
|
+
Primitive.button,
|
|
79
|
+
{
|
|
80
|
+
type: "button",
|
|
81
|
+
role: "switch",
|
|
82
|
+
"aria-checked": checked,
|
|
83
|
+
"aria-required": required,
|
|
84
|
+
"data-state": getState(checked),
|
|
85
|
+
"data-disabled": disabled ? "" : void 0,
|
|
86
|
+
disabled,
|
|
87
|
+
value,
|
|
88
|
+
...switchProps,
|
|
89
|
+
ref: composedRefs,
|
|
90
|
+
onClick: composeEventHandlers(onClick, (event) => {
|
|
91
|
+
setChecked((prevChecked) => !prevChecked);
|
|
92
|
+
if (bubbleInput && isFormControl) {
|
|
93
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
94
|
+
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
SwitchTrigger.displayName = TRIGGER_NAME;
|
|
16
102
|
var Switch = React.forwardRef(
|
|
17
103
|
(props, forwardedRef) => {
|
|
18
104
|
const {
|
|
19
105
|
__scopeSwitch,
|
|
20
106
|
name,
|
|
21
|
-
checked
|
|
107
|
+
checked,
|
|
22
108
|
defaultChecked,
|
|
23
109
|
required,
|
|
24
110
|
disabled,
|
|
25
|
-
value
|
|
111
|
+
value,
|
|
26
112
|
onCheckedChange,
|
|
27
113
|
form,
|
|
28
114
|
...switchProps
|
|
29
115
|
} = props;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"data-disabled": disabled ? "" : void 0,
|
|
50
|
-
disabled,
|
|
51
|
-
value,
|
|
52
|
-
...switchProps,
|
|
53
|
-
ref: composedRefs,
|
|
54
|
-
onClick: composeEventHandlers(props.onClick, (event) => {
|
|
55
|
-
setChecked((prevChecked) => !prevChecked);
|
|
56
|
-
if (isFormControl) {
|
|
57
|
-
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
58
|
-
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
|
116
|
+
return /* @__PURE__ */ jsx(
|
|
117
|
+
SwitchProvider,
|
|
118
|
+
{
|
|
119
|
+
__scopeSwitch,
|
|
120
|
+
checked,
|
|
121
|
+
defaultChecked,
|
|
122
|
+
disabled,
|
|
123
|
+
required,
|
|
124
|
+
onCheckedChange,
|
|
125
|
+
name,
|
|
126
|
+
form,
|
|
127
|
+
value,
|
|
128
|
+
internal_do_not_use_render: ({ isFormControl }) => /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
129
|
+
/* @__PURE__ */ jsx(
|
|
130
|
+
SwitchTrigger,
|
|
131
|
+
{
|
|
132
|
+
...switchProps,
|
|
133
|
+
ref: forwardedRef,
|
|
134
|
+
__scopeSwitch
|
|
59
135
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
checked,
|
|
71
|
-
required,
|
|
72
|
-
disabled,
|
|
73
|
-
form,
|
|
74
|
-
style: { transform: "translateX(-100%)" }
|
|
75
|
-
}
|
|
76
|
-
)
|
|
77
|
-
] });
|
|
136
|
+
),
|
|
137
|
+
isFormControl && /* @__PURE__ */ jsx(
|
|
138
|
+
SwitchBubbleInput,
|
|
139
|
+
{
|
|
140
|
+
__scopeSwitch
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
] })
|
|
144
|
+
}
|
|
145
|
+
);
|
|
78
146
|
}
|
|
79
147
|
);
|
|
80
148
|
Switch.displayName = SWITCH_NAME;
|
|
@@ -97,19 +165,25 @@ var SwitchThumb = React.forwardRef(
|
|
|
97
165
|
SwitchThumb.displayName = THUMB_NAME;
|
|
98
166
|
var BUBBLE_INPUT_NAME = "SwitchBubbleInput";
|
|
99
167
|
var SwitchBubbleInput = React.forwardRef(
|
|
100
|
-
({
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
168
|
+
({ __scopeSwitch, ...props }, forwardedRef) => {
|
|
169
|
+
const {
|
|
170
|
+
control,
|
|
171
|
+
hasConsumerStoppedPropagationRef,
|
|
172
|
+
checked,
|
|
173
|
+
defaultChecked,
|
|
174
|
+
required,
|
|
175
|
+
disabled,
|
|
176
|
+
name,
|
|
177
|
+
value,
|
|
178
|
+
form,
|
|
179
|
+
bubbleInput,
|
|
180
|
+
setBubbleInput
|
|
181
|
+
} = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);
|
|
182
|
+
const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);
|
|
109
183
|
const prevChecked = usePrevious(checked);
|
|
110
184
|
const controlSize = useSize(control);
|
|
111
185
|
React.useEffect(() => {
|
|
112
|
-
const input =
|
|
186
|
+
const input = bubbleInput;
|
|
113
187
|
if (!input) return;
|
|
114
188
|
const inputProto = window.HTMLInputElement.prototype;
|
|
115
189
|
const descriptor = Object.getOwnPropertyDescriptor(
|
|
@@ -117,18 +191,25 @@ var SwitchBubbleInput = React.forwardRef(
|
|
|
117
191
|
"checked"
|
|
118
192
|
);
|
|
119
193
|
const setChecked = descriptor.set;
|
|
194
|
+
const bubbles = !hasConsumerStoppedPropagationRef.current;
|
|
120
195
|
if (prevChecked !== checked && setChecked) {
|
|
121
196
|
const event = new Event("click", { bubbles });
|
|
122
197
|
setChecked.call(input, checked);
|
|
123
198
|
input.dispatchEvent(event);
|
|
124
199
|
}
|
|
125
|
-
}, [prevChecked, checked,
|
|
200
|
+
}, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);
|
|
201
|
+
const defaultCheckedRef = React.useRef(checked);
|
|
126
202
|
return /* @__PURE__ */ jsx(
|
|
127
|
-
|
|
203
|
+
Primitive.input,
|
|
128
204
|
{
|
|
129
205
|
type: "checkbox",
|
|
130
206
|
"aria-hidden": true,
|
|
131
|
-
defaultChecked:
|
|
207
|
+
defaultChecked: defaultChecked ?? defaultCheckedRef.current,
|
|
208
|
+
required,
|
|
209
|
+
disabled,
|
|
210
|
+
name,
|
|
211
|
+
value,
|
|
212
|
+
form,
|
|
132
213
|
...props,
|
|
133
214
|
tabIndex: -1,
|
|
134
215
|
ref: composedRefs,
|
|
@@ -138,23 +219,34 @@ var SwitchBubbleInput = React.forwardRef(
|
|
|
138
219
|
position: "absolute",
|
|
139
220
|
pointerEvents: "none",
|
|
140
221
|
opacity: 0,
|
|
141
|
-
margin: 0
|
|
222
|
+
margin: 0,
|
|
223
|
+
// We transform because the input is absolutely positioned but we have
|
|
224
|
+
// rendered it **after** the button. This pulls it back to sit on top
|
|
225
|
+
// of the button.
|
|
226
|
+
transform: "translateX(-100%)"
|
|
142
227
|
}
|
|
143
228
|
}
|
|
144
229
|
);
|
|
145
230
|
}
|
|
146
231
|
);
|
|
147
232
|
SwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;
|
|
233
|
+
function isFunction(value) {
|
|
234
|
+
return typeof value === "function";
|
|
235
|
+
}
|
|
148
236
|
function getState(checked) {
|
|
149
237
|
return checked ? "checked" : "unchecked";
|
|
150
238
|
}
|
|
151
|
-
var Root = Switch;
|
|
152
|
-
var Thumb = SwitchThumb;
|
|
153
239
|
export {
|
|
154
|
-
Root,
|
|
240
|
+
Switch as Root,
|
|
155
241
|
Switch,
|
|
156
242
|
SwitchThumb,
|
|
157
|
-
Thumb,
|
|
158
|
-
createSwitchScope
|
|
243
|
+
SwitchThumb as Thumb,
|
|
244
|
+
createSwitchScope,
|
|
245
|
+
SwitchBubbleInput as unstable_BubbleInput,
|
|
246
|
+
SwitchProvider as unstable_Provider,
|
|
247
|
+
SwitchBubbleInput as unstable_SwitchBubbleInput,
|
|
248
|
+
SwitchProvider as unstable_SwitchProvider,
|
|
249
|
+
SwitchTrigger as unstable_SwitchTrigger,
|
|
250
|
+
SwitchTrigger as unstable_Trigger
|
|
159
251
|
};
|
|
160
252
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/switch.tsx"],
|
|
4
|
-
"sourcesContent": ["import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = { checked: boolean; disabled?: boolean };\nconst [SwitchProvider, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\ntype SwitchElement = React.ComponentRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends PrimitiveButtonProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = React.forwardRef<SwitchElement, SwitchProps>(\n (props: ScopedProps<SwitchProps>, forwardedRef) => {\n const {\n __scopeSwitch,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n form,\n ...switchProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = button ? form || !!button.closest('form') : true;\n const [checked, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked ?? false,\n onChange: onCheckedChange,\n caller: SWITCH_NAME,\n });\n\n return (\n <SwitchProvider scope={__scopeSwitch} checked={checked} disabled={disabled}>\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(props.onClick, (event) => {\n setChecked((prevChecked) => !prevChecked);\n if (isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect switch updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n {isFormControl && (\n <SwitchBubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n form={form}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </SwitchProvider>\n );\n },\n);\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ComponentRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME, __scopeSwitch);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n ref={forwardedRef}\n />\n );\n },\n);\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchBubbleInput\n * -----------------------------------------------------------------------------------------------*/\n\nconst BUBBLE_INPUT_NAME = 'SwitchBubbleInput';\n\ntype InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;\ninterface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst SwitchBubbleInput = React.forwardRef<HTMLInputElement, SwitchBubbleInputProps>(\n (\n {\n __scopeSwitch,\n control,\n checked,\n bubbles = true,\n ...props\n }: ScopedProps<SwitchBubbleInputProps>,\n forwardedRef,\n ) => {\n const ref = React.useRef<HTMLInputElement>(null);\n const composedRefs = useComposedRefs(ref, forwardedRef);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current;\n if (!input) return;\n\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked',\n ) as PropertyDescriptor;\n const setChecked = descriptor.set;\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n }\n }, [prevChecked, checked, bubbles]);\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={checked}\n {...props}\n tabIndex={-1}\n ref={composedRefs}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n );\n },\n);\n\nSwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nconst Root = Switch;\nconst Thumb = SwitchThumb;\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n};\nexport type { SwitchProps, SwitchThumbProps };\n"],
|
|
5
|
-
"mappings": ";;;AAAA,YAAY,WAAW;AACvB,SAAS,4BAA4B;AACrC,SAAS,uBAAuB;AAChC,SAAS,0BAA0B;AACnC,SAAS,4BAA4B;AACrC,SAAS,mBAAmB;AAC5B,SAAS,eAAe;AACxB,SAAS,iBAAiB;
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = {\n checked: boolean;\n setChecked: React.Dispatch<React.SetStateAction<boolean>>;\n disabled: boolean | undefined;\n control: HTMLButtonElement | null;\n setControl: React.Dispatch<React.SetStateAction<HTMLButtonElement | null>>;\n name: string | undefined;\n form: string | undefined;\n value: string | number | readonly string[];\n hasConsumerStoppedPropagationRef: React.RefObject<boolean>;\n required: boolean | undefined;\n defaultChecked: boolean | undefined;\n isFormControl: boolean;\n bubbleInput: HTMLInputElement | null;\n setBubbleInput: React.Dispatch<React.SetStateAction<HTMLInputElement | null>>;\n};\n\nconst [SwitchProviderImpl, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchProvider\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SwitchProviderProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n name?: string;\n form?: string;\n disabled?: boolean;\n value?: string | number | readonly string[];\n children?: React.ReactNode;\n}\n\nfunction SwitchProvider(props: ScopedProps<SwitchProviderProps>) {\n const {\n __scopeSwitch,\n checked: checkedProp,\n children,\n defaultChecked,\n disabled,\n form,\n name,\n onCheckedChange,\n required,\n value = 'on',\n // @ts-expect-error\n internal_do_not_use_render,\n } = props;\n\n const [checked, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked ?? false,\n onChange: onCheckedChange,\n caller: SWITCH_NAME,\n });\n const [control, setControl] = React.useState<HTMLButtonElement | null>(null);\n const [bubbleInput, setBubbleInput] = React.useState<HTMLInputElement | null>(null);\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n const isFormControl = control\n ? !!form || !!control.closest('form')\n : // We set this to true by default so that events bubble to forms without JS (SSR)\n true;\n\n const context: SwitchContextValue = {\n checked,\n setChecked,\n disabled,\n control,\n setControl,\n name,\n form,\n value,\n hasConsumerStoppedPropagationRef,\n required,\n defaultChecked,\n isFormControl,\n bubbleInput,\n setBubbleInput,\n };\n\n return (\n <SwitchProviderImpl scope={__scopeSwitch} {...context}>\n {isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children}\n </SwitchProviderImpl>\n );\n}\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchTrigger\n * -----------------------------------------------------------------------------------------------*/\n\nconst TRIGGER_NAME = 'SwitchTrigger';\n\ninterface SwitchTriggerProps extends Omit<\n React.ComponentPropsWithoutRef<typeof Primitive.button>,\n keyof SwitchProviderProps\n> {\n children?: React.ReactNode;\n}\n\nconst SwitchTrigger = React.forwardRef<HTMLButtonElement, SwitchTriggerProps>(\n ({ __scopeSwitch, onClick, ...switchProps }: ScopedProps<SwitchTriggerProps>, forwardedRef) => {\n const {\n value,\n disabled,\n checked,\n required,\n setControl,\n setChecked,\n hasConsumerStoppedPropagationRef,\n isFormControl,\n bubbleInput,\n } = useSwitchContext(TRIGGER_NAME, __scopeSwitch);\n const composedRefs = useComposedRefs(forwardedRef, setControl);\n\n return (\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(onClick, (event) => {\n setChecked((prevChecked) => !prevChecked);\n if (bubbleInput && isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch has a bubble input and is a form control, stop\n // propagation from the button so that we only propagate one click\n // event (from the input). We propagate changes from an input so\n // that native form validation works and form events reflect switch\n // updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n );\n },\n);\n\nSwitchTrigger.displayName = TRIGGER_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\ntype SwitchElement = React.ComponentRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = React.forwardRef<SwitchElement, SwitchProps>(\n (props: ScopedProps<SwitchProps>, forwardedRef) => {\n const {\n __scopeSwitch,\n name,\n checked,\n defaultChecked,\n required,\n disabled,\n value,\n onCheckedChange,\n form,\n ...switchProps\n } = props;\n\n return (\n <SwitchProvider\n __scopeSwitch={__scopeSwitch}\n checked={checked}\n defaultChecked={defaultChecked}\n disabled={disabled}\n required={required}\n onCheckedChange={onCheckedChange}\n name={name}\n form={form}\n value={value}\n // @ts-expect-error\n internal_do_not_use_render={({ isFormControl }: SwitchContextValue) => (\n <>\n <SwitchTrigger\n {...switchProps}\n ref={forwardedRef}\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n {isFormControl && (\n <SwitchBubbleInput\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n )}\n </>\n )}\n />\n );\n },\n);\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ComponentRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME, __scopeSwitch);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n ref={forwardedRef}\n />\n );\n },\n);\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchBubbleInput\n * -----------------------------------------------------------------------------------------------*/\n\nconst BUBBLE_INPUT_NAME = 'SwitchBubbleInput';\n\ntype InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;\ninterface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {}\n\nconst SwitchBubbleInput = React.forwardRef<HTMLInputElement, SwitchBubbleInputProps>(\n ({ __scopeSwitch, ...props }: ScopedProps<SwitchBubbleInputProps>, forwardedRef) => {\n const {\n control,\n hasConsumerStoppedPropagationRef,\n checked,\n defaultChecked,\n required,\n disabled,\n name,\n value,\n form,\n bubbleInput,\n setBubbleInput,\n } = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);\n\n const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = bubbleInput;\n if (!input) return;\n\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked',\n ) as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n const bubbles = !hasConsumerStoppedPropagationRef.current;\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n }\n }, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);\n\n const defaultCheckedRef = React.useRef(checked);\n return (\n <Primitive.input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={defaultChecked ?? defaultCheckedRef.current}\n required={required}\n disabled={disabled}\n name={name}\n value={value}\n form={form}\n {...props}\n tabIndex={-1}\n ref={composedRefs}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n transform: 'translateX(-100%)',\n }}\n />\n );\n },\n);\n\nSwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction isFunction(value: unknown): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchProvider,\n SwitchTrigger,\n SwitchThumb,\n SwitchBubbleInput,\n //\n Switch as Root,\n SwitchProvider as Provider,\n SwitchTrigger as Trigger,\n SwitchThumb as Thumb,\n SwitchBubbleInput as BubbleInput,\n};\nexport type {\n SwitchProps,\n SwitchProviderProps,\n SwitchTriggerProps,\n SwitchThumbProps,\n SwitchBubbleInputProps,\n};\n"],
|
|
5
|
+
"mappings": ";;;AAAA,YAAY,WAAW;AACvB,SAAS,4BAA4B;AACrC,SAAS,uBAAuB;AAChC,SAAS,0BAA0B;AACnC,SAAS,4BAA4B;AACrC,SAAS,mBAAmB;AAC5B,SAAS,eAAe;AACxB,SAAS,iBAAiB;AA4FtB,SA0GM,UA1GN,KA0GM,YA1GN;AAxFJ,IAAM,cAAc;AAGpB,IAAM,CAAC,qBAAqB,iBAAiB,IAAI,mBAAmB,WAAW;AAmB/E,IAAM,CAAC,oBAAoB,gBAAgB,IAAI,oBAAwC,WAAW;AAkBlG,SAAS,eAAe,OAAyC;AAC/D,QAAM;AAAA,IACJ;AAAA,IACA,SAAS;AAAA,IACT;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQ;AAAA;AAAA,IAER;AAAA,EACF,IAAI;AAEJ,QAAM,CAAC,SAAS,UAAU,IAAI,qBAAqB;AAAA,IACjD,MAAM;AAAA,IACN,aAAa,kBAAkB;AAAA,IAC/B,UAAU;AAAA,IACV,QAAQ;AAAA,EACV,CAAC;AACD,QAAM,CAAC,SAAS,UAAU,IAAU,eAAmC,IAAI;AAC3E,QAAM,CAAC,aAAa,cAAc,IAAU,eAAkC,IAAI;AAClF,QAAM,mCAAyC,aAAO,KAAK;AAC3D,QAAM,gBAAgB,UAClB,CAAC,CAAC,QAAQ,CAAC,CAAC,QAAQ,QAAQ,MAAM;AAAA;AAAA,IAElC;AAAA;AAEJ,QAAM,UAA8B;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA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AAW;AAE9B,YAAM,UAAU,CAAC,iCAAiC;AAClD,UAAI,gBAAgB,WAAW,YAAY;AACzC,cAAM,QAAQ,IAAI,MAAM,SAAS,EAAE,QAAQ,CAAC;AAC5C,mBAAW,KAAK,OAAO,OAAO;AAC9B,cAAM,cAAc,KAAK;AAAA,MAC3B;AAAA,IACF,GAAG,CAAC,aAAa,aAAa,SAAS,gCAAgC,CAAC;AAExE,UAAM,oBAA0B,aAAO,OAAO;AAC9C,WACE;AAAA,MAAC,UAAU;AAAA,MAAV;AAAA,QACC,MAAK;AAAA,QACL,eAAW;AAAA,QACX,gBAAgB,kBAAkB,kBAAkB;AAAA,QACpD;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACC,GAAG;AAAA,QACJ,UAAU;AAAA,QACV,KAAK;AAAA,QACL,OAAO;AAAA,UACL,GAAG,MAAM;AAAA,UACT,GAAG;AAAA,UACH,UAAU;AAAA,UACV,eAAe;AAAA,UACf,SAAS;AAAA,UACT,QAAQ;AAAA;AAAA;AAAA;AAAA,UAIR,WAAW;AAAA,QACb;AAAA;AAAA,IACF;AAAA,EAEJ;AACF;AAEA,kBAAkB,cAAc;AAIhC,SAAS,WAAW,OAAkD;AACpE,SAAO,OAAO,UAAU;AAC1B;AAEA,SAAS,SAAS,SAAkB;AAClC,SAAO,UAAU,YAAY;AAC/B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radix-ui/react-switch",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0-rc.1780442461555",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -13,11 +13,11 @@
|
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@radix-ui/react-compose-refs": "1.1.2",
|
|
15
15
|
"@radix-ui/primitive": "1.1.3",
|
|
16
|
-
"@radix-ui/react-context": "1.1.3",
|
|
17
|
-
"@radix-ui/react-primitive": "2.1.5-rc.1780436093918",
|
|
18
16
|
"@radix-ui/react-use-controllable-state": "1.2.2",
|
|
17
|
+
"@radix-ui/react-context": "1.1.3",
|
|
18
|
+
"@radix-ui/react-use-previous": "1.1.1",
|
|
19
19
|
"@radix-ui/react-use-size": "1.1.1",
|
|
20
|
-
"@radix-ui/react-
|
|
20
|
+
"@radix-ui/react-primitive": "2.1.5-rc.1780442461555"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/react": "^19.2.2",
|