-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.tsx
76 lines (63 loc) · 2.12 KB
/
index.tsx
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
71
72
73
74
75
76
import React from 'react';
import { Tooltip } from '../Tooltip';
import styled from '../../../';
export type BadgeColor = 'purple' | 'yellow' | 'green' | 'red' | 'blue' | 'pink' | 'lightGray';
export interface BadgeProps extends React.ComponentProps<'div'> {
label: string;
color?: BadgeColor;
tooltip?: string;
className?: string;
}
interface BadgeState {
tooltipIsVisible: boolean;
}
const Container = styled.div(({ theme }) => ({
display: 'inline-block',
font: theme.fonts.desktop.small,
fontWeight: 500,
marginLeft: '1em',
marginRight: '1em',
position: 'relative',
[theme.breakpoints.mobileAndLower]: {
position: 'static',
},
}));
const Label = styled.span<{ color: BadgeColor }>(({ color, theme }) => ({
padding: '4px 12px',
whiteSpace: 'nowrap',
position: 'relative',
borderRadius: theme.borderRadius.large,
background: theme.colors[color],
color: color === 'yellow' || color === 'lightGray' ? theme.colors.darkIndigo : theme.colors.white,
}));
export class Badge extends React.Component<BadgeProps, BadgeState> {
static displayName = 'Collector.Badge';
state: BadgeState = {
tooltipIsVisible: false,
};
private showTooltip = () => {
this.setState({ tooltipIsVisible: true });
};
private hideTooltip = () => {
this.setState({ tooltipIsVisible: false });
};
private toggleTooltip = () => {
this.setState(prevState => ({ tooltipIsVisible: !prevState.tooltipIsVisible }));
};
render() {
const { label, tooltip, color = 'purple', ...rest } = this.props;
return (
<Container {...rest}>
<Label
color={color}
onMouseEnter={tooltip ? this.showTooltip : undefined}
onMouseLeave={tooltip ? this.hideTooltip : undefined}
onClick={tooltip ? this.toggleTooltip : undefined}
>
{label}
</Label>
{tooltip && <Tooltip visible={this.state.tooltipIsVisible}>{tooltip}</Tooltip>}
</Container>
);
}
}