/
Pill.tsx
156 lines (141 loc) · 4.01 KB
/
Pill.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
Box,
BoxProps,
createVarsResolver,
ElementProps,
factory,
Factory,
getRadius,
getSize,
MantineRadius,
MantineSize,
StylesApiProps,
useProps,
useStyles,
} from '../../core';
import { CloseButton } from '../CloseButton';
import { usePillsInputContext } from '../PillsInput/PillsInput.context';
import { usePillGroupContext } from './PillGroup.context';
import { PillGroup } from './PillGroup/PillGroup';
import classes from './Pill.module.css';
export type PillStylesNames = 'root' | 'label' | 'remove';
export type PillVariant = 'default' | 'contrast';
export type PillCssVariables = {
root: '--pill-fz' | '--pill-radius' | '--pill-height';
};
export interface PillProps extends BoxProps, StylesApiProps<PillFactory>, ElementProps<'div'> {
/** Controls pill `font-size` and `padding`, `'sm'` by default */
size?: MantineSize;
/** Determines whether the remove button should be displayed, `false` by default */
withRemoveButton?: boolean;
/** Called when the remove button is clicked */
onRemove?: () => void;
/** Props passed down to the remove button */
removeButtonProps?: React.ComponentPropsWithoutRef<'button'>;
/** Key of `theme.radius` or any valid CSS value to set border-radius. Numbers are converted to rem. `'xl'` by default. */
radius?: MantineRadius;
/** If pill is disabled it has higher contrast to be visible on the disabled input background and the remove button is hidden */
disabled?: boolean;
}
export type PillFactory = Factory<{
props: PillProps;
ref: HTMLDivElement;
stylesNames: PillStylesNames;
vars: PillCssVariables;
variant: PillVariant;
ctx: { size: MantineSize | (string & {}) | undefined };
staticComponents: {
Group: typeof PillGroup;
};
}>;
const defaultProps: Partial<PillProps> = {
variant: 'default',
};
const varsResolver = createVarsResolver<PillFactory>((_, { radius }, { size }) => ({
root: {
'--pill-fz': getSize(size, 'pill-fz'),
'--pill-height': getSize(size, 'pill-height'),
'--pill-radius': radius === undefined ? undefined : getRadius(radius),
},
}));
export const Pill = factory<PillFactory>((_props, ref) => {
const props = useProps('Pill', defaultProps, _props);
const {
classNames,
className,
style,
styles,
unstyled,
vars,
variant,
children,
withRemoveButton,
onRemove,
removeButtonProps,
radius,
size,
disabled,
mod,
...others
} = props;
const ctx = usePillGroupContext();
const pillsInputCtx = usePillsInputContext();
const _size = size || ctx?.size || undefined;
const _variant = pillsInputCtx?.variant === 'filled' ? 'contrast' : variant || 'default';
const getStyles = useStyles<PillFactory>({
name: 'Pill',
classes,
props,
className,
style,
classNames,
styles,
unstyled,
vars,
varsResolver,
stylesCtx: { size: _size },
});
return (
<Box
component="span"
ref={ref}
variant={_variant}
size={_size}
{...getStyles('root', { variant: _variant })}
mod={[
{ 'with-remove': withRemoveButton && !disabled, disabled: disabled || ctx?.disabled },
mod,
]}
{...others}
>
<span {...getStyles('label')}>{children}</span>
{withRemoveButton && (
<CloseButton
variant="transparent"
radius={radius}
tabIndex={-1}
aria-hidden
unstyled={unstyled}
{...removeButtonProps}
{...getStyles('remove', {
className: removeButtonProps?.className,
style: removeButtonProps?.style,
})}
onMouseDown={(event) => {
event.preventDefault();
event.stopPropagation();
removeButtonProps?.onMouseDown?.(event);
}}
onClick={(event) => {
event.stopPropagation();
onRemove?.();
removeButtonProps?.onClick?.(event);
}}
/>
)}
</Box>
);
});
Pill.classes = classes;
Pill.displayName = '@mantine/core/Pill';
Pill.Group = PillGroup;