UNPKG

22.1 kBJavaScriptView Raw
1"use strict";
2'use client';
3
4var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
6Object.defineProperty(exports, "__esModule", {
7 value: true
8});
9exports.default = void 0;
10var React = _interopRequireWildcard(require("react"));
11var _propTypes = _interopRequireDefault(require("prop-types"));
12var _clsx = _interopRequireDefault(require("clsx"));
13var _composeClasses = _interopRequireDefault(require("@mui/utils/composeClasses"));
14var _colorManipulator = require("@mui/system/colorManipulator");
15var _Cancel = _interopRequireDefault(require("../internal/svg-icons/Cancel"));
16var _useForkRef = _interopRequireDefault(require("../utils/useForkRef"));
17var _unsupportedProp = _interopRequireDefault(require("../utils/unsupportedProp"));
18var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
19var _ButtonBase = _interopRequireDefault(require("../ButtonBase"));
20var _zeroStyled = require("../zero-styled");
21var _memoTheme = _interopRequireDefault(require("../utils/memoTheme"));
22var _createSimplePaletteValueFilter = _interopRequireDefault(require("../utils/createSimplePaletteValueFilter"));
23var _DefaultPropsProvider = require("../DefaultPropsProvider");
24var _chipClasses = _interopRequireWildcard(require("./chipClasses"));
25var _jsxRuntime = require("react/jsx-runtime");
26const useUtilityClasses = ownerState => {
27 const {
28 classes,
29 disabled,
30 size,
31 color,
32 iconColor,
33 onDelete,
34 clickable,
35 variant
36 } = ownerState;
37 const slots = {
38 root: ['root', variant, disabled && 'disabled', `size${(0, _capitalize.default)(size)}`, `color${(0, _capitalize.default)(color)}`, clickable && 'clickable', clickable && `clickableColor${(0, _capitalize.default)(color)}`, onDelete && 'deletable', onDelete && `deletableColor${(0, _capitalize.default)(color)}`, `${variant}${(0, _capitalize.default)(color)}`],
39 label: ['label', `label${(0, _capitalize.default)(size)}`],
40 avatar: ['avatar', `avatar${(0, _capitalize.default)(size)}`, `avatarColor${(0, _capitalize.default)(color)}`],
41 icon: ['icon', `icon${(0, _capitalize.default)(size)}`, `iconColor${(0, _capitalize.default)(iconColor)}`],
42 deleteIcon: ['deleteIcon', `deleteIcon${(0, _capitalize.default)(size)}`, `deleteIconColor${(0, _capitalize.default)(color)}`, `deleteIcon${(0, _capitalize.default)(variant)}Color${(0, _capitalize.default)(color)}`]
43 };
44 return (0, _composeClasses.default)(slots, _chipClasses.getChipUtilityClass, classes);
45};
46const ChipRoot = (0, _zeroStyled.styled)('div', {
47 name: 'MuiChip',
48 slot: 'Root',
49 overridesResolver: (props, styles) => {
50 const {
51 ownerState
52 } = props;
53 const {
54 color,
55 iconColor,
56 clickable,
57 onDelete,
58 size,
59 variant
60 } = ownerState;
61 return [{
62 [`& .${_chipClasses.default.avatar}`]: styles.avatar
63 }, {
64 [`& .${_chipClasses.default.avatar}`]: styles[`avatar${(0, _capitalize.default)(size)}`]
65 }, {
66 [`& .${_chipClasses.default.avatar}`]: styles[`avatarColor${(0, _capitalize.default)(color)}`]
67 }, {
68 [`& .${_chipClasses.default.icon}`]: styles.icon
69 }, {
70 [`& .${_chipClasses.default.icon}`]: styles[`icon${(0, _capitalize.default)(size)}`]
71 }, {
72 [`& .${_chipClasses.default.icon}`]: styles[`iconColor${(0, _capitalize.default)(iconColor)}`]
73 }, {
74 [`& .${_chipClasses.default.deleteIcon}`]: styles.deleteIcon
75 }, {
76 [`& .${_chipClasses.default.deleteIcon}`]: styles[`deleteIcon${(0, _capitalize.default)(size)}`]
77 }, {
78 [`& .${_chipClasses.default.deleteIcon}`]: styles[`deleteIconColor${(0, _capitalize.default)(color)}`]
79 }, {
80 [`& .${_chipClasses.default.deleteIcon}`]: styles[`deleteIcon${(0, _capitalize.default)(variant)}Color${(0, _capitalize.default)(color)}`]
81 }, styles.root, styles[`size${(0, _capitalize.default)(size)}`], styles[`color${(0, _capitalize.default)(color)}`], clickable && styles.clickable, clickable && color !== 'default' && styles[`clickableColor${(0, _capitalize.default)(color)})`], onDelete && styles.deletable, onDelete && color !== 'default' && styles[`deletableColor${(0, _capitalize.default)(color)}`], styles[variant], styles[`${variant}${(0, _capitalize.default)(color)}`]];
82 }
83})((0, _memoTheme.default)(({
84 theme
85}) => {
86 const textColor = theme.palette.mode === 'light' ? theme.palette.grey[700] : theme.palette.grey[300];
87 return {
88 maxWidth: '100%',
89 fontFamily: theme.typography.fontFamily,
90 fontSize: theme.typography.pxToRem(13),
91 display: 'inline-flex',
92 alignItems: 'center',
93 justifyContent: 'center',
94 height: 32,
95 color: (theme.vars || theme).palette.text.primary,
96 backgroundColor: (theme.vars || theme).palette.action.selected,
97 borderRadius: 32 / 2,
98 whiteSpace: 'nowrap',
99 transition: theme.transitions.create(['background-color', 'box-shadow']),
100 // reset cursor explicitly in case ButtonBase is used
101 cursor: 'unset',
102 // We disable the focus ring for mouse, touch and keyboard users.
103 outline: 0,
104 textDecoration: 'none',
105 border: 0,
106 // Remove `button` border
107 padding: 0,
108 // Remove `button` padding
109 verticalAlign: 'middle',
110 boxSizing: 'border-box',
111 [`&.${_chipClasses.default.disabled}`]: {
112 opacity: (theme.vars || theme).palette.action.disabledOpacity,
113 pointerEvents: 'none'
114 },
115 [`& .${_chipClasses.default.avatar}`]: {
116 marginLeft: 5,
117 marginRight: -6,
118 width: 24,
119 height: 24,
120 color: theme.vars ? theme.vars.palette.Chip.defaultAvatarColor : textColor,
121 fontSize: theme.typography.pxToRem(12)
122 },
123 [`& .${_chipClasses.default.avatarColorPrimary}`]: {
124 color: (theme.vars || theme).palette.primary.contrastText,
125 backgroundColor: (theme.vars || theme).palette.primary.dark
126 },
127 [`& .${_chipClasses.default.avatarColorSecondary}`]: {
128 color: (theme.vars || theme).palette.secondary.contrastText,
129 backgroundColor: (theme.vars || theme).palette.secondary.dark
130 },
131 [`& .${_chipClasses.default.avatarSmall}`]: {
132 marginLeft: 4,
133 marginRight: -4,
134 width: 18,
135 height: 18,
136 fontSize: theme.typography.pxToRem(10)
137 },
138 [`& .${_chipClasses.default.icon}`]: {
139 marginLeft: 5,
140 marginRight: -6
141 },
142 [`& .${_chipClasses.default.deleteIcon}`]: {
143 WebkitTapHighlightColor: 'transparent',
144 color: theme.vars ? `rgba(${theme.vars.palette.text.primaryChannel} / 0.26)` : (0, _colorManipulator.alpha)(theme.palette.text.primary, 0.26),
145 fontSize: 22,
146 cursor: 'pointer',
147 margin: '0 5px 0 -6px',
148 '&:hover': {
149 color: theme.vars ? `rgba(${theme.vars.palette.text.primaryChannel} / 0.4)` : (0, _colorManipulator.alpha)(theme.palette.text.primary, 0.4)
150 }
151 },
152 variants: [{
153 props: {
154 size: 'small'
155 },
156 style: {
157 height: 24,
158 [`& .${_chipClasses.default.icon}`]: {
159 fontSize: 18,
160 marginLeft: 4,
161 marginRight: -4
162 },
163 [`& .${_chipClasses.default.deleteIcon}`]: {
164 fontSize: 16,
165 marginRight: 4,
166 marginLeft: -4
167 }
168 }
169 }, ...Object.entries(theme.palette).filter((0, _createSimplePaletteValueFilter.default)(['contrastText'])).map(([color]) => {
170 return {
171 props: {
172 color
173 },
174 style: {
175 backgroundColor: (theme.vars || theme).palette[color].main,
176 color: (theme.vars || theme).palette[color].contrastText,
177 [`& .${_chipClasses.default.deleteIcon}`]: {
178 color: theme.vars ? `rgba(${theme.vars.palette[color].contrastTextChannel} / 0.7)` : (0, _colorManipulator.alpha)(theme.palette[color].contrastText, 0.7),
179 '&:hover, &:active': {
180 color: (theme.vars || theme).palette[color].contrastText
181 }
182 }
183 }
184 };
185 }), {
186 props: props => props.iconColor === props.color,
187 style: {
188 [`& .${_chipClasses.default.icon}`]: {
189 color: theme.vars ? theme.vars.palette.Chip.defaultIconColor : textColor
190 }
191 }
192 }, {
193 props: props => props.iconColor === props.color && props.color !== 'default',
194 style: {
195 [`& .${_chipClasses.default.icon}`]: {
196 color: 'inherit'
197 }
198 }
199 }, {
200 props: {
201 onDelete: true
202 },
203 style: {
204 [`&.${_chipClasses.default.focusVisible}`]: {
205 backgroundColor: theme.vars ? `rgba(${theme.vars.palette.action.selectedChannel} / calc(${theme.vars.palette.action.selectedOpacity} + ${theme.vars.palette.action.focusOpacity}))` : (0, _colorManipulator.alpha)(theme.palette.action.selected, theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity)
206 }
207 }
208 }, ...Object.entries(theme.palette).filter((0, _createSimplePaletteValueFilter.default)(['dark'])).map(([color]) => {
209 return {
210 props: {
211 color,
212 onDelete: true
213 },
214 style: {
215 [`&.${_chipClasses.default.focusVisible}`]: {
216 background: (theme.vars || theme).palette[color].dark
217 }
218 }
219 };
220 }), {
221 props: {
222 clickable: true
223 },
224 style: {
225 userSelect: 'none',
226 WebkitTapHighlightColor: 'transparent',
227 cursor: 'pointer',
228 '&:hover': {
229 backgroundColor: theme.vars ? `rgba(${theme.vars.palette.action.selectedChannel} / calc(${theme.vars.palette.action.selectedOpacity} + ${theme.vars.palette.action.hoverOpacity}))` : (0, _colorManipulator.alpha)(theme.palette.action.selected, theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity)
230 },
231 [`&.${_chipClasses.default.focusVisible}`]: {
232 backgroundColor: theme.vars ? `rgba(${theme.vars.palette.action.selectedChannel} / calc(${theme.vars.palette.action.selectedOpacity} + ${theme.vars.palette.action.focusOpacity}))` : (0, _colorManipulator.alpha)(theme.palette.action.selected, theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity)
233 },
234 '&:active': {
235 boxShadow: (theme.vars || theme).shadows[1]
236 }
237 }
238 }, ...Object.entries(theme.palette).filter((0, _createSimplePaletteValueFilter.default)(['dark'])).map(([color]) => ({
239 props: {
240 color,
241 clickable: true
242 },
243 style: {
244 [`&:hover, &.${_chipClasses.default.focusVisible}`]: {
245 backgroundColor: (theme.vars || theme).palette[color].dark
246 }
247 }
248 })), {
249 props: {
250 variant: 'outlined'
251 },
252 style: {
253 backgroundColor: 'transparent',
254 border: theme.vars ? `1px solid ${theme.vars.palette.Chip.defaultBorder}` : `1px solid ${theme.palette.mode === 'light' ? theme.palette.grey[400] : theme.palette.grey[700]}`,
255 [`&.${_chipClasses.default.clickable}:hover`]: {
256 backgroundColor: (theme.vars || theme).palette.action.hover
257 },
258 [`&.${_chipClasses.default.focusVisible}`]: {
259 backgroundColor: (theme.vars || theme).palette.action.focus
260 },
261 [`& .${_chipClasses.default.avatar}`]: {
262 marginLeft: 4
263 },
264 [`& .${_chipClasses.default.avatarSmall}`]: {
265 marginLeft: 2
266 },
267 [`& .${_chipClasses.default.icon}`]: {
268 marginLeft: 4
269 },
270 [`& .${_chipClasses.default.iconSmall}`]: {
271 marginLeft: 2
272 },
273 [`& .${_chipClasses.default.deleteIcon}`]: {
274 marginRight: 5
275 },
276 [`& .${_chipClasses.default.deleteIconSmall}`]: {
277 marginRight: 3
278 }
279 }
280 }, ...Object.entries(theme.palette).filter((0, _createSimplePaletteValueFilter.default)()) // no need to check for mainChannel as it's calculated from main
281 .map(([color]) => ({
282 props: {
283 variant: 'outlined',
284 color
285 },
286 style: {
287 color: (theme.vars || theme).palette[color].main,
288 border: `1px solid ${theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / 0.7)` : (0, _colorManipulator.alpha)(theme.palette[color].main, 0.7)}`,
289 [`&.${_chipClasses.default.clickable}:hover`]: {
290 backgroundColor: theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / ${theme.vars.palette.action.hoverOpacity})` : (0, _colorManipulator.alpha)(theme.palette[color].main, theme.palette.action.hoverOpacity)
291 },
292 [`&.${_chipClasses.default.focusVisible}`]: {
293 backgroundColor: theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / ${theme.vars.palette.action.focusOpacity})` : (0, _colorManipulator.alpha)(theme.palette[color].main, theme.palette.action.focusOpacity)
294 },
295 [`& .${_chipClasses.default.deleteIcon}`]: {
296 color: theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / 0.7)` : (0, _colorManipulator.alpha)(theme.palette[color].main, 0.7),
297 '&:hover, &:active': {
298 color: (theme.vars || theme).palette[color].main
299 }
300 }
301 }
302 }))]
303 };
304}));
305const ChipLabel = (0, _zeroStyled.styled)('span', {
306 name: 'MuiChip',
307 slot: 'Label',
308 overridesResolver: (props, styles) => {
309 const {
310 ownerState
311 } = props;
312 const {
313 size
314 } = ownerState;
315 return [styles.label, styles[`label${(0, _capitalize.default)(size)}`]];
316 }
317})({
318 overflow: 'hidden',
319 textOverflow: 'ellipsis',
320 paddingLeft: 12,
321 paddingRight: 12,
322 whiteSpace: 'nowrap',
323 variants: [{
324 props: {
325 variant: 'outlined'
326 },
327 style: {
328 paddingLeft: 11,
329 paddingRight: 11
330 }
331 }, {
332 props: {
333 size: 'small'
334 },
335 style: {
336 paddingLeft: 8,
337 paddingRight: 8
338 }
339 }, {
340 props: {
341 size: 'small',
342 variant: 'outlined'
343 },
344 style: {
345 paddingLeft: 7,
346 paddingRight: 7
347 }
348 }]
349});
350function isDeleteKeyboardEvent(keyboardEvent) {
351 return keyboardEvent.key === 'Backspace' || keyboardEvent.key === 'Delete';
352}
353
354/**
355 * Chips represent complex entities in small blocks, such as a contact.
356 */
357const Chip = /*#__PURE__*/React.forwardRef(function Chip(inProps, ref) {
358 const props = (0, _DefaultPropsProvider.useDefaultProps)({
359 props: inProps,
360 name: 'MuiChip'
361 });
362 const {
363 avatar: avatarProp,
364 className,
365 clickable: clickableProp,
366 color = 'default',
367 component: ComponentProp,
368 deleteIcon: deleteIconProp,
369 disabled = false,
370 icon: iconProp,
371 label,
372 onClick,
373 onDelete,
374 onKeyDown,
375 onKeyUp,
376 size = 'medium',
377 variant = 'filled',
378 tabIndex,
379 skipFocusWhenDisabled = false,
380 // TODO v6: Rename to `focusableWhenDisabled`.
381 ...other
382 } = props;
383 const chipRef = React.useRef(null);
384 const handleRef = (0, _useForkRef.default)(chipRef, ref);
385 const handleDeleteIconClick = event => {
386 // Stop the event from bubbling up to the `Chip`
387 event.stopPropagation();
388 if (onDelete) {
389 onDelete(event);
390 }
391 };
392 const handleKeyDown = event => {
393 // Ignore events from children of `Chip`.
394 if (event.currentTarget === event.target && isDeleteKeyboardEvent(event)) {
395 // Will be handled in keyUp, otherwise some browsers
396 // might init navigation
397 event.preventDefault();
398 }
399 if (onKeyDown) {
400 onKeyDown(event);
401 }
402 };
403 const handleKeyUp = event => {
404 // Ignore events from children of `Chip`.
405 if (event.currentTarget === event.target) {
406 if (onDelete && isDeleteKeyboardEvent(event)) {
407 onDelete(event);
408 }
409 }
410 if (onKeyUp) {
411 onKeyUp(event);
412 }
413 };
414 const clickable = clickableProp !== false && onClick ? true : clickableProp;
415 const component = clickable || onDelete ? _ButtonBase.default : ComponentProp || 'div';
416 const ownerState = {
417 ...props,
418 component,
419 disabled,
420 size,
421 color,
422 iconColor: /*#__PURE__*/React.isValidElement(iconProp) ? iconProp.props.color || color : color,
423 onDelete: !!onDelete,
424 clickable,
425 variant
426 };
427 const classes = useUtilityClasses(ownerState);
428 const moreProps = component === _ButtonBase.default ? {
429 component: ComponentProp || 'div',
430 focusVisibleClassName: classes.focusVisible,
431 ...(onDelete && {
432 disableRipple: true
433 })
434 } : {};
435 let deleteIcon = null;
436 if (onDelete) {
437 deleteIcon = deleteIconProp && /*#__PURE__*/React.isValidElement(deleteIconProp) ? (/*#__PURE__*/React.cloneElement(deleteIconProp, {
438 className: (0, _clsx.default)(deleteIconProp.props.className, classes.deleteIcon),
439 onClick: handleDeleteIconClick
440 })) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Cancel.default, {
441 className: (0, _clsx.default)(classes.deleteIcon),
442 onClick: handleDeleteIconClick
443 });
444 }
445 let avatar = null;
446 if (avatarProp && /*#__PURE__*/React.isValidElement(avatarProp)) {
447 avatar = /*#__PURE__*/React.cloneElement(avatarProp, {
448 className: (0, _clsx.default)(classes.avatar, avatarProp.props.className)
449 });
450 }
451 let icon = null;
452 if (iconProp && /*#__PURE__*/React.isValidElement(iconProp)) {
453 icon = /*#__PURE__*/React.cloneElement(iconProp, {
454 className: (0, _clsx.default)(classes.icon, iconProp.props.className)
455 });
456 }
457 if (process.env.NODE_ENV !== 'production') {
458 if (avatar && icon) {
459 console.error('MUI: The Chip component can not handle the avatar ' + 'and the icon prop at the same time. Pick one.');
460 }
461 }
462 return /*#__PURE__*/(0, _jsxRuntime.jsxs)(ChipRoot, {
463 as: component,
464 className: (0, _clsx.default)(classes.root, className),
465 disabled: clickable && disabled ? true : undefined,
466 onClick: onClick,
467 onKeyDown: handleKeyDown,
468 onKeyUp: handleKeyUp,
469 ref: handleRef,
470 tabIndex: skipFocusWhenDisabled && disabled ? -1 : tabIndex,
471 ownerState: ownerState,
472 ...moreProps,
473 ...other,
474 children: [avatar || icon, /*#__PURE__*/(0, _jsxRuntime.jsx)(ChipLabel, {
475 className: (0, _clsx.default)(classes.label),
476 ownerState: ownerState,
477 children: label
478 }), deleteIcon]
479 });
480});
481process.env.NODE_ENV !== "production" ? Chip.propTypes /* remove-proptypes */ = {
482 // ┌────────────────────────────── Warning ──────────────────────────────┐
483 // │ These PropTypes are generated from the TypeScript type definitions. │
484 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
485 // └─────────────────────────────────────────────────────────────────────┘
486 /**
487 * The Avatar element to display.
488 */
489 avatar: _propTypes.default.element,
490 /**
491 * This prop isn't supported.
492 * Use the `component` prop if you need to change the children structure.
493 */
494 children: _unsupportedProp.default,
495 /**
496 * Override or extend the styles applied to the component.
497 */
498 classes: _propTypes.default.object,
499 /**
500 * @ignore
501 */
502 className: _propTypes.default.string,
503 /**
504 * If `true`, the chip will appear clickable, and will raise when pressed,
505 * even if the onClick prop is not defined.
506 * If `false`, the chip will not appear clickable, even if onClick prop is defined.
507 * This can be used, for example,
508 * along with the component prop to indicate an anchor Chip is clickable.
509 * Note: this controls the UI and does not affect the onClick event.
510 */
511 clickable: _propTypes.default.bool,
512 /**
513 * The color of the component.
514 * It supports both default and custom theme colors, which can be added as shown in the
515 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
516 * @default 'default'
517 */
518 color: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_propTypes.default.oneOf(['default', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), _propTypes.default.string]),
519 /**
520 * The component used for the root node.
521 * Either a string to use a HTML element or a component.
522 */
523 component: _propTypes.default.elementType,
524 /**
525 * Override the default delete icon element. Shown only if `onDelete` is set.
526 */
527 deleteIcon: _propTypes.default.element,
528 /**
529 * If `true`, the component is disabled.
530 * @default false
531 */
532 disabled: _propTypes.default.bool,
533 /**
534 * Icon element.
535 */
536 icon: _propTypes.default.element,
537 /**
538 * The content of the component.
539 */
540 label: _propTypes.default.node,
541 /**
542 * @ignore
543 */
544 onClick: _propTypes.default.func,
545 /**
546 * Callback fired when the delete icon is clicked.
547 * If set, the delete icon will be shown.
548 */
549 onDelete: _propTypes.default.func,
550 /**
551 * @ignore
552 */
553 onKeyDown: _propTypes.default.func,
554 /**
555 * @ignore
556 */
557 onKeyUp: _propTypes.default.func,
558 /**
559 * The size of the component.
560 * @default 'medium'
561 */
562 size: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_propTypes.default.oneOf(['medium', 'small']), _propTypes.default.string]),
563 /**
564 * If `true`, allows the disabled chip to escape focus.
565 * If `false`, allows the disabled chip to receive focus.
566 * @default false
567 */
568 skipFocusWhenDisabled: _propTypes.default.bool,
569 /**
570 * The system prop that allows defining system overrides as well as additional CSS styles.
571 */
572 sx: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object, _propTypes.default.bool])), _propTypes.default.func, _propTypes.default.object]),
573 /**
574 * @ignore
575 */
576 tabIndex: _propTypes.default.number,
577 /**
578 * The variant to use.
579 * @default 'filled'
580 */
581 variant: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_propTypes.default.oneOf(['filled', 'outlined']), _propTypes.default.string])
582} : void 0;
583var _default = exports.default = Chip;
\No newline at end of file