@radix-ui/react-switch 1.3.3 → 1.3.4-rc.1784059564143

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.js CHANGED
@@ -6,6 +6,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
10
  var __export = (target, all) => {
10
11
  for (var name in all)
11
12
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -51,7 +52,6 @@ var import_primitive = require("@radix-ui/primitive");
51
52
  var import_react_compose_refs = require("@radix-ui/react-compose-refs");
52
53
  var import_react_context = require("@radix-ui/react-context");
53
54
  var import_react_use_controllable_state = require("@radix-ui/react-use-controllable-state");
54
- var import_react_use_previous = require("@radix-ui/react-use-previous");
55
55
  var import_react_use_size = require("@radix-ui/react-use-size");
56
56
  var import_react_primitive = require("@radix-ui/react-primitive");
57
57
  var import_jsx_runtime = require("react/jsx-runtime");
@@ -82,6 +82,10 @@ function SwitchProvider(props) {
82
82
  const [control, setControl] = React.useState(null);
83
83
  const [bubbleInput, setBubbleInput] = React.useState(null);
84
84
  const hasConsumerStoppedPropagationRef = React.useRef(false);
85
+ const [userInteractionCount, onUserInteraction] = React.useReducer(
86
+ (count) => count + 1,
87
+ 0
88
+ );
85
89
  const isFormControl = control ? !!form || !!control.closest("form") : (
86
90
  // We set this to true by default so that events bubble to forms without JS (SSR)
87
91
  true
@@ -96,6 +100,8 @@ function SwitchProvider(props) {
96
100
  form,
97
101
  value,
98
102
  hasConsumerStoppedPropagationRef,
103
+ userInteractionCount,
104
+ onUserInteraction,
99
105
  required,
100
106
  defaultChecked,
101
107
  isFormControl,
@@ -104,9 +110,10 @@ function SwitchProvider(props) {
104
110
  };
105
111
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SwitchProviderImpl, { scope: __scopeSwitch, ...context, children: isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children });
106
112
  }
113
+ __name(SwitchProvider, "SwitchProvider");
107
114
  var TRIGGER_NAME = "SwitchTrigger";
108
- var SwitchTrigger = React.forwardRef(
109
- ({ __scopeSwitch, onClick, ...switchProps }, forwardedRef) => {
115
+ var SwitchTrigger = /* @__PURE__ */ React.forwardRef(
116
+ /* @__PURE__ */ __name(function SwitchTrigger2({ __scopeSwitch, onClick, ...switchProps }, forwardedRef) {
110
117
  const {
111
118
  control,
112
119
  form,
@@ -117,6 +124,7 @@ var SwitchTrigger = React.forwardRef(
117
124
  setControl,
118
125
  setChecked,
119
126
  hasConsumerStoppedPropagationRef,
127
+ onUserInteraction,
120
128
  isFormControl,
121
129
  bubbleInput
122
130
  } = useSwitchContext(TRIGGER_NAME, __scopeSwitch);
@@ -125,7 +133,7 @@ var SwitchTrigger = React.forwardRef(
125
133
  React.useEffect(() => {
126
134
  const associatedForm = form ? control?.ownerDocument.getElementById(form) : control?.form;
127
135
  if (associatedForm instanceof HTMLFormElement) {
128
- const reset = () => setChecked(initialCheckedStateRef.current);
136
+ const reset = /* @__PURE__ */ __name(() => setChecked(initialCheckedStateRef.current), "reset");
129
137
  associatedForm.addEventListener("reset", reset);
130
138
  return () => associatedForm.removeEventListener("reset", reset);
131
139
  }
@@ -144,6 +152,7 @@ var SwitchTrigger = React.forwardRef(
144
152
  ...switchProps,
145
153
  ref: composedRefs,
146
154
  onClick: (0, import_primitive.composeEventHandlers)(onClick, (event) => {
155
+ onUserInteraction();
147
156
  setChecked((prevChecked) => !prevChecked);
148
157
  if (bubbleInput && isFormControl) {
149
158
  hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
@@ -152,11 +161,11 @@ var SwitchTrigger = React.forwardRef(
152
161
  })
153
162
  }
154
163
  );
155
- }
164
+ }, "SwitchTrigger")
156
165
  );
157
- SwitchTrigger.displayName = TRIGGER_NAME;
158
- var Switch = React.forwardRef(
159
- (props, forwardedRef) => {
166
+ var Switch = /* @__PURE__ */ React.forwardRef(
167
+ // blank line to reduce diff noise
168
+ /* @__PURE__ */ __name(function Switch2(props, forwardedRef) {
160
169
  const {
161
170
  __scopeSwitch,
162
171
  name,
@@ -199,12 +208,11 @@ var Switch = React.forwardRef(
199
208
  ] })
200
209
  }
201
210
  );
202
- }
211
+ }, "Switch")
203
212
  );
204
- Switch.displayName = SWITCH_NAME;
205
213
  var THUMB_NAME = "SwitchThumb";
206
- var SwitchThumb = React.forwardRef(
207
- (props, forwardedRef) => {
214
+ var SwitchThumb = /* @__PURE__ */ React.forwardRef(
215
+ /* @__PURE__ */ __name(function SwitchThumb2(props, forwardedRef) {
208
216
  const { __scopeSwitch, ...thumbProps } = props;
209
217
  const context = useSwitchContext(THUMB_NAME, __scopeSwitch);
210
218
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
@@ -216,15 +224,16 @@ var SwitchThumb = React.forwardRef(
216
224
  ref: forwardedRef
217
225
  }
218
226
  );
219
- }
227
+ }, "SwitchThumb")
220
228
  );
221
- SwitchThumb.displayName = THUMB_NAME;
222
229
  var BUBBLE_INPUT_NAME = "SwitchBubbleInput";
223
- var SwitchBubbleInput = React.forwardRef(
224
- ({ __scopeSwitch, ...props }, forwardedRef) => {
230
+ var SwitchBubbleInput = /* @__PURE__ */ React.forwardRef(
231
+ // blank line to reduce diff noise
232
+ /* @__PURE__ */ __name(function SwitchBubbleInput2({ __scopeSwitch, onClick, ...props }, forwardedRef) {
225
233
  const {
226
234
  control,
227
235
  hasConsumerStoppedPropagationRef,
236
+ userInteractionCount,
228
237
  checked,
229
238
  defaultChecked,
230
239
  required,
@@ -236,8 +245,10 @@ var SwitchBubbleInput = React.forwardRef(
236
245
  setBubbleInput
237
246
  } = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);
238
247
  const composedRefs = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, setBubbleInput);
239
- const prevChecked = (0, import_react_use_previous.usePrevious)(checked);
240
248
  const controlSize = (0, import_react_use_size.useSize)(control);
249
+ const shouldStopClickPropagationRef = React.useRef(false);
250
+ const prevCheckedRef = React.useRef(checked);
251
+ const prevUserInteractionCountRef = React.useRef(userInteractionCount);
241
252
  React.useEffect(() => {
242
253
  const input = bubbleInput;
243
254
  if (!input) return;
@@ -247,13 +258,19 @@ var SwitchBubbleInput = React.forwardRef(
247
258
  "checked"
248
259
  );
249
260
  const setChecked = descriptor.set;
250
- const bubbles = !hasConsumerStoppedPropagationRef.current;
251
- if (prevChecked !== checked && setChecked) {
261
+ const isUserInteraction = userInteractionCount !== prevUserInteractionCountRef.current;
262
+ prevUserInteractionCountRef.current = userInteractionCount;
263
+ const checkedChanged = prevCheckedRef.current !== checked;
264
+ prevCheckedRef.current = checked;
265
+ const bubbles = !(isUserInteraction && hasConsumerStoppedPropagationRef.current);
266
+ if (checkedChanged && setChecked) {
267
+ shouldStopClickPropagationRef.current = !isUserInteraction;
252
268
  const event = new Event("click", { bubbles });
253
269
  setChecked.call(input, checked);
254
270
  input.dispatchEvent(event);
271
+ shouldStopClickPropagationRef.current = false;
255
272
  }
256
- }, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);
273
+ }, [bubbleInput, checked, hasConsumerStoppedPropagationRef, userInteractionCount]);
257
274
  const defaultCheckedRef = React.useRef(checked);
258
275
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
259
276
  import_react_primitive.Primitive.input,
@@ -269,6 +286,11 @@ var SwitchBubbleInput = React.forwardRef(
269
286
  ...props,
270
287
  tabIndex: -1,
271
288
  ref: composedRefs,
289
+ onClick: (0, import_primitive.composeEventHandlers)(onClick, (event) => {
290
+ if (shouldStopClickPropagationRef.current) {
291
+ event.stopPropagation();
292
+ }
293
+ }),
272
294
  style: {
273
295
  ...props.style,
274
296
  ...controlSize,
@@ -283,13 +305,14 @@ var SwitchBubbleInput = React.forwardRef(
283
305
  }
284
306
  }
285
307
  );
286
- }
308
+ }, "SwitchBubbleInput")
287
309
  );
288
- SwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;
289
310
  function isFunction(value) {
290
311
  return typeof value === "function";
291
312
  }
313
+ __name(isFunction, "isFunction");
292
314
  function getState(checked) {
293
315
  return checked ? "checked" : "unchecked";
294
316
  }
317
+ __name(getState, "getState");
295
318
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts", "../src/switch.tsx"],
4
- "sourcesContent": ["'use client';\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchProvider as unstable_SwitchProvider,\n SwitchTrigger as unstable_SwitchTrigger,\n SwitchThumb,\n SwitchBubbleInput as unstable_SwitchBubbleInput,\n //\n Root,\n Provider as unstable_Provider,\n Trigger as unstable_Trigger,\n Thumb,\n BubbleInput as unstable_BubbleInput,\n} from './switch';\nexport type {\n SwitchProps,\n SwitchProviderProps as unstable_SwitchProviderProps,\n SwitchTriggerProps as unstable_SwitchTriggerProps,\n SwitchThumbProps,\n SwitchBubbleInputProps as unstable_SwitchBubbleInputProps,\n} from './switch';\n", "import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = {\n checked: boolean;\n setChecked: React.Dispatch<React.SetStateAction<boolean>>;\n disabled: boolean | undefined;\n control: HTMLButtonElement | null;\n setControl: React.Dispatch<React.SetStateAction<HTMLButtonElement | null>>;\n name: string | undefined;\n form: string | undefined;\n value: string | number | readonly string[];\n hasConsumerStoppedPropagationRef: React.RefObject<boolean>;\n required: boolean | undefined;\n defaultChecked: boolean | undefined;\n isFormControl: boolean;\n bubbleInput: HTMLInputElement | null;\n setBubbleInput: React.Dispatch<React.SetStateAction<HTMLInputElement | null>>;\n};\n\nconst [SwitchProviderImpl, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchProvider\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SwitchProviderProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n name?: string;\n form?: string;\n disabled?: boolean;\n value?: string | number | readonly string[];\n children?: React.ReactNode;\n}\n\nfunction SwitchProvider(props: ScopedProps<SwitchProviderProps>) {\n const {\n __scopeSwitch,\n checked: checkedProp,\n children,\n defaultChecked,\n disabled,\n form,\n name,\n onCheckedChange,\n required,\n value = 'on',\n // @ts-expect-error\n internal_do_not_use_render,\n } = props;\n\n const [checked, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked ?? false,\n onChange: onCheckedChange,\n caller: SWITCH_NAME,\n });\n const [control, setControl] = React.useState<HTMLButtonElement | null>(null);\n const [bubbleInput, setBubbleInput] = React.useState<HTMLInputElement | null>(null);\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n const isFormControl = control\n ? !!form || !!control.closest('form')\n : // We set this to true by default so that events bubble to forms without JS (SSR)\n true;\n\n const context: SwitchContextValue = {\n checked,\n setChecked,\n disabled,\n control,\n setControl,\n name,\n form,\n value,\n hasConsumerStoppedPropagationRef,\n required,\n defaultChecked,\n isFormControl,\n bubbleInput,\n setBubbleInput,\n };\n\n return (\n <SwitchProviderImpl scope={__scopeSwitch} {...context}>\n {isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children}\n </SwitchProviderImpl>\n );\n}\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchTrigger\n * -----------------------------------------------------------------------------------------------*/\n\nconst TRIGGER_NAME = 'SwitchTrigger';\n\ninterface SwitchTriggerProps extends Omit<\n React.ComponentPropsWithoutRef<typeof Primitive.button>,\n keyof SwitchProviderProps\n> {\n children?: React.ReactNode;\n}\n\nconst SwitchTrigger = React.forwardRef<HTMLButtonElement, SwitchTriggerProps>(\n ({ __scopeSwitch, onClick, ...switchProps }: ScopedProps<SwitchTriggerProps>, forwardedRef) => {\n const {\n control,\n form,\n value,\n disabled,\n checked,\n required,\n setControl,\n setChecked,\n hasConsumerStoppedPropagationRef,\n isFormControl,\n bubbleInput,\n } = useSwitchContext(TRIGGER_NAME, __scopeSwitch);\n const composedRefs = useComposedRefs(forwardedRef, setControl);\n\n const initialCheckedStateRef = React.useRef(checked);\n React.useEffect(() => {\n const associatedForm = form ? control?.ownerDocument.getElementById(form) : control?.form;\n if (associatedForm instanceof HTMLFormElement) {\n const reset = () => setChecked(initialCheckedStateRef.current);\n associatedForm.addEventListener('reset', reset);\n return () => associatedForm.removeEventListener('reset', reset);\n }\n }, [control, form, setChecked]);\n\n return (\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(onClick, (event) => {\n setChecked((prevChecked) => !prevChecked);\n if (bubbleInput && isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch has a bubble input and is a form control, stop\n // propagation from the button so that we only propagate one click\n // event (from the input). We propagate changes from an input so\n // that native form validation works and form events reflect switch\n // updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n );\n },\n);\n\nSwitchTrigger.displayName = TRIGGER_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\ntype SwitchElement = React.ComponentRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = React.forwardRef<SwitchElement, SwitchProps>(\n (props: ScopedProps<SwitchProps>, forwardedRef) => {\n const {\n __scopeSwitch,\n name,\n checked,\n defaultChecked,\n required,\n disabled,\n value,\n onCheckedChange,\n form,\n ...switchProps\n } = props;\n\n return (\n <SwitchProvider\n __scopeSwitch={__scopeSwitch}\n checked={checked}\n defaultChecked={defaultChecked}\n disabled={disabled}\n required={required}\n onCheckedChange={onCheckedChange}\n name={name}\n form={form}\n value={value}\n // @ts-expect-error\n internal_do_not_use_render={({ isFormControl }: SwitchContextValue) => (\n <>\n <SwitchTrigger\n {...switchProps}\n ref={forwardedRef}\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n {isFormControl && (\n <SwitchBubbleInput\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n )}\n </>\n )}\n />\n );\n },\n);\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ComponentRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME, __scopeSwitch);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n ref={forwardedRef}\n />\n );\n },\n);\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchBubbleInput\n * -----------------------------------------------------------------------------------------------*/\n\nconst BUBBLE_INPUT_NAME = 'SwitchBubbleInput';\n\ntype InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;\ninterface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {}\n\nconst SwitchBubbleInput = React.forwardRef<HTMLInputElement, SwitchBubbleInputProps>(\n ({ __scopeSwitch, ...props }: ScopedProps<SwitchBubbleInputProps>, forwardedRef) => {\n const {\n control,\n hasConsumerStoppedPropagationRef,\n checked,\n defaultChecked,\n required,\n disabled,\n name,\n value,\n form,\n bubbleInput,\n setBubbleInput,\n } = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);\n\n const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = bubbleInput;\n if (!input) return;\n\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked',\n ) as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n const bubbles = !hasConsumerStoppedPropagationRef.current;\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n }\n }, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);\n\n const defaultCheckedRef = React.useRef(checked);\n return (\n <Primitive.input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={defaultChecked ?? defaultCheckedRef.current}\n required={required}\n disabled={disabled}\n name={name}\n value={value}\n form={form}\n {...props}\n tabIndex={-1}\n ref={composedRefs}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n transform: 'translateX(-100%)',\n }}\n />\n );\n },\n);\n\nSwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction isFunction(value: unknown): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchProvider,\n SwitchTrigger,\n SwitchThumb,\n SwitchBubbleInput,\n //\n Switch as Root,\n SwitchProvider as Provider,\n SwitchTrigger as Trigger,\n SwitchThumb as Thumb,\n SwitchBubbleInput as BubbleInput,\n};\nexport type {\n SwitchProps,\n SwitchProviderProps,\n SwitchTriggerProps,\n SwitchThumbProps,\n SwitchBubbleInputProps,\n};\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,uBAAqC;AACrC,gCAAgC;AAChC,2BAAmC;AACnC,0CAAqC;AACrC,gCAA4B;AAC5B,4BAAwB;AACxB,6BAA0B;AA4FtB;AAxFJ,IAAM,cAAc;AAGpB,IAAM,CAAC,qBAAqB,iBAAiB,QAAI,yCAAmB,WAAW;AAmB/E,IAAM,CAAC,oBAAoB,gBAAgB,IAAI,oBAAwC,WAAW;AAkBlG,SAAS,eAAe,OAAyC;AAC/D,QAAM;AAAA,IACJ;AAAA,IACA,SAAS;AAAA,IACT;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQ;AAAA;AAAA,IAER;AAAA,EACF,IAAI;AAEJ,QAAM,CAAC,SAAS,UAAU,QAAI,0DAAqB;AAAA,IACjD,MAAM;AAAA,IACN,aAAa,kBAAkB;AAAA,IAC/B,UAAU;AAAA,IACV,QAA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AA,QACJ,UAAU;AAAA,QACV,KAAK;AAAA,QACL,OAAO;AAAA,UACL,GAAG,MAAM;AAAA,UACT,GAAG;AAAA,UACH,UAAU;AAAA,UACV,eAAe;AAAA,UACf,SAAS;AAAA,UACT,QAAQ;AAAA;AAAA;AAAA;AAAA,UAIR,WAAW;AAAA,QACb;AAAA;AAAA,IACF;AAAA,EAEJ;AACF;AAEA,kBAAkB,cAAc;AAIhC,SAAS,WAAW,OAAkD;AACpE,SAAO,OAAO,UAAU;AAC1B;AAEA,SAAS,SAAS,SAAkB;AAClC,SAAO,UAAU,YAAY;AAC/B;",
6
- "names": []
4
+ "sourcesContent": ["'use client';\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchProvider as unstable_SwitchProvider,\n SwitchTrigger as unstable_SwitchTrigger,\n SwitchThumb,\n SwitchBubbleInput as unstable_SwitchBubbleInput,\n //\n Root,\n Provider as unstable_Provider,\n Trigger as unstable_Trigger,\n Thumb,\n BubbleInput as unstable_BubbleInput,\n} from './switch';\nexport type {\n SwitchProps,\n SwitchProviderProps as unstable_SwitchProviderProps,\n SwitchTriggerProps as unstable_SwitchTriggerProps,\n SwitchThumbProps,\n SwitchBubbleInputProps as unstable_SwitchBubbleInputProps,\n} from './switch';\n", "import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = {\n checked: boolean;\n setChecked: React.Dispatch<React.SetStateAction<boolean>>;\n disabled: boolean | undefined;\n control: HTMLButtonElement | null;\n setControl: React.Dispatch<React.SetStateAction<HTMLButtonElement | null>>;\n name: string | undefined;\n form: string | undefined;\n value: string | number | readonly string[];\n hasConsumerStoppedPropagationRef: React.RefObject<boolean>;\n userInteractionCount: number;\n onUserInteraction: () => void;\n required: boolean | undefined;\n defaultChecked: boolean | undefined;\n isFormControl: boolean;\n bubbleInput: HTMLInputElement | null;\n setBubbleInput: React.Dispatch<React.SetStateAction<HTMLInputElement | null>>;\n};\n\nconst [SwitchProviderImpl, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchProvider\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SwitchProviderProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n name?: string;\n form?: string;\n disabled?: boolean;\n value?: string | number | readonly string[];\n children?: React.ReactNode;\n}\n\nfunction SwitchProvider(props: ScopedProps<SwitchProviderProps>) {\n const {\n __scopeSwitch,\n checked: checkedProp,\n children,\n defaultChecked,\n disabled,\n form,\n name,\n onCheckedChange,\n required,\n value = 'on',\n // @ts-expect-error\n internal_do_not_use_render,\n } = props;\n\n const [checked, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked ?? false,\n onChange: onCheckedChange,\n caller: SWITCH_NAME,\n });\n const [control, setControl] = React.useState<HTMLButtonElement | null>(null);\n const [bubbleInput, setBubbleInput] = React.useState<HTMLInputElement | null>(null);\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n\n // Incremented on every user interaction with the trigger. The bubble input\n // compares this against the value it last handled to tell whether a `checked`\n // change was driven by the user (vs. a controlled/programmatic update). Using\n // a counter guarantees the marker is updated in the same commit as the\n // resulting render, so it can never go stale and leak into a later\n // programmatic update.\n const [userInteractionCount, onUserInteraction] = React.useReducer(\n (count: number): number => count + 1,\n 0,\n );\n\n const isFormControl = control\n ? !!form || !!control.closest('form')\n : // We set this to true by default so that events bubble to forms without JS (SSR)\n true;\n\n const context: SwitchContextValue = {\n checked,\n setChecked,\n disabled,\n control,\n setControl,\n name,\n form,\n value,\n hasConsumerStoppedPropagationRef,\n userInteractionCount,\n onUserInteraction,\n required,\n defaultChecked,\n isFormControl,\n bubbleInput,\n setBubbleInput,\n };\n\n return (\n <SwitchProviderImpl scope={__scopeSwitch} {...context}>\n {isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children}\n </SwitchProviderImpl>\n );\n}\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchTrigger\n * -----------------------------------------------------------------------------------------------*/\n\nconst TRIGGER_NAME = 'SwitchTrigger';\n\ninterface SwitchTriggerProps extends Omit<\n React.ComponentPropsWithoutRef<typeof Primitive.button>,\n keyof SwitchProviderProps\n> {\n children?: React.ReactNode;\n}\n\nconst SwitchTrigger = /* @__PURE__ */ React.forwardRef<HTMLButtonElement, SwitchTriggerProps>(\n function SwitchTrigger(\n { __scopeSwitch, onClick, ...switchProps }: ScopedProps<SwitchTriggerProps>,\n forwardedRef,\n ) {\n const {\n control,\n form,\n value,\n disabled,\n checked,\n required,\n setControl,\n setChecked,\n hasConsumerStoppedPropagationRef,\n onUserInteraction,\n isFormControl,\n bubbleInput,\n } = useSwitchContext(TRIGGER_NAME, __scopeSwitch);\n const composedRefs = useComposedRefs(forwardedRef, setControl);\n\n const initialCheckedStateRef = React.useRef(checked);\n React.useEffect(() => {\n const associatedForm = form ? control?.ownerDocument.getElementById(form) : control?.form;\n if (associatedForm instanceof HTMLFormElement) {\n const reset = () => setChecked(initialCheckedStateRef.current);\n associatedForm.addEventListener('reset', reset);\n return () => associatedForm.removeEventListener('reset', reset);\n }\n }, [control, form, setChecked]);\n\n return (\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(onClick, (event) => {\n onUserInteraction();\n setChecked((prevChecked) => !prevChecked);\n if (bubbleInput && isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch has a bubble input and is a form control, stop\n // propagation from the button so that we only propagate one click\n // event (from the input). We propagate changes from an input so\n // that native form validation works and form events reflect switch\n // updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n );\n },\n);\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\ntype SwitchElement = React.ComponentRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = /* @__PURE__ */ React.forwardRef<SwitchElement, SwitchProps>(\n // blank line to reduce diff noise\n function Switch(props: ScopedProps<SwitchProps>, forwardedRef) {\n const {\n __scopeSwitch,\n name,\n checked,\n defaultChecked,\n required,\n disabled,\n value,\n onCheckedChange,\n form,\n ...switchProps\n } = props;\n\n return (\n <SwitchProvider\n __scopeSwitch={__scopeSwitch}\n checked={checked}\n defaultChecked={defaultChecked}\n disabled={disabled}\n required={required}\n onCheckedChange={onCheckedChange}\n name={name}\n form={form}\n value={value}\n // @ts-expect-error\n internal_do_not_use_render={({ isFormControl }: SwitchContextValue) => (\n <>\n <SwitchTrigger\n {...switchProps}\n ref={forwardedRef}\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n {isFormControl && (\n <SwitchBubbleInput\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n )}\n </>\n )}\n />\n );\n },\n);\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ComponentRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = /* @__PURE__ */ React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n function SwitchThumb(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\n/* -------------------------------------------------------------------------------------------------\n * SwitchBubbleInput\n * -----------------------------------------------------------------------------------------------*/\n\nconst BUBBLE_INPUT_NAME = 'SwitchBubbleInput';\n\ntype InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;\ninterface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {}\n\nconst SwitchBubbleInput = /* @__PURE__ */ React.forwardRef<\n HTMLInputElement,\n SwitchBubbleInputProps\n>(\n // blank line to reduce diff noise\n function SwitchBubbleInput(\n { __scopeSwitch, onClick, ...props }: ScopedProps<SwitchBubbleInputProps>,\n forwardedRef,\n ) {\n const {\n control,\n hasConsumerStoppedPropagationRef,\n userInteractionCount,\n checked,\n defaultChecked,\n required,\n disabled,\n name,\n value,\n form,\n bubbleInput,\n setBubbleInput,\n } = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);\n\n const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);\n const controlSize = useSize(control);\n // When the checked change is not driven by a user interaction (e.g. a\n // controlled `checked` update), the `click` event we dispatch to notify\n // forms must not reach ancestor `onClick` handlers. We can't simply make it\n // non-bubbling because React derives the switch's `change` event from a\n // bubbling `click`. Instead we stop propagation of the synthetic click,\n // which still lets the `change` event reach the form.\n const shouldStopClickPropagationRef = React.useRef(false);\n\n // The `checked` value we last synced to the input, and the interaction\n // counter we last accounted for. Comparing against these lets us detect a\n // genuine `checked` change and whether it followed a user interaction, even\n // on renders caused by clicks that don't change `checked` (e.g. a\n // controlled value that ignores the change).\n const prevCheckedRef = React.useRef(checked);\n const prevUserInteractionCountRef = React.useRef(userInteractionCount);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = bubbleInput;\n if (!input) return;\n\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked',\n ) as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n const isUserInteraction = userInteractionCount !== prevUserInteractionCountRef.current;\n prevUserInteractionCountRef.current = userInteractionCount;\n const checkedChanged = prevCheckedRef.current !== checked;\n prevCheckedRef.current = checked;\n\n const bubbles = !(isUserInteraction && hasConsumerStoppedPropagationRef.current);\n if (checkedChanged && setChecked) {\n shouldStopClickPropagationRef.current = !isUserInteraction;\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n shouldStopClickPropagationRef.current = false;\n }\n }, [bubbleInput, checked, hasConsumerStoppedPropagationRef, userInteractionCount]);\n\n const defaultCheckedRef = React.useRef(checked);\n return (\n <Primitive.input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={defaultChecked ?? defaultCheckedRef.current}\n required={required}\n disabled={disabled}\n name={name}\n value={value}\n form={form}\n {...props}\n tabIndex={-1}\n ref={composedRefs}\n onClick={composeEventHandlers(onClick, (event) => {\n // Prevent the synthetic click dispatched on controlled/programmatic\n // updates from triggering ancestor `onClick` handlers, while still\n // allowing the resulting `change` event to reach the form.\n if (shouldStopClickPropagationRef.current) {\n event.stopPropagation();\n }\n })}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n transform: 'translateX(-100%)',\n }}\n />\n );\n },\n);\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction isFunction(value: unknown): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchProvider,\n SwitchTrigger,\n SwitchThumb,\n SwitchBubbleInput,\n //\n Switch as Root,\n SwitchProvider as Provider,\n SwitchTrigger as Trigger,\n SwitchThumb as Thumb,\n SwitchBubbleInput as BubbleInput,\n};\nexport type {\n SwitchProps,\n SwitchProviderProps,\n SwitchTriggerProps,\n SwitchThumbProps,\n SwitchBubbleInputProps,\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,YAAuB;AACvB,uBAAqC;AACrC,gCAAgC;AAChC,2BAAmC;AACnC,0CAAqC;AACrC,4BAAwB;AACxB,6BAA0B;AA4GtB;AAxGJ,IAAM,cAAc;AAGpB,IAAM,CAAC,qBAAqB,iBAAiB,QAAI,yCAAmB,WAAW;AAqB/E,IAAM,CAAC,oBAAoB,gBAAgB,IAAI,oBAAwC,WAAW;AAkBlG,SAAS,eAAe,OAAyC;AAC/D,QAAM;AAAA,IACJ;AAAA,IACA,SAAS;AAAA,IACT;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQ;AAAA;AAAA,IAER;AAAA,EACF,IAAI;AAEJ,QAAM,CAAC,SAAS,UAAU,QAAI,0DAAqB;AAAA,IACjD,MAAM;AAAA,IACN,aAAa,kBAAkB;AAAA,IAC/B,UAAU;AAAA,IACV,QAAQ;AAAA,EACV,CAAC;AACD,QAAM,CAAC,SAAS,UAAU,IAAU,eAAmC,IAAI;AAC3E,QAAM,CAAC,aAAa,cAAc,IAAU,eAAkC,IAAI;AAClF,QAAM,mCAAyC,aAAO,KAAK;AAQ3D,QAAM,CAAC,sBAAsB,iBAAiB,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CAAkC,oBAAoB,CAAC;AAEjF,UAAM,oBAA0B,aAAO,OAAO;AAC9C,WACE;AAAA,MAAC,iCAAU;AAAA,MAAV;AAAA,QACC,MAAK;AAAA,QACL,eAAW;AAAA,QACX,gBAAgB,kBAAkB,kBAAkB;AAAA,QACpD;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACC,GAAG;AAAA,QACJ,UAAU;AAAA,QACV,KAAK;AAAA,QACL,aAAS,uCAAqB,SAAS,CAAC,UAAU;AAIhD,cAAI,8BAA8B,SAAS;AACzC,kBAAM,gBAAgB;AAAA,UACxB;AAAA,QACF,CAAC;AAAA,QACD,OAAO;AAAA,UACL,GAAG,MAAM;AAAA,UACT,GAAG;AAAA,UACH,UAAU;AAAA,UACV,eAAe;AAAA,UACf,SAAS;AAAA,UACT,QAAQ;AAAA;AAAA;AAAA;AAAA,UAIR,WAAW;AAAA,QACb;AAAA;AAAA,IACF;AAAA,EAEJ,GApGA;AAqGF;AAIA,SAAS,WAAW,OAAkD;AACpE,SAAO,OAAO,UAAU;AAC1B;AAFS;AAIT,SAAS,SAAS,SAAkB;AAClC,SAAO,UAAU,YAAY;AAC/B;AAFS;",
6
+ "names": ["SwitchTrigger", "Switch", "SwitchThumb", "SwitchBubbleInput"]
7
7
  }
package/dist/index.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  "use client";
2
+ var __defProp = Object.defineProperty;
3
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
2
4
 
3
5
  // src/switch.tsx
4
6
  import * as React from "react";
@@ -6,7 +8,6 @@ import { composeEventHandlers } from "@radix-ui/primitive";
6
8
  import { useComposedRefs } from "@radix-ui/react-compose-refs";
7
9
  import { createContextScope } from "@radix-ui/react-context";
8
10
  import { useControllableState } from "@radix-ui/react-use-controllable-state";
9
- import { usePrevious } from "@radix-ui/react-use-previous";
10
11
  import { useSize } from "@radix-ui/react-use-size";
11
12
  import { Primitive } from "@radix-ui/react-primitive";
12
13
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
@@ -37,6 +38,10 @@ function SwitchProvider(props) {
37
38
  const [control, setControl] = React.useState(null);
38
39
  const [bubbleInput, setBubbleInput] = React.useState(null);
39
40
  const hasConsumerStoppedPropagationRef = React.useRef(false);
41
+ const [userInteractionCount, onUserInteraction] = React.useReducer(
42
+ (count) => count + 1,
43
+ 0
44
+ );
40
45
  const isFormControl = control ? !!form || !!control.closest("form") : (
41
46
  // We set this to true by default so that events bubble to forms without JS (SSR)
42
47
  true
@@ -51,6 +56,8 @@ function SwitchProvider(props) {
51
56
  form,
52
57
  value,
53
58
  hasConsumerStoppedPropagationRef,
59
+ userInteractionCount,
60
+ onUserInteraction,
54
61
  required,
55
62
  defaultChecked,
56
63
  isFormControl,
@@ -59,9 +66,10 @@ function SwitchProvider(props) {
59
66
  };
60
67
  return /* @__PURE__ */ jsx(SwitchProviderImpl, { scope: __scopeSwitch, ...context, children: isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children });
61
68
  }
69
+ __name(SwitchProvider, "SwitchProvider");
62
70
  var TRIGGER_NAME = "SwitchTrigger";
63
- var SwitchTrigger = React.forwardRef(
64
- ({ __scopeSwitch, onClick, ...switchProps }, forwardedRef) => {
71
+ var SwitchTrigger = /* @__PURE__ */ React.forwardRef(
72
+ /* @__PURE__ */ __name(function SwitchTrigger2({ __scopeSwitch, onClick, ...switchProps }, forwardedRef) {
65
73
  const {
66
74
  control,
67
75
  form,
@@ -72,6 +80,7 @@ var SwitchTrigger = React.forwardRef(
72
80
  setControl,
73
81
  setChecked,
74
82
  hasConsumerStoppedPropagationRef,
83
+ onUserInteraction,
75
84
  isFormControl,
76
85
  bubbleInput
77
86
  } = useSwitchContext(TRIGGER_NAME, __scopeSwitch);
@@ -80,7 +89,7 @@ var SwitchTrigger = React.forwardRef(
80
89
  React.useEffect(() => {
81
90
  const associatedForm = form ? control?.ownerDocument.getElementById(form) : control?.form;
82
91
  if (associatedForm instanceof HTMLFormElement) {
83
- const reset = () => setChecked(initialCheckedStateRef.current);
92
+ const reset = /* @__PURE__ */ __name(() => setChecked(initialCheckedStateRef.current), "reset");
84
93
  associatedForm.addEventListener("reset", reset);
85
94
  return () => associatedForm.removeEventListener("reset", reset);
86
95
  }
@@ -99,6 +108,7 @@ var SwitchTrigger = React.forwardRef(
99
108
  ...switchProps,
100
109
  ref: composedRefs,
101
110
  onClick: composeEventHandlers(onClick, (event) => {
111
+ onUserInteraction();
102
112
  setChecked((prevChecked) => !prevChecked);
103
113
  if (bubbleInput && isFormControl) {
104
114
  hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
@@ -107,11 +117,11 @@ var SwitchTrigger = React.forwardRef(
107
117
  })
108
118
  }
109
119
  );
110
- }
120
+ }, "SwitchTrigger")
111
121
  );
112
- SwitchTrigger.displayName = TRIGGER_NAME;
113
- var Switch = React.forwardRef(
114
- (props, forwardedRef) => {
122
+ var Switch = /* @__PURE__ */ React.forwardRef(
123
+ // blank line to reduce diff noise
124
+ /* @__PURE__ */ __name(function Switch2(props, forwardedRef) {
115
125
  const {
116
126
  __scopeSwitch,
117
127
  name,
@@ -154,12 +164,11 @@ var Switch = React.forwardRef(
154
164
  ] })
155
165
  }
156
166
  );
157
- }
167
+ }, "Switch")
158
168
  );
159
- Switch.displayName = SWITCH_NAME;
160
169
  var THUMB_NAME = "SwitchThumb";
161
- var SwitchThumb = React.forwardRef(
162
- (props, forwardedRef) => {
170
+ var SwitchThumb = /* @__PURE__ */ React.forwardRef(
171
+ /* @__PURE__ */ __name(function SwitchThumb2(props, forwardedRef) {
163
172
  const { __scopeSwitch, ...thumbProps } = props;
164
173
  const context = useSwitchContext(THUMB_NAME, __scopeSwitch);
165
174
  return /* @__PURE__ */ jsx(
@@ -171,15 +180,16 @@ var SwitchThumb = React.forwardRef(
171
180
  ref: forwardedRef
172
181
  }
173
182
  );
174
- }
183
+ }, "SwitchThumb")
175
184
  );
176
- SwitchThumb.displayName = THUMB_NAME;
177
185
  var BUBBLE_INPUT_NAME = "SwitchBubbleInput";
178
- var SwitchBubbleInput = React.forwardRef(
179
- ({ __scopeSwitch, ...props }, forwardedRef) => {
186
+ var SwitchBubbleInput = /* @__PURE__ */ React.forwardRef(
187
+ // blank line to reduce diff noise
188
+ /* @__PURE__ */ __name(function SwitchBubbleInput2({ __scopeSwitch, onClick, ...props }, forwardedRef) {
180
189
  const {
181
190
  control,
182
191
  hasConsumerStoppedPropagationRef,
192
+ userInteractionCount,
183
193
  checked,
184
194
  defaultChecked,
185
195
  required,
@@ -191,8 +201,10 @@ var SwitchBubbleInput = React.forwardRef(
191
201
  setBubbleInput
192
202
  } = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);
193
203
  const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);
194
- const prevChecked = usePrevious(checked);
195
204
  const controlSize = useSize(control);
205
+ const shouldStopClickPropagationRef = React.useRef(false);
206
+ const prevCheckedRef = React.useRef(checked);
207
+ const prevUserInteractionCountRef = React.useRef(userInteractionCount);
196
208
  React.useEffect(() => {
197
209
  const input = bubbleInput;
198
210
  if (!input) return;
@@ -202,13 +214,19 @@ var SwitchBubbleInput = React.forwardRef(
202
214
  "checked"
203
215
  );
204
216
  const setChecked = descriptor.set;
205
- const bubbles = !hasConsumerStoppedPropagationRef.current;
206
- if (prevChecked !== checked && setChecked) {
217
+ const isUserInteraction = userInteractionCount !== prevUserInteractionCountRef.current;
218
+ prevUserInteractionCountRef.current = userInteractionCount;
219
+ const checkedChanged = prevCheckedRef.current !== checked;
220
+ prevCheckedRef.current = checked;
221
+ const bubbles = !(isUserInteraction && hasConsumerStoppedPropagationRef.current);
222
+ if (checkedChanged && setChecked) {
223
+ shouldStopClickPropagationRef.current = !isUserInteraction;
207
224
  const event = new Event("click", { bubbles });
208
225
  setChecked.call(input, checked);
209
226
  input.dispatchEvent(event);
227
+ shouldStopClickPropagationRef.current = false;
210
228
  }
211
- }, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);
229
+ }, [bubbleInput, checked, hasConsumerStoppedPropagationRef, userInteractionCount]);
212
230
  const defaultCheckedRef = React.useRef(checked);
213
231
  return /* @__PURE__ */ jsx(
214
232
  Primitive.input,
@@ -224,6 +242,11 @@ var SwitchBubbleInput = React.forwardRef(
224
242
  ...props,
225
243
  tabIndex: -1,
226
244
  ref: composedRefs,
245
+ onClick: composeEventHandlers(onClick, (event) => {
246
+ if (shouldStopClickPropagationRef.current) {
247
+ event.stopPropagation();
248
+ }
249
+ }),
227
250
  style: {
228
251
  ...props.style,
229
252
  ...controlSize,
@@ -238,15 +261,16 @@ var SwitchBubbleInput = React.forwardRef(
238
261
  }
239
262
  }
240
263
  );
241
- }
264
+ }, "SwitchBubbleInput")
242
265
  );
243
- SwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;
244
266
  function isFunction(value) {
245
267
  return typeof value === "function";
246
268
  }
269
+ __name(isFunction, "isFunction");
247
270
  function getState(checked) {
248
271
  return checked ? "checked" : "unchecked";
249
272
  }
273
+ __name(getState, "getState");
250
274
  export {
251
275
  Switch as Root,
252
276
  Switch,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/switch.tsx"],
4
- "sourcesContent": ["import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = {\n checked: boolean;\n setChecked: React.Dispatch<React.SetStateAction<boolean>>;\n disabled: boolean | undefined;\n control: HTMLButtonElement | null;\n setControl: React.Dispatch<React.SetStateAction<HTMLButtonElement | null>>;\n name: string | undefined;\n form: string | undefined;\n value: string | number | readonly string[];\n hasConsumerStoppedPropagationRef: React.RefObject<boolean>;\n required: boolean | undefined;\n defaultChecked: boolean | undefined;\n isFormControl: boolean;\n bubbleInput: HTMLInputElement | null;\n setBubbleInput: React.Dispatch<React.SetStateAction<HTMLInputElement | null>>;\n};\n\nconst [SwitchProviderImpl, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchProvider\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SwitchProviderProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n name?: string;\n form?: string;\n disabled?: boolean;\n value?: string | number | readonly string[];\n children?: React.ReactNode;\n}\n\nfunction SwitchProvider(props: ScopedProps<SwitchProviderProps>) {\n const {\n __scopeSwitch,\n checked: checkedProp,\n children,\n defaultChecked,\n disabled,\n form,\n name,\n onCheckedChange,\n required,\n value = 'on',\n // @ts-expect-error\n internal_do_not_use_render,\n } = props;\n\n const [checked, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked ?? false,\n onChange: onCheckedChange,\n caller: SWITCH_NAME,\n });\n const [control, setControl] = React.useState<HTMLButtonElement | null>(null);\n const [bubbleInput, setBubbleInput] = React.useState<HTMLInputElement | null>(null);\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n const isFormControl = control\n ? !!form || !!control.closest('form')\n : // We set this to true by default so that events bubble to forms without JS (SSR)\n true;\n\n const context: SwitchContextValue = {\n checked,\n setChecked,\n disabled,\n control,\n setControl,\n name,\n form,\n value,\n hasConsumerStoppedPropagationRef,\n required,\n defaultChecked,\n isFormControl,\n bubbleInput,\n setBubbleInput,\n };\n\n return (\n <SwitchProviderImpl scope={__scopeSwitch} {...context}>\n {isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children}\n </SwitchProviderImpl>\n );\n}\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchTrigger\n * -----------------------------------------------------------------------------------------------*/\n\nconst TRIGGER_NAME = 'SwitchTrigger';\n\ninterface SwitchTriggerProps extends Omit<\n React.ComponentPropsWithoutRef<typeof Primitive.button>,\n keyof SwitchProviderProps\n> {\n children?: React.ReactNode;\n}\n\nconst SwitchTrigger = React.forwardRef<HTMLButtonElement, SwitchTriggerProps>(\n ({ __scopeSwitch, onClick, ...switchProps }: ScopedProps<SwitchTriggerProps>, forwardedRef) => {\n const {\n control,\n form,\n value,\n disabled,\n checked,\n required,\n setControl,\n setChecked,\n hasConsumerStoppedPropagationRef,\n isFormControl,\n bubbleInput,\n } = useSwitchContext(TRIGGER_NAME, __scopeSwitch);\n const composedRefs = useComposedRefs(forwardedRef, setControl);\n\n const initialCheckedStateRef = React.useRef(checked);\n React.useEffect(() => {\n const associatedForm = form ? control?.ownerDocument.getElementById(form) : control?.form;\n if (associatedForm instanceof HTMLFormElement) {\n const reset = () => setChecked(initialCheckedStateRef.current);\n associatedForm.addEventListener('reset', reset);\n return () => associatedForm.removeEventListener('reset', reset);\n }\n }, [control, form, setChecked]);\n\n return (\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(onClick, (event) => {\n setChecked((prevChecked) => !prevChecked);\n if (bubbleInput && isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch has a bubble input and is a form control, stop\n // propagation from the button so that we only propagate one click\n // event (from the input). We propagate changes from an input so\n // that native form validation works and form events reflect switch\n // updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n );\n },\n);\n\nSwitchTrigger.displayName = TRIGGER_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\ntype SwitchElement = React.ComponentRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = React.forwardRef<SwitchElement, SwitchProps>(\n (props: ScopedProps<SwitchProps>, forwardedRef) => {\n const {\n __scopeSwitch,\n name,\n checked,\n defaultChecked,\n required,\n disabled,\n value,\n onCheckedChange,\n form,\n ...switchProps\n } = props;\n\n return (\n <SwitchProvider\n __scopeSwitch={__scopeSwitch}\n checked={checked}\n defaultChecked={defaultChecked}\n disabled={disabled}\n required={required}\n onCheckedChange={onCheckedChange}\n name={name}\n form={form}\n value={value}\n // @ts-expect-error\n internal_do_not_use_render={({ isFormControl }: SwitchContextValue) => (\n <>\n <SwitchTrigger\n {...switchProps}\n ref={forwardedRef}\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n {isFormControl && (\n <SwitchBubbleInput\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n )}\n </>\n )}\n />\n );\n },\n);\n\nSwitch.displayName = SWITCH_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ComponentRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n (props: ScopedProps<SwitchThumbProps>, forwardedRef) => {\n const { __scopeSwitch, ...thumbProps } = props;\n const context = useSwitchContext(THUMB_NAME, __scopeSwitch);\n return (\n <Primitive.span\n data-state={getState(context.checked)}\n data-disabled={context.disabled ? '' : undefined}\n {...thumbProps}\n ref={forwardedRef}\n />\n );\n },\n);\n\nSwitchThumb.displayName = THUMB_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchBubbleInput\n * -----------------------------------------------------------------------------------------------*/\n\nconst BUBBLE_INPUT_NAME = 'SwitchBubbleInput';\n\ntype InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;\ninterface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {}\n\nconst SwitchBubbleInput = React.forwardRef<HTMLInputElement, SwitchBubbleInputProps>(\n ({ __scopeSwitch, ...props }: ScopedProps<SwitchBubbleInputProps>, forwardedRef) => {\n const {\n control,\n hasConsumerStoppedPropagationRef,\n checked,\n defaultChecked,\n required,\n disabled,\n name,\n value,\n form,\n bubbleInput,\n setBubbleInput,\n } = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);\n\n const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = bubbleInput;\n if (!input) return;\n\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked',\n ) as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n const bubbles = !hasConsumerStoppedPropagationRef.current;\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n }\n }, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);\n\n const defaultCheckedRef = React.useRef(checked);\n return (\n <Primitive.input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={defaultChecked ?? defaultCheckedRef.current}\n required={required}\n disabled={disabled}\n name={name}\n value={value}\n form={form}\n {...props}\n tabIndex={-1}\n ref={composedRefs}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n transform: 'translateX(-100%)',\n }}\n />\n );\n },\n);\n\nSwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction isFunction(value: unknown): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchProvider,\n SwitchTrigger,\n SwitchThumb,\n SwitchBubbleInput,\n //\n Switch as Root,\n SwitchProvider as Provider,\n SwitchTrigger as Trigger,\n SwitchThumb as Thumb,\n SwitchBubbleInput as BubbleInput,\n};\nexport type {\n SwitchProps,\n SwitchProviderProps,\n SwitchTriggerProps,\n SwitchThumbProps,\n SwitchBubbleInputProps,\n};\n"],
5
- "mappings": ";;;AAAA,YAAY,WAAW;AACvB,SAAS,4BAA4B;AACrC,SAAS,uBAAuB;AAChC,SAAS,0BAA0B;AACnC,SAAS,4BAA4B;AACrC,SAAS,mBAAmB;AAC5B,SAAS,eAAe;AACxB,SAAS,iBAAiB;AA4FtB,SAsHM,UAtHN,KAsHM,YAtHN;AAxFJ,IAAM,cAAc;AAGpB,IAAM,CAAC,qBAAqB,iBAAiB,IAAI,mBAAmB,WAAW;AAmB/E,IAAM,CAAC,oBAAoB,gBAAgB,IAAI,oBAAwC,WAAW;AAkBlG,SAAS,eAAe,OAAyC;AAC/D,QAAM;AAAA,IACJ;AAAA,IACA,SAAS;AAAA,IACT;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQ;AAAA;AAAA,IAER;AAAA,EACF,IAAI;AAEJ,QAAM,CAAC,SAAS,UAAU,IAAI,qBAAqB;AAAA,IACjD,MAAM;AAAA,IACN,aAAa,kBAAkB;AAAA,IAC/B,UAAU;AAAA,IACV,QAAQ;AAAA,EACV,CAAC;AACD,QAAM,CAAC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O;AAAA,UACL,GAAG,MAAM;AAAA,UACT,GAAG;AAAA,UACH,UAAU;AAAA,UACV,eAAe;AAAA,UACf,SAAS;AAAA,UACT,QAAQ;AAAA;AAAA;AAAA;AAAA,UAIR,WAAW;AAAA,QACb;AAAA;AAAA,IACF;AAAA,EAEJ;AACF;AAEA,kBAAkB,cAAc;AAIhC,SAAS,WAAW,OAAkD;AACpE,SAAO,OAAO,UAAU;AAC1B;AAEA,SAAS,SAAS,SAAkB;AAClC,SAAO,UAAU,YAAY;AAC/B;",
6
- "names": []
4
+ "sourcesContent": ["import * as React from 'react';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type { Scope } from '@radix-ui/react-context';\n\nconst SWITCH_NAME = 'Switch';\n\ntype ScopedProps<P> = P & { __scopeSwitch?: Scope };\nconst [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);\n\ntype SwitchContextValue = {\n checked: boolean;\n setChecked: React.Dispatch<React.SetStateAction<boolean>>;\n disabled: boolean | undefined;\n control: HTMLButtonElement | null;\n setControl: React.Dispatch<React.SetStateAction<HTMLButtonElement | null>>;\n name: string | undefined;\n form: string | undefined;\n value: string | number | readonly string[];\n hasConsumerStoppedPropagationRef: React.RefObject<boolean>;\n userInteractionCount: number;\n onUserInteraction: () => void;\n required: boolean | undefined;\n defaultChecked: boolean | undefined;\n isFormControl: boolean;\n bubbleInput: HTMLInputElement | null;\n setBubbleInput: React.Dispatch<React.SetStateAction<HTMLInputElement | null>>;\n};\n\nconst [SwitchProviderImpl, useSwitchContext] = createSwitchContext<SwitchContextValue>(SWITCH_NAME);\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchProvider\n * -----------------------------------------------------------------------------------------------*/\n\ninterface SwitchProviderProps {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n name?: string;\n form?: string;\n disabled?: boolean;\n value?: string | number | readonly string[];\n children?: React.ReactNode;\n}\n\nfunction SwitchProvider(props: ScopedProps<SwitchProviderProps>) {\n const {\n __scopeSwitch,\n checked: checkedProp,\n children,\n defaultChecked,\n disabled,\n form,\n name,\n onCheckedChange,\n required,\n value = 'on',\n // @ts-expect-error\n internal_do_not_use_render,\n } = props;\n\n const [checked, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked ?? false,\n onChange: onCheckedChange,\n caller: SWITCH_NAME,\n });\n const [control, setControl] = React.useState<HTMLButtonElement | null>(null);\n const [bubbleInput, setBubbleInput] = React.useState<HTMLInputElement | null>(null);\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n\n // Incremented on every user interaction with the trigger. The bubble input\n // compares this against the value it last handled to tell whether a `checked`\n // change was driven by the user (vs. a controlled/programmatic update). Using\n // a counter guarantees the marker is updated in the same commit as the\n // resulting render, so it can never go stale and leak into a later\n // programmatic update.\n const [userInteractionCount, onUserInteraction] = React.useReducer(\n (count: number): number => count + 1,\n 0,\n );\n\n const isFormControl = control\n ? !!form || !!control.closest('form')\n : // We set this to true by default so that events bubble to forms without JS (SSR)\n true;\n\n const context: SwitchContextValue = {\n checked,\n setChecked,\n disabled,\n control,\n setControl,\n name,\n form,\n value,\n hasConsumerStoppedPropagationRef,\n userInteractionCount,\n onUserInteraction,\n required,\n defaultChecked,\n isFormControl,\n bubbleInput,\n setBubbleInput,\n };\n\n return (\n <SwitchProviderImpl scope={__scopeSwitch} {...context}>\n {isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children}\n </SwitchProviderImpl>\n );\n}\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchTrigger\n * -----------------------------------------------------------------------------------------------*/\n\nconst TRIGGER_NAME = 'SwitchTrigger';\n\ninterface SwitchTriggerProps extends Omit<\n React.ComponentPropsWithoutRef<typeof Primitive.button>,\n keyof SwitchProviderProps\n> {\n children?: React.ReactNode;\n}\n\nconst SwitchTrigger = /* @__PURE__ */ React.forwardRef<HTMLButtonElement, SwitchTriggerProps>(\n function SwitchTrigger(\n { __scopeSwitch, onClick, ...switchProps }: ScopedProps<SwitchTriggerProps>,\n forwardedRef,\n ) {\n const {\n control,\n form,\n value,\n disabled,\n checked,\n required,\n setControl,\n setChecked,\n hasConsumerStoppedPropagationRef,\n onUserInteraction,\n isFormControl,\n bubbleInput,\n } = useSwitchContext(TRIGGER_NAME, __scopeSwitch);\n const composedRefs = useComposedRefs(forwardedRef, setControl);\n\n const initialCheckedStateRef = React.useRef(checked);\n React.useEffect(() => {\n const associatedForm = form ? control?.ownerDocument.getElementById(form) : control?.form;\n if (associatedForm instanceof HTMLFormElement) {\n const reset = () => setChecked(initialCheckedStateRef.current);\n associatedForm.addEventListener('reset', reset);\n return () => associatedForm.removeEventListener('reset', reset);\n }\n }, [control, form, setChecked]);\n\n return (\n <Primitive.button\n type=\"button\"\n role=\"switch\"\n aria-checked={checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...switchProps}\n ref={composedRefs}\n onClick={composeEventHandlers(onClick, (event) => {\n onUserInteraction();\n setChecked((prevChecked) => !prevChecked);\n if (bubbleInput && isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if switch has a bubble input and is a form control, stop\n // propagation from the button so that we only propagate one click\n // event (from the input). We propagate changes from an input so\n // that native form validation works and form events reflect switch\n // updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n );\n },\n);\n\n/* -------------------------------------------------------------------------------------------------\n * Switch\n * -----------------------------------------------------------------------------------------------*/\n\ntype SwitchElement = React.ComponentRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: boolean;\n defaultChecked?: boolean;\n required?: boolean;\n onCheckedChange?(checked: boolean): void;\n}\n\nconst Switch = /* @__PURE__ */ React.forwardRef<SwitchElement, SwitchProps>(\n // blank line to reduce diff noise\n function Switch(props: ScopedProps<SwitchProps>, forwardedRef) {\n const {\n __scopeSwitch,\n name,\n checked,\n defaultChecked,\n required,\n disabled,\n value,\n onCheckedChange,\n form,\n ...switchProps\n } = props;\n\n return (\n <SwitchProvider\n __scopeSwitch={__scopeSwitch}\n checked={checked}\n defaultChecked={defaultChecked}\n disabled={disabled}\n required={required}\n onCheckedChange={onCheckedChange}\n name={name}\n form={form}\n value={value}\n // @ts-expect-error\n internal_do_not_use_render={({ isFormControl }: SwitchContextValue) => (\n <>\n <SwitchTrigger\n {...switchProps}\n ref={forwardedRef}\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n {isFormControl && (\n <SwitchBubbleInput\n // @ts-expect-error\n __scopeSwitch={__scopeSwitch}\n />\n )}\n </>\n )}\n />\n );\n },\n);\n\n/* -------------------------------------------------------------------------------------------------\n * SwitchThumb\n * -----------------------------------------------------------------------------------------------*/\n\nconst THUMB_NAME = 'SwitchThumb';\n\ntype SwitchThumbElement = React.ComponentRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface SwitchThumbProps extends PrimitiveSpanProps {}\n\nconst SwitchThumb = /* @__PURE__ */ React.forwardRef<SwitchThumbElement, SwitchThumbProps>(\n function SwitchThumb(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\n/* -------------------------------------------------------------------------------------------------\n * SwitchBubbleInput\n * -----------------------------------------------------------------------------------------------*/\n\nconst BUBBLE_INPUT_NAME = 'SwitchBubbleInput';\n\ntype InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;\ninterface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {}\n\nconst SwitchBubbleInput = /* @__PURE__ */ React.forwardRef<\n HTMLInputElement,\n SwitchBubbleInputProps\n>(\n // blank line to reduce diff noise\n function SwitchBubbleInput(\n { __scopeSwitch, onClick, ...props }: ScopedProps<SwitchBubbleInputProps>,\n forwardedRef,\n ) {\n const {\n control,\n hasConsumerStoppedPropagationRef,\n userInteractionCount,\n checked,\n defaultChecked,\n required,\n disabled,\n name,\n value,\n form,\n bubbleInput,\n setBubbleInput,\n } = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);\n\n const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);\n const controlSize = useSize(control);\n // When the checked change is not driven by a user interaction (e.g. a\n // controlled `checked` update), the `click` event we dispatch to notify\n // forms must not reach ancestor `onClick` handlers. We can't simply make it\n // non-bubbling because React derives the switch's `change` event from a\n // bubbling `click`. Instead we stop propagation of the synthetic click,\n // which still lets the `change` event reach the form.\n const shouldStopClickPropagationRef = React.useRef(false);\n\n // The `checked` value we last synced to the input, and the interaction\n // counter we last accounted for. Comparing against these lets us detect a\n // genuine `checked` change and whether it followed a user interaction, even\n // on renders caused by clicks that don't change `checked` (e.g. a\n // controlled value that ignores the change).\n const prevCheckedRef = React.useRef(checked);\n const prevUserInteractionCountRef = React.useRef(userInteractionCount);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = bubbleInput;\n if (!input) return;\n\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(\n inputProto,\n 'checked',\n ) as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n const isUserInteraction = userInteractionCount !== prevUserInteractionCountRef.current;\n prevUserInteractionCountRef.current = userInteractionCount;\n const checkedChanged = prevCheckedRef.current !== checked;\n prevCheckedRef.current = checked;\n\n const bubbles = !(isUserInteraction && hasConsumerStoppedPropagationRef.current);\n if (checkedChanged && setChecked) {\n shouldStopClickPropagationRef.current = !isUserInteraction;\n const event = new Event('click', { bubbles });\n setChecked.call(input, checked);\n input.dispatchEvent(event);\n shouldStopClickPropagationRef.current = false;\n }\n }, [bubbleInput, checked, hasConsumerStoppedPropagationRef, userInteractionCount]);\n\n const defaultCheckedRef = React.useRef(checked);\n return (\n <Primitive.input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={defaultChecked ?? defaultCheckedRef.current}\n required={required}\n disabled={disabled}\n name={name}\n value={value}\n form={form}\n {...props}\n tabIndex={-1}\n ref={composedRefs}\n onClick={composeEventHandlers(onClick, (event) => {\n // Prevent the synthetic click dispatched on controlled/programmatic\n // updates from triggering ancestor `onClick` handlers, while still\n // allowing the resulting `change` event to reach the form.\n if (shouldStopClickPropagationRef.current) {\n event.stopPropagation();\n }\n })}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n transform: 'translateX(-100%)',\n }}\n />\n );\n },\n);\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction isFunction(value: unknown): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n\nfunction getState(checked: boolean) {\n return checked ? 'checked' : 'unchecked';\n}\n\nexport {\n createSwitchScope,\n //\n Switch,\n SwitchProvider,\n SwitchTrigger,\n SwitchThumb,\n SwitchBubbleInput,\n //\n Switch as Root,\n SwitchProvider as Provider,\n SwitchTrigger as Trigger,\n SwitchThumb as Thumb,\n SwitchBubbleInput as BubbleInput,\n};\nexport type {\n SwitchProps,\n SwitchProviderProps,\n SwitchTriggerProps,\n SwitchThumbProps,\n SwitchBubbleInputProps,\n};\n"],
5
+ "mappings": ";;;;;AAAA,YAAY,WAAW;AACvB,SAAS,4BAA4B;AACrC,SAAS,uBAAuB;AAChC,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;AAAA,UACf,SAAS;AAAA,UACT,QAAQ;AAAA;AAAA;AAAA;AAAA,UAIR,WAAW;AAAA,QACb;AAAA;AAAA,IACF;AAAA,EAEJ,GApGA;AAqGF;AAIA,SAAS,WAAW,OAAkD;AACpE,SAAO,OAAO,UAAU;AAC1B;AAFS;AAIT,SAAS,SAAS,SAAkB;AAClC,SAAO,UAAU,YAAY;AAC/B;AAFS;",
6
+ "names": ["SwitchTrigger", "Switch", "SwitchThumb", "SwitchBubbleInput"]
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radix-ui/react-switch",
3
- "version": "1.3.3",
3
+ "version": "1.3.4-rc.1784059564143",
4
4
  "license": "MIT",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./dist/index.js",
@@ -12,12 +12,11 @@
12
12
  "sideEffects": false,
13
13
  "dependencies": {
14
14
  "@radix-ui/primitive": "1.1.5",
15
- "@radix-ui/react-compose-refs": "1.1.3",
16
- "@radix-ui/react-context": "1.2.0",
17
15
  "@radix-ui/react-primitive": "2.1.7",
16
+ "@radix-ui/react-compose-refs": "1.1.3",
18
17
  "@radix-ui/react-use-controllable-state": "1.2.3",
19
- "@radix-ui/react-use-previous": "1.1.2",
20
- "@radix-ui/react-use-size": "1.1.2"
18
+ "@radix-ui/react-use-size": "1.1.2",
19
+ "@radix-ui/react-context": "1.2.0"
21
20
  },
22
21
  "devDependencies": {
23
22
  "@types/react": "^19.2.2",