UNPKG

4.17 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
5const _excluded = ["badgeContent", "children", "invisible", "max", "slotProps", "slots", "showZero"];
6import * as React from 'react';
7import PropTypes from 'prop-types';
8import { unstable_composeClasses as composeClasses } from '../composeClasses';
9import { useBadge } from '../useBadge';
10import { getBadgeUtilityClass } from './badgeClasses';
11import { useSlotProps } from '../utils';
12import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
13import { jsx as _jsx } from "react/jsx-runtime";
14import { jsxs as _jsxs } from "react/jsx-runtime";
15const useUtilityClasses = ownerState => {
16 const {
17 invisible
18 } = ownerState;
19 const slots = {
20 root: ['root'],
21 badge: ['badge', invisible && 'invisible']
22 };
23 return composeClasses(slots, useClassNamesOverride(getBadgeUtilityClass));
24};
25/**
26 *
27 * Demos:
28 *
29 * - [Badge](https://mui.com/base-ui/react-badge/)
30 *
31 * API:
32 *
33 * - [Badge API](https://mui.com/base-ui/react-badge/components-api/#badge)
34 */
35const Badge = /*#__PURE__*/React.forwardRef(function Badge(props, forwardedRef) {
36 var _slots$root, _slots$badge;
37 const {
38 children,
39 max: maxProp = 99,
40 slotProps = {},
41 slots = {},
42 showZero = false
43 } = props,
44 other = _objectWithoutPropertiesLoose(props, _excluded);
45 const {
46 badgeContent,
47 max,
48 displayValue,
49 invisible
50 } = useBadge(_extends({}, props, {
51 max: maxProp
52 }));
53 const ownerState = _extends({}, props, {
54 badgeContent,
55 invisible,
56 max,
57 showZero
58 });
59 const classes = useUtilityClasses(ownerState);
60 const Root = (_slots$root = slots.root) != null ? _slots$root : 'span';
61 const rootProps = useSlotProps({
62 elementType: Root,
63 externalSlotProps: slotProps.root,
64 externalForwardedProps: other,
65 additionalProps: {
66 ref: forwardedRef
67 },
68 ownerState,
69 className: classes.root
70 });
71 const BadgeComponent = (_slots$badge = slots.badge) != null ? _slots$badge : 'span';
72 const badgeProps = useSlotProps({
73 elementType: BadgeComponent,
74 externalSlotProps: slotProps.badge,
75 ownerState,
76 className: classes.badge
77 });
78 return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, {
79 children: [children, /*#__PURE__*/_jsx(BadgeComponent, _extends({}, badgeProps, {
80 children: displayValue
81 }))]
82 }));
83});
84process.env.NODE_ENV !== "production" ? Badge.propTypes /* remove-proptypes */ = {
85 // ┌────────────────────────────── Warning ──────────────────────────────┐
86 // │ These PropTypes are generated from the TypeScript type definitions. │
87 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
88 // └─────────────────────────────────────────────────────────────────────┘
89 /**
90 * The content rendered within the badge.
91 */
92 badgeContent: PropTypes.node,
93 /**
94 * The badge will be added relative to this node.
95 */
96 children: PropTypes.node,
97 /**
98 * If `true`, the badge is invisible.
99 * @default false
100 */
101 invisible: PropTypes.bool,
102 /**
103 * Max count to show.
104 * @default 99
105 */
106 max: PropTypes.number,
107 /**
108 * Controls whether the badge is hidden when `badgeContent` is zero.
109 * @default false
110 */
111 showZero: PropTypes.bool,
112 /**
113 * The props used for each slot inside the Badge.
114 * @default {}
115 */
116 slotProps: PropTypes.shape({
117 badge: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
118 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
119 }),
120 /**
121 * The components used for each slot inside the Badge.
122 * Either a string to use a HTML element or a component.
123 * @default {}
124 */
125 slots: PropTypes.shape({
126 badge: PropTypes.elementType,
127 root: PropTypes.elementType
128 })
129} : void 0;
130export { Badge };
\No newline at end of file