Komponenter
Avatar
Avatar viser et bilde, initialer eller ikon for en person, enhet eller profil.
Unable to parse html
const Preview = () => <Avatar aria-label='Ola Nordmann' />; render(<Preview />)
Bruk
Avatar bruker klassenavnet ds-avatar på en <span> med role="img". Sørg for at avatar har tilgjengelig navn.
CSS variabler og data-attributter
Størrelser styres med data-size og farger med data-color. Komponenten vil arve nærmeste forelder med disse satt.
Ingen relevante data-attributter funnet.
| Navn | Verdi |
|---|---|
| --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) |
Eksempel
Med initialer
Legg tekst inn i avatar for å vise initialer.
Unable to parse html
const Initials = () => <Avatar aria-label='Ola Nordmann'>ON</Avatar>; render(<Initials />)
Med bilde eller ikon
Avatar viser som standard et brukerikon, men dette kan overstyres i CSS med variabelen --dsc-avatar-icon-url.
Dersom du legger bilder eller ikon som barn må du sørge for å sette aria-hidden="true" for å unngå dobbel informasjon. Du må selv sette aria-label på avatar for å forklare hvem eller hva avataren representerer.
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 />)
Form og avrunding
Du kan bruke --dsc-avatar-radius CSS custom property for å tilpasse avrundingen på 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. |
Komponering
Komponenten skal kunne komponeres inn i andre komponenter.
Her er et eksempel på bruk av avatar inne i en Dropdown-komponent.
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 />)