본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Avatar

원문 보기 위키 갱신

Shoelace의 Avatar 컴포넌트(sl-avatar)는 사람이나 객체를 나타낼 때 사용해요. 기본적으로는 일반적인 아이콘이 표시되지만, 커스텀 아이콘이나 이니셜(initials), 이미지로 개성 있게 꾸밀 수 있답니다.

출처: 문서

본문

Avatar는 사람이나 객체를 나타내는 데 사용돼요. 기본적으로는 일반적인 아이콘이 표시되지만, 커스텀 아이콘이나 이니셜, 이미지를 추가해서 개인화할 수 있어요. 보조 기기(assistive devices)를 위해 항상 label을 제공해야 해요.

<sl-avatar label="User avatar"></sl-avatar>
import SlAvatar from '@shoelace-style/shoelace/dist/react/avatar';
const App = () => <SlAvatar label="User avatar" />;

Examples

Images

Avatar에 이미지를 사용하려면 image와 label 속성(attribute)을 설정하면 돼요. 이미지는 이니셜과 아이콘보다 우선해서 표시돼요. loading 속성(attribute)을 lazy로 설정하면 Avatar 이미지를 지연 로딩(lazy load)할 수 있어요.

<sl-avatar
  image="https://images.unsplash.com/photo-1529778873920-4da4926a72c2?ixlib=rb-1.2.1&auto=format&fit=crop&w=300&q=80"
  label="Avatar of a gray tabby kitten looking down"
></sl-avatar>
<sl-avatar
  image="https://images.unsplash.com/photo-1591871937573-74dbba515c4c?ixlib=rb-1.2.1&auto=format&fit=crop&w=300&q=80"
  label="Avatar of a white and grey kitten on grey textile"
  loading="lazy"
></sl-avatar>
import SlAvatar from '@shoelace-style/shoelace/dist/react/avatar';
const App = () => (
  <SlAvatar
    image="https://images.unsplash.com/photo-1529778873920-4da4926a72c2?ixlib=rb-1.2.1&auto=format&fit=crop&w=300&q=80"
    label="Avatar of a gray tabby kitten looking down"
  />
  <SlAvatar
    image="https://images.unsplash.com/photo-1591871937573-74dbba515c4c?ixlib=rb-1.2.1&auto=format&fit=crop&w=300&q=80"
    label="Avatar of a white and grey kitten on grey textile"
    loading="lazy"
  />
);

Initials

사용할 이미지가 없을 때는 initials 속성(attribute)을 설정해서 아이콘보다 더 개인화된 무언가를 보여줄 수 있어요.

<sl-avatar initials="SL" label="Avatar with initials: SL"></sl-avatar>
import SlAvatar from '@shoelace-style/shoelace/dist/react/avatar';
const App = () => <SlAvatar initials="SL" label="Avatar with initials: SL" />;

Custom Icons

이미지나 이니셜이 설정되어 있지 않으면 아이콘이 표시돼요. 기본 Avatar는 일반적인 "user" 아이콘을 보여주지만, icon 슬롯으로 커스터마이즈할 수 있어요.

<sl-avatar label="Avatar with an image icon">
  <sl-icon slot="icon" name="image"></sl-icon>
</sl-avatar>
<sl-avatar label="Avatar with an archive icon">
  <sl-icon slot="icon" name="archive"></sl-icon>
</sl-avatar>
<sl-avatar label="Avatar with a briefcase icon">
  <sl-icon slot="icon" name="briefcase"></sl-icon>
</sl-avatar>
import SlAvatar from '@shoelace-style/shoelace/dist/react/avatar';
import SlIcon from '@shoelace-style/shoelace/dist/react/icon';
const App = () => (
  <>
    <SlAvatar label="Avatar with an image icon">
      <SlIcon slot="icon" name="image" />
    </SlAvatar>
    <SlAvatar label="Avatar with an archive icon">
      <SlIcon slot="icon" name="archive" />
    </SlAvatar>
    <SlAvatar label="Avatar with a briefcase icon">
      <SlIcon slot="icon" name="briefcase" />
    </SlAvatar>
  </>
);

Shapes

shape 속성(attribute)으로 Avatar의 모양을 바꿀 수 있어요.

<sl-avatar shape="square" label="Square avatar"></sl-avatar>
<sl-avatar shape="rounded" label="Rounded avatar"></sl-avatar>
<sl-avatar shape="circle" label="Circle avatar"></sl-avatar>
import SlAvatar from '@shoelace-style/shoelace/dist/react/avatar';
import SlIcon from '@shoelace-style/shoelace/dist/react/icon';
const App = () => (
  <>
    <SlAvatar shape="square" label="Square avatar" />
    <SlAvatar shape="rounded" label="Rounded avatar" />
    <SlAvatar shape="circle" label="Circle avatar" />
  </>
);

