@radix-ui/react-switch 0.0.14 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,21 +1,19 @@
1
1
  import * as React from "react";
2
+ import * as Radix from "@radix-ui/react-primitive";
2
3
  import { Primitive } from "@radix-ui/react-primitive";
3
- import * as Polymorphic from "@radix-ui/react-polymorphic";
4
- declare const SWITCH_DEFAULT_TAG = "button";
5
- type InputDOMProps = React.ComponentProps<'input'>;
6
- type SwitchOwnProps = Polymorphic.Merge<Polymorphic.OwnProps<typeof Primitive>, {
4
+ type PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;
5
+ export interface SwitchProps extends PrimitiveButtonProps {
7
6
  checked?: boolean;
8
7
  defaultChecked?: boolean;
9
- required?: InputDOMProps['required'];
8
+ required?: boolean;
10
9
  onCheckedChange?(checked: boolean): void;
11
- }>;
12
- export type SwitchPrimitive = Polymorphic.ForwardRefComponent<typeof SWITCH_DEFAULT_TAG, SwitchOwnProps>;
13
- export const Switch: SwitchPrimitive;
14
- declare const THUMB_DEFAULT_TAG = "span";
15
- type SwitchThumbOwnProps = Polymorphic.OwnProps<typeof Primitive>;
16
- export type SwitchThumbPrimitive = Polymorphic.ForwardRefComponent<typeof THUMB_DEFAULT_TAG, SwitchThumbOwnProps>;
17
- export const SwitchThumb: SwitchThumbPrimitive;
18
- export const Root: SwitchPrimitive;
19
- export const Thumb: SwitchThumbPrimitive;
10
+ }
11
+ export const Switch: React.ForwardRefExoticComponent<SwitchProps & React.RefAttributes<HTMLButtonElement>>;
12
+ type PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;
13
+ export interface SwitchThumbProps extends PrimitiveSpanProps {
14
+ }
15
+ export const SwitchThumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
16
+ export const Root: React.ForwardRefExoticComponent<SwitchProps & React.RefAttributes<HTMLButtonElement>>;
17
+ export const Thumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
20
18
 
