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

버튼

원문 보기 위키 갱신

버튼 (Button)

다른 태그로 렌더링하거나 비활성화 상태에서도 포커스를 유지할 수 있는 고품질 React 버튼 컴포넌트예요. 비스타일이라 직접 스타일링하기 좋아요.

출처: Base UI Button

본문

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.

/* index.tsx */
import * as React from 'react';
import { Button } from '@base-ui/react/button';

export default function ExampleButton() {
  return (
    <Button className="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-data-disabled:bg-neutral-100 active:not-data-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-data-disabled:bg-neutral-800 dark:active:not-data-disabled:bg-neutral-700 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400">
      Submit
    </Button>
  );
}

CSS Modules

이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.

/* index.module.css */
.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
/* index.tsx */
import * as React from 'react';
import { Button } from '@base-ui/react/button';
import styles from './index.module.css';

export default function ExampleButton() {
  return <Button className={styles.Button}>Submit</Button>;
}

사용 가이드라인

  • 제출 버튼 (Submit buttons): 네이티브 button 요소와 달리, Button이 제출 버튼으로 동작하려면 type="submit"을 명시해야 해요.
  • 링크 (Links): Button 컴포넌트는 버튼 의미론(role="button", 키보드 상호작용, 비활성 상태)을 강제해요. 링크에는 사용하면 안 돼요. 아래 링크를 버튼으로 렌더링하기를 참고하세요.

구조 (Anatomy)

컴포넌트를 임포트해요.

import { Button } from '@base-ui/react/button';

<Button />;

예시

다른 태그로 렌더링하기

nativeButton={false}를 지정하면 <div> 같은 다른 태그로 렌더링되더라도 키보드 접근성을 유지할 수 있어요.

import { Button } from '@base-ui/react/button';

// @highlight-text "nativeButton={false}"
<Button render={<div />} nativeButton={false}>
  Button that can contain complex children
</Button>;

링크를 버튼으로 렌더링하기

Button 컴포넌트는 버튼 의미론을 강제해요. nativeButton={false}는 렌더링되는 태그가 <button>이 아니라는 신호이지만, 여전히 버튼 의미론(role="button", 키보드 상호작용 핸들러)을 받을 수 있는 태그여야 해요. 링크(<a>)는 고유한 의미론을 가지므로 render prop으로 버튼처럼 렌더링하면 안 돼요.

링크가 시각적으로 버튼처럼 보여야 한다면 Button 컴포넌트 대신 <a> 요소를 CSS로 직접 스타일링하세요.

로딩 상태

활성화 후 로딩 상태에 들어가는 버튼의 경우, 비활성화되는 동안에도 버튼에 포커스가 남도록 focusableWhenDisabled를 지정하세요. 일부 브라우저와 스크린 리더 조합은 포커스된 버튼의 하위 텍스트 변경을 안정적으로 알리지 못하기 때문에, aria-labelledby를 사용해 변경되는 텍스트를 버튼의 명시적 접근 가능한 이름(accessible name)으로 만들어 주세요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.

/* index.tsx */
'use client';
import * as React from 'react';
import { Button } from '@base-ui/react/button';

export default function ExampleButton() {
  const [loading, setLoading] = React.useState(false);
  const labelId = React.useId();

  return (
    <Button
      className="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-data-disabled:bg-neutral-100 active:not-data-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-data-disabled:bg-neutral-800 dark:active:not-data-disabled:bg-neutral-700 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400"
      disabled={loading}
      focusableWhenDisabled
      aria-labelledby={labelId}
      onClick={() => {
        setLoading(true);
        setTimeout(() => {
          setLoading(false);
        }, 4000);
      }}
    >
      <span id={labelId}>{loading ? 'Submitting' : 'Submit'}</span>
    </Button>
  );
}

CSS Modules

이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.

/* index.module.css */
.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
/* index.tsx */
'use client';
import * as React from 'react';
import { Button } from '@base-ui/react/button';
import styles from './index.module.css';

export default function ExampleButton() {
  const [loading, setLoading] = React.useState(false);
  const labelId = React.useId();

  return (
    <Button
      className={styles.Button}
      disabled={loading}
      focusableWhenDisabled
      aria-labelledby={labelId}
      onClick={() => {
        setLoading(true);
        setTimeout(() => {
          setLoading(false);
        }, 4000);
      }}
    >
      <span id={labelId}>{loading ? 'Submitting' : 'Submit'}</span>
    </Button>
  );
}

API 레퍼런스

Button

동작(액션)을 트리거하는 데 사용할 수 있는 버튼 컴포넌트예요. <button> 요소를 렌더링해요.

Button Props:

Prop Type Default Description
focusableWhenDisabled boolean false 비활성화 상태에서도 버튼을 포커스할 수 있을지 여부.
nativeButton boolean true render prop으로 요소를 대체할 때 네이티브 <button> 요소를 렌더링할지 여부. 렌더링되는 요소가 <div>처럼 버튼이 아니면 false로 설정하세요.
className string | ((state: Button.State) => string | undefined) - 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태에 따라 클래스를 반환하는 함수.
style React.CSSProperties | ((state: Button.State) => React.CSSProperties | undefined) - 요소에 적용되는 스타일, 또는 컴포넌트 상태에 따라 스타일 객체를 반환하는 함수.
render ReactElement | ((props: HTMLProps, state: Button.State) => ReactElement) - 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요.

Button Data Attributes:

Attribute Type Description
data-disabled - 버튼이 비활성화되었을 때 존재해요.

Button.Props

Button props의 재내보내기(re-export)예요.

Button.State

type ButtonState = {
  /** Whether the button should ignore user interaction. */
  disabled: boolean;
};

Canonical Types

Canonical: Alias 매핑 — Canonical의 네임스페이스가 이미 임포트되어 있으면 Canonical을, 아니면 Alias를 사용하세요.

  • Button.State: ButtonState
  • Button.Props: ButtonProps

더 알아보기 (Learn more)