forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AvatarStack.tsx
149 lines (125 loc) · 3.21 KB
/
AvatarStack.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import classnames from 'classnames'
import React from 'react'
import styled from 'styled-components'
import {get} from './constants'
import {Box} from '.'
import sx, {SxProp} from './sx'
type StyledAvatarStackWrapperProps = {
count?: number
} & SxProp
const AvatarStackWrapper = styled.span<StyledAvatarStackWrapperProps>`
display: flex;
position: relative;
height: 20px;
min-width: ${props => (props.count === 1 ? '20px' : props.count === 2 ? '30px' : '38px')};
.pc-AvatarItem {
flex-shrink: 0;
height: 20px;
width: 20px;
box-shadow: 0 0 0 1px ${get('colors.canvas.default')};
position: relative;
overflow: hidden;
transition: margin 0.2s ease-in-out, opacity 0.2s ease-in-out, visibility 0.2s ease-in-out,
box-shadow 0.1s ease-in-out;
&:first-child {
margin-left: 0;
z-index: 10;
}
&:nth-child(n + 2) {
margin-left: -11px;
z-index: 9;
}
&:nth-child(n + 3) {
margin-left: -17px;
opacity: ${100 - 3 * 15}%;
z-index: 8;
}
&:nth-child(n + 4) {
opacity: ${100 - 4 * 15}%;
z-index: 7;
}
&:nth-child(n + 5) {
opacity: ${100 - 5 * 15}%;
z-index: 6;
}
&:nth-child(n + 6) {
opacity: 0;
visibility: hidden;
}
}
&.pc-AvatarStack--two {
min-width: 30px;
}
&.pc-AvatarStack--three-plus {
min-width: 38px;
}
&.pc-AvatarStack--right {
justify-content: flex-end;
.pc-AvatarItem {
margin-left: 0 !important;
&:first-child {
margin-right: 0;
}
&:nth-child(n + 2) {
margin-right: -11px;
}
&:nth-child(n + 3) {
margin-right: -17px;
}
}
.pc-AvatarStackBody {
flex-direction: row-reverse;
&:hover {
.pc-AvatarItem {
margin-right: ${get('space.1')}!important;
margin-left: 0 !important;
&:first-child {
margin-right: 0 !important;
}
}
}
}
}
.pc-AvatarStackBody:hover {
width: auto;
.pc-AvatarItem {
margin-left: ${get('space.1')};
opacity: 100%;
visibility: visible;
box-shadow: 0 0 0 4px ${get('colors.canvas.default')};
&:first-child {
margin-left: 0;
}
}
}
${sx};
`
const transformChildren = (children: React.ReactNode) => {
return React.Children.map(children, child => {
if (!React.isValidElement(child)) return child
return React.cloneElement(child, {
...child.props,
className: classnames(child.props.className, 'pc-AvatarItem')
})
})
}
export type AvatarStackProps = {
alignRight?: boolean
children: React.ReactNode
} & SxProp
const AvatarStack = ({children, alignRight, sx: sxProp}: AvatarStackProps) => {
const count = React.Children.count(children)
const wrapperClassNames = classnames({
'pc-AvatarStack--two': count === 2,
'pc-AvatarStack--three-plus': count > 2,
'pc-AvatarStack--right': alignRight
})
return (
<AvatarStackWrapper count={count} className={wrapperClassNames} sx={sxProp}>
<Box position="absolute" display="flex" width="38px" className="pc-AvatarStackBody">
{transformChildren(children)}
</Box>
</AvatarStackWrapper>
)
}
export default AvatarStack