Todae

Todae

Button

Button.Root가 컨트롤입니다. Button.Icon은 컴파운드 파트입니다. 아이콘 위치는 DOM 순서를 따릅니다. 아이콘은 장식용입니다(aria-hidden). 아이콘만 있는 루트는 Button.Root에 aria-label이나 aria-labelledby가 필요합니다.

이 페이지의 내용

아이콘만 있는 루트는 그 이름을 팁으로 보여 줍니다. 레이블이 있는 버튼은 레이블이 넘칠 때만 전체 문자열을 팁으로 보여 줍니다. tooltip={false}로 끌 수 있습니다. 레이블이 다 들어가는 버튼은 팁을 보여 주지 않습니다. 자동 팁은 절대 인터랙티브가 아닙니다.

변형은 기존 페인트 팩을 씁니다. critical은 확인 대화상자에서 되돌릴 수 없는 액션입니다. Delete가 secondary Cancel 옆에 있는 경우가 그렇습니다. 기본 type은 button입니다. aria-disabled="true"는 버튼을 비활성 모양으로 그리지만 포커스는 받을 수 있게 둡니다. 마지막 페이지의 Next처럼 포커스를 가진 채 쓸 수 없게 되는 버튼에 쓰세요. disabled는 그 포커스를 페이지로 떨어뜨립니다. 이때 클릭은 아무 일도 하지 않습니다. onClick이 실행되지 않고, 토글은 바뀌지 않으며, 버튼은 폼을 제출하지도 링크를 따라가지도 않습니다.

호버는 control.motion.duration 동안 색을 바꾸고, 누르면 색이 바로 바뀝니다. 브랜드는 button.base.motion.press.scale로 누른 버튼을 줄일 수도 있습니다. 예를 들면 0.97입니다. 기본값은 1이라 버튼은 움직이지 않습니다. 버튼 자체가 아니라 테마나, 안쪽 버튼에 닿는 ThemeProvider 재정의로 설정하세요. 버튼은 transform으로 바로 줄어들고, 손을 떼면 control.motion.duration 동안 원래 크기로 돌아옵니다. 주변은 아무것도 움직이지 않고, 포인터는 여전히 제자리의 버튼 전체에 닿습니다. 그래서 가장자리 근처를 눌러도 호버가 유지되고 클릭도 됩니다. 버튼의 팁은 버튼과 함께 움직이며 간격을 유지합니다. 컨테이너 스타일 쿼리가 없는 브라우저에서는 모든 버튼이 그대로 있습니다. 비활성 버튼과 Button.Group 또는 가로 ToggleGroup의 세그먼트는 크기를 유지하고, 동작 줄이기 설정에서는 어떤 버튼도 줄어들지 않습니다.

import { Button } from '@tounsoo/todae';

export default function ButtonVariants() {
  return (
    <>
      <Button.Root>Save</Button.Root>
      <Button.Root variant="secondary">Cancel</Button.Root>
      <Button.Root variant="ghost">Ghost</Button.Root>
      <Button.Root variant="critical">Delete</Button.Root>
      <Button.Root disabled>Disabled</Button.Root>
    </>
  );
}

앞쪽 아이콘, 뒤쪽 아이콘, 아이콘만.

import { Button } from '@tounsoo/todae';
import { ArrowRight, Plus } from 'lucide-react';

export default function ButtonIcons() {
  return (
    <>
      <Button.Root>
        <Button.Icon>
          <Plus />
        </Button.Icon>
        Save
      </Button.Root>
      <Button.Root variant="secondary">
        Next
        <Button.Icon>
          <ArrowRight />
        </Button.Icon>
      </Button.Root>
      <Button.Root aria-label="Add">
        <Button.Icon>
          <Plus />
        </Button.Icon>
      </Button.Root>
    </>
  );
}

넘치는 레이블.

import { Button } from '@tounsoo/todae';

