UNPKG

4.06 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 const {
37 children,
38 max: maxProp = 99,
39 slotProps = {},
40 slots = {},
41 showZero = false
42 } = props,
43 other = _objectWithoutPropertiesLoose(props, _excluded);
44 const {
45 badgeContent,
46 max,
47 displayValue,
48 invisible
49 } = useBadge(_extends({}, props, {
50 max: maxProp
51 }));
52 const ownerState = _extends({}, props, {
53 badgeContent,
54 invisible,
55 max,
56 showZero
57 });
58 const classes = useUtilityClasses(ownerState);
59 const Root = slots.root ?? 'span';
60 const rootProps = useSlotProps({
61 elementType: Root,
62 externalSlotProps: slotProps.root,
63 externalForwardedProps: other,
64 additionalProps: {
65 ref: forwardedRef
66 },
67 ownerState,
68 className: classes.root
69 });
70 const BadgeComponent = slots.badge ?? 'span';
71 const badgeProps = useSlotProps({
72 elementType: BadgeComponent,
73 externalSlotProps: slotProps.badge,
74 ownerState,
75 className: classes.badge
76 });
77 return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, {
78 children: [children, /*#__PURE__*/_jsx(BadgeComponent, _extends({}, badgeProps, {
79 children: displayValue
80 }))]
81 }));
82});
83process.env.NODE_ENV !== "production" ? Badge.propTypes /* remove-proptypes */ = {
84 // ┌────────────────────────────── Warning ──────────────────────────────┐
85 // │ These PropTypes are generated from the TypeScript type definitions. │
86 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
87 // └─────────────────────────────────────────────────────────────────────┘
88 /**
89 * The content rendered within the badge.
90 */
91 badgeContent: PropTypes.node,
92 /**
93 * The badge will be added relative to this node.
94 */
95 children: PropTypes.node,
96 /**
97 * If `true`, the badge is invisible.
98 * @default false
99 */
100 invisible: PropTypes.bool,
101 /**
102 * Max count to show.
103 * @default 99
104 */
105 max: PropTypes.number,
106 /**
107 * Controls whether the badge is hidden when `badgeContent` is zero.
108 * @default false
109 */
110 showZero: PropTypes.bool,
111 /**
112 * The props used for each slot inside the Badge.
113 * @default {}
114 */
115 slotProps: PropTypes.shape({
116 badge: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
117 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
118 }),
119 /**
120 * The components used for each slot inside the Badge.
121 * Either a string to use a HTML element or a component.
122 * @default {}
123 */
124 slots: PropTypes.shape({
125 badge: PropTypes.elementType,
126 root: PropTypes.elementType
127 })
128} : void 0;
129export { Badge };
\No newline at end of file