Components
Avatar
Avatar displays an image, initials, or icon for a person, entity, or profile.
Unable to parse html
const Preview = () => <Avatar aria-label='Ola Nordmann' />; render(<Preview />)
Usage
Avatar uses the class name ds-avatar on a <span> with role="img".Ensure the avatar has an accessible name.
CSS variables and data attributes
Sizes are controlled with data-size and colors with data-color. The component will inherit from the closest parent where these are set.
No relevant data attributes found.
| Name | Value |
|---|---|
| --dsc-avatar-background | var(--ds-color-base-default) |
| --dsc-avatar-color | var(--ds-color-base-contrast-default) |
| --dsc-avatar-font-size | calc(.4 * var(--dsc-avatar-size)) |
| --dsc-avatar-icon-url | url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%221em%22%20height%3D%221em%22%20fill%3D%22none%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20fill%3D%22currentColor%22%20fill-rule%3D%22evenodd%22%20d%3D%22M8.25%207.5a3.75%203.75%200%201%201%207.5%200%203.75%203.75%200%200%201-7.5%200M12%202.25a5.25%205.25%200%201%200%200%2010.5%205.25%205.25%200%200%200%200-10.5M8.288%2017.288A5.25%205.25%200%200%201%2017.25%2021a.75.75%200%200%200%201.5%200%206.75%206.75%200%200%200-13.5%200%20.75.75%200%200%200%201.5%200%205.25%205.25%200%200%201%201.538-3.712%22%20clip-rule%3D%22evenodd%22%2F%3E%3C%2Fsvg%3E") |
| --dsc-avatar-padding | var(--ds-size-2) |
| --dsc-avatar-radius | var(--ds-border-radius-full) |
| --dsc-avatar-size | var(--ds-size-12) |
Example
With initials
Place text inside avatar to show initials.
Unable to parse html
const Initials = () => <Avatar aria-label='Ola Nordmann'>ON</Avatar>; render(<Initials />)
With image or icon
Avatar displays a user icon by default, but this can be overridden in CSS using the --dsc-avatar-icon-url variable.
If you add an image or icon as content inside the avatar, make sure to set aria-hidden="true" to avoid duplicate information. You must add an aria-label to the avatar yourself to explain who or what the avatar represents.
Unable to parse html
const WithImageAndIcon = () => ( <> <Avatar aria-label='Ola Nordmann'> <img src='/img/component-docs/cats/cat1.webp' alt='' /> </Avatar> <Avatar aria-label='Ola Nordmann'> <BriefcaseIcon /> </Avatar> </> ); render(<WithImageAndIcon />)
Shape and radius
Use --dsc-avatar-radius CSS custom property to adjust the radius of the avatar.
Unable to parse html
const Radius = () => ( <> <Avatar aria-label='radius default' /> <Avatar aria-label='radius 1rem' style={ { '--dsc-avatar-radius': '1rem', } as React.CSSProperties } /> <Avatar aria-label='radius lg' style={ { '--dsc-avatar-radius': 'var(--ds-border-radius-lg)', } as React.CSSProperties } > ON </Avatar> <Avatar aria-label='radius none' style={ { '--dsc-avatar-radius': '0', } as React.CSSProperties } > ON </Avatar> </> ); render(<Radius />)
React
Avatar
- aria-label
- Type
string
- data-tooltip
- Type
string
- aria-hidden
- Type
Booleanish
- variant
- Description
The shape of the avatar. @deprecated Please use `style={{ '--dsc-avatar-radius': VALUE }}` instead
- Type
"circle" | "square"- Default
'circle'
- initials
- Description
Initials to display inside the avatar. @deprecated Please use text content as children instead
- Type
string
- data-initials
- Description
Initials to display inside the avatar. @deprecated Please use text content as children instead
- Type
string
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
- children
- Description
Image, icon or initials to display inside the avatar.
- Type
ReactNode
| Name | Type | Default | Description |
|---|---|---|---|
| aria-label | string | - | - |
| data-tooltip | string | - | - |
| aria-hidden | Booleanish | - | - |
| variant | "circle" | "square" | 'circle' | The shape of the avatar. @deprecated Please use `style={{ '--dsc-avatar-radius': VALUE }}` instead |
| initials | string | - | Initials to display inside the avatar. @deprecated Please use text content as children instead |
| data-initials | string | - | Initials to display inside the avatar. @deprecated Please use text content as children instead |
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
| children | ReactNode | - | Image, icon or initials to display inside the avatar. |
Composition
The component should be able to be composed into other components. Here is an example of using avatar inside a dropdown component.
Unable to parse html
const InDropdown = () => ( <> <Button popovertarget='dropdown' variant='tertiary'> <Avatar aria-hidden='true' data-size='sm'> ON </Avatar> Ola Nordmann </Button> <Dropdown id='dropdown' placement='bottom-end' autoPlacement={false}> <Dropdown.List> <Dropdown.Item> <Dropdown.Button> <Badge.Position overlap='circle'> <Badge data-color='danger' data-size='sm'></Badge> <Avatar aria-hidden='true' data-size='xs'> ON </Avatar> </Badge.Position> Ola Nordmann </Dropdown.Button> </Dropdown.Item> <Dropdown.Item> <Dropdown.Button> <Avatar aria-hidden='true' data-size='xs'> <BriefcaseIcon /> </Avatar> Sogndal kommune </Dropdown.Button> </Dropdown.Item> </Dropdown.List> </Dropdown> </> ); render(<InDropdown />)