Skip to content

Commit

Permalink
[Label] Don't eagerly prevent double-click (radix-ui#2753)
Browse files Browse the repository at this point in the history
* [Label] Don't eagerly prevent double-click

Fixes radix-ui#2656

* Update Label.stories.tsx

* Update Label.tsx

* PR feedback
  • Loading branch information
benoitgrelard authored Mar 5, 2024
1 parent b5b3162 commit ad69155
Show file tree
Hide file tree
Showing 3 changed files with 19 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
9 changes: 9 additions & 0 deletions packages/react/label/src/Label.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export const WithControl = () => {
);
};

export const WithInputNumber = (props: any) => {
return (
<Label>
<span>Name:</span>
<input type="number" />
</Label>
);
};

const Control = (props: any) => {
return (
<button className={controlClass()} {...props} onClick={() => window.alert('clicked')}>
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('button, input, select, textarea')) 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 ad69155

Please sign in to comment.