-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Chip.tsx
385 lines (360 loc) · 12.7 KB
/
Chip.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import * as React from 'react';
import clsx from 'clsx';
import PropTypes from 'prop-types';
import { unstable_composeClasses as composeClasses, useButton } from '@mui/base';
import { OverridableComponent } from '@mui/types';
import { unstable_capitalize as capitalize, unstable_useId as useId } from '@mui/utils';
import { useThemeProps } from '../styles';
import styled from '../styles/styled';
import chipClasses, { getChipUtilityClass } from './chipClasses';
import { ChipProps, ChipOwnerState, ChipTypeMap } from './ChipProps';
import ChipContext from './ChipContext';
import useSlot from '../utils/useSlot';
const useUtilityClasses = (ownerState: ChipOwnerState) => {
const { disabled, size, color, clickable, variant, focusVisible } = ownerState;
const slots = {
root: [
'root',
disabled && 'disabled',
color && `color${capitalize(color)}`,
size && `size${capitalize(size)}`,
variant && `variant${capitalize(variant)}`,
clickable && 'clickable',
],
action: ['action', disabled && 'disabled', focusVisible && 'focusVisible'],
label: ['label', size && `label${capitalize(size)}`],
startDecorator: ['startDecorator'],
endDecorator: ['endDecorator'],
};
return composeClasses(slots, getChipUtilityClass, {});
};
const ChipRoot = styled('div', {
name: 'JoyChip',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})<{ ownerState: ChipOwnerState }>(({ theme, ownerState }) => {
return [
{
// for controlling chip delete margin offset
'--Chip-decorator-childOffset':
'min(calc(var(--Chip-paddingInline) - (var(--_Chip-minHeight) - 2 * var(--variant-borderWidth, 0px) - var(--Chip-decorator-childHeight)) / 2), var(--Chip-paddingInline))',
'--Chip-decorator-childRadius':
'max(var(--_Chip-radius) - var(--_Chip-paddingBlock), min(var(--_Chip-paddingBlock) / 2, var(--_Chip-radius) / 2))',
'--Chip-delete-radius': 'var(--Chip-decorator-childRadius)',
'--Chip-delete-size': 'var(--Chip-decorator-childHeight)',
'--Avatar-radius': 'var(--Chip-decorator-childRadius)',
'--Avatar-size': 'var(--Chip-decorator-childHeight)',
'--Icon-margin': 'initial', // reset the icon's margin.
'--internal-action-radius': 'var(--_Chip-radius)', // to be used with Radio or Checkbox
...(ownerState.size === 'sm' && {
'--Chip-gap': '0.25rem',
'--Chip-paddingInline': '0.5rem',
'--Chip-decorator-childHeight':
'calc(min(1.125rem, var(--_Chip-minHeight)) - 2 * var(--variant-borderWidth, 0px))',
'--Icon-fontSize': 'calc(var(--_Chip-minHeight) / 1.714)', // 0.875rem by default
'--_Chip-minHeight': 'var(--Chip-minHeight, 1.5rem)',
fontSize: theme.vars.fontSize.xs,
}),
...(ownerState.size === 'md' && {
'--Chip-gap': '0.375rem',
'--Chip-paddingInline': '0.75rem',
'--Chip-decorator-childHeight': 'min(1.375rem, var(--_Chip-minHeight))',
'--Icon-fontSize': 'calc(var(--_Chip-minHeight) / 1.778)', // 1.125rem by default
'--_Chip-minHeight': 'var(--Chip-minHeight, 2rem)',
fontSize: theme.vars.fontSize.sm,
}),
...(ownerState.size === 'lg' && {
'--Chip-gap': '0.5rem',
'--Chip-paddingInline': '1rem',
'--Chip-decorator-childHeight': 'min(1.75rem, var(--_Chip-minHeight))',
'--Icon-fontSize': 'calc(var(--_Chip-minHeight) / 2)', // 1.25rem by default
'--_Chip-minHeight': 'var(--Chip-minHeight, 2.5rem)',
fontSize: theme.vars.fontSize.md,
}),
'--_Chip-radius': 'var(--Chip-radius, 1.5rem)',
'--_Chip-paddingBlock':
'max((var(--_Chip-minHeight) - 2 * var(--variant-borderWidth, 0px) - var(--Chip-decorator-childHeight)) / 2, 0px)',
minHeight: 'var(--_Chip-minHeight)',
paddingInline: 'var(--Chip-paddingInline)',
borderRadius: 'var(--_Chip-radius)',
position: 'relative',
fontWeight: theme.vars.fontWeight.md,
fontFamily: theme.vars.fontFamily.body,
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
whiteSpace: 'nowrap',
transition:
'background-color 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms, box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
textDecoration: 'none',
verticalAlign: 'middle',
boxSizing: 'border-box',
[`&.${chipClasses.disabled}`]: {
color: theme.vars.palette[ownerState.color!]?.[`${ownerState.variant!}DisabledColor`],
},
},
...(!ownerState.clickable
? [
theme.variants[ownerState.variant!]?.[ownerState.color!],
{
[`&.${chipClasses.disabled}`]:
theme.variants[`${ownerState.variant!}Disabled`]?.[ownerState.color!],
},
]
: [
{
'--variant-borderWidth': '0px',
color: theme.vars.palette[ownerState.color!]?.[`${ownerState.variant!}Color`],
},
]),
];
});
const ChipLabel = styled('span', {
name: 'JoyChip',
slot: 'Label',
overridesResolver: (props, styles) => styles.label,
})<{ ownerState: ChipOwnerState }>(({ ownerState }) => ({
display: 'inline-block',
overflow: 'hidden',
textOverflow: 'ellipsis',
order: 1,
minInlineSize: 0,
flexGrow: 1,
...(ownerState.clickable && {
zIndex: 1,
pointerEvents: 'none',
}),
}));
const ChipAction = styled('button', {
name: 'JoyChip',
slot: 'Action',
overridesResolver: (props, styles) => styles.action,
})<{ ownerState: ChipOwnerState }>(({ theme, ownerState }) => [
{
position: 'absolute',
zIndex: 0,
top: 0,
left: 0,
bottom: 0,
right: 0,
border: 'none',
cursor: 'pointer',
padding: 'initial',
margin: 'initial',
backgroundColor: 'initial',
textDecoration: 'none',
borderRadius: 'inherit',
transition:
'background-color 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms, box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
[theme.focus.selector]: theme.focus.default,
},
theme.variants[ownerState.variant!]?.[ownerState.color!],
{ '&:hover': theme.variants[`${ownerState.variant!}Hover`]?.[ownerState.color!] },
{ '&:active': theme.variants[`${ownerState.variant!}Active`]?.[ownerState.color!] },
{
[`&.${chipClasses.disabled}`]:
theme.variants[`${ownerState.variant!}Disabled`]?.[ownerState.color!],
},
]);
const ChipStartDecorator = styled('span', {
name: 'JoyChip',
slot: 'StartDecorator',
overridesResolver: (props, styles) => styles.startDecorator,
})<{ ownerState: ChipOwnerState }>({
'--Avatar-marginInlineStart': 'calc(var(--Chip-decorator-childOffset) * -1)',
'--Chip-delete-margin': '0 0 0 calc(var(--Chip-decorator-childOffset) * -1)',
'--Icon-margin': '0 0 0 calc(var(--Chip-paddingInline) / -4)',
display: 'inherit',
marginInlineEnd: 'var(--Chip-gap)',
// set zIndex to 1 with order to stay on top of other controls, eg. Checkbox, Radio
order: 0,
zIndex: 1,
pointerEvents: 'none',
});
const ChipEndDecorator = styled('span', {
name: 'JoyChip',
slot: 'EndDecorator',
overridesResolver: (props, styles) => styles.endDecorator,
})<{ ownerState: ChipOwnerState }>({
'--Chip-delete-margin': '0 calc(var(--Chip-decorator-childOffset) * -1) 0 0',
'--Icon-margin': '0 calc(var(--Chip-paddingInline) / -4) 0 0',
display: 'inherit',
marginInlineStart: 'var(--Chip-gap)',
// set zIndex to 1 with order to stay on top of other controls, eg. Checkbox, Radio
order: 2,
zIndex: 1,
pointerEvents: 'none',
});
/**
* Chips represent complex entities in small blocks, such as a contact.
*/
const Chip = React.forwardRef(function Chip(inProps, ref) {
const props = useThemeProps<typeof inProps & ChipProps>({ props: inProps, name: 'JoyChip' });
const {
children,
className,
color = 'primary',
slotProps = {},
onClick,
disabled = false,
size = 'md',
variant = 'solid',
startDecorator,
endDecorator,
...other
} = props;
const clickable = !!onClick || !!slotProps.action;
const ownerState: ChipOwnerState = {
...props,
disabled,
size,
color,
variant,
clickable,
focusVisible: false,
};
const resolvedActionProps =
typeof slotProps.action === 'function' ? slotProps.action(ownerState) : slotProps.action;
const actionRef = React.useRef<HTMLElement | null>(null);
const { focusVisible, getRootProps } = useButton({
...resolvedActionProps,
disabled,
ref: actionRef,
});
ownerState.focusVisible = focusVisible;
const classes = useUtilityClasses(ownerState);
const externalForwardedProps = { ...other, slotProps };
const [SlotRoot, rootProps] = useSlot('root', {
ref,
className: clsx(classes.root, className),
elementType: ChipRoot,
externalForwardedProps,
ownerState,
});
const [SlotLabel, labelProps] = useSlot('label', {
className: classes.label,
elementType: ChipLabel,
externalForwardedProps,
ownerState,
});
// @ts-ignore internal logic.
const id = useId(labelProps.id);
const [SlotAction, actionProps] = useSlot('action', {
className: classes.action,
elementType: ChipAction,
externalForwardedProps,
ownerState,
getSlotProps: getRootProps,
additionalProps: {
'aria-labelledby': id,
as: resolvedActionProps?.component,
onClick,
},
});
const [SlotStartDecorator, startDecoratorProps] = useSlot('startDecorator', {
className: classes.startDecorator,
elementType: ChipStartDecorator,
externalForwardedProps,
ownerState,
});
const [SlotEndDecorator, endDecoratorProps] = useSlot('endDecorator', {
className: classes.endDecorator,
elementType: ChipEndDecorator,
externalForwardedProps,
ownerState,
});
const chipContextValue = React.useMemo(
() => ({ disabled, variant, color }),
[color, disabled, variant],
);
return (
<ChipContext.Provider value={chipContextValue}>
<SlotRoot {...rootProps}>
{clickable && <SlotAction {...actionProps} />}
{/* label is always the first element for integrating with other controls, eg. Checkbox, Radio. Use CSS order to rearrange position */}
<SlotLabel {...labelProps} id={id}>
{children}
</SlotLabel>
{startDecorator && (
<SlotStartDecorator {...startDecoratorProps}>{startDecorator}</SlotStartDecorator>
)}
{endDecorator && <SlotEndDecorator {...endDecoratorProps}>{endDecorator}</SlotEndDecorator>}
</SlotRoot>
</ChipContext.Provider>
);
}) as OverridableComponent<ChipTypeMap>;
Chip.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the component. It supports those theme colors that make sense for this component.
* @default 'primary'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['danger', 'info', 'neutral', 'primary', 'success', 'warning']),
PropTypes.string,
]),
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* Element placed after the children.
*/
endDecorator: PropTypes.node,
/**
* Element action click handler.
*/
onClick: PropTypes.func,
/**
* The size of the component.
* It accepts theme values between 'sm' and 'lg'.
* @default 'md'
*/
size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['lg', 'md', 'sm']),
PropTypes.string,
]),
/**
* @ignore
*/
slotProps: PropTypes.shape({
action: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
endDecorator: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
label: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
startDecorator: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
}),
/**
* Element placed before the children.
*/
startDecorator: PropTypes.node,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* The variant to use.
* @default 'solid'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
PropTypes.string,
]),
} as any;
export default Chip;