@radix-ui/react-switch 0.1.1-rc.1 → 0.1.1-rc.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
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"}
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;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"],"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,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;
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,n=require("@radix-ui/react-use-previous").usePrevious,o=require("@radix-ui/react-use-controllable-state").useControllableState,u=require("@radix-ui/react-context").createContext,s=require("@radix-ui/react-compose-refs").useComposedRefs,d=require("@radix-ui/primitive").composeEventHandlers,l=(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,h]=u("Switch"),f=/*#__PURE__*/l.forwardRef(((e,t)=>{const{"aria-labelledby":r,name:c,checked:n,defaultChecked:u,required:h,disabled:f,value:m="on",onCheckedChange:v,...q}=e,[y,E]=l.useState(null),w=s(t,(e=>E(e))),C=a(y),S=r||C,g=l.useRef(!1),P=!y||Boolean(y.closest("form")),[R=!1,O]=o({prop:n,defaultProp:u,onChange:v});/*#__PURE__*/return l.createElement(p,{checked:R,disabled:f},/*#__PURE__*/l.createElement(i.button,b({type:"button",role:"switch","aria-checked":R,"aria-labelledby":S,"aria-required":h,"data-state":k(R),"data-disabled":f?"":void 0,disabled:f,value:m},q,{ref:w,onClick:d(e.onClick,(e=>{O((e=>!e)),P&&(g.current=e.isPropagationStopped(),g.current||e.stopPropagation())}))})),P&&/*#__PURE__*/l.createElement(x,{control:y,bubbles:!g.current,name:c,value:m,checked:R,required:h,disabled:f,style:{transform:"translateX(-100%)"}}))}));exports.Switch=f;const m=/*#__PURE__*/l.forwardRef(((e,t)=>{const r=h("SwitchThumb");/*#__PURE__*/return l.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,o=l.useRef(null),u=n(r),s=c(t);return l.useEffect((()=>{const e=o.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__*/l.createElement("input",b({type:"checkbox","aria-hidden":!0,defaultChecked:r},i,{tabIndex:-1,ref:o,style:{...e.style,...s,position:"absolute",pointerEvents:"none",opacity:0,margin:0}}))};function k(e){return e?"checked":"unchecked"}const v=f;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,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,EAHLC,eA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iD,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
+ {"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,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,QAAO,GAEhDC,GAAgBX,GAASY,QAAQZ,EAAOa,QAAQ,UAC/CrB,GAAU,EAAOsB,GAAcC,EAAqB,CACzDC,KAAMvB,EACNwB,YAAavB,EACbwB,SAAUpB,iBAGZ,OACEqB,EAAAC,cAACC,EAAD,CAAgB7B,QAASA,EAASI,SAAUA,gBAC1CuB,EAAAC,cAACE,EAAUtB,OAAXuB,EAAA,CACEC,KAAK,SACLC,KAAK,SACL,eAAcjC,EACd,kBAAiBgB,EACjB,gBAAeb,EACf,aAAY+B,EAASlC,GACrB,gBAAeI,EAAW,QAAK+B,EAC/B/B,SAA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ACHc,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 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 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,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;
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 i}from"@radix-ui/react-context";import{useComposedRefs as c}from"@radix-ui/react-compose-refs";import{composeEventHandlers as n}from"@radix-ui/primitive";import*as d from"react";import s from"@babel/runtime/helpers/esm/extends";const[l,u]=i("Switch");export const Switch=/*#__PURE__*/d.forwardRef(((r,o)=>{const{"aria-labelledby":i,name:u,checked:b,defaultChecked:f,required:h,disabled:k,value:x="on",onCheckedChange:w,...v}=r,[y,E]=d.useState(null),S=c(o,(e=>E(e))),C=e(y),g=i||C,R=d.useRef(!1),T=!y||Boolean(y.closest("form")),[P=!1,q]=a({prop:b,defaultProp:f,onChange:w});/*#__PURE__*/return d.createElement(l,{checked:P,disabled:k},/*#__PURE__*/d.createElement(t.button,s({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__*/d.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__*/d.forwardRef(((e,r)=>{const o=u("SwitchThumb");/*#__PURE__*/return d.createElement(t.span,s({"data-state":p(o.checked),"data-disabled":o.disabled?"":void 0},e,{ref:r}))}));/*#__PURE__*/const m=e=>{const{control:t,checked:a,bubbles:i=!0,...c}=e,n=d.useRef(null),l=o(a),u=r(t);return d.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:i});r.call(e,a),e.dispatchEvent(t)}}),[l,a,i]),/*#__PURE__*/d.createElement("input",s({type:"checkbox","aria-hidden":!0,defaultChecked:a},c,{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 Th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,EAAOC,KACN,MAAMqD,EAAU3D,EARD,4BASf,OACEoC,EAAAC,cAACE,EAAUqB,KAAXpB,EAAA,CACE,aAAYG,EAASgB,EAAQlD,SAC7B,gBAAekD,EAAQ9C,SAAW,QAAK+B,GACnCvC,EAHN,CAIEwC,IAAKvC,qBAiBb,MAAMuD,EAAexD,IACnB,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,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"}
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,E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,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,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,WACL,eAAA,EACA9B,eAAgBF,GACZqD,EAJN,CAKEgB,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 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 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.1.1-rc.1",
3
+ "version": "0.1.1-rc.13",
4
4
  "license": "MIT",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
@@ -20,8 +20,8 @@
20
20
  "@radix-ui/primitive": "0.1.0",
21
21
  "@radix-ui/react-compose-refs": "0.1.0",
22
22
  "@radix-ui/react-context": "0.1.0",
23
- "@radix-ui/react-label": "0.1.1-rc.1",
24
- "@radix-ui/react-primitive": "0.1.1-rc.1",
23
+ "@radix-ui/react-label": "0.1.1-rc.13",
24
+ "@radix-ui/react-primitive": "0.1.1-rc.13",
25
25
  "@radix-ui/react-use-controllable-state": "0.1.0",
26
26
  "@radix-ui/react-use-previous": "0.1.0",
27
27
  "@radix-ui/react-use-size": "0.1.0"