@radix-ui/react-switch 1.0.3-rc.5 → 1.0.3-rc.7

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.
@@ -0,0 +1,20 @@
1
+ import * as React from "react";
2
+ import * as Radix from "@radix-ui/react-primitive";
3
+ import { Primitive } from "@radix-ui/react-primitive";
4
+ export const createSwitchScope: import("@radix-ui/react-context").CreateScope;
5
+ type PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;
6
+ export interface SwitchProps extends PrimitiveButtonProps {
7
+ checked?: boolean;
8
+ defaultChecked?: boolean;
9
+ required?: boolean;
10
+ onCheckedChange?(checked: boolean): void;
11
+ }
12
+ export const Switch: React.ForwardRefExoticComponent<SwitchProps & React.RefAttributes<HTMLButtonElement>>;
13
+ type PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;
14
+ export interface SwitchThumbProps extends PrimitiveSpanProps {
15
+ }
16
+ export const SwitchThumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
17
+ export const Root: React.ForwardRefExoticComponent<SwitchProps & React.RefAttributes<HTMLButtonElement>>;
18
+ export const Thumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
19
+
20
+ //# sourceMappingURL=index.d.ts.map
@@ -141,4 +141,4 @@ const $6be4966fd9bbc698$export$6521433ed15a34db = $6be4966fd9bbc698$export$4d07b
141
141
 
142
142
 
143
143
  export {$6be4966fd9bbc698$export$cf7f5f17f69cbd43 as createSwitchScope, $6be4966fd9bbc698$export$b5d5cf8927ab7262 as Switch, $6be4966fd9bbc698$export$4d07bf653ea69106 as SwitchThumb, $6be4966fd9bbc698$export$be92b6f5f03c0fe9 as Root, $6be4966fd9bbc698$export$6521433ed15a34db as Thumb};
