Skip to content

Commit

Permalink
[Label] Don't eagerly prevent double-click
Browse files Browse the repository at this point in the history
Fixes #2656
  • Loading branch information
benoitgrelard committed Mar 5, 2024
1 parent 7d884d2 commit c1e9c99
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .yarn/versions/ded3a040.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
releases:
"@radix-ui/react-form": patch
"@radix-ui/react-label": patch

declined:
- primitives
7 changes: 7 additions & 0 deletions packages/react/label/src/Label.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export const WithControl = () => {
<Control className={controlClass()} /> Label
</Label>

<br />

<Label>
<span>Name:</span>
<input type="number" />
</Label>

<h1>Referencing control</h1>
<Control id="control" className={controlClass()} />
<Label htmlFor="control">Label</Label>
Expand Down
4 changes: 4 additions & 0 deletions packages/react/label/src/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const Label = React.forwardRef<LabelElement, LabelProps>((props, forwardedRef) =
{...props}
ref={forwardedRef}
onMouseDown={(event) => {
// only prevent text selection if clicking inside the label itself
const target = event.target as HTMLElement;
if (target.closest('input, button')) return;

props.onMouseDown?.(event);
// prevent text selection when double clicking label
if (!event.defaultPrevented && event.detail > 1) event.preventDefault();
Expand Down

0 comments on commit c1e9c99

Please sign in to comment.