UNPKG

@primer/components

Version:
49 lines (46 loc) 2.98 kB
import classnames from 'classnames'; import React from 'react'; import styled from 'styled-components'; import { get } from './constants'; import { Box } from '.'; import sx from './sx'; const AvatarStackWrapper = styled.span.withConfig({ displayName: "AvatarStack__AvatarStackWrapper", componentId: "sc-1qgzd2v-0" })(["display:flex;position:relative;height:20px;min-width:", ";.pc-AvatarItem{flex-shrink:0;height:20px;width:20px;box-shadow:0 0 0 1px ", ";margin-left:-11px;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;}&:nth-child(n + 4){display:none;}}&.pc-AvatarStack--two{min-width:30px;.pc-AvatarItem{&:nth-child(n + 3){display:none;}}}&.pc-AvatarStack--three-plus{min-width:38px;.pc-AvatarItem{&:nth-child(3){opacity:", "%;margin-left:-17px;}&:nth-child(4){opacity:", "%;margin-left:-17px;}&:nth-child(5){opacity:", "%;margin-left:-17px;}&:nth-child(n + 4){display:block;}&:nth-child(n + 6){opacity:0;visibility:hidden;}}}&.pc-AvatarStack--right{justify-content:flex-end;.pc-AvatarItem{margin-left:0 !important;margin-right:-11px;&:first-child{margin-right:0;}}.pc-AvatarStackBody{flex-direction:row-reverse;&:hover{.pc-AvatarItem{margin-right:", "!important;margin-left:0 !important;&:first-child{margin-right:0 !important;}}}}}&.pc-AvatarStack--three-plus.pc-AvatarStack--right{.pc-AvatarItem{&:nth-child(3){margin-right:-17px;}&:nth-child(4){margin-right:-17px;}&:nth-child(5){margin-right:-17px;}}}.pc-AvatarStackBody:hover{width:auto;.pc-AvatarItem{margin-left:", ";opacity:100%;visibility:visible;box-shadow:0 0 0 4px ", ";&:first-child{margin-left:0;}}}", ";"], props => props.count === 1 ? '20px' : props.count === 2 ? '30px' : '38px', get('colors.canvas.default'), 100 - 3 * 15, 100 - 4 * 15, 100 - 5 * 15, get('space.1'), get('space.1'), get('colors.canvas.default'), sx); const transformChildren = children => { return React.Children.map(children, (child, index) => { if (! /*#__PURE__*/React.isValidElement(child)) return child; return /*#__PURE__*/React.cloneElement(child, { className: classnames(child.props.className, 'pc-AvatarItem'), sx: { zIndex: 10 - index, ...child.props.sx } }); }); }; const AvatarStack = ({ children, alignRight, sx: sxProp }) => { 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 /*#__PURE__*/React.createElement(AvatarStackWrapper, { count: count, className: wrapperClassNames, sx: sxProp }, /*#__PURE__*/React.createElement(Box, { position: "absolute", display: "flex", width: "38px", className: "pc-AvatarStackBody" }, transformChildren(children))); }; AvatarStack.displayName = "AvatarStack"; export default AvatarStack;