@radix-ui/react-switch 0.1.6-rc.1 → 0.1.6-rc.4
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.ts.map +1 -1
- package/dist/index.js +177 -1
- package/dist/index.js.map +1 -1
- package/dist/index.module.js +158 -1
- package/dist/index.module.js.map +1 -1
- package/package.json +8 -8
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;AAoBA,OAAA,wFAAgF,CAAC;AAMjF,4BAA4B,MAAM,wBAAwB,CAAC,OAAO,UAAU,MAAM,CAAC,CAAC;AACpF,4BAAsB,SAAQ,oBAAoB;IAChD,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,eAAe,CAAC,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;CAC1C;AAED,OAAA,MAAM,6FAsEL,CAAC;AAWF,0BAA0B,MAAM,wBAAwB,CAAC,OAAO,UAAU,IAAI,CAAC,CAAC;AAChF,iCAA2B,SAAQ,kBAAkB;CAAG;AAExD,OAAA,MAAM,qGAaL,CAAC;AAwDF,OAAA,MAAM,2FAAa,CAAC;AACpB,OAAA,MAAM,+FAAmB,CAAC","sources":["packages/react/switch/src/packages/react/switch/src/Switch.tsx","packages/react/switch/src/packages/react/switch/src/index.ts","packages/react/switch/src/index.ts"],"sourcesContent":[null,null,"export * from './Switch';\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,178 @@
|
|
|
1
|
-
var
|
|
1
|
+
var $cWV9h$babelruntimehelpersextends = require("@babel/runtime/helpers/extends");
|
|
2
|
+
var $cWV9h$react = require("react");
|
|
3
|
+
var $cWV9h$radixuiprimitive = require("@radix-ui/primitive");
|
|
4
|
+
var $cWV9h$radixuireactcomposerefs = require("@radix-ui/react-compose-refs");
|
|
5
|
+
var $cWV9h$radixuireactcontext = require("@radix-ui/react-context");
|
|
6
|
+
var $cWV9h$radixuireactusecontrollablestate = require("@radix-ui/react-use-controllable-state");
|
|
7
|
+
var $cWV9h$radixuireactuseprevious = require("@radix-ui/react-use-previous");
|
|
8
|
+
var $cWV9h$radixuireactusesize = require("@radix-ui/react-use-size");
|
|
9
|
+
var $cWV9h$radixuireactprimitive = require("@radix-ui/react-primitive");
|
|
10
|
+
var $cWV9h$radixuireactlabel = require("@radix-ui/react-label");
|
|
11
|
+
|
|
12
|
+
function $parcel$exportWildcard(dest, source) {
|
|
13
|
+
Object.keys(source).forEach(function(key) {
|
|
14
|
+
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
Object.defineProperty(dest, key, {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function get() {
|
|
21
|
+
return source[key];
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return dest;
|
|
27
|
+
}
|
|
28
|
+
function $parcel$interopDefault(a) {
|
|
29
|
+
return a && a.__esModule ? a.default : a;
|
|
30
|
+
}
|
|
31
|
+
function $parcel$export(e, n, v, s) {
|
|
32
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
33
|
+
}
|
|
34
|
+
var $4465bdeb0ef4ccd7$exports = {};
|
|
35
|
+
|
|
36
|
+
$parcel$export($4465bdeb0ef4ccd7$exports, "createSwitchScope", () => $4465bdeb0ef4ccd7$export$cf7f5f17f69cbd43);
|
|
37
|
+
$parcel$export($4465bdeb0ef4ccd7$exports, "Switch", () => $4465bdeb0ef4ccd7$export$b5d5cf8927ab7262);
|
|
38
|
+
$parcel$export($4465bdeb0ef4ccd7$exports, "SwitchThumb", () => $4465bdeb0ef4ccd7$export$4d07bf653ea69106);
|
|
39
|
+
$parcel$export($4465bdeb0ef4ccd7$exports, "Root", () => $4465bdeb0ef4ccd7$export$be92b6f5f03c0fe9);
|
|
40
|
+
$parcel$export($4465bdeb0ef4ccd7$exports, "Thumb", () => $4465bdeb0ef4ccd7$export$6521433ed15a34db);
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/* -------------------------------------------------------------------------------------------------
|
|
52
|
+
* Switch
|
|
53
|
+
* -----------------------------------------------------------------------------------------------*/ const $4465bdeb0ef4ccd7$var$SWITCH_NAME = 'Switch';
|
|
54
|
+
const [$4465bdeb0ef4ccd7$var$createSwitchContext, $4465bdeb0ef4ccd7$export$cf7f5f17f69cbd43] = $cWV9h$radixuireactcontext.createContextScope($4465bdeb0ef4ccd7$var$SWITCH_NAME);
|
|
55
|
+
const [$4465bdeb0ef4ccd7$var$SwitchProvider, $4465bdeb0ef4ccd7$var$useSwitchContext] = $4465bdeb0ef4ccd7$var$createSwitchContext($4465bdeb0ef4ccd7$var$SWITCH_NAME);
|
|
56
|
+
const $4465bdeb0ef4ccd7$export$b5d5cf8927ab7262 = /*#__PURE__*/ $cWV9h$react.forwardRef((props, forwardedRef)=>{
|
|
57
|
+
const { __scopeSwitch: __scopeSwitch , 'aria-labelledby': ariaLabelledby , name: name , checked: checkedProp , defaultChecked: defaultChecked , required: required , disabled: disabled , value: value = 'on' , onCheckedChange: onCheckedChange , ...switchProps } = props;
|
|
58
|
+
const [button, setButton] = $cWV9h$react.useState(null);
|
|
59
|
+
const composedRefs = $cWV9h$radixuireactcomposerefs.useComposedRefs(forwardedRef, (node)=>setButton(node)
|
|
60
|
+
);
|
|
61
|
+
const labelId = $cWV9h$radixuireactlabel.useLabelContext(button);
|
|
62
|
+
const labelledBy = ariaLabelledby || labelId;
|
|
63
|
+
const hasConsumerStoppedPropagationRef = $cWV9h$react.useRef(false); // We set this to true by default so that events bubble to forms without JS (SSR)
|
|
64
|
+
const isFormControl = button ? Boolean(button.closest('form')) : true;
|
|
65
|
+
const [checked = false, setChecked] = $cWV9h$radixuireactusecontrollablestate.useControllableState({
|
|
66
|
+
prop: checkedProp,
|
|
67
|
+
defaultProp: defaultChecked,
|
|
68
|
+
onChange: onCheckedChange
|
|
69
|
+
});
|
|
70
|
+
return /*#__PURE__*/ $cWV9h$react.createElement($4465bdeb0ef4ccd7$var$SwitchProvider, {
|
|
71
|
+
scope: __scopeSwitch,
|
|
72
|
+
checked: checked,
|
|
73
|
+
disabled: disabled
|
|
74
|
+
}, /*#__PURE__*/ $cWV9h$react.createElement($cWV9h$radixuireactprimitive.Primitive.button, ($parcel$interopDefault($cWV9h$babelruntimehelpersextends))({
|
|
75
|
+
type: "button",
|
|
76
|
+
role: "switch",
|
|
77
|
+
"aria-checked": checked,
|
|
78
|
+
"aria-labelledby": labelledBy,
|
|
79
|
+
"aria-required": required,
|
|
80
|
+
"data-state": $4465bdeb0ef4ccd7$var$getState(checked),
|
|
81
|
+
"data-disabled": disabled ? '' : undefined,
|
|
82
|
+
disabled: disabled,
|
|
83
|
+
value: value
|
|
84
|
+
}, switchProps, {
|
|
85
|
+
ref: composedRefs,
|
|
86
|
+
onClick: $cWV9h$radixuiprimitive.composeEventHandlers(props.onClick, (event)=>{
|
|
87
|
+
setChecked((prevChecked)=>!prevChecked
|
|
88
|
+
);
|
|
89
|
+
if (isFormControl) {
|
|
90
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(); // if switch is in a form, stop propagation from the button so that we only propagate
|
|
91
|
+
// one click event (from the input). We propagate changes from an input so that native
|
|
92
|
+
// form validation works and form events reflect switch updates.
|
|
93
|
+
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
})), isFormControl && /*#__PURE__*/ $cWV9h$react.createElement($4465bdeb0ef4ccd7$var$BubbleInput, {
|
|
97
|
+
control: button,
|
|
98
|
+
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
99
|
+
name: name,
|
|
100
|
+
value: value,
|
|
101
|
+
checked: checked,
|
|
102
|
+
required: required,
|
|
103
|
+
disabled: disabled // We transform because the input is absolutely positioned but we have
|
|
104
|
+
,
|
|
105
|
+
style: {
|
|
106
|
+
transform: 'translateX(-100%)'
|
|
107
|
+
}
|
|
108
|
+
}));
|
|
109
|
+
});
|
|
110
|
+
/*#__PURE__*/ Object.assign($4465bdeb0ef4ccd7$export$b5d5cf8927ab7262, {
|
|
111
|
+
displayName: $4465bdeb0ef4ccd7$var$SWITCH_NAME
|
|
112
|
+
});
|
|
113
|
+
/* -------------------------------------------------------------------------------------------------
|
|
114
|
+
* SwitchThumb
|
|
115
|
+
* -----------------------------------------------------------------------------------------------*/ const $4465bdeb0ef4ccd7$var$THUMB_NAME = 'SwitchThumb';
|
|
116
|
+
const $4465bdeb0ef4ccd7$export$4d07bf653ea69106 = /*#__PURE__*/ $cWV9h$react.forwardRef((props, forwardedRef)=>{
|
|
117
|
+
const { __scopeSwitch: __scopeSwitch , ...thumbProps } = props;
|
|
118
|
+
const context = $4465bdeb0ef4ccd7$var$useSwitchContext($4465bdeb0ef4ccd7$var$THUMB_NAME, __scopeSwitch);
|
|
119
|
+
return /*#__PURE__*/ $cWV9h$react.createElement($cWV9h$radixuireactprimitive.Primitive.span, ($parcel$interopDefault($cWV9h$babelruntimehelpersextends))({
|
|
120
|
+
"data-state": $4465bdeb0ef4ccd7$var$getState(context.checked),
|
|
121
|
+
"data-disabled": context.disabled ? '' : undefined
|
|
122
|
+
}, thumbProps, {
|
|
123
|
+
ref: forwardedRef
|
|
124
|
+
}));
|
|
125
|
+
});
|
|
126
|
+
/*#__PURE__*/ Object.assign($4465bdeb0ef4ccd7$export$4d07bf653ea69106, {
|
|
127
|
+
displayName: $4465bdeb0ef4ccd7$var$THUMB_NAME
|
|
128
|
+
});
|
|
129
|
+
/* ---------------------------------------------------------------------------------------------- */ const $4465bdeb0ef4ccd7$var$BubbleInput = (props)=>{
|
|
130
|
+
const { control: control , checked: checked , bubbles: bubbles = true , ...inputProps } = props;
|
|
131
|
+
const ref = $cWV9h$react.useRef(null);
|
|
132
|
+
const prevChecked = $cWV9h$radixuireactuseprevious.usePrevious(checked);
|
|
133
|
+
const controlSize = $cWV9h$radixuireactusesize.useSize(control); // Bubble checked change to parents (e.g form change event)
|
|
134
|
+
$cWV9h$react.useEffect(()=>{
|
|
135
|
+
const input = ref.current;
|
|
136
|
+
const inputProto = window.HTMLInputElement.prototype;
|
|
137
|
+
const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked');
|
|
138
|
+
const setChecked = descriptor.set;
|
|
139
|
+
if (prevChecked !== checked && setChecked) {
|
|
140
|
+
const event = new Event('click', {
|
|
141
|
+
bubbles: bubbles
|
|
142
|
+
});
|
|
143
|
+
setChecked.call(input, checked);
|
|
144
|
+
input.dispatchEvent(event);
|
|
145
|
+
}
|
|
146
|
+
}, [
|
|
147
|
+
prevChecked,
|
|
148
|
+
checked,
|
|
149
|
+
bubbles
|
|
150
|
+
]);
|
|
151
|
+
return /*#__PURE__*/ $cWV9h$react.createElement("input", ($parcel$interopDefault($cWV9h$babelruntimehelpersextends))({
|
|
152
|
+
type: "checkbox",
|
|
153
|
+
"aria-hidden": true,
|
|
154
|
+
defaultChecked: checked
|
|
155
|
+
}, inputProps, {
|
|
156
|
+
tabIndex: -1,
|
|
157
|
+
ref: ref,
|
|
158
|
+
style: {
|
|
159
|
+
...props.style,
|
|
160
|
+
...controlSize,
|
|
161
|
+
position: 'absolute',
|
|
162
|
+
pointerEvents: 'none',
|
|
163
|
+
opacity: 0,
|
|
164
|
+
margin: 0
|
|
165
|
+
}
|
|
166
|
+
}));
|
|
167
|
+
};
|
|
168
|
+
function $4465bdeb0ef4ccd7$var$getState(checked) {
|
|
169
|
+
return checked ? 'checked' : 'unchecked';
|
|
170
|
+
}
|
|
171
|
+
const $4465bdeb0ef4ccd7$export$be92b6f5f03c0fe9 = $4465bdeb0ef4ccd7$export$b5d5cf8927ab7262;
|
|
172
|
+
const $4465bdeb0ef4ccd7$export$6521433ed15a34db = $4465bdeb0ef4ccd7$export$4d07bf653ea69106;
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
$parcel$exportWildcard(module.exports, $4465bdeb0ef4ccd7$exports);
|
|
176
|
+
|
|
177
|
+
|
|
2
178
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"IAYgCA,EAAMC,EAZNC,8bAYAF,KAAMC,mBACpCE,OAAOC,KAAKH,GAAQI,SAAQ,SAASC,GACvB,YAARA,GAA6B,eAARA,GAIzBH,OAAOI,eAAeP,EAAMM,EAAK,CAC/BE,YAAY,EACZC,IAAK,WACH,OAAOR,EAAOK,SAKbN,MA1BuBE,8CAClBA,EAAEQ,WAAaR,EAAES,QAAUT,ECgBzC,MAGOU,EAAqBC,GAAqBC,EAH7B,sCAMpB,MAAOC,EAAgBC,GAAoBJ,EANvB,UAiBdK,eAASC,EAAMC,YACnB,CAACC,EAAiCC,KAChC,MAAMC,cACJA,EACA,kBAAmBC,EAFfC,KAGJA,EACAC,QAASC,EAJLC,eAKJA,EALIC,SAMJA,EANIC,SAOJA,EAPIC,MAQJA,EAAQ,KARJC,gBASJA,KACGC,GACDZ,GACGa,EAAQC,GAAahB,EAAMiB,SAAmC,MAC/DC,EAAeC,EAAgBhB,GAAeiB,GAASJ,EAAUI,KACjEC,EAAUC,EAAgBP,GAC1BQ,EAAalB,GAAkBgB,EAC/BG,EAAmCxB,EAAMyB,QAAO,GAEhDC,GAAgBX,GAASY,QAAQZ,EAAOa,QAAQ,UAC/CrB,GAAU,EAAOsB,GAAcC,EAAqB,CACzDC,KAAMvB,EACNwB,YAAavB,EACbwB,SAAUpB,iBAGZ,OACEqB,EAAAC,cAACC,EAAD,CAAgBC,MAAOjC,EAAeG,QAASA,EAASI,SAAUA,gB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,EAAO5D,GACvB4D,EAAMS,cAAc7B,MAErB,CAACC,EAAazC,EAAS+C,iBAGxBpB,EAAAC,cAAA,QAAAI,EAAA,CACEC,KAAK,WACL,eAAA,EACA/B,eAAgBF,GACZuD,EAJN,CAKEe,UAAW,EACXjC,IAAKA,EACLW,MAAO,IACFrD,EAAMqD,SACNS,EACHc,SAAU,WACVC,cAAe,OACfC,QAAS,EACTC,OAAQ,OAMhB,SAASvC,EAASnC,GAChB,OAAOA,EAAU,UAAY,YAG/B,MAAM2E,EAAOnF,iBACb,MAAMoF,EAAQ1B","sources":["./node_modules/@parcel/scope-hoisting/lib/helpers.js","./packages/react/switch/src/Switch.tsx"],"sourcesContent":["function $parcel$interopDefault(a) {\n return a && a.__esModule ? a.default : a;\n}\n\nfunction $parcel$defineInteropFlag(a) {\n Object.defineProperty(a, '__esModule', {value: true});\n}\n\nfunction $parcel$reexport(e, n, v) {\n Object.defineProperty(e, n, {get: v, enumerable: true});\n}\n\nfunction $parcel$exportWildcard(dest, source) {\n Object.keys(source).forEach(function(key) {\n if (key === 'default' || key === '__esModule') {\n return;\n }\n\n Object.defineProperty(dest, key, {\n enumerable: true,\n get: function get() {\n return source[key];\n },\n });\n });\n\n return dest;\n}\n\nfunction $parcel$missingModule(name) {\n var err = new Error(\"Cannot find module '\" + name + \"'\");\n err.code = 'MODULE_NOT_FOUND';\n throw err;\n}\n\nvar $parcel$global =\n typeof globalThis !== 'undefined'\n ? globalThis\n : typeof self !== 'undefined'\n ? self\n : typeof window !== 'undefined'\n ? window\n : typeof global !== 'undefined'\n ? global\n : {};\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';\nimport { useLabelContext } from '@radix-ui/react-label';\n\nimport type * as Radix from '@radix-ui/react-primitive';\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.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.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 'aria-labelledby': ariaLabelledby,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n ...switchProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const labelId = useLabelContext(button);\n const labelledBy = ariaLabelledby || labelId;\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 ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\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-labelledby={labelledBy}\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 <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\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.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.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\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\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 const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') 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 {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\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"],"names":["dest","source","a","Object","keys","forEach","key","defineProperty","enumerable","get","__esModule","default","createSwitchContext","createSwitchScope","createContextScope","SwitchProvider","useSwitchContext","Switch","React","forwardRef","props","forwardedRef","__scopeSwitch","ariaLabelledby","name","checked","checkedProp","defaultChecked","required","disabled","value","onCheckedChange","switchProps","button","setButton","useState","composedRefs","useComposedRefs","node","labelId","useLabelContext","labelledBy","hasConsumerStoppedPropagationRef","useRef","isFormControl","Boolean","closest","setChecked","useControllableState","prop","defaultProp","onChange","_react","createElement","$e251e027f448086991ed2332fcb5c67f$var$SwitchProvider","scope","Primitive","_babelRuntimeHelpersExtends","type","role","getState","undefined","ref","onClick","composeEventHandlers","event","prevChecked","current","isPropagationStopped","stopPropagation","$e251e027f448086991ed2332fcb5c67f$var$BubbleInput","control","bubbles","style","transform","SwitchThumb","thumbProps","context","span","BubbleInput","inputProps","usePrevious","controlSize","useSize","useEffect","input","inputProto","window","HTMLInputElement","prototype","getOwnPropertyDescriptor","set","Event","call","dispatchEvent","tabIndex","position","pointerEvents","opacity","margin","Root","Thumb"],"version":3,"file":"index.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;A;;;;;;;;;;ACaA;;oGAEA,CAEA,MAAMS,iCAAW,GAAG,QAApB,AAAA;AAGA,MAAM,CAACC,yCAAD,EAAsBC,yCAAtB,CAAA,GAA2CR,6CAAkB,CAACM,iCAAD,CAAnE,AAAA;AAGA,MAAM,CAACG,oCAAD,EAAiBC,sCAAjB,CAAA,GAAqCH,yCAAmB,CAAqBD,iCAArB,CAA9D,AAAA;AAWA,MAAMK,yCAAM,GAAA,aAAGd,CAAAA,uBAAA,CACb,CAACgB,KAAD,EAAkCC,YAAlC,GAAmD;IACjD,MAAM,E,eACJC,aADI,CAAA,EAEJ,iBAAA,EAAmBC,cAFf,CAAA,E,MAGJC,IAHI,CAAA,EAIJC,OAAO,EAAEC,WAJL,CAAA,E,gBAKJC,cALI,CAAA,E,UAMJC,QANI,CAAA,E,UAOJC,QAPI,CAAA,SAQJC,KAAK,GAAG,IARJ,G,iBASJC,eATI,CAAA,EAUJ,GAAGC,WAAH,EAVI,GAWFZ,KAXJ,AAAM;IAYN,MAAM,CAACa,MAAD,EAASC,SAAT,CAAA,GAAsB9B,qBAAA,CAAyC,IAAzC,CAA5B,AAAA;IACA,MAAMgC,YAAY,GAAG9B,8CAAe,CAACe,YAAD,EAAgBgB,CAAAA,IAAD,GAAUH,SAAS,CAACG,IAAD,CAAlC;IAAA,CAApC,AAAA;IACA,MAAMC,OAAO,GAAG1B,wCAAe,CAACqB,MAAD,CAA/B,AAAA;IACA,MAAMM,UAAU,GAAGhB,cAAc,IAAIe,OAArC,AAAA;IACA,MAAME,gCAAgC,GAAGpC,mBAAA,CAAa,KAAb,CAAzC,AAjBiD,EAkBjD,iFADA;IAEA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P;QAOE,KAAK,EAAE;YACL,GAAG9C,KAAK,CAAC6D,KADJ;YAEL,GAAGd,WAFE;YAGLe,QAAQ,EAAE,UAHL;YAILC,aAAa,EAAE,MAJV;YAKLC,OAAO,EAAE,CALJ;YAMLC,MAAM,EAAE,CAARA;SANK;KAPT,CAAA,CADF,CACE;CApBJ,AAqCC;AAED,SAASpC,8BAAT,CAAkBxB,OAAlB,EAAoC;IAClC,OAAOA,OAAO,GAAG,SAAH,GAAe,WAA7B,CAAA;CACD;AAED,MAAM6D,yCAAI,GAAGpE,yCAAb,AAAA;AACA,MAAMqE,yCAAK,GAAG5B,yCAAd,AAAA;;AD5LA","sources":["packages/react/switch/src/index.ts","packages/react/switch/src/Switch.tsx"],"sourcesContent":["export * 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';\nimport { useLabelContext } from '@radix-ui/react-label';\n\nimport type * as Radix from '@radix-ui/react-primitive';\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.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.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 'aria-labelledby': ariaLabelledby,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n ...switchProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const labelId = useLabelContext(button);\n const labelledBy = ariaLabelledby || labelId;\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 ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\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-labelledby={labelledBy}\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 <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\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.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.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\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\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 const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') 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 {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\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"],"names":["React","composeEventHandlers","useComposedRefs","createContextScope","useControllableState","usePrevious","useSize","Primitive","useLabelContext","SWITCH_NAME","createSwitchContext","createSwitchScope","SwitchProvider","useSwitchContext","Switch","forwardRef","props","forwardedRef","__scopeSwitch","ariaLabelledby","name","checked","checkedProp","defaultChecked","required","disabled","value","onCheckedChange","switchProps","button","setButton","useState","composedRefs","node","labelId","labelledBy","hasConsumerStoppedPropagationRef","useRef","isFormControl","Boolean","closest","setChecked","prop","defaultProp","onChange","getState","undefined","onClick","event","prevChecked","current","isPropagationStopped","stopPropagation","transform","THUMB_NAME","SwitchThumb","thumbProps","context","BubbleInput","control","bubbles","inputProps","ref","controlSize","useEffect","input","inputProto","window","HTMLInputElement","prototype","descriptor","Object","getOwnPropertyDescriptor","set","Event","call","dispatchEvent","style","position","pointerEvents","opacity","margin","Root","Thumb"],"version":3,"file":"index.js.map"}
|
package/dist/index.module.js
CHANGED
|
@@ -1,2 +1,159 @@
|
|
|
1
|
-
import
|
|
1
|
+
import $irPIl$babelruntimehelpersesmextends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import {forwardRef as $irPIl$forwardRef, useState as $irPIl$useState, useRef as $irPIl$useRef, createElement as $irPIl$createElement, useEffect as $irPIl$useEffect} from "react";
|
|
3
|
+
import {composeEventHandlers as $irPIl$composeEventHandlers} from "@radix-ui/primitive";
|
|
4
|
+
import {useComposedRefs as $irPIl$useComposedRefs} from "@radix-ui/react-compose-refs";
|
|
5
|
+
import {createContextScope as $irPIl$createContextScope} from "@radix-ui/react-context";
|
|
6
|
+
import {useControllableState as $irPIl$useControllableState} from "@radix-ui/react-use-controllable-state";
|
|
7
|
+
import {usePrevious as $irPIl$usePrevious} from "@radix-ui/react-use-previous";
|
|
8
|
+
import {useSize as $irPIl$useSize} from "@radix-ui/react-use-size";
|
|
9
|
+
import {Primitive as $irPIl$Primitive} from "@radix-ui/react-primitive";
|
|
10
|
+
import {useLabelContext as $irPIl$useLabelContext} from "@radix-ui/react-label";
|
|
11
|
+
|
|
12
|
+
function $parcel$export(e, n, v, s) {
|
|
13
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
14
|
+
}
|
|
15
|
+
var $6be4966fd9bbc698$exports = {};
|
|
16
|
+
|
|
17
|
+
$parcel$export($6be4966fd9bbc698$exports, "createSwitchScope", () => $6be4966fd9bbc698$export$cf7f5f17f69cbd43);
|
|
18
|
+
$parcel$export($6be4966fd9bbc698$exports, "Switch", () => $6be4966fd9bbc698$export$b5d5cf8927ab7262);
|
|
19
|
+
$parcel$export($6be4966fd9bbc698$exports, "SwitchThumb", () => $6be4966fd9bbc698$export$4d07bf653ea69106);
|
|
20
|
+
$parcel$export($6be4966fd9bbc698$exports, "Root", () => $6be4966fd9bbc698$export$be92b6f5f03c0fe9);
|
|
21
|
+
$parcel$export($6be4966fd9bbc698$exports, "Thumb", () => $6be4966fd9bbc698$export$6521433ed15a34db);
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
/* -------------------------------------------------------------------------------------------------
|
|
33
|
+
* Switch
|
|
34
|
+
* -----------------------------------------------------------------------------------------------*/ const $6be4966fd9bbc698$var$SWITCH_NAME = 'Switch';
|
|
35
|
+
const [$6be4966fd9bbc698$var$createSwitchContext, $6be4966fd9bbc698$export$cf7f5f17f69cbd43] = $irPIl$createContextScope($6be4966fd9bbc698$var$SWITCH_NAME);
|
|
36
|
+
const [$6be4966fd9bbc698$var$SwitchProvider, $6be4966fd9bbc698$var$useSwitchContext] = $6be4966fd9bbc698$var$createSwitchContext($6be4966fd9bbc698$var$SWITCH_NAME);
|
|
37
|
+
const $6be4966fd9bbc698$export$b5d5cf8927ab7262 = /*#__PURE__*/ $irPIl$forwardRef((props, forwardedRef)=>{
|
|
38
|
+
const { __scopeSwitch: __scopeSwitch , 'aria-labelledby': ariaLabelledby , name: name , checked: checkedProp , defaultChecked: defaultChecked , required: required , disabled: disabled , value: value = 'on' , onCheckedChange: onCheckedChange , ...switchProps } = props;
|
|
39
|
+
const [button, setButton] = $irPIl$useState(null);
|
|
40
|
+
const composedRefs = $irPIl$useComposedRefs(forwardedRef, (node)=>setButton(node)
|
|
41
|
+
);
|
|
42
|
+
const labelId = $irPIl$useLabelContext(button);
|
|
43
|
+
const labelledBy = ariaLabelledby || labelId;
|
|
44
|
+
const hasConsumerStoppedPropagationRef = $irPIl$useRef(false); // We set this to true by default so that events bubble to forms without JS (SSR)
|
|
45
|
+
const isFormControl = button ? Boolean(button.closest('form')) : true;
|
|
46
|
+
const [checked = false, setChecked] = $irPIl$useControllableState({
|
|
47
|
+
prop: checkedProp,
|
|
48
|
+
defaultProp: defaultChecked,
|
|
49
|
+
onChange: onCheckedChange
|
|
50
|
+
});
|
|
51
|
+
return /*#__PURE__*/ $irPIl$createElement($6be4966fd9bbc698$var$SwitchProvider, {
|
|
52
|
+
scope: __scopeSwitch,
|
|
53
|
+
checked: checked,
|
|
54
|
+
disabled: disabled
|
|
55
|
+
}, /*#__PURE__*/ $irPIl$createElement($irPIl$Primitive.button, $irPIl$babelruntimehelpersesmextends({
|
|
56
|
+
type: "button",
|
|
57
|
+
role: "switch",
|
|
58
|
+
"aria-checked": checked,
|
|
59
|
+
"aria-labelledby": labelledBy,
|
|
60
|
+
"aria-required": required,
|
|
61
|
+
"data-state": $6be4966fd9bbc698$var$getState(checked),
|
|
62
|
+
"data-disabled": disabled ? '' : undefined,
|
|
63
|
+
disabled: disabled,
|
|
64
|
+
value: value
|
|
65
|
+
}, switchProps, {
|
|
66
|
+
ref: composedRefs,
|
|
67
|
+
onClick: $irPIl$composeEventHandlers(props.onClick, (event)=>{
|
|
68
|
+
setChecked((prevChecked)=>!prevChecked
|
|
69
|
+
);
|
|
70
|
+
if (isFormControl) {
|
|
71
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(); // if switch is in a form, stop propagation from the button so that we only propagate
|
|
72
|
+
// one click event (from the input). We propagate changes from an input so that native
|
|
73
|
+
// form validation works and form events reflect switch updates.
|
|
74
|
+
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
})), isFormControl && /*#__PURE__*/ $irPIl$createElement($6be4966fd9bbc698$var$BubbleInput, {
|
|
78
|
+
control: button,
|
|
79
|
+
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
80
|
+
name: name,
|
|
81
|
+
value: value,
|
|
82
|
+
checked: checked,
|
|
83
|
+
required: required,
|
|
84
|
+
disabled: disabled // We transform because the input is absolutely positioned but we have
|
|
85
|
+
,
|
|
86
|
+
style: {
|
|
87
|
+
transform: 'translateX(-100%)'
|
|
88
|
+
}
|
|
89
|
+
}));
|
|
90
|
+
});
|
|
91
|
+
/*#__PURE__*/ Object.assign($6be4966fd9bbc698$export$b5d5cf8927ab7262, {
|
|
92
|
+
displayName: $6be4966fd9bbc698$var$SWITCH_NAME
|
|
93
|
+
});
|
|
94
|
+
/* -------------------------------------------------------------------------------------------------
|
|
95
|
+
* SwitchThumb
|
|
96
|
+
* -----------------------------------------------------------------------------------------------*/ const $6be4966fd9bbc698$var$THUMB_NAME = 'SwitchThumb';
|
|
97
|
+
const $6be4966fd9bbc698$export$4d07bf653ea69106 = /*#__PURE__*/ $irPIl$forwardRef((props, forwardedRef)=>{
|
|
98
|
+
const { __scopeSwitch: __scopeSwitch , ...thumbProps } = props;
|
|
99
|
+
const context = $6be4966fd9bbc698$var$useSwitchContext($6be4966fd9bbc698$var$THUMB_NAME, __scopeSwitch);
|
|
100
|
+
return /*#__PURE__*/ $irPIl$createElement($irPIl$Primitive.span, $irPIl$babelruntimehelpersesmextends({
|
|
101
|
+
"data-state": $6be4966fd9bbc698$var$getState(context.checked),
|
|
102
|
+
"data-disabled": context.disabled ? '' : undefined
|
|
103
|
+
}, thumbProps, {
|
|
104
|
+
ref: forwardedRef
|
|
105
|
+
}));
|
|
106
|
+
});
|
|
107
|
+
/*#__PURE__*/ Object.assign($6be4966fd9bbc698$export$4d07bf653ea69106, {
|
|
108
|
+
displayName: $6be4966fd9bbc698$var$THUMB_NAME
|
|
109
|
+
});
|
|
110
|
+
/* ---------------------------------------------------------------------------------------------- */ const $6be4966fd9bbc698$var$BubbleInput = (props)=>{
|
|
111
|
+
const { control: control , checked: checked , bubbles: bubbles = true , ...inputProps } = props;
|
|
112
|
+
const ref = $irPIl$useRef(null);
|
|
113
|
+
const prevChecked = $irPIl$usePrevious(checked);
|
|
114
|
+
const controlSize = $irPIl$useSize(control); // Bubble checked change to parents (e.g form change event)
|
|
115
|
+
$irPIl$useEffect(()=>{
|
|
116
|
+
const input = ref.current;
|
|
117
|
+
const inputProto = window.HTMLInputElement.prototype;
|
|
118
|
+
const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked');
|
|
119
|
+
const setChecked = descriptor.set;
|
|
120
|
+
if (prevChecked !== checked && setChecked) {
|
|
121
|
+
const event = new Event('click', {
|
|
122
|
+
bubbles: bubbles
|
|
123
|
+
});
|
|
124
|
+
setChecked.call(input, checked);
|
|
125
|
+
input.dispatchEvent(event);
|
|
126
|
+
}
|
|
127
|
+
}, [
|
|
128
|
+
prevChecked,
|
|
129
|
+
checked,
|
|
130
|
+
bubbles
|
|
131
|
+
]);
|
|
132
|
+
return /*#__PURE__*/ $irPIl$createElement("input", $irPIl$babelruntimehelpersesmextends({
|
|
133
|
+
type: "checkbox",
|
|
134
|
+
"aria-hidden": true,
|
|
135
|
+
defaultChecked: checked
|
|
136
|
+
}, inputProps, {
|
|
137
|
+
tabIndex: -1,
|
|
138
|
+
ref: ref,
|
|
139
|
+
style: {
|
|
140
|
+
...props.style,
|
|
141
|
+
...controlSize,
|
|
142
|
+
position: 'absolute',
|
|
143
|
+
pointerEvents: 'none',
|
|
144
|
+
opacity: 0,
|
|
145
|
+
margin: 0
|
|
146
|
+
}
|
|
147
|
+
}));
|
|
148
|
+
};
|
|
149
|
+
function $6be4966fd9bbc698$var$getState(checked) {
|
|
150
|
+
return checked ? 'checked' : 'unchecked';
|
|
151
|
+
}
|
|
152
|
+
const $6be4966fd9bbc698$export$be92b6f5f03c0fe9 = $6be4966fd9bbc698$export$b5d5cf8927ab7262;
|
|
153
|
+
const $6be4966fd9bbc698$export$6521433ed15a34db = $6be4966fd9bbc698$export$4d07bf653ea69106;
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
export {$6be4966fd9bbc698$export$cf7f5f17f69cbd43 as createSwitchScope, $6be4966fd9bbc698$export$b5d5cf8927ab7262 as Switch, $6be4966fd9bbc698$export$4d07bf653ea69106 as SwitchThumb, $6be4966fd9bbc698$export$be92b6f5f03c0fe9 as Root, $6be4966fd9bbc698$export$6521433ed15a34db as Thumb};
|
|
2
159
|
//# sourceMappingURL=index.module.js.map
|
package/dist/index.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"2iBAiBA,MAGOA,EAAqBC,GAAqBC,EAH7B,yCAMpB,MAAOC,EAAgBC,GAAoBJ,EANvB,iBAiBpB,MAAMK,oBAASC,EAAMC,YACnB,CAACC,EAAiCC,KAChC,MAAMC,cACJA,EACA,kBAAmBC,EAFfC,KAGJA,EACAC,QAASC,EAJLC,eAKJA,EALIC,SAMJA,EANIC,SAOJA,EAPIC,MAQJA,EAAQ,KARJC,gBASJA,KACGC,GACDZ,GACGa,EAAQC,GAAahB,EAAMiB,SAAmC,MAC/DC,EAAeC,EAAgBhB,GAAeiB,GAASJ,EAAUI,KACjEC,EAAUC,EAAgBP,GAC1BQ,EAAalB,GAAkBgB,EAC/BG,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MkE,WAAU,KACd,MAAMC,EAAQvB,EAAIK,QACZmB,EAAaC,OAAOC,iBAAiBC,UAErC1C,EADa2C,OAAOC,yBAAyBL,EAAY,WACjCM,IAC9B,GAAI1B,IAAgBzC,GAAWsB,EAAY,CACzC,MAAMkB,EAAQ,IAAI4B,MAAM,QAAS,CAAErB,QAAAA,IACnCzB,EAAW+C,KAAKT,EAAO5D,GACvB4D,EAAMU,cAAc9B,MAErB,CAACC,EAAazC,EAAS+C,iBAGxBpB,EAAAC,cAAA,QAAAI,EAAA,CACEC,KAAK,WACL,eAAA,EACA/B,eAAgBF,GACZuD,EAJN,CAKEgB,UAAW,EACXlC,IAAKA,EACLW,MAAO,IACFrD,EAAMqD,SACNS,EACHe,SAAU,WACVC,cAAe,OACfC,QAAS,EACTC,OAAQ,OAMhB,SAASxC,EAASnC,GAChB,OAAOA,EAAU,UAAY,mBAG/B,MAAM4E,KAAOpF,cACb,MAAMqF,MAAQ3B","sources":["./packages/react/switch/src/Switch.tsx"],"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';\nimport { useLabelContext } from '@radix-ui/react-label';\n\nimport type * as Radix from '@radix-ui/react-primitive';\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.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.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 'aria-labelledby': ariaLabelledby,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n ...switchProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const labelId = useLabelContext(button);\n const labelledBy = ariaLabelledby || labelId;\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 ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\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-labelledby={labelledBy}\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 <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\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.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.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\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\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 const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') 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 {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\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"],"names":["createSwitchContext","createSwitchScope","createContextScope","SwitchProvider","useSwitchContext","Switch","React","forwardRef","props","forwardedRef","__scopeSwitch","ariaLabelledby","name","checked","checkedProp","defaultChecked","required","disabled","value","onCheckedChange","switchProps","button","setButton","useState","composedRefs","useComposedRefs","node","labelId","useLabelContext","labelledBy","hasConsumerStoppedPropagationRef","useRef","isFormControl","Boolean","closest","setChecked","useControllableState","prop","defaultProp","onChange","_react","createElement","$a39756f91f0c788d079b2ac1958ff$var$SwitchProvider","scope","Primitive","_babelRuntimeHelpersEsmExtends","type","role","getState","undefined","ref","onClick","composeEventHandlers","event","prevChecked","current","isPropagationStopped","stopPropagation","$a39756f91f0c788d079b2ac1958ff$var$BubbleInput","control","bubbles","style","transform","SwitchThumb","thumbProps","context","span","BubbleInput","inputProps","usePrevious","controlSize","useSize","useEffect","input","inputProto","window","HTMLInputElement","prototype","Object","getOwnPropertyDescriptor","set","Event","call","dispatchEvent","tabIndex","position","pointerEvents","opacity","margin","Root","Thumb"],"version":3,"file":"index.module.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;A;;;;;;;;;;ACaA;;oGAEA,CAEA,MAAMS,iCAAW,GAAG,QAApB,AAAA;AAGA,MAAM,CAACC,yCAAD,EAAsBC,yCAAtB,CAAA,GAA2CR,yBAAkB,CAACM,iCAAD,CAAnE,AAAA;AAGA,MAAM,CAACG,oCAAD,EAAiBC,sCAAjB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/switch/src/index.ts","packages/react/switch/src/Switch.tsx"],"sourcesContent":["export * 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';\nimport { useLabelContext } from '@radix-ui/react-label';\n\nimport type * as Radix from '@radix-ui/react-primitive';\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.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.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 'aria-labelledby': ariaLabelledby,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n ...switchProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const labelId = useLabelContext(button);\n const labelledBy = ariaLabelledby || labelId;\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 ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\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-labelledby={labelledBy}\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 <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\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.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.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\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\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 const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') 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 {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\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"],"names":["React","composeEventHandlers","useComposedRefs","createContextScope","useControllableState","usePrevious","useSize","Primitive","useLabelContext","SWITCH_NAME","createSwitchContext","createSwitchScope","SwitchProvider","useSwitchContext","Switch","forwardRef","props","forwardedRef","__scopeSwitch","ariaLabelledby","name","checked","checkedProp","defaultChecked","required","disabled","value","onCheckedChange","switchProps","button","setButton","useState","composedRefs","node","labelId","labelledBy","hasConsumerStoppedPropagationRef","useRef","isFormControl","Boolean","closest","setChecked","prop","defaultProp","onChange","getState","undefined","onClick","event","prevChecked","current","isPropagationStopped","stopPropagation","transform","THUMB_NAME","SwitchThumb","thumbProps","context","BubbleInput","control","bubbles","inputProps","ref","controlSize","useEffect","input","inputProto","window","HTMLInputElement","prototype","descriptor","Object","getOwnPropertyDescriptor","set","Event","call","dispatchEvent","style","position","pointerEvents","opacity","margin","Root","Thumb"],"version":3,"file":"index.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radix-ui/react-switch",
|
|
3
|
-
"version": "0.1.6-rc.
|
|
3
|
+
"version": "0.1.6-rc.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@babel/runtime": "^7.13.10",
|
|
20
20
|
"@radix-ui/primitive": "0.1.0",
|
|
21
|
-
"@radix-ui/react-compose-refs": "0.1.1-rc.
|
|
22
|
-
"@radix-ui/react-context": "0.1.2-rc.
|
|
23
|
-
"@radix-ui/react-label": "0.1.6-rc.
|
|
24
|
-
"@radix-ui/react-primitive": "0.1.5-rc.
|
|
25
|
-
"@radix-ui/react-use-controllable-state": "0.1.1-rc.
|
|
26
|
-
"@radix-ui/react-use-previous": "0.1.2-rc.
|
|
27
|
-
"@radix-ui/react-use-size": "0.1.2-rc.
|
|
21
|
+
"@radix-ui/react-compose-refs": "0.1.1-rc.4",
|
|
22
|
+
"@radix-ui/react-context": "0.1.2-rc.4",
|
|
23
|
+
"@radix-ui/react-label": "0.1.6-rc.4",
|
|
24
|
+
"@radix-ui/react-primitive": "0.1.5-rc.4",
|
|
25
|
+
"@radix-ui/react-use-controllable-state": "0.1.1-rc.4",
|
|
26
|
+
"@radix-ui/react-use-previous": "0.1.2-rc.4",
|
|
27
|
+
"@radix-ui/react-use-size": "0.1.2-rc.4"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"react": "^16.8 || ^17.0 || ^18.0"
|