UNPKG

924 BJavaScriptView Raw
1'use client';
2
3import { usePreviousProps } from '@mui/utils';
4/**
5 *
6 * Demos:
7 *
8 * - [Badge](https://mui.com/base-ui/react-badge/#hook)
9 *
10 * API:
11 *
12 * - [useBadge API](https://mui.com/base-ui/react-badge/hooks-api/#use-badge)
13 */
14function useBadge(parameters) {
15 const {
16 badgeContent: badgeContentProp,
17 invisible: invisibleProp = false,
18 max: maxProp = 99,
19 showZero = false
20 } = parameters;
21 const prevProps = usePreviousProps({
22 badgeContent: badgeContentProp,
23 max: maxProp
24 });
25 let invisible = invisibleProp;
26 if (invisibleProp === false && badgeContentProp === 0 && !showZero) {
27 invisible = true;
28 }
29 const {
30 badgeContent,
31 max = maxProp
32 } = invisible ? prevProps : parameters;
33 const displayValue = badgeContent && Number(badgeContent) > max ? `${max}+` : badgeContent;
34 return {
35 badgeContent,
36 invisible,
37 max,
38 displayValue
39 };
40}
41export default useBadge;
\No newline at end of file