Avatar Groups

몇 줄의 CSS만으로 Avatar를 그룹으로 묶을 수 있어요.

<div class="avatar-group">
  <sl-avatar
    image="https://images.unsplash.com/photo-1490150028299-bf57d78394e0?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=256&h=256&q=80&crop=right"
    label="Avatar 1 of 4"
  ></sl-avatar>
  <sl-avatar
    image="https://images.unsplash.com/photo-1503454537195-1dcabb73ffb9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=256&h=256&crop=left&q=80"
    label="Avatar 2 of 4"
  ></sl-avatar>
  <sl-avatar
    image="https://images.unsplash.com/photo-1456439663599-95b042d50252?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=256&h=256&crop=left&q=80"
    label="Avatar 3 of 4"
  ></sl-avatar>
  <sl-avatar
    image="https://images.unsplash.com/flagged/photo-1554078875-e37cb8b0e27d?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=256&h=256&crop=top&q=80"
    label="Avatar 4 of 4"
  ></sl-avatar>
</div>
<style>
  .avatar-group sl-avatar:not(:first-of-type) {
    margin-left: -1rem;
  }
  .avatar-group sl-avatar::part(base) {
    border: solid 2px var(--sl-color-neutral-0);
  }
</style>
import SlAvatar from '@shoelace-style/shoelace/dist/react/avatar';
import SlIcon from '@shoelace-style/shoelace/dist/react/icon';
const css = `
  .avatar-group sl-avatar:not(:first-of-type) {
    margin-left: -1rem;
  }
  .avatar-group sl-avatar::part(base) {
    border: solid 2px var(--sl-color-neutral-0);
  }
`;
const App = () => (
  <>
    <div className="avatar-group">
      <SlAvatar
        image="https://images.unsplash.com/photo-1490150028299-bf57d78394e0?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=256&h=256&q=80&crop=right"
        label="Avatar 1 of 4"
      />
      <SlAvatar
        image="https://images.unsplash.com/photo-1503454537195-1dcabb73ffb9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=256&h=256&crop=left&q=80"
        label="Avatar 2 of 4"
      />
      <SlAvatar
        image="https://images.unsplash.com/photo-1456439663599-95b042d50252?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=256&h=256&crop=left&q=80"
        label="Avatar 3 of 4"
      />
      <SlAvatar
        image="https://images.unsplash.com/flagged/photo-1554078875-e37cb8b0e27d?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=256&h=256&crop=top&q=80"
        label="Avatar 4 of 4"
      />
    </div>
    <style>{css}</style>
  </>
);

Importing

autoloader나 전통적인 loader를 사용하고 있다면 이 부분은 무시해도 돼요. 그 외의 경우 아래 스니펫 중 하나를 골라 이 컴포넌트만 골라(cherry pick) 가져올 수 있답니다.

Script / Import / Bundler / React

CDN에서 script 태그로 이 컴포넌트를 가져오려면:

<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/avatar/avatar.js"></script>

CDN에서 JavaScript import로 이 컴포넌트를 가져오려면:

import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/avatar/avatar.js';

bundler를 사용해 이 컴포넌트를 가져오려면:

import '@shoelace-style/shoelace/dist/components/avatar/avatar.js';

이 컴포넌트를 React 컴포넌트로 가져오려면:

import SlAvatar from '@shoelace-style/shoelace/dist/react/avatar';

Slots

Name Description
icon The default icon to use when no image or initials are present. Works best with sl-icon.

슬롯(slots) 사용에 대해 더 알아보기.

Properties

Name Description Reflects Type Default
image The image source to use for the avatar. string ''
label A label to use to describe the avatar to assistive devices. string ''
initials Initials to use as a fallback when no image is available (1–2 characters max recommended). string ''
loading Indicates how the browser should load the image. 'eager' | 'lazy' 'eager'
shape The shape of the avatar. 'circle' | 'square' | 'rounded' 'circle'
updateComplete A read-only promise that resolves when the component has finished updating.

속성과 프로퍼티(attributes and properties)에 대해 더 알아보기.

Events

Name React Event Description Event Detail
sl-error onSlError The image could not be loaded. This may because of an invalid URL, a temporary network condition, or some unknown cause. -

이벤트(events)에 대해 더 알아보기.

Custom Properties

Name Description Default
--size The size of the avatar.

CSS 커스텀 프로퍼티(custom properties) 커스터마이징에 대해 더 알아보기.

Parts

Name Description
base The component’s base wrapper.
icon The container that wraps the avatar’s icon.
initials The container that wraps the avatar’s initials.
image The avatar image. Only shown when the image attribute is set.

CSS 파트(parts) 커스터마이징에 대해 더 알아보기.

Dependencies

이 컴포넌트는 다음 의존성들을 자동으로 가져와요.

  • sl-icon

더 알아보기 (Learn more)