export default function ButtonOverflow() {
  return (
    <div style={{ width: 96 }}>
      <Button.Root>Very long archive label</Button.Root>
    </div>
  );
}

토글

pressed나 defaultPressed를 주면 버튼이 토글이 됩니다. aria-pressed를 설정하고, aria-disabled가 아니면 클릭할 때 상태가 바뀝니다. onPressedChange는 새 상태를 받습니다. 눌린 버튼은 active 색과 안쪽 테두리를 유지합니다. 토글 여러 개를 묶을 때는 ToggleGroup을 쓰세요.

import { Button } from '@tounsoo/todae';
import { useState } from 'react';

export default function ButtonToggle() {
  const [muted, setMuted] = useState(false);
  return (
    <Button.Root variant="secondary" pressed={muted} onPressedChange={setMuted}>
      Mute
    </Button.Root>
  );
}

Button.Group

형제 외형입니다. role="group"입니다. 자식은 Button.Root입니다. 붙은 가장자리는 테두리 두께만큼 겹치고 바깥 모서리 반경을 공유합니다. 선택 상태는 없습니다.

import { Button } from '@tounsoo/todae';
import { AlignCenter, AlignLeft, AlignRight } from 'lucide-react';

export default function ButtonGroupAlign() {
  return (
    <Button.Group aria-label="Align">
      <Button.Root variant="secondary" aria-label="Left">
        <Button.Icon>
          <AlignLeft />
        </Button.Icon>
      </Button.Root>
      <Button.Root variant="secondary" aria-label="Center">
        <Button.Icon>
          <AlignCenter />
        </Button.Icon>
      </Button.Root>
      <Button.Root variant="secondary" aria-label="Right">
        <Button.Icon>
          <AlignRight />
        </Button.Icon>
      </Button.Root>
    </Button.Group>
  );
}

아이콘

Button.Icon에는 Lucide, Phosphor, Tabler 같은 어떤 라이브러리의 아이콘도 넣을 수 있습니다. 그 안에 바로 들어간 svg나 img는 아이콘 자체의 size나 width와 상관없이 button.base.icon.size 토큰의 크기를 따릅니다. 아이콘만 있는 버튼은 레이블이 있는 버튼과 높이가 같은 정사각형입니다.

import { Button, Stack } from '@tounsoo/todae';
import { ArrowRight as PhArrowRight, GearSix, Plus as PhPlus } from '@phosphor-icons/react';
import { IconArrowRight, IconPlus, IconSettings } from '@tabler/icons-react';
import { ArrowRight, Plus, Settings } from 'lucide-react';