144
- //# sourceMappingURL=index.module.js.map
144
+ //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"mappings":";;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ADvLA","sources":["packages/react/switch/src/index.ts","packages/react/switch/src/Switch.tsx"],"sourcesContent":["export {\n createSwitchScope,\n //\n Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n} from './Switch';\nexport type { SwitchProps, SwitchThumbProps } from './Switch';\n","import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type * as Radix from '@radix-ui/react-primitive';\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = { checked: boolean; disabled?: boolean };\nconst [SwitchProvider, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\ntype SwitchElement = React.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends PrimitiveButtonProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = React.forwardRef<SwitchElement, SwitchProps>(\n (props: ScopedProps<SwitchProps>, forwardedRef) => {\n const {\n __scopeSwitch,\n 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 hasConsumerStoppedPropagationRef = React.useRef(false);\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = button ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\n });\n\n return (\n <SwitchProvider scope={__scopeSwitch} checked={checked} disabled={disabled}>\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(props.onClick, (event) => {\n setChecked((prevChecked) => !prevChecked);\n if (isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect switch updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n {isFormControl && (\n <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </SwitchProvider>\n );\n }\n);\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME, __scopeSwitch);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n ref={forwardedRef}\n />\n );\n }\n);\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!;\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor;\n const setChecked = descriptor.set;\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n }\n }, [prevChecked, checked, bubbles]);\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n );\n};\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nconst Root = Switch;\nconst Thumb = SwitchThumb;\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n};\nexport type { SwitchProps, SwitchThumbProps };\n"],"names":["createSwitchScope","Switch","SwitchThumb","Root","Thumb","React","composeEventHandlers","useComposedRefs","createContextScope","useControllableState","usePrevious","useSize","Primitive","SWITCH_NAME","createSwitchContext","SwitchProvider","useSwitchContext","forwardRef","props","forwardedRef","__scopeSwitch","name","checked","checkedProp","defaultChecked","required","disabled","value","onCheckedChange","switchProps","button","setButton","useState","composedRefs","node","hasConsumerStoppedPropagationRef","useRef","isFormControl","Boolean","closest","setChecked","prop","defaultProp","onChange","getState","undefined","onClick","event","prevChecked","current","isPropagationStopped","stopPropagation","transform","THUMB_NAME","thumbProps","context","BubbleInput","control","bubbles","inputProps","ref","controlSize","useEffect","input","inputProto","window","HTMLInputElement","prototype","descriptor","Object","getOwnPropertyDescriptor","set","Event","call","dispatchEvent","style","position","pointerEvents","opacity","margin"],"version":3,"file":"index.module.js.map"}
1
+ {"mappings":";;;;;;;;;;A;;;;;;;;;ACYA;;oGAEA,CAEA,MAAMa,iCAAW,GAAG,QAApB,AAAA;AAGA,MAAM,CAACC,yCAAD,EAAsBd,yCAAtB,CAAA,GAA2CQ,yBAAkB,CAACK,iCAAD,CAAnE,AAAA;AAGA,MAAM,CAACE,oCAAD,EAAiBC,sCAAjB,CAAA,GAAqCF,yCAAmB,CAAqBD,iCAArB,CAA9D,AAAA;AAWA,MAAMZ,yCAAM,GAAA,aAAGI,CAAAA,iBAAA,CACb,CAACa,KAAD,EAAkCC,YAAlC,GAAmD;IACjD,MAAM,E,eACJC,aADI,CAAA,E,MAEJC,IAFI,CAAA,EAGJC,OAAO,EAAEC,WAHL,CAAA,E,gBAIJC,cAJI,CAAA,E,UAKJC,QALI,CAAA,E,UAMJC,QANI,CAAA,SAOJC,KAAK,GAAG,IAPJ,G,iBAQJC,eARI,CAAA,EASJ,GAAGC,WAAH,EATI,GAUFX,KAVJ,AAAM;IAWN,MAAM,CAACY,MAAD,EAASC,SAAT,CAAA,GAAsB1B,eAAA,CAAyC,IAAzC,CAA5B,AAAA;IACA,MAAM4B,YAAY,GAAG1B,sBAAe,CAACY,YAAD,EAAgBe,CAAAA,IAAD,GAAUH,SAAS,CAACG,IAAD,CAAlC;IAAA,CAApC,AAAA;IACA,MAAMC,gCAAgC,GAAG9B,aAAA,CAAa,KAAb,CAAzC,AAdiD,EAejD,iFADA;IAEA,MAAMgC,aAAa,GAAGP,MAAM,GAAGQ,OAAO,CAACR,MAAM,CAACS,O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AILC,aAAa,EAAE,MAJV;YAKLC,OAAO,EAAE,CALJ;YAMLC,MAAM,EAAE,CAARA;SANK;KAPT,CAAA,CADF,CACE;CApBJ,AAqCC;AAED,SAASnC,8BAAT,CAAkBtB,OAAlB,EAAoC;IAClC,OAAOA,OAAO,GAAG,SAAH,GAAe,WAA7B,CAAA;CACD;AAED,MAAMnB,yCAAI,GAAGF,yCAAb,AAAA;AACA,MAAMG,yCAAK,GAAGF,yCAAd,AAAA;;ADvLA","sources":["packages/react/switch/src/index.ts","packages/react/switch/src/Switch.tsx"],"sourcesContent":["export {\n createSwitchScope,\n //\n Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n} from './Switch';\nexport type { SwitchProps, SwitchThumbProps } from './Switch';\n","import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type * as Radix from '@radix-ui/react-primitive';\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = { checked: boolean; disabled?: boolean };\nconst [SwitchProvider, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\ntype SwitchElement = React.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends PrimitiveButtonProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = React.forwardRef<SwitchElement, SwitchProps>(\n (props: ScopedProps<SwitchProps>, forwardedRef) => {\n const {\n __scopeSwitch,\n 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 hasConsumerStoppedPropagationRef = React.useRef(false);\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = button ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\n });\n\n return (\n <SwitchProvider scope={__scopeSwitch} checked={checked} disabled={disabled}>\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(props.onClick, (event) => {\n setChecked((prevChecked) => !prevChecked);\n if (isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect switch updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n {isFormControl && (\n <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </SwitchProvider>\n );\n }\n);\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME, __scopeSwitch);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n ref={forwardedRef}\n />\n );\n }\n);\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: boolean;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!;\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor;\n const setChecked = descriptor.set;\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n }\n }, [prevChecked, checked, bubbles]);\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n );\n};\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nconst Root = Switch;\nconst Thumb = SwitchThumb;\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchThumb,\n //\n Root,\n Thumb,\n};\nexport type { SwitchProps, SwitchThumbProps };\n"],"names":["createSwitchScope","Switch","SwitchThumb","Root","Thumb","React","composeEventHandlers","useComposedRefs","createContextScope","useControllableState","usePrevious","useSize","Primitive","SWITCH_NAME","createSwitchContext","SwitchProvider","useSwitchContext","forwardRef","props","forwardedRef","__scopeSwitch","name","checked","checkedProp","defaultChecked","required","disabled","value","onCheckedChange","switchProps","button","setButton","useState","composedRefs","node","hasConsumerStoppedPropagationRef","useRef","isFormControl","Boolean","closest","setChecked","prop","defaultProp","onChange","getState","undefined","onClick","event","prevChecked","current","isPropagationStopped","stopPropagation","transform","THUMB_NAME","thumbProps","context","BubbleInput","control","bubbles","inputProps","ref","controlSize","useEffect","input","inputProto","window","HTMLInputElement","prototype","descriptor","Object","getOwnPropertyDescriptor","set","Event","call","dispatchEvent","style","position","pointerEvents","opacity","margin"],"version":3,"file":"index.mjs.map"}
package/package.json CHANGED
@@ -1,11 +1,23 @@
1
1
  {
2
2
  "name": "@radix-ui/react-switch",
3
- "version": "1.0.3-rc.5",
3
+ "version": "1.0.3-rc.7",
4
4
  "license": "MIT",
5
- "source": "src/index.ts",
6
- "main": "dist/index.js",
7
- "module": "dist/index.module.js",
8
- "types": "dist/index.d.ts",
5
+ "exports": {
6
+ ".": {
7
+ "import": {
8
+ "types": "./dist/index.d.mts",
9
+ "default": "./dist/index.mjs"
10
+ },
11
+ "require": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ }
15
+ }
16
+ },
17
+ "source": "./src/index.ts",
18
+ "main": "./dist/index.js",
19
+ "module": "./dist/index.mjs",
20
+ "types": "./dist/index.d.ts",
9
21
  "files": [
10
22
  "dist",
11
23
  "README.md"
@@ -17,13 +29,13 @@
17
29
  },
18
30
  "dependencies": {
19
31
  "@babel/runtime": "^7.13.10",
20
- "@radix-ui/primitive": "1.0.0",
21
- "@radix-ui/react-compose-refs": "1.0.0",
22
- "@radix-ui/react-context": "1.0.0",
23
- "@radix-ui/react-primitive": "1.0.3-rc.5",
24
- "@radix-ui/react-use-controllable-state": "1.0.0",
25
- "@radix-ui/react-use-previous": "1.0.0",
26
- "@radix-ui/react-use-size": "1.0.0"
32
+ "@radix-ui/primitive": "1.0.1-rc.2",
33
+ "@radix-ui/react-compose-refs": "1.0.1-rc.2",
34
+ "@radix-ui/react-context": "1.0.1-rc.2",
35
+ "@radix-ui/react-primitive": "1.0.3-rc.7",
36
+ "@radix-ui/react-use-controllable-state": "1.0.1-rc.2",
37
+ "@radix-ui/react-use-previous": "1.0.1-rc.2",
38
+ "@radix-ui/react-use-size": "1.0.1-rc.2"
27
39
  },
28
40
  "peerDependencies": {
29
41
  "react": "^16.8 || ^17.0 || ^18.0",