Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Switch] fix : Hidden input for Switch does not render "over the button" #3250

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .yarn/versions/9b826bac.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
releases:
"@radix-ui/react-switch": patch

declined:
- primitives
73 changes: 37 additions & 36 deletions packages/react/switch/src/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,44 +57,45 @@ const Switch = React.forwardRef<SwitchElement, SwitchProps>(

return (
<SwitchProvider scope={__scopeSwitch} checked={checked} disabled={disabled}>
<Primitive.button
type="button"
role="switch"
aria-checked={checked}
aria-required={required}
data-state={getState(checked)}
data-disabled={disabled ? '' : undefined}
disabled={disabled}
value={value}
{...switchProps}
ref={composedRefs}
onClick={composeEventHandlers(props.onClick, (event) => {
setChecked((prevChecked) => !prevChecked);
if (isFormControl) {
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
// if switch is in a form, stop propagation from the button so that we only propagate
// one click event (from the input). We propagate changes from an input so that native
// form validation works and form events reflect switch updates.
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
}
})}
/>
{isFormControl && (
<BubbleInput
control={button}
bubbles={!hasConsumerStoppedPropagationRef.current}
name={name}
value={value}
checked={checked}
required={required}
{/*
Wrap with div to prevent input from escaping component bounds
*/}
<div style={{ position: 'relative', display: 'inline-flex' }}>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add div element.

<Primitive.button
type="button"
role="switch"
aria-checked={checked}
aria-required={required}
data-state={getState(checked)}
data-disabled={disabled ? '' : undefined}
disabled={disabled}
form={form}
// 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.
style={{ transform: 'translateX(-100%)' }}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove transform inline style.

value={value}
{...switchProps}
ref={composedRefs}
onClick={composeEventHandlers(props.onClick, (event) => {
setChecked((prevChecked) => !prevChecked);
if (isFormControl) {
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
// if switch is in a form, stop propagation from the button so that we only propagate
// one click event (from the input). We propagate changes from an input so that native
// form validation works and form events reflect switch updates.
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
}
})}
/>
)}
{isFormControl && (
<BubbleInput
control={button}
bubbles={!hasConsumerStoppedPropagationRef.current}
name={name}
value={value}
checked={checked}
required={required}
disabled={disabled}
form={form}
/>
)}
</div>
</SwitchProvider>
);
}
Expand Down