-
Notifications
You must be signed in to change notification settings - Fork 735
/
Label.tsx
43 lines (34 loc) · 1.29 KB
/
Label.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import * as React from 'react';
import { Primitive } from '@radix-ui/react-primitive';
/* -------------------------------------------------------------------------------------------------
* Label
* -----------------------------------------------------------------------------------------------*/
const NAME = 'Label';
type LabelElement = React.ElementRef<typeof Primitive.label>;
type PrimitiveLabelProps = React.ComponentPropsWithoutRef<typeof Primitive.label>;
interface LabelProps extends PrimitiveLabelProps {}
const Label = React.forwardRef<LabelElement, LabelProps>((props, forwardedRef) => {
return (
<Primitive.label
{...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();
}}
/>
);
});
Label.displayName = NAME;
/* -----------------------------------------------------------------------------------------------*/
const Root = Label;
export {
Label,
//
Root,
};
export type { LabelProps };