Komponenter
Avatar
Avatar viser et bilde, initialer eller ikon for en person, enhet eller profil.
React
const Preview = () => ( <Avatar aria-label='Ola Nordmann' variant='circle' /> ); render(<Preview />)
- aria-label
- Description
The name of the person the avatar represents.
- Type
string
- variant
- Description
The shape of the avatar.
- Type
"circle" | "square"- Default
circle
- initials
- Description
Initials to display inside the avatar.
- Type
string
- children
- Description
Image, icon or initials to display inside the avatar. Gets `aria-hidden="true"`
- Type
any
| Name | Type | Default | Description |
|---|---|---|---|
| aria-label | string | - | The name of the person the avatar represents. |
| variant | "circle" | "square" | circle | The shape of the avatar. |
| initials | string | - | Initials to display inside the avatar. |
| children | any | - | Image, icon or initials to display inside the avatar. Gets `aria-hidden="true"` |
Bruk
Eksempel
Standard Ikon
Dersom du ikke sender inn noen children, vil Avatar vise et standard brukerikon.
Dette kan overstyres i css med variabelen --dsc-avatar-icon-url. Se liste over CSS variabler nederst på siden.
React
const NoName = () => { return <Avatar aria-label='Ola' />; }; render(<NoName />)
Størrelser
Komponenten styres med data-size, og vil arve størrelse fra nærmeste forelder med data-size satt.
I tillegg til sm, md og lg, støtter Avatar også størrelsen xs.
React
const Sizes = () => ( <> <Avatar data-size='xs' aria-label='extra small' initials='xs' /> <Avatar data-size='sm' aria-label='small' initials='sm' /> <Avatar data-size='md' aria-label='medium' initials='md' /> <Avatar data-size='lg' aria-label='large' initials='lg' /> </> ); render(<Sizes />)
Farger
Alle farger i ditt tema er gyldige, og komponenten vil arve nærmeste data-color.
React
const ColorVariants = () => { const colors = ['neutral', 'accent', 'brand1', 'brand2', 'brand3']; return ( <> {colors.map((color) => ( <Avatar key={color} data-color={color as AvatarProps['data-color']} aria-label={`color ${color}`} /> ))} </> ); }; render(<ColorVariants />)
Varianter
variant prop kan settes til square eller circle. circle er standard.
React
const ShapeVariants = () => ( <> <Avatar variant='circle' aria-label='variant circle' /> <Avatar variant='square' aria-label='variant square' /> <Avatar variant='circle' aria-label='Ola Nordmann'> ON </Avatar> <Avatar variant='square' aria-label='Ola Nordmann'> ON </Avatar> </> ); render(<ShapeVariants />)
Med bilde eller ikon
Dersom du legger bilder eller ikon som barn vil disse få aria-hidden="true" automatisk for å unngå dobbel informasjon.
Du må selv sette aria-label på Avatar for å forklare hvem eller hva avataren representerer.
React
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 />)
Komponering
Komponenten skal kunne komponeres inn i andre komponenter.
Her er et eksempel på bruk av Avatar inne i en Dropdown-komponent.
React
const InDropdown = () => ( <Dropdown.TriggerContext> <Dropdown.Trigger variant='tertiary'> <Avatar aria-hidden='true' data-size='sm'> ON </Avatar> Ola Nordmann </Dropdown.Trigger> <Dropdown placement='bottom-end' autoPlacement={false} data-size='md'> <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> </Dropdown.TriggerContext> ); render(<InDropdown />)
HTML
Avatar bruker klassenavnet ds-avatar på en <span> med role="img".
CSS variabler og data-attributter
| Navn | Verdi |
|---|---|
| --dsc-avatar-icon-url | url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' fill='none' viewBox='0 0 24 24'%3E%3Cpath fill='currentColor' fill-rule='evenodd' d='M8.25 7.5a3.75 3.75 0 1 1 7.5 0 3.75 3.75 0 0 1-7.5 0M12 2.25a5.25 5.25 0 1 0 0 10.5 5.25 5.25 0 0 0 0-10.5M8.288 17.288A5.25 5.25 0 0 1 17.25 21a.75.75 0 0 0 1.5 0 6.75 6.75 0 0 0-13.5 0 .75.75 0 0 0 1.5 0 5.25 5.25 0 0 1 1.538-3.712' clip-rule='evenodd'/%3E%3C/svg%3E") |
| --dsc-avatar-background | var(--ds-color-base-default) |
| --dsc-avatar-color | var(--ds-color-base-contrast-default) |
| --dsc-avatar-size | 2.625em |
| --dsc-avatar-padding | 0.45em |
| Navn | Verdi |
|---|---|
| data-initials | |
| data-variant | square |