export default function ButtonIconLibraries() {
  return (
    <Stack.Root>
      <Stack.Root direction="row">
        <Button.Root>
          <Button.Icon>
            <Plus />
          </Button.Icon>
          Lucide
        </Button.Root>
        <Button.Root variant="secondary">
          Next
          <Button.Icon>
            <ArrowRight />
          </Button.Icon>

링크

버튼처럼 보여야 하는 링크도 여전히 링크입니다. 어딘가로 이동하고, Enter로 따라가며, 스크린 리더는 페이지의 링크 목록에 넣습니다. render를 넘기고, 받은 props를 그대로 넘긴 링크를 반환하세요. 라우터가 Link에 붙는 방식과 같습니다. 버튼은 모습과 아이콘과 팁을 그대로 가지고, 요소는 자기 역할을 그대로 가집니다. 링크는 type, disabled, pressed를 받지 않으므로 render와 함께 쓸 수 없습니다. href가 없는 a는 아무 데도 가지 않으며 비활성 모습으로 보입니다. render는 a나 button을 반환해야 하며, 그 밖의 요소는 개발 모드에서 오류를 기록합니다. 반환하는 button은 자기 type을 직접 설정합니다.

import NextLink from 'next/link';

<Button.Root render={(props) => <NextLink {...props} href="/guides/build-on-todae" />}>
  Get started
</Button.Root>
<Button.Root variant="secondary" render={(props) => <a {...props} href="https://github.com/tounsoo/todae" />}>
  Source
</Button.Root>

Button.Root는 클라이언트 컴포넌트이므로 서버 컴포넌트는 render 함수를 넘길 수 없습니다. 링크는 직접 만든 클라이언트 컴포넌트 안에 쓰고, 그 컴포넌트를 렌더링하세요. 그 컴포넌트의 props는 Omit<ButtonRenderedProps, 'render'>에 받을 href를 더한 타입으로 정하세요. 그러면 Button.Root의 다른 props가 모두 링크에 전달됩니다.

import { Button } from '@tounsoo/todae';
import { ArrowRight } from 'lucide-react';

/** Links drawn as buttons through render. The one without href looks disabled. */
export default function ButtonLinks() {
  return (
    <>
      <Button.Root render={(props) => <a {...props} href="https://www.npmjs.com/package/@tounsoo/todae" />}>
        Install
        <Button.Icon>
          <ArrowRight />
        </Button.Icon>
      </Button.Root>
      <Button.Root variant="secondary" render={(props) => <a {...props} href="https://github.com/tounsoo/todae" />}>
        Source
      </Button.Root>
      <Button.Root variant="secondary" render={(props) => <a {...props} />}>
        Not yet available
      </Button.Root>
    </>
  );
}

접근성을 지키는 조합

Todae가 맡는 것: 네이티브 <button>이고 type="button"이므로 브라우저의 키보드 동작을 그대로 가지며, :focus-visible에서 포커스 링을 보여 줍니다. render를 쓰면 요소는 자기 역할을 그대로 가지며, Todae는 역할을 더하지 않습니다. pressed는 aria-pressed를 설정합니다. Button.Icon은 aria-hidden입니다. 아이콘만 있는 루트는 aria-label이나 aria-labelledby의 텍스트를 툴팁으로 보여 주고, 이름이 없으면 개발 모드에서 오류를 기록합니다. 팁은 이름을 되풀이하므로 스크린 리더에서 숨겨지고 aria-describedby에 아무것도 더하지 않아, 이름은 한 번만 읽힙니다. Button.Group은 항상 role="group"입니다.

직접 제공할 것: 아이콘만 있는 모든 버튼의 이름, 그리고 각 Button.Group의 aria-label. 버튼이 아이콘만 있을 수 있는 래퍼는 props 타입에서 이름을 필수로 받고, 그 이름을 aria-label로 넘기고, 나머지는 펼쳐 전달해서 ref와 disabled가 버튼에 닿게 해야 합니다.

import type { ReactNode } from 'react';
import { Button, type ButtonRootProps } from '@tounsoo/todae';

type IconButtonProps = Omit<ButtonRootProps, 'aria-label' | 'aria-labelledby' | 'children'> & {
  label: string;
  icon: ReactNode;
};

export function IconButton({ label, icon, ...props }: IconButtonProps) {
  return (
    <Button.Root {...props} aria-label={label}>
      <Button.Icon>{icon}</Button.Icon>
    </Button.Root>
  );
}

주의할 점: 나머지 props를 버려서 ref와 ARIA를 잃는 래퍼. 링크 안의 Button.Root나 버튼 안의 링크. 인터랙티브 콘텐츠는 중첩할 수 없으니, 대신 render로 링크를 버튼으로 그리세요. aria-pressed가 바뀔 때 "Mute"에서 "Unmute"로 텍스트까지 바뀌는 토글. 레이블은 하나로 두고 상태는 pressed에 맡기세요. 아이콘만 있는 버튼의 tooltip={false}. 눈에 보이는 유일한 이름이 사라집니다. 아이콘만 있거나 레이블이 잘린 버튼에서 preventDefault()를 호출하는 onPointerEnter나 onFocus. 자동 툴팁이 나타나지 않습니다. 넘긴 aria-describedby는 넘긴 그대로 남습니다.

언제 쓰나요

Button.Root

언제: 네이티브 버튼 하나로 하는 액션 하나. 변형은 primary / secondary / ghost / critical이고, 기본값은 type="button"입니다. critical은 계정을 삭제하는 것처럼 되돌릴 수 없는 액션을 확인하며, secondary 취소 버튼 옆에 둡니다. 아이콘만 있으면 aria-label이 필요하며, 이를 자동으로 팁으로 보여 줍니다.

쓰지 않을 때: 글 속의 이동 → Link. 버튼처럼 보이는 이동 → render를 쓴 Button.Root. 바로 적용되는 설정 → Switch. 폼의 선택 → Checkbox. 선택된 세그먼트 → ToggleGroup(Button.Group에는 선택 상태가 없음). 되돌릴 수 있는 액션에 critical을 쓰거나, 액션이 아닌 상태 글에 쓰는 경우 → Alert tone="critical".

Button.Icon

언제: Button.Root 안의 장식용 아이콘. 위치 = DOM 순서. 항상 aria-hidden입니다.

쓰지 않을 때: 필드 안의 아이콘 → Field.Icon. Root에 aria-label 없이 아이콘이 유일한 이름일 때. Button.Root 바깥.

Button.Group

언제: 서로 관련된 Button.Root 액션을 붙여 놓은 가로 행(role="group"). aria-label로 이름을 붙이세요.

쓰지 않을 때: 토글 → ToggleGroup(여기에는 선택 상태가 없음). 세로 행이나 줄바꿈되는 행 → Stack. 관련 없는 액션 → Stack 안의 별도 버튼.

Props

Button.Root props
이름타입설명
variant"primary"| "secondary"| "ghost"| "critical"

시각 스타일. primary가 아니면 data-button-variant를 설정합니다.

기본값: "primary"

type"button" | "submit" | "reset"

네이티브 버튼 type.

기본값: "button"

disabledboolean

컨트롤을 비활성화합니다.

기본값: false

aria-labelstring

유일한 자식이 Button.Icon일 때 필수인 접근 가능한 이름. 아이콘 전용 버튼의 자동 팁 텍스트입니다.

tooltipboolean

아이콘 전용 버튼은 접근 가능한 이름을 팁으로 보여 줍니다. 넘치는 레이블은 전체 문자열을 팁으로 보여 줍니다. false면 끕니다. 레이블이 다 들어가는 버튼에는 팁을 띄우지 않습니다.

기본값: true

pressedboolean

제어되는 눌림 상태. 버튼을 aria-pressed가 있는 토글로 만듭니다.

defaultPressedboolean

제어되지 않는 토글의 처음 눌림 상태. 버튼을 aria-pressed가 있는 토글로 만듭니다.

onPressedChange(pressed: boolean) => void

토글을 클릭하면 새 상태로 호출됩니다. onClick이 먼저 실행되며, 거기서 preventDefault()를 부르면 바뀌지 않습니다.

childrenReact.ReactNode

레이블과 선택적 Button.Icon. 순서에 따라 앞쪽 또는 뒤쪽에 놓입니다.

render(props: ButtonRenderProps) => React.ReactElement

버튼을 a나 라우터 링크 같은 다른 요소에 그립니다. ref를 포함한 모든 prop을 받으며, 그것을 a나 button에 넘겨야 합니다. 요소는 자기 역할을 그대로 가지므로 type, disabled, pressed는 쓸 수 없습니다. href가 없는 a는 비활성 모습으로 보입니다.

Button.Icon props
이름타입설명
childrenReact.ReactNode

아이콘 그래픽.

Button.Group props
이름타입설명
aria-labelstring

그룹의 접근 가능한 이름.

childrenReact.ReactNode

Button.Root 자식. 가로 방향만 지원합니다.