UEA-PRODEM
This commit is contained in:
+27
-4
@@ -1,10 +1,31 @@
|
||||
import * as react_jsx_runtime from 'react/jsx-runtime';
|
||||
import * as _radix_ui_react_context from '@radix-ui/react-context';
|
||||
import { Scope } from '@radix-ui/react-context';
|
||||
import * as React from 'react';
|
||||
import { Primitive } from '@radix-ui/react-primitive';
|
||||
|
||||
type ScopedProps<P> = P & {
|
||||
__scopeSwitch?: Scope;
|
||||
};
|
||||
declare const createSwitchScope: _radix_ui_react_context.CreateScope;
|
||||
interface SwitchProviderProps {
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
required?: boolean;
|
||||
onCheckedChange?(checked: boolean): void;
|
||||
name?: string;
|
||||
form?: string;
|
||||
disabled?: boolean;
|
||||
value?: string | number | readonly string[];
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
declare function SwitchProvider(props: ScopedProps<SwitchProviderProps>): react_jsx_runtime.JSX.Element;
|
||||
interface SwitchTriggerProps extends Omit<React.ComponentPropsWithoutRef<typeof Primitive.button>, keyof SwitchProviderProps> {
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
declare const SwitchTrigger: React.ForwardRefExoticComponent<SwitchTriggerProps & React.RefAttributes<HTMLButtonElement>>;
|
||||
type PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;
|
||||
interface SwitchProps extends PrimitiveButtonProps {
|
||||
interface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
required?: boolean;
|
||||
@@ -15,7 +36,9 @@ type PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;
|
||||
interface SwitchThumbProps extends PrimitiveSpanProps {
|
||||
}
|
||||
declare const SwitchThumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
|
||||
declare const Root: React.ForwardRefExoticComponent<SwitchProps & React.RefAttributes<HTMLButtonElement>>;
|
||||
declare const Thumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
|
||||
type InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;
|
||||
interface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {
|
||||
}
|
||||
declare const SwitchBubbleInput: React.ForwardRefExoticComponent<SwitchBubbleInputProps & React.RefAttributes<HTMLInputElement>>;
|
||||
|
||||
export { Root, Switch, type SwitchProps, SwitchThumb, type SwitchThumbProps, Thumb, createSwitchScope };
|
||||
export { Switch as Root, Switch, type SwitchProps, SwitchThumb, type SwitchThumbProps, SwitchThumb as Thumb, createSwitchScope, SwitchBubbleInput as unstable_BubbleInput, SwitchProvider as unstable_Provider, SwitchBubbleInput as unstable_SwitchBubbleInput, type SwitchBubbleInputProps as unstable_SwitchBubbleInputProps, SwitchProvider as unstable_SwitchProvider, type SwitchProviderProps as unstable_SwitchProviderProps, SwitchTrigger as unstable_SwitchTrigger, type SwitchTriggerProps as unstable_SwitchTriggerProps, SwitchTrigger as unstable_Trigger };
|
||||
|
||||
+27
-4
@@ -1,10 +1,31 @@
|
||||
import * as react_jsx_runtime from 'react/jsx-runtime';
|
||||
import * as _radix_ui_react_context from '@radix-ui/react-context';
|
||||
import { Scope } from '@radix-ui/react-context';
|
||||
import * as React from 'react';
|
||||
import { Primitive } from '@radix-ui/react-primitive';
|
||||
|
||||
type ScopedProps<P> = P & {
|
||||
__scopeSwitch?: Scope;
|
||||
};
|
||||
declare const createSwitchScope: _radix_ui_react_context.CreateScope;
|
||||
interface SwitchProviderProps {
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
required?: boolean;
|
||||
onCheckedChange?(checked: boolean): void;
|
||||
name?: string;
|
||||
form?: string;
|
||||
disabled?: boolean;
|
||||
value?: string | number | readonly string[];
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
declare function SwitchProvider(props: ScopedProps<SwitchProviderProps>): react_jsx_runtime.JSX.Element;
|
||||
interface SwitchTriggerProps extends Omit<React.ComponentPropsWithoutRef<typeof Primitive.button>, keyof SwitchProviderProps> {
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
declare const SwitchTrigger: React.ForwardRefExoticComponent<SwitchTriggerProps & React.RefAttributes<HTMLButtonElement>>;
|
||||
type PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;
|
||||
interface SwitchProps extends PrimitiveButtonProps {
|
||||
interface SwitchProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
required?: boolean;
|
||||
@@ -15,7 +36,9 @@ type PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;
|
||||
interface SwitchThumbProps extends PrimitiveSpanProps {
|
||||
}
|
||||
declare const SwitchThumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
|
||||
declare const Root: React.ForwardRefExoticComponent<SwitchProps & React.RefAttributes<HTMLButtonElement>>;
|
||||
declare const Thumb: React.ForwardRefExoticComponent<SwitchThumbProps & React.RefAttributes<HTMLSpanElement>>;
|
||||
type InputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;
|
||||
interface SwitchBubbleInputProps extends Omit<InputProps, 'checked'> {
|
||||
}
|
||||
declare const SwitchBubbleInput: React.ForwardRefExoticComponent<SwitchBubbleInputProps & React.RefAttributes<HTMLInputElement>>;
|
||||
|
||||
export { Root, Switch, type SwitchProps, SwitchThumb, type SwitchThumbProps, Thumb, createSwitchScope };
|
||||
export { Switch as Root, Switch, type SwitchProps, SwitchThumb, type SwitchThumbProps, SwitchThumb as Thumb, createSwitchScope, SwitchBubbleInput as unstable_BubbleInput, SwitchProvider as unstable_Provider, SwitchBubbleInput as unstable_SwitchBubbleInput, type SwitchBubbleInputProps as unstable_SwitchBubbleInputProps, SwitchProvider as unstable_SwitchProvider, type SwitchProviderProps as unstable_SwitchProviderProps, SwitchTrigger as unstable_SwitchTrigger, type SwitchTriggerProps as unstable_SwitchTriggerProps, SwitchTrigger as unstable_Trigger };
|
||||
|
||||
+161
-69
@@ -31,11 +31,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
||||
// src/index.ts
|
||||
var index_exports = {};
|
||||
__export(index_exports, {
|
||||
Root: () => Root,
|
||||
Root: () => Switch,
|
||||
Switch: () => Switch,
|
||||
SwitchThumb: () => SwitchThumb,
|
||||
Thumb: () => Thumb,
|
||||
createSwitchScope: () => createSwitchScope
|
||||
Thumb: () => SwitchThumb,
|
||||
createSwitchScope: () => createSwitchScope,
|
||||
unstable_BubbleInput: () => SwitchBubbleInput,
|
||||
unstable_Provider: () => SwitchProvider,
|
||||
unstable_SwitchBubbleInput: () => SwitchBubbleInput,
|
||||
unstable_SwitchProvider: () => SwitchProvider,
|
||||
unstable_SwitchTrigger: () => SwitchTrigger,
|
||||
unstable_Trigger: () => SwitchTrigger
|
||||
});
|
||||
module.exports = __toCommonJS(index_exports);
|
||||
|
||||
@@ -51,69 +57,137 @@ var import_react_primitive = require("@radix-ui/react-primitive");
|
||||
var import_jsx_runtime = require("react/jsx-runtime");
|
||||
var SWITCH_NAME = "Switch";
|
||||
var [createSwitchContext, createSwitchScope] = (0, import_react_context.createContextScope)(SWITCH_NAME);
|
||||
var [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
||||
var [SwitchProviderImpl, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
||||
function SwitchProvider(props) {
|
||||
const {
|
||||
__scopeSwitch,
|
||||
checked: checkedProp,
|
||||
children,
|
||||
defaultChecked,
|
||||
disabled,
|
||||
form,
|
||||
name,
|
||||
onCheckedChange,
|
||||
required,
|
||||
value = "on",
|
||||
// @ts-expect-error
|
||||
internal_do_not_use_render
|
||||
} = props;
|
||||
const [checked, setChecked] = (0, import_react_use_controllable_state.useControllableState)({
|
||||
prop: checkedProp,
|
||||
defaultProp: defaultChecked ?? false,
|
||||
onChange: onCheckedChange,
|
||||
caller: SWITCH_NAME
|
||||
});
|
||||
const [control, setControl] = React.useState(null);
|
||||
const [bubbleInput, setBubbleInput] = React.useState(null);
|
||||
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
||||
const isFormControl = control ? !!form || !!control.closest("form") : (
|
||||
// We set this to true by default so that events bubble to forms without JS (SSR)
|
||||
true
|
||||
);
|
||||
const context = {
|
||||
checked,
|
||||
setChecked,
|
||||
disabled,
|
||||
control,
|
||||
setControl,
|
||||
name,
|
||||
form,
|
||||
value,
|
||||
hasConsumerStoppedPropagationRef,
|
||||
required,
|
||||
defaultChecked,
|
||||
isFormControl,
|
||||
bubbleInput,
|
||||
setBubbleInput
|
||||
};
|
||||
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 });
|
||||
}
|
||||
var TRIGGER_NAME = "SwitchTrigger";
|
||||
var SwitchTrigger = React.forwardRef(
|
||||
({ __scopeSwitch, onClick, ...switchProps }, forwardedRef) => {
|
||||
const {
|
||||
value,
|
||||
disabled,
|
||||
checked,
|
||||
required,
|
||||
setControl,
|
||||
setChecked,
|
||||
hasConsumerStoppedPropagationRef,
|
||||
isFormControl,
|
||||
bubbleInput
|
||||
} = useSwitchContext(TRIGGER_NAME, __scopeSwitch);
|
||||
const composedRefs = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, setControl);
|
||||
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
||||
import_react_primitive.Primitive.button,
|
||||
{
|
||||
type: "button",
|
||||
role: "switch",
|
||||
"aria-checked": checked,
|
||||
"aria-required": required,
|
||||
"data-state": getState(checked),
|
||||
"data-disabled": disabled ? "" : void 0,
|
||||
disabled,
|
||||
value,
|
||||
...switchProps,
|
||||
ref: composedRefs,
|
||||
onClick: (0, import_primitive.composeEventHandlers)(onClick, (event) => {
|
||||
setChecked((prevChecked) => !prevChecked);
|
||||
if (bubbleInput && isFormControl) {
|
||||
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
||||
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
||||
}
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
SwitchTrigger.displayName = TRIGGER_NAME;
|
||||
var Switch = React.forwardRef(
|
||||
(props, forwardedRef) => {
|
||||
const {
|
||||
__scopeSwitch,
|
||||
name,
|
||||
checked: checkedProp,
|
||||
checked,
|
||||
defaultChecked,
|
||||
required,
|
||||
disabled,
|
||||
value = "on",
|
||||
value,
|
||||
onCheckedChange,
|
||||
form,
|
||||
...switchProps
|
||||
} = props;
|
||||
const [button, setButton] = React.useState(null);
|
||||
const composedRefs = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, (node) => setButton(node));
|
||||
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
||||
const isFormControl = button ? form || !!button.closest("form") : true;
|
||||
const [checked, setChecked] = (0, import_react_use_controllable_state.useControllableState)({
|
||||
prop: checkedProp,
|
||||
defaultProp: defaultChecked ?? false,
|
||||
onChange: onCheckedChange,
|
||||
caller: SWITCH_NAME
|
||||
});
|
||||
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(SwitchProvider, { scope: __scopeSwitch, checked, disabled, children: [
|
||||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
||||
import_react_primitive.Primitive.button,
|
||||
{
|
||||
type: "button",
|
||||
role: "switch",
|
||||
"aria-checked": checked,
|
||||
"aria-required": required,
|
||||
"data-state": getState(checked),
|
||||
"data-disabled": disabled ? "" : void 0,
|
||||
disabled,
|
||||
value,
|
||||
...switchProps,
|
||||
ref: composedRefs,
|
||||
onClick: (0, import_primitive.composeEventHandlers)(props.onClick, (event) => {
|
||||
setChecked((prevChecked) => !prevChecked);
|
||||
if (isFormControl) {
|
||||
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
||||
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
||||
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
||||
SwitchProvider,
|
||||
{
|
||||
__scopeSwitch,
|
||||
checked,
|
||||
defaultChecked,
|
||||
disabled,
|
||||
required,
|
||||
onCheckedChange,
|
||||
name,
|
||||
form,
|
||||
value,
|
||||
internal_do_not_use_render: ({ isFormControl }) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
||||
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
||||
SwitchTrigger,
|
||||
{
|
||||
...switchProps,
|
||||
ref: forwardedRef,
|
||||
__scopeSwitch
|
||||
}
|
||||
})
|
||||
}
|
||||
),
|
||||
isFormControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
||||
SwitchBubbleInput,
|
||||
{
|
||||
control: button,
|
||||
bubbles: !hasConsumerStoppedPropagationRef.current,
|
||||
name,
|
||||
value,
|
||||
checked,
|
||||
required,
|
||||
disabled,
|
||||
form,
|
||||
style: { transform: "translateX(-100%)" }
|
||||
}
|
||||
)
|
||||
] });
|
||||
),
|
||||
isFormControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
||||
SwitchBubbleInput,
|
||||
{
|
||||
__scopeSwitch
|
||||
}
|
||||
)
|
||||
] })
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
Switch.displayName = SWITCH_NAME;
|
||||
@@ -136,19 +210,25 @@ var SwitchThumb = React.forwardRef(
|
||||
SwitchThumb.displayName = THUMB_NAME;
|
||||
var BUBBLE_INPUT_NAME = "SwitchBubbleInput";
|
||||
var SwitchBubbleInput = React.forwardRef(
|
||||
({
|
||||
__scopeSwitch,
|
||||
control,
|
||||
checked,
|
||||
bubbles = true,
|
||||
...props
|
||||
}, forwardedRef) => {
|
||||
const ref = React.useRef(null);
|
||||
const composedRefs = (0, import_react_compose_refs.useComposedRefs)(ref, forwardedRef);
|
||||
({ __scopeSwitch, ...props }, forwardedRef) => {
|
||||
const {
|
||||
control,
|
||||
hasConsumerStoppedPropagationRef,
|
||||
checked,
|
||||
defaultChecked,
|
||||
required,
|
||||
disabled,
|
||||
name,
|
||||
value,
|
||||
form,
|
||||
bubbleInput,
|
||||
setBubbleInput
|
||||
} = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);
|
||||
const composedRefs = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, setBubbleInput);
|
||||
const prevChecked = (0, import_react_use_previous.usePrevious)(checked);
|
||||
const controlSize = (0, import_react_use_size.useSize)(control);
|
||||
React.useEffect(() => {
|
||||
const input = ref.current;
|
||||
const input = bubbleInput;
|
||||
if (!input) return;
|
||||
const inputProto = window.HTMLInputElement.prototype;
|
||||
const descriptor = Object.getOwnPropertyDescriptor(
|
||||
@@ -156,18 +236,25 @@ var SwitchBubbleInput = React.forwardRef(
|
||||
"checked"
|
||||
);
|
||||
const setChecked = descriptor.set;
|
||||
const bubbles = !hasConsumerStoppedPropagationRef.current;
|
||||
if (prevChecked !== checked && setChecked) {
|
||||
const event = new Event("click", { bubbles });
|
||||
setChecked.call(input, checked);
|
||||
input.dispatchEvent(event);
|
||||
}
|
||||
}, [prevChecked, checked, bubbles]);
|
||||
}, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);
|
||||
const defaultCheckedRef = React.useRef(checked);
|
||||
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
||||
"input",
|
||||
import_react_primitive.Primitive.input,
|
||||
{
|
||||
type: "checkbox",
|
||||
"aria-hidden": true,
|
||||
defaultChecked: checked,
|
||||
defaultChecked: defaultChecked ?? defaultCheckedRef.current,
|
||||
required,
|
||||
disabled,
|
||||
name,
|
||||
value,
|
||||
form,
|
||||
...props,
|
||||
tabIndex: -1,
|
||||
ref: composedRefs,
|
||||
@@ -177,16 +264,21 @@ var SwitchBubbleInput = React.forwardRef(
|
||||
position: "absolute",
|
||||
pointerEvents: "none",
|
||||
opacity: 0,
|
||||
margin: 0
|
||||
margin: 0,
|
||||
// We transform because the input is absolutely positioned but we have
|
||||
// rendered it **after** the button. This pulls it back to sit on top
|
||||
// of the button.
|
||||
transform: "translateX(-100%)"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
SwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;
|
||||
function isFunction(value) {
|
||||
return typeof value === "function";
|
||||
}
|
||||
function getState(checked) {
|
||||
return checked ? "checked" : "unchecked";
|
||||
}
|
||||
var Root = Switch;
|
||||
var Thumb = SwitchThumb;
|
||||
//# sourceMappingURL=index.js.map
|
||||
|
||||
+2
-2
File diff suppressed because one or more lines are too long
+162
-70
@@ -9,72 +9,140 @@ import { useControllableState } from "@radix-ui/react-use-controllable-state";
|
||||
import { usePrevious } from "@radix-ui/react-use-previous";
|
||||
import { useSize } from "@radix-ui/react-use-size";
|
||||
import { Primitive } from "@radix-ui/react-primitive";
|
||||
import { jsx, jsxs } from "react/jsx-runtime";
|
||||
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
||||
var SWITCH_NAME = "Switch";
|
||||
var [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);
|
||||
var [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
||||
var [SwitchProviderImpl, useSwitchContext] = createSwitchContext(SWITCH_NAME);
|
||||
function SwitchProvider(props) {
|
||||
const {
|
||||
__scopeSwitch,
|
||||
checked: checkedProp,
|
||||
children,
|
||||
defaultChecked,
|
||||
disabled,
|
||||
form,
|
||||
name,
|
||||
onCheckedChange,
|
||||
required,
|
||||
value = "on",
|
||||
// @ts-expect-error
|
||||
internal_do_not_use_render
|
||||
} = props;
|
||||
const [checked, setChecked] = useControllableState({
|
||||
prop: checkedProp,
|
||||
defaultProp: defaultChecked ?? false,
|
||||
onChange: onCheckedChange,
|
||||
caller: SWITCH_NAME
|
||||
});
|
||||
const [control, setControl] = React.useState(null);
|
||||
const [bubbleInput, setBubbleInput] = React.useState(null);
|
||||
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
||||
const isFormControl = control ? !!form || !!control.closest("form") : (
|
||||
// We set this to true by default so that events bubble to forms without JS (SSR)
|
||||
true
|
||||
);
|
||||
const context = {
|
||||
checked,
|
||||
setChecked,
|
||||
disabled,
|
||||
control,
|
||||
setControl,
|
||||
name,
|
||||
form,
|
||||
value,
|
||||
hasConsumerStoppedPropagationRef,
|
||||
required,
|
||||
defaultChecked,
|
||||
isFormControl,
|
||||
bubbleInput,
|
||||
setBubbleInput
|
||||
};
|
||||
return /* @__PURE__ */ jsx(SwitchProviderImpl, { scope: __scopeSwitch, ...context, children: isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children });
|
||||
}
|
||||
var TRIGGER_NAME = "SwitchTrigger";
|
||||
var SwitchTrigger = React.forwardRef(
|
||||
({ __scopeSwitch, onClick, ...switchProps }, forwardedRef) => {
|
||||
const {
|
||||
value,
|
||||
disabled,
|
||||
checked,
|
||||
required,
|
||||
setControl,
|
||||
setChecked,
|
||||
hasConsumerStoppedPropagationRef,
|
||||
isFormControl,
|
||||
bubbleInput
|
||||
} = useSwitchContext(TRIGGER_NAME, __scopeSwitch);
|
||||
const composedRefs = useComposedRefs(forwardedRef, setControl);
|
||||
return /* @__PURE__ */ jsx(
|
||||
Primitive.button,
|
||||
{
|
||||
type: "button",
|
||||
role: "switch",
|
||||
"aria-checked": checked,
|
||||
"aria-required": required,
|
||||
"data-state": getState(checked),
|
||||
"data-disabled": disabled ? "" : void 0,
|
||||
disabled,
|
||||
value,
|
||||
...switchProps,
|
||||
ref: composedRefs,
|
||||
onClick: composeEventHandlers(onClick, (event) => {
|
||||
setChecked((prevChecked) => !prevChecked);
|
||||
if (bubbleInput && isFormControl) {
|
||||
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
||||
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
||||
}
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
SwitchTrigger.displayName = TRIGGER_NAME;
|
||||
var Switch = React.forwardRef(
|
||||
(props, forwardedRef) => {
|
||||
const {
|
||||
__scopeSwitch,
|
||||
name,
|
||||
checked: checkedProp,
|
||||
checked,
|
||||
defaultChecked,
|
||||
required,
|
||||
disabled,
|
||||
value = "on",
|
||||
value,
|
||||
onCheckedChange,
|
||||
form,
|
||||
...switchProps
|
||||
} = props;
|
||||
const [button, setButton] = React.useState(null);
|
||||
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
||||
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
||||
const isFormControl = button ? form || !!button.closest("form") : true;
|
||||
const [checked, setChecked] = useControllableState({
|
||||
prop: checkedProp,
|
||||
defaultProp: defaultChecked ?? false,
|
||||
onChange: onCheckedChange,
|
||||
caller: SWITCH_NAME
|
||||
});
|
||||
return /* @__PURE__ */ jsxs(SwitchProvider, { scope: __scopeSwitch, checked, disabled, children: [
|
||||
/* @__PURE__ */ jsx(
|
||||
Primitive.button,
|
||||
{
|
||||
type: "button",
|
||||
role: "switch",
|
||||
"aria-checked": checked,
|
||||
"aria-required": required,
|
||||
"data-state": getState(checked),
|
||||
"data-disabled": disabled ? "" : void 0,
|
||||
disabled,
|
||||
value,
|
||||
...switchProps,
|
||||
ref: composedRefs,
|
||||
onClick: composeEventHandlers(props.onClick, (event) => {
|
||||
setChecked((prevChecked) => !prevChecked);
|
||||
if (isFormControl) {
|
||||
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
||||
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
||||
return /* @__PURE__ */ jsx(
|
||||
SwitchProvider,
|
||||
{
|
||||
__scopeSwitch,
|
||||
checked,
|
||||
defaultChecked,
|
||||
disabled,
|
||||
required,
|
||||
onCheckedChange,
|
||||
name,
|
||||
form,
|
||||
value,
|
||||
internal_do_not_use_render: ({ isFormControl }) => /* @__PURE__ */ jsxs(Fragment, { children: [
|
||||
/* @__PURE__ */ jsx(
|
||||
SwitchTrigger,
|
||||
{
|
||||
...switchProps,
|
||||
ref: forwardedRef,
|
||||
__scopeSwitch
|
||||
}
|
||||
})
|
||||
}
|
||||
),
|
||||
isFormControl && /* @__PURE__ */ jsx(
|
||||
SwitchBubbleInput,
|
||||
{
|
||||
control: button,
|
||||
bubbles: !hasConsumerStoppedPropagationRef.current,
|
||||
name,
|
||||
value,
|
||||
checked,
|
||||
required,
|
||||
disabled,
|
||||
form,
|
||||
style: { transform: "translateX(-100%)" }
|
||||
}
|
||||
)
|
||||
] });
|
||||
),
|
||||
isFormControl && /* @__PURE__ */ jsx(
|
||||
SwitchBubbleInput,
|
||||
{
|
||||
__scopeSwitch
|
||||
}
|
||||
)
|
||||
] })
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
Switch.displayName = SWITCH_NAME;
|
||||
@@ -97,19 +165,25 @@ var SwitchThumb = React.forwardRef(
|
||||
SwitchThumb.displayName = THUMB_NAME;
|
||||
var BUBBLE_INPUT_NAME = "SwitchBubbleInput";
|
||||
var SwitchBubbleInput = React.forwardRef(
|
||||
({
|
||||
__scopeSwitch,
|
||||
control,
|
||||
checked,
|
||||
bubbles = true,
|
||||
...props
|
||||
}, forwardedRef) => {
|
||||
const ref = React.useRef(null);
|
||||
const composedRefs = useComposedRefs(ref, forwardedRef);
|
||||
({ __scopeSwitch, ...props }, forwardedRef) => {
|
||||
const {
|
||||
control,
|
||||
hasConsumerStoppedPropagationRef,
|
||||
checked,
|
||||
defaultChecked,
|
||||
required,
|
||||
disabled,
|
||||
name,
|
||||
value,
|
||||
form,
|
||||
bubbleInput,
|
||||
setBubbleInput
|
||||
} = useSwitchContext(BUBBLE_INPUT_NAME, __scopeSwitch);
|
||||
const composedRefs = useComposedRefs(forwardedRef, setBubbleInput);
|
||||
const prevChecked = usePrevious(checked);
|
||||
const controlSize = useSize(control);
|
||||
React.useEffect(() => {
|
||||
const input = ref.current;
|
||||
const input = bubbleInput;
|
||||
if (!input) return;
|
||||
const inputProto = window.HTMLInputElement.prototype;
|
||||
const descriptor = Object.getOwnPropertyDescriptor(
|
||||
@@ -117,18 +191,25 @@ var SwitchBubbleInput = React.forwardRef(
|
||||
"checked"
|
||||
);
|
||||
const setChecked = descriptor.set;
|
||||
const bubbles = !hasConsumerStoppedPropagationRef.current;
|
||||
if (prevChecked !== checked && setChecked) {
|
||||
const event = new Event("click", { bubbles });
|
||||
setChecked.call(input, checked);
|
||||
input.dispatchEvent(event);
|
||||
}
|
||||
}, [prevChecked, checked, bubbles]);
|
||||
}, [bubbleInput, prevChecked, checked, hasConsumerStoppedPropagationRef]);
|
||||
const defaultCheckedRef = React.useRef(checked);
|
||||
return /* @__PURE__ */ jsx(
|
||||
"input",
|
||||
Primitive.input,
|
||||
{
|
||||
type: "checkbox",
|
||||
"aria-hidden": true,
|
||||
defaultChecked: checked,
|
||||
defaultChecked: defaultChecked ?? defaultCheckedRef.current,
|
||||
required,
|
||||
disabled,
|
||||
name,
|
||||
value,
|
||||
form,
|
||||
...props,
|
||||
tabIndex: -1,
|
||||
ref: composedRefs,
|
||||
@@ -138,23 +219,34 @@ var SwitchBubbleInput = React.forwardRef(
|
||||
position: "absolute",
|
||||
pointerEvents: "none",
|
||||
opacity: 0,
|
||||
margin: 0
|
||||
margin: 0,
|
||||
// We transform because the input is absolutely positioned but we have
|
||||
// rendered it **after** the button. This pulls it back to sit on top
|
||||
// of the button.
|
||||
transform: "translateX(-100%)"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
SwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;
|
||||
function isFunction(value) {
|
||||
return typeof value === "function";
|
||||
}
|
||||
function getState(checked) {
|
||||
return checked ? "checked" : "unchecked";
|
||||
}
|
||||
var Root = Switch;
|
||||
var Thumb = SwitchThumb;
|
||||
export {
|
||||
Root,
|
||||
Switch as Root,
|
||||
Switch,
|
||||
SwitchThumb,
|
||||
Thumb,
|
||||
createSwitchScope
|
||||
SwitchThumb as Thumb,
|
||||
createSwitchScope,
|
||||
SwitchBubbleInput as unstable_BubbleInput,
|
||||
SwitchProvider as unstable_Provider,
|
||||
SwitchBubbleInput as unstable_SwitchBubbleInput,
|
||||
SwitchProvider as unstable_SwitchProvider,
|
||||
SwitchTrigger as unstable_SwitchTrigger,
|
||||
SwitchTrigger as unstable_Trigger
|
||||
};
|
||||
//# sourceMappingURL=index.mjs.map
|
||||
|
||||
+2
-2
File diff suppressed because one or more lines are too long
+17
-17
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@radix-ui/react-switch",
|
||||
"version": "1.2.6",
|
||||
"version": "1.3.0",
|
||||
"license": "MIT",
|
||||
"source": "./src/index.ts",
|
||||
"main": "./dist/index.js",
|
||||
@@ -11,23 +11,21 @@
|
||||
],
|
||||
"sideEffects": false,
|
||||
"dependencies": {
|
||||
"@radix-ui/primitive": "1.1.3",
|
||||
"@radix-ui/react-compose-refs": "1.1.2",
|
||||
"@radix-ui/react-context": "1.1.2",
|
||||
"@radix-ui/react-primitive": "2.1.3",
|
||||
"@radix-ui/react-use-previous": "1.1.1",
|
||||
"@radix-ui/react-use-size": "1.1.1",
|
||||
"@radix-ui/react-use-controllable-state": "1.2.2"
|
||||
"@radix-ui/primitive": "1.1.4",
|
||||
"@radix-ui/react-context": "1.1.4",
|
||||
"@radix-ui/react-compose-refs": "1.1.3",
|
||||
"@radix-ui/react-primitive": "2.1.5",
|
||||
"@radix-ui/react-use-controllable-state": "1.2.3",
|
||||
"@radix-ui/react-use-previous": "1.1.2",
|
||||
"@radix-ui/react-use-size": "1.1.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^19.0.7",
|
||||
"@types/react-dom": "^19.0.3",
|
||||
"eslint": "^9.18.0",
|
||||
"react": "^19.1.0",
|
||||
"react-dom": "^19.1.0",
|
||||
"typescript": "^5.7.3",
|
||||
"@types/react": "^19.2.2",
|
||||
"@types/react-dom": "^19.2.2",
|
||||
"react": "^19.2.0",
|
||||
"react-dom": "^19.2.0",
|
||||
"typescript": "^5.9.3",
|
||||
"@repo/builder": "0.0.0",
|
||||
"@repo/eslint-config": "0.0.0",
|
||||
"@repo/typescript-config": "0.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
@@ -47,14 +45,16 @@
|
||||
"homepage": "https://radix-ui.com/primitives",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/radix-ui/primitives.git"
|
||||
"url": "git+https://github.com/radix-ui/primitives.git",
|
||||
"directory": "packages/react/switch"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/radix-ui/primitives/issues"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint --max-warnings 0 src",
|
||||
"lint": "oxlint --max-warnings 0 src",
|
||||
"clean": "rm -rf dist",
|
||||
"reset": "rm -rf dist node_modules",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"build": "radix-build"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user