21
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"mappings":"A;A;A;AAiBA,QAAA,MAAM,6BAA6B,CAAC;AAEpC,qBAAqB,MAAM,cAAc,CAAC,OAAO,CAAC,CAAC;AACnD,sBAAsB,YAAY,KAAK,CACrC,YAAY,QAAQ,CAAC,gBAAgB,CAAC,EACtC;IACE,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;IACrC,eAAe,CAAC,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;CAC1C,CACF,CAAC;AAEF,8BAAuB,YAAY,mBAAmB,CAAC,yBAAyB,EAAE,cAAc,CAAC,CAAC;AAMlG,OAAA,MAAM,uBAqEe,CAAC;AAStB,QAAA,MAAM,0BAA0B,CAAC;AAEjC,2BAA2B,YAAY,QAAQ,CAAC,gBAAgB,CAAC,CAAC;AAClE,mCAA4B,YAAY,mBAAmB,CACzD,wBAAwB,EACxB,mBAAmB,CACpB,CAAC;AAEF,OAAA,MAAM,iCAYoB,CAAC;AAsD3B,OAAA,MAAM,qBAAa,CAAC;AACpB,OAAA,MAAM,2BAAmB,CAAC","sources":["./packages/react/switch/src/packages/react/switch/src/Switch.tsx","./packages/react/switch/src/packages/react/switch/src/index.ts"],"sourcesContent":[null,null],"names":[],"version":3,"file":"index.d.ts.map"}
1
+ {"mappings":"A;A;A;AAsBA,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,6FAmEJ,CAAC;AAWH,0BAA0B,MAAM,wBAAwB,CAAC,OAAO,UAAU,IAAI,CAAC,CAAC;AAChF,iCAA2B,SAAQ,kBAAkB;CAAG;AAExD,OAAA,MAAM,qGAYL,CAAC;AAuDF,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"],"sourcesContent":[null,null],"names":[],"version":3,"file":"index.d.ts.map"}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var e,t,r,a=require("@radix-ui/react-label").useLabelContext,i=require("@radix-ui/react-primitive").Primitive,c=require("@radix-ui/react-use-size").useSize,o=require("@radix-ui/react-use-previous").usePrevious,n=require("@radix-ui/react-use-controllable-state").useControllableState,s=require("@radix-ui/react-context").createContext,u=require("@radix-ui/react-compose-refs").useComposedRefs,l=require("@radix-ui/primitive").composeEventHandlers,d=(e={},t=require("react"),Object.keys(t).forEach((function(r){"default"!==r&&"__esModule"!==r&&Object.defineProperty(e,r,{enumerable:!0,get:function(){return t[r]}})})),e),b=(r=require("@babel/runtime/helpers/extends"))&&r.__esModule?r.default:r;const p="button",[f,h]=s("Switch"),m=/*#__PURE__*/d.forwardRef(((e,t)=>{const{as:r=p,"aria-labelledby":c,name:o,checked:s,defaultChecked:h,required:m,disabled:x,value:k="on",onCheckedChange:y,...E}=e,[w,C]=d.useState(null),S=u(t,(e=>C(e))),g=a(w),P=c||g,R=d.useRef(!1),O=!w||Boolean(w.closest("form")),[T=!1,_]=n({prop:s,defaultProp:h,onChange:y});/*#__PURE__*/return d.createElement(f,{checked:T,disabled:x},/*#__PURE__*/d.createElement(i,b({type:"button",role:"switch","aria-checked":T,"aria-labelledby":P,"aria-required":m,"data-state":q(T),"data-disabled":x?"":void 0,disabled:x,value:k},E,{as:r,ref:S,onClick:l(e.onClick,(e=>{_((e=>!e)),O&&(R.current=e.isPropagationStopped(),R.current||e.stopPropagation())}))})),O&&/*#__PURE__*/d.createElement(v,{control:w,bubbles:!R.current,name:o,value:k,checked:T,required:m,disabled:x,style:{transform:"translateX(-100%)"}}))}));exports.Switch=m;const x="span",k=/*#__PURE__*/d.forwardRef(((e,t)=>{const{as:r=x,...a}=e,c=h("SwitchThumb");/*#__PURE__*/return d.createElement(i,b({"data-state":q(c.checked),"data-disabled":c.disabled?"":void 0},a,{as:r,ref:t}))}));exports.SwitchThumb=k;const v=e=>{const{control:t,checked:r,bubbles:a=!0,...i}=e,n=d.useRef(null),s=o(r),u=c(t);return d.useEffect((()=>{const e=n.current,t=window.HTMLInputElement.prototype,i=Object.getOwnPropertyDescriptor(t,"checked").set;if(s!==r&&i){const t=new Event("click",{bubbles:a});i.call(e,r),e.dispatchEvent(t)}}),[s,r,a]),/*#__PURE__*/d.createElement("input",b({type:"checkbox",defaultChecked:r},i,{tabIndex:-1,ref:n,style:{...e.style,...u,position:"absolute",pointerEvents:"none",opacity:0,margin:0}}))};function q(e){return e?"checked":"unchecked"}const y=m;exports.Root=y;const E=k;exports.Thumb=E;
1
+ var e,t,r,a=require("@radix-ui/react-label").useLabelContext,i=require("@radix-ui/react-primitive").Primitive,c=require("@radix-ui/react-use-size").useSize,o=require("@radix-ui/react-use-previous").usePrevious,n=require("@radix-ui/react-use-controllable-state").useControllableState,u=require("@radix-ui/react-context").createContext,s=require("@radix-ui/react-compose-refs").useComposedRefs,l=require("@radix-ui/primitive").composeEventHandlers,d=(e={},t=require("react"),Object.keys(t).forEach((function(r){"default"!==r&&"__esModule"!==r&&Object.defineProperty(e,r,{enumerable:!0,get:function(){return t[r]}})})),e),b=(r=require("@babel/runtime/helpers/extends"))&&r.__esModule?r.default:r;const[p,f]=u("Switch"),h=/*#__PURE__*/d.forwardRef(((e,t)=>{const{"aria-labelledby":r,name:c,checked:o,defaultChecked:u,required:f,disabled:h,value:m="on",onCheckedChange:v,...q}=e,[y,E]=d.useState(null),w=s(t,(e=>E(e))),C=a(y),S=r||C,g=d.useRef(!1),P=!y||Boolean(y.closest("form")),[R=!1,O]=n({prop:o,defaultProp:u,onChange:v});/*#__PURE__*/return d.createElement(p,{checked:R,disabled:h},/*#__PURE__*/d.createElement(i.button,b({type:"button",role:"switch","aria-checked":R,"aria-labelledby":S,"aria-required":f,"data-state":k(R),"data-disabled":h?"":void 0,disabled:h,value:m},q,{ref:w,onClick:l(e.onClick,(e=>{O((e=>!e)),P&&(g.current=e.isPropagationStopped(),g.current||e.stopPropagation())}))})),P&&/*#__PURE__*/d.createElement(x,{control:y,bubbles:!g.current,name:c,value:m,checked:R,required:f,disabled:h,style:{transform:"translateX(-100%)"}}))}));exports.Switch=h;const m=/*#__PURE__*/d.forwardRef(((e,t)=>{const r=f("SwitchThumb");/*#__PURE__*/return d.createElement(i.span,b({"data-state":k(r.checked),"data-disabled":r.disabled?"":void 0},e,{ref:t}))}));exports.SwitchThumb=m;const x=e=>{const{control:t,checked:r,bubbles:a=!0,...i}=e,n=d.useRef(null),u=o(r),s=c(t);return d.useEffect((()=>{const e=n.current,t=window.HTMLInputElement.prototype,i=Object.getOwnPropertyDescriptor(t,"checked").set;if(u!==r&&i){const t=new Event("click",{bubbles:a});i.call(e,r),e.dispatchEvent(t)}}),[u,r,a]),/*#__PURE__*/d.createElement("input",b({type:"checkbox",defaultChecked:r},i,{tabIndex:-1,ref:n,style:{...e.style,...s,position:"absolute",pointerEvents:"none",opacity:0,margin:0}}))};function k(e){return e?"checked":"unchecked"}const v=h;exports.Root=v;const q=m;exports.Thumb=q;
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"IAYgCA,EAAMC,EAZNC,ybAYAF,KAAMC,mBACpCE,OAAOC,KAAKH,GAAQI,SAAQ,SAASC,GACvB,YAARA,GAA6B,eAARA,GAIzBH,OAAOI,eAAeP,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,SAAUA,EAIV+C,MAAO,CAAEC,UAAW,4CAa9B,MACMC,EAAoB,OAQpBC,eAAc9D,EAAMC,YAAW,CAACC,EAAOC,KAC3C,MAAMC,GAAEA,EAAKyD,KAAsBE,GAAe7D,EAC5C8D,EAAUnE,EAXC,4BAYjB,OACEsC,EAAAC,cAACE,EAADC,EAAA,CACEK,aAAYC,EAASmB,EAAQxD,SAC7BsC,gBAAekB,EAAQpD,SAAW,QAAKmC,GACnCgB,EAHN,CAIE3D,GAAIA,EACJ4C,IAAK7C,8BAeX,MAAM8D,EAAe/D,IACnB,MAAMuD,QAAEA,EAAFjD,QAAWA,EAAXkD,QAAoBA,GAAU,KAASQ,GAAehE,EACtD8C,EAAMhD,EAAM0B,OAAyB,MACrC0B,EAAce,EAAY3D,GAC1B4D,EAAcC,EAAQZ,GAe5B,OAZAzD,EAAMsE,WAAU,KACd,MAAMC,EAAQvB,EAAIK,QACZmB,EAAaC,OAAOC,iBAAiBC,UAErC7C,EADa5C,OAAO0F,yBAAyBJ,EAAY,WACjCK,IAC9B,GAAIzB,IAAgB5C,GAAWsB,EAAY,CACzC,MAAMqB,EAAQ,IAAI2B,MAAM,QAAS,CAAEpB,QAAAA,IACnC5B,EAAWiD,KAAKR,EAAO/D,GACvB+D,EAAMS,cAAc7B,MAErB,CAACC,EAAa5C,EAASkD,iBAGxBvB,EAAAC,cAAA,QAAAG,EAAA,CACEC,KAAK,WACL9B,eAAgBF,GACZ0D,EAHN,CAIEe,UAAW,EACXjC,IAAKA,EACLW,MAAO,IACFzD,EAAMyD,SACNS,EACHc,SAAU,WACVC,cAAe,OACfC,QAAS,EACTC,OAAQ,OAMhB,SAASxC,EAASrC,GAChB,OAAOA,EAAU,UAAY,YAG/B,MAAM8E,EAAOvF,iBACb,MAAMwF,EAAQzB","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 { createContext } 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 Polymorphic from '@radix-ui/react-polymorphic';\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nconst SWITCH_NAME = 'Switch';\nconst SWITCH_DEFAULT_TAG = 'button';\n\ntype InputDOMProps = React.ComponentProps<'input'>;\ntype SwitchOwnProps = Polymorphic.Merge<\n Polymorphic.OwnProps<typeof Primitive>,\n {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: InputDOMProps['required'];\n onCheckedChange?(checked: boolean): void;\n }\n>;\n\ntype SwitchPrimitive = Polymorphic.ForwardRefComponent<typeof SWITCH_DEFAULT_TAG, SwitchOwnProps>;\n\ntype SwitchContextValue = { checked: boolean; disabled?: boolean };\n\nconst [SwitchProvider, useSwitchContext] = createContext<SwitchContextValue>(SWITCH_NAME);\n\nconst Switch = React.forwardRef((props, forwardedRef) => {\n const {\n as = SWITCH_DEFAULT_TAG,\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 checked={checked} disabled={disabled}>\n <Primitive\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 as={as}\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}) as SwitchPrimitive;\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\nconst THUMB_DEFAULT_TAG = 'span';\n\ntype SwitchThumbOwnProps = Polymorphic.OwnProps<typeof Primitive>;\ntype SwitchThumbPrimitive = Polymorphic.ForwardRefComponent<\n typeof THUMB_DEFAULT_TAG,\n SwitchThumbOwnProps\n>;\n\nconst SwitchThumb = React.forwardRef((props, forwardedRef) => {\n const { as = THUMB_DEFAULT_TAG, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME);\n return (\n <Primitive\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n as={as}\n ref={forwardedRef}\n />\n );\n}) as SwitchThumbPrimitive;\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype BubbleInputProps = Omit<React.ComponentProps<'input'>, '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 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 Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n};\nexport type { SwitchPrimitive, SwitchThumbPrimitive };\n"],"names":["dest","source","a","Object","keys","forEach","key","defineProperty","enumerable","get","__esModule","default","SWITCH_DEFAULT_TAG","SwitchProvider","useSwitchContext","createContext","Switch","React","forwardRef","props","forwardedRef","as","aria-labelledby","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","Primitive","_babelRuntimeHelpersExtends","type","role","aria-checked","aria-required","data-state","getState","data-disabled","undefined","ref","onClick","composeEventHandlers","event","prevChecked","current","isPropagationStopped","stopPropagation","$e251e027f448086991ed2332fcb5c67f$var$BubbleInput","control","bubbles","style","transform","THUMB_DEFAULT_TAG","SwitchThumb","thumbProps","context","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":"IAYgCA,EAAMC,EAZNC,ybAYAF,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,ECezC,MAGOU,EAAgBC,GAAoBC,EAHvB,UAcdC,eAASC,EAAMC,YAAuC,CAACC,EAAOC,KAClE,MACE,kBAAmBC,EADfC,KAEJA,EACAC,QAASC,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ACnB,MAAMiD,QAAEA,EAAF7C,QAAWA,EAAX8C,QAAoBA,GAAU,KAASO,GAAezD,EACtDwC,EAAM1C,EAAMwB,OAAyB,MACrCsB,EAAcc,EAAYtD,GAC1BuD,EAAcC,EAAQX,GAe5B,OAZAnD,EAAM+D,WAAU,KACd,MAAMC,EAAQtB,EAAIK,QACZkB,EAAaC,OAAOC,iBAAiBC,UAErCxC,EADazC,OAAOkF,yBAAyBJ,EAAY,WACjCK,IAC9B,GAAIxB,IAAgBxC,GAAWsB,EAAY,CACzC,MAAMiB,EAAQ,IAAI0B,MAAM,QAAS,CAAEnB,QAAAA,IACnCxB,EAAW4C,KAAKR,EAAO1D,GACvB0D,EAAMS,cAAc5B,MAErB,CAACC,EAAaxC,EAAS8C,iBAGxBnB,EAAAC,cAAA,QAAAG,EAAA,CACEC,KAAK,WACL9B,eAAgBF,GACZqD,EAHN,CAIEe,UAAW,EACXhC,IAAKA,EACLW,MAAO,IACFnD,EAAMmD,SACNQ,EACHc,SAAU,WACVC,cAAe,OACfC,QAAS,EACTC,OAAQ,OAMhB,SAAStC,EAASlC,GAChB,OAAOA,EAAU,UAAY,YAG/B,MAAMyE,EAAOhF,iBACb,MAAMiF,EAAQzB","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 { createContext } 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';\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nconst SWITCH_NAME = 'Switch';\n\ntype SwitchContextValue = { checked: boolean; disabled?: boolean };\nconst [SwitchProvider, useSwitchContext] = createContext<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>((props, forwardedRef) => {\n const {\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 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\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, forwardedRef) => {\n const context = useSwitchContext(THUMB_NAME);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...props}\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 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 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","SwitchProvider","useSwitchContext","createContext","Switch","React","forwardRef","props","forwardedRef","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","Primitive","_babelRuntimeHelpersExtends","type","role","getState","undefined","ref","onClick","composeEventHandlers","event","prevChecked","current","isPropagationStopped","stopPropagation","$e251e027f448086991ed2332fcb5c67f$var$BubbleInput","control","bubbles","style","transform","SwitchThumb","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,2 +1,2 @@
1
- import{useLabelContext as e}from"@radix-ui/react-label";import{Primitive as t}from"@radix-ui/react-primitive";import{useSize as r}from"@radix-ui/react-use-size";import{usePrevious as o}from"@radix-ui/react-use-previous";import{useControllableState as a}from"@radix-ui/react-use-controllable-state";import{createContext as c}from"@radix-ui/react-context";import{useComposedRefs as i}from"@radix-ui/react-compose-refs";import{composeEventHandlers as n}from"@radix-ui/primitive";import*as s from"react";import d from"@babel/runtime/helpers/esm/extends";const l="button",[u,m]=c("Switch");export const Switch=/*#__PURE__*/s.forwardRef(((r,o)=>{const{as:c=l,"aria-labelledby":m,name:p,checked:h,defaultChecked:k,required:x,disabled:w,value:v="on",onCheckedChange:y,...E}=r,[S,C]=s.useState(null),g=i(o,(e=>C(e))),R=e(S),T=m||R,P=s.useRef(!1),q=!S||Boolean(S.closest("form")),[I=!1,O]=a({prop:h,defaultProp:k,onChange:y});/*#__PURE__*/return s.createElement(u,{checked:I,disabled:w},/*#__PURE__*/s.createElement(t,d({type:"button",role:"switch","aria-checked":I,"aria-labelledby":T,"aria-required":x,"data-state":f(I),"data-disabled":w?"":void 0,disabled:w,value:v},E,{as:c,ref:g,onClick:n(r.onClick,(e=>{O((e=>!e)),q&&(P.current=e.isPropagationStopped(),P.current||e.stopPropagation())}))})),q&&/*#__PURE__*/s.createElement(b,{control:S,bubbles:!P.current,name:p,value:v,checked:I,required:x,disabled:w,style:{transform:"translateX(-100%)"}}))}));/*#__PURE__*/const p="span";export const SwitchThumb=/*#__PURE__*/s.forwardRef(((e,r)=>{const{as:o=p,...a}=e,c=m("SwitchThumb");/*#__PURE__*/return s.createElement(t,d({"data-state":f(c.checked),"data-disabled":c.disabled?"":void 0},a,{as:o,ref:r}))}));/*#__PURE__*/const b=e=>{const{control:t,checked:a,bubbles:c=!0,...i}=e,n=s.useRef(null),l=o(a),u=r(t);return s.useEffect((()=>{const e=n.current,t=window.HTMLInputElement.prototype,r=Object.getOwnPropertyDescriptor(t,"checked").set;if(l!==a&&r){const t=new Event("click",{bubbles:c});r.call(e,a),e.dispatchEvent(t)}}),[l,a,c]),/*#__PURE__*/s.createElement("input",d({type:"checkbox",defaultChecked:a},i,{tabIndex:-1,ref:n,style:{...e.style,...u,position:"absolute",pointerEvents:"none",opacity:0,margin:0}}))};function f(e){return e?"checked":"unchecked"}export const Root=Switch;export const Thumb=SwitchThumb;
1
+ import{useLabelContext as e}from"@radix-ui/react-label";import{Primitive as t}from"@radix-ui/react-primitive";import{useSize as r}from"@radix-ui/react-use-size";import{usePrevious as o}from"@radix-ui/react-use-previous";import{useControllableState as a}from"@radix-ui/react-use-controllable-state";import{createContext as c}from"@radix-ui/react-context";import{useComposedRefs as i}from"@radix-ui/react-compose-refs";import{composeEventHandlers as n}from"@radix-ui/primitive";import*as s from"react";import d from"@babel/runtime/helpers/esm/extends";const[l,u]=c("Switch");export const Switch=/*#__PURE__*/s.forwardRef(((r,o)=>{const{"aria-labelledby":c,name:u,checked:b,defaultChecked:f,required:h,disabled:k,value:x="on",onCheckedChange:w,...v}=r,[y,E]=s.useState(null),S=i(o,(e=>E(e))),C=e(y),g=c||C,R=s.useRef(!1),T=!y||Boolean(y.closest("form")),[P=!1,q]=a({prop:b,defaultProp:f,onChange:w});/*#__PURE__*/return s.createElement(l,{checked:P,disabled:k},/*#__PURE__*/s.createElement(t.button,d({type:"button",role:"switch","aria-checked":P,"aria-labelledby":g,"aria-required":h,"data-state":p(P),"data-disabled":k?"":void 0,disabled:k,value:x},v,{ref:S,onClick:n(r.onClick,(e=>{q((e=>!e)),T&&(R.current=e.isPropagationStopped(),R.current||e.stopPropagation())}))})),T&&/*#__PURE__*/s.createElement(m,{control:y,bubbles:!R.current,name:u,value:x,checked:P,required:h,disabled:k,style:{transform:"translateX(-100%)"}}))}));/*#__PURE__*/export const SwitchThumb=/*#__PURE__*/s.forwardRef(((e,r)=>{const o=u("SwitchThumb");/*#__PURE__*/return s.createElement(t.span,d({"data-state":p(o.checked),"data-disabled":o.disabled?"":void 0},e,{ref:r}))}));/*#__PURE__*/const m=e=>{const{control:t,checked:a,bubbles:c=!0,...i}=e,n=s.useRef(null),l=o(a),u=r(t);return s.useEffect((()=>{const e=n.current,t=window.HTMLInputElement.prototype,r=Object.getOwnPropertyDescriptor(t,"checked").set;if(l!==a&&r){const t=new Event("click",{bubbles:c});r.call(e,a),e.dispatchEvent(t)}}),[l,a,c]),/*#__PURE__*/s.createElement("input",d({type:"checkbox",defaultChecked:a},i,{tabIndex:-1,ref:n,style:{...e.style,...u,position:"absolute",pointerEvents:"none",opacity:0,margin:0}}))};function p(e){return e?"checked":"unchecked"}export const Root=Switch;export const Thumb=SwitchThumb;
2
2
  //# sourceMappingURL=index.module.js.map
@@ -1 +1 @@
1
- {"mappings":"siBAgBA,MACMA,EAAqB,UAiBpBC,EAAgBC,GAAoBC,EAlBvB,iBAoBpB,MAAMC,oBAASC,EAAMC,YAAW,CAACC,EAAOC,KACtC,MAAMC,GACJA,EAAKT,EACLU,kBAAmBC,EAFfC,KAGJA,EACAC,QAASC,EAJLC,eAKJA,EALIC,SAMJA,EANIC,SAOJA,EAPIC,MAQJA,EAAQ,KARJC,gBASJA,KACGC,GACDb,GACGc,EAAQC,GAAajB,EAAMkB,SAAmC,MAC/DC,EAAeC,EAAgBjB,GAAekB,GAASJ,EAAUI,KACjEC,EAAUC,EAAgBP,GAC1BQ,EAAalB,GAAkBgB,EAC/BG,EAAmCzB,EAAM0B,QAAO,GAEhDC,GAAgBX,GAASY,QAAQZ,EAAOa,QAAQ,UAC/CrB,GAAU,EAAOsB,GAAcC,EAAqB,CACzDC,KAAMvB,EACN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AyBL,EAAY,WACjCM,IAC9B,GAAI1B,IAAgB5C,GAAWsB,EAAY,CACzC,MAAMqB,EAAQ,IAAI4B,MAAM,QAAS,CAAErB,QAAAA,IACnC5B,EAAWkD,KAAKT,EAAO/D,GACvB+D,EAAMU,cAAc9B,MAErB,CAACC,EAAa5C,EAASkD,iBAGxBvB,EAAAC,cAAA,QAAAG,EAAA,CACEC,KAAK,WACL9B,eAAgBF,GACZ0D,EAHN,CAIEgB,UAAW,EACXlC,IAAKA,EACLW,MAAO,IACFzD,EAAMyD,SACNS,EACHe,SAAU,WACVC,cAAe,OACfC,QAAS,EACTC,OAAQ,OAMhB,SAASzC,EAASrC,GAChB,OAAOA,EAAU,UAAY,mBAG/B,MAAM+E,KAAOxF,cACb,MAAMyF,MAAQ1B","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 { createContext } 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 Polymorphic from '@radix-ui/react-polymorphic';\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nconst SWITCH_NAME = 'Switch';\nconst SWITCH_DEFAULT_TAG = 'button';\n\ntype InputDOMProps = React.ComponentProps<'input'>;\ntype SwitchOwnProps = Polymorphic.Merge<\n Polymorphic.OwnProps<typeof Primitive>,\n {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: InputDOMProps['required'];\n onCheckedChange?(checked: boolean): void;\n }\n>;\n\ntype SwitchPrimitive = Polymorphic.ForwardRefComponent<typeof SWITCH_DEFAULT_TAG, SwitchOwnProps>;\n\ntype SwitchContextValue = { checked: boolean; disabled?: boolean };\n\nconst [SwitchProvider, useSwitchContext] = createContext<SwitchContextValue>(SWITCH_NAME);\n\nconst Switch = React.forwardRef((props, forwardedRef) => {\n const {\n as = SWITCH_DEFAULT_TAG,\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 checked={checked} disabled={disabled}>\n <Primitive\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 as={as}\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}) as SwitchPrimitive;\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\nconst THUMB_DEFAULT_TAG = 'span';\n\ntype SwitchThumbOwnProps = Polymorphic.OwnProps<typeof Primitive>;\ntype SwitchThumbPrimitive = Polymorphic.ForwardRefComponent<\n typeof THUMB_DEFAULT_TAG,\n SwitchThumbOwnProps\n>;\n\nconst SwitchThumb = React.forwardRef((props, forwardedRef) => {\n const { as = THUMB_DEFAULT_TAG, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME);\n return (\n <Primitive\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n as={as}\n ref={forwardedRef}\n />\n );\n}) as SwitchThumbPrimitive;\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype BubbleInputProps = Omit<React.ComponentProps<'input'>, '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 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 Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n};\nexport type { SwitchPrimitive, SwitchThumbPrimitive };\n"],"names":["SWITCH_DEFAULT_TAG","SwitchProvider","useSwitchContext","createContext","Switch","React","forwardRef","props","forwardedRef","as","aria-labelledby","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","Primitive","_babelRuntimeHelpersEsmExtends","type","role","aria-checked","aria-required","data-state","getState","data-disabled","undefined","ref","onClick","composeEventHandlers","event","prevChecked","current","isPropagationStopped","stopPropagation","$a39756f91f0c788d079b2ac1958ff$var$BubbleInput","control","bubbles","style","transform","THUMB_DEFAULT_TAG","SwitchThumb","thumbProps","context","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":"siBAgBA,MAGOA,EAAgBC,GAAoBC,EAHvB,iBAcpB,MAAMC,oBAASC,EAAMC,YAAuC,CAACC,EAAOC,KAClE,MACE,kBAAmBC,EADfC,KAEJA,EACAC,QAASC,EAHLC,eAIJA,EAJIC,SAKJA,EALIC,SAMJA,EANIC,MAOJA,EAAQ,KAPJC,gBAQJA,KACGC,GACDX,GACGY,EAAQC,GAAaf,EAAMgB,SAAmC,MAC/DC,EAAeC,EAAgBf,GAAegB,GAASJ,EAAUI,KACjEC,EAAUC,EAAgBP,GAC1BQ,EAAalB,GAAkBgB,EAC/BG,EAAmCvB,EAAMwB,Q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xC,EADayC,OAAOC,yBAAyBL,EAAY,WACjCM,IAC9B,GAAIzB,IAAgBxC,GAAWsB,EAAY,CACzC,MAAMiB,EAAQ,IAAI2B,MAAM,QAAS,CAAEpB,QAAAA,IACnCxB,EAAW6C,KAAKT,EAAO1D,GACvB0D,EAAMU,cAAc7B,MAErB,CAACC,EAAaxC,EAAS8C,iBAGxBnB,EAAAC,cAAA,QAAAG,EAAA,CACEC,KAAK,WACL9B,eAAgBF,GACZqD,EAHN,CAIEgB,UAAW,EACXjC,IAAKA,EACLW,MAAO,IACFnD,EAAMmD,SACNQ,EACHe,SAAU,WACVC,cAAe,OACfC,QAAS,EACTC,OAAQ,OAMhB,SAASvC,EAASlC,GAChB,OAAOA,EAAU,UAAY,mBAG/B,MAAM0E,KAAOjF,cACb,MAAMkF,MAAQ1B","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 { createContext } 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';\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nconst SWITCH_NAME = 'Switch';\n\ntype SwitchContextValue = { checked: boolean; disabled?: boolean };\nconst [SwitchProvider, useSwitchContext] = createContext<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>((props, forwardedRef) => {\n const {\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 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\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, forwardedRef) => {\n const context = useSwitchContext(THUMB_NAME);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...props}\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 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 Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n};\nexport type { SwitchProps, SwitchThumbProps };\n"],"names":["SwitchProvider","useSwitchContext","createContext","Switch","React","forwardRef","props","forwardedRef","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","Primitive","_babelRuntimeHelpersEsmExtends","type","role","getState","undefined","ref","onClick","composeEventHandlers","event","prevChecked","current","isPropagationStopped","stopPropagation","$a39756f91f0c788d079b2ac1958ff$var$BubbleInput","control","bubbles","style","transform","SwitchThumb","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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radix-ui/react-switch",
3
- "version": "0.0.14",
3
+ "version": "0.1.0",
4
4
  "license": "MIT",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
@@ -13,20 +13,18 @@
13
13
  "sideEffects": false,
14
14
  "scripts": {
15
15
  "clean": "rm -rf dist",
16
- "prepublish": "yarn clean",
17
16
  "version": "yarn version"
18
17
  },
19
18
  "dependencies": {
20
19
  "@babel/runtime": "^7.13.10",
21
- "@radix-ui/primitive": "0.0.5",
22
- "@radix-ui/react-compose-refs": "0.0.5",
23
- "@radix-ui/react-context": "0.0.5",
24
- "@radix-ui/react-label": "0.0.14",
25
- "@radix-ui/react-polymorphic": "0.0.12",
26
- "@radix-ui/react-primitive": "0.0.14",
27
- "@radix-ui/react-use-controllable-state": "0.0.6",
28
- "@radix-ui/react-use-previous": "0.0.5",
29
- "@radix-ui/react-use-size": "0.0.6"
20
+ "@radix-ui/primitive": "0.1.0",
21
+ "@radix-ui/react-compose-refs": "0.1.0",
22
+ "@radix-ui/react-context": "0.1.0",
23
+ "@radix-ui/react-label": "0.1.0",
24
+ "@radix-ui/react-primitive": "0.1.0",
25
+ "@radix-ui/react-use-controllable-state": "0.1.0",
26
+ "@radix-ui/react-use-previous": "0.1.0",
27
+ "@radix-ui/react-use-size": "0.1.0"
30
28
  },
31
29
  "peerDependencies": {
32
30
  "react": "^16.8 || ^17.0"