-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathBadge.jsx
70 lines (60 loc) · 1.68 KB
/
Badge.jsx
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
/**
* @component
*/
import classNames from 'clsx';
import PropTypes from 'prop-types';
import React, { forwardRef, useCallback, useState } from 'react';
/**
* Badges are small, circular containers used to decorate other components with
* glancable information.
*/
const Badge = forwardRef(
({ children, className, style, badgeRef, ...other }, ref) => {
const [minWidth, setMinWidth] = useState();
const measureRef = useCallback((node) => {
if (node) {
setMinWidth(node.getBoundingClientRect().height);
}
}, []);
return (
<div
className={classNames(className, 'badge')}
ref={(node) => {
measureRef(node);
if (ref) {
ref(node);
}
if (badgeRef) {
badgeRef(node);
}
}}
style={{ minWidth, ...style }}
{...other}
>
{children}
</div>
);
}
);
export default Badge;
Badge.propTypes = {
/**
* A React node that is displayed inside of the Badge.
*/
children: PropTypes.node.isRequired,
/**
* A classname that is applied to the Badge `<div>`-element.
*/
className: PropTypes.string,
/**
* A React style object that will be applied to the Badge `<div>`-element.
*/
style: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
/**
* Retrieves the ref to the Badge `<div>`-element.
*/
badgeRef: PropTypes.func,
};
Badge.displayName = 'Badge';