|
| 1 | +/* |
| 2 | + * Copyright 2024 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import {AriaLabelingProps, DOMProps, DOMRef, DOMRefValue} from '@react-types/shared'; |
| 14 | +import {AvatarContext} from './Avatar'; |
| 15 | +import {ContextValue} from 'react-aria-components'; |
| 16 | +import {createContext, CSSProperties, forwardRef, ReactNode} from 'react'; |
| 17 | +import {filterDOMProps} from '@react-aria/utils'; |
| 18 | +import {getAllowedOverrides, StylesPropWithoutWidth, UnsafeStyles} from './style-utils' with {type: 'macro'}; |
| 19 | +import {style} from '../style/spectrum-theme' with {type: 'macro'}; |
| 20 | +import {useDOMRef} from '@react-spectrum/utils'; |
| 21 | +import {useLabel} from 'react-aria'; |
| 22 | +import {useSpectrumContextProps} from './useSpectrumContextProps'; |
| 23 | + |
| 24 | +export interface AvatarGroupProps extends UnsafeStyles, DOMProps, AriaLabelingProps { |
| 25 | + /** Avatar children of the avatar group. */ |
| 26 | + children: ReactNode, |
| 27 | + /** The label for the avatar group. */ |
| 28 | + label?: string, |
| 29 | + /** |
| 30 | + * The size of the avatar group. |
| 31 | + * @default 24 |
| 32 | + */ |
| 33 | + size?: 16 | 20 | 24 | 28 | 32 | 36 | 40, |
| 34 | + /** Spectrum-defined styles, returned by the `style()` macro. */ |
| 35 | + styles?: StylesPropWithoutWidth |
| 36 | +} |
| 37 | + |
| 38 | +export const AvatarGroupContext = createContext<ContextValue<AvatarGroupProps, DOMRefValue<HTMLDivElement>>>(null); |
| 39 | + |
| 40 | +const avatar = style({ |
| 41 | + marginStart: { |
| 42 | + default: '[calc(var(--size) / -4)]', |
| 43 | + ':first-child': 0 |
| 44 | + } |
| 45 | +}); |
| 46 | + |
| 47 | +const text = style({ |
| 48 | + marginStart: 8, |
| 49 | + truncate: true, |
| 50 | + font: { |
| 51 | + size: { |
| 52 | + 16: 'ui-xs', |
| 53 | + 20: 'ui-sm', |
| 54 | + 24: 'ui', |
| 55 | + 28: 'ui-lg', |
| 56 | + 32: 'ui-xl', |
| 57 | + 36: 'ui-2xl', |
| 58 | + 40: 'ui-3xl' |
| 59 | + } |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +const container = style({ |
| 64 | + display: 'flex', |
| 65 | + alignItems: 'center' |
| 66 | +}, getAllowedOverrides({width: false})); |
| 67 | + |
| 68 | +function AvatarGroup(props: AvatarGroupProps, ref: DOMRef<HTMLDivElement>) { |
| 69 | + [props, ref] = useSpectrumContextProps(props, ref, AvatarGroupContext); |
| 70 | + let domRef = useDOMRef(ref); |
| 71 | + let {children, label, size = 24, styles, UNSAFE_style, UNSAFE_className, ...otherProps} = props; |
| 72 | + let {labelProps, fieldProps} = useLabel({ |
| 73 | + ...props, |
| 74 | + labelElementType: 'span' |
| 75 | + }); |
| 76 | + |
| 77 | + return ( |
| 78 | + <AvatarContext.Provider value={{styles: avatar, size, isOverBackground: true}}> |
| 79 | + <div |
| 80 | + ref={domRef} |
| 81 | + {...filterDOMProps(otherProps)} |
| 82 | + {...fieldProps} |
| 83 | + role="group" |
| 84 | + className={(UNSAFE_className ?? '') + container(null, styles)} |
| 85 | + style={{ |
| 86 | + ...UNSAFE_style, |
| 87 | + '--size': size / 16 + 'rem' |
| 88 | + } as CSSProperties}> |
| 89 | + {children} |
| 90 | + {label && <span {...labelProps} className={text({size: String(size)})}>{label}</span>} |
| 91 | + </div> |
| 92 | + </AvatarContext.Provider> |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * An avatar group is a grouping of avatars that are related to each other. |
| 98 | + */ |
| 99 | +let _AvatarGroup = forwardRef(AvatarGroup); |
| 100 | +export {_AvatarGroup as AvatarGroup}; |
0 commit comments