Taster

진단부품톤 고르기
부품 갤러리 / Toast

Toast

Toast and alert

A neutral surface with a one pixel border; color appears only on a small title or icon slot, never a tinted full-width banner. Toasts carry one follow-up action, pause on hover and can offer undo.

내 사이트에서 가져오기

색을 고르면 아래 부품이 그 색으로 바뀝니다.

실제로 돌아가는 부품입니다. 눌러 보고 입력해 보세요.

내 앱에 넣기

  1. 아직 npm 패키지가 없어서 Taster 리포를 받아 그 안의 명령을 씁니다. 내 앱 폴더가 들어 있는 폴더에서 한 번만 받습니다.

    git clone https://github.com/makim-414/taster.git && npm ci --prefix taster/packages/engine
  2. 내 앱 폴더에서 이 부품을 넣습니다. 부품 파일과 토큰 파일이 src/components/taster 에 들어갑니다.

    node ../taster/packages/engine/cli.mjs add blocks/toast --from https://taster-six.vercel.app --dir src/components/taster
  3. 부품은 Tailwind 클래스로 그리고, Taster 프리셋이 그 클래스를 토큰에 연결합니다. Tailwind CSS 를 설치하고, tokens.css 를 앱 루트에서 한 번 불러온 뒤, 앱 루트에 이 설정을 둡니다. 프리셋이 없으면 빌드는 되어도 부품이 스타일 없이 나옵니다.

    // tailwind.config.cjs
    module.exports = {
      presets: [require('./src/components/taster/tailwind-preset.cjs')],
      content: ['./index.html', './src/**/*.{ts,tsx}'],
    };

    Tailwind v3 는 이 파일을 그대로 읽습니다. v4 는 Tailwind 를 불러오는 src/ 의 스타일시트 맨 위에 이 두 줄을 넣습니다.

    @import "tailwindcss";
    @config "../tailwind.config.cjs";

    tokens.css 는 :root 에 --bg, --text, --border, --accent 를 정의합니다. 내 앱의 스타일시트가 같은 이름을 뒤에서 다시 정의하면 그쪽 색이 이깁니다. 새 Vite 앱의 스타터 CSS 처럼 그런 변수가 있으면 지우거나 이름을 바꾸세요.

언제 쓰나요

  • Callout and toastA neutral surface with a one pixel border; color appears only on a small title or icon slot, never a tinted full-width banner. Toasts carry one follow-up action, pause on hover and can offer undo.

대신 없애 주는 AI 티

  • Tinted notice band (AC-06)
  • Thick colored accent bar on one edge of a card (AC-10)
  • Rainbow of tinted boxes (COL-06)

정보

설치할 패키지
tailwindcss
라이선스
MIT
코드 보기
'use client'

import * as React from 'react'
import { cn } from './_cn'

// Toasts. ToastProvider owns a stack in a polite live region at the bottom edge; useToast().toast({...}) adds one.
// A toast says what happened in a few words, can carry one follow-up action, and leaves on its own after
// `duration` ms. The timer pauses while the pointer is over the stack or focus is inside it, so nobody loses a
// message they are reading. Failures use role="alert" and stay until dismissed by default.
// The surface is the popover token with a hairline; tone colors only the small mark. Enter is a short fade and
// lift on the motion tokens, which reduced motion shortens to an instant change.
// ToastCard is the same surface without the provider, for inline previews and tests.

export type ToastTone = 'neutral' | 'success' | 'danger'

export interface ToastOptions {
  title: string
  description?: string
  tone?: ToastTone
  action?: { label: string; onClick: () => void }
  /** ms before it leaves. Defaults to 5000, or Infinity for danger. */
  duration?: number
}

interface ToastRecord extends ToastOptions {
  id: number
}

interface ToastApi {
  toast: (options: ToastOptions) => number
  dismiss: (id: number) => void
}

const ToastContext = React.createContext<ToastApi | null>(null)

export function useToast(): ToastApi {
  const ctx = React.useContext(ToastContext)
  if (!ctx) throw new Error('useToast must be used inside <ToastProvider>')
  return ctx
}

function Mark({ tone }: { tone: ToastTone }) {
  if (tone === 'neutral') return null
  return (
    <svg
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth={1.75}
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
      className={cn('mt-0.5 shrink-0', tone === 'success' ? 'text-success' : 'text-danger')}
    >
      {tone === 'success' ? (
        <path d="M5 13l4 4 10-10" />
      ) : (
        <>
          <circle cx="12" cy="12" r="9" />
          <path d="M12 7.5v5.5M12 16.5h.01" />
        </>
      )}
    </svg>
  )
}

export interface ToastCardProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> {
  title: string
  description?: string
  tone?: ToastTone
  action?: { label: string; onClick: () => void }
  onDismiss?: () => void
  dismissLabel?: string
}

export const ToastCard = React.forwardRef<HTMLDivElement, ToastCardProps>(
  ({ title, description, tone = 'neutral', action, onDismiss, dismissLabel = 'Dismiss', className, ...props }, ref) => (
    <div
      ref={ref}
      className={cn(
        'pointer-events-auto flex w-full items-start gap-3 rounded-md border border-border bg-bg-popover py-3 pl-4 pr-1 text-sm shadow-pop',
        className,
      )}
      {...props}
    >
      <Mark tone={tone} />
      <div className="min-w-0 flex-1 py-0.5">
        <div className="font-medium text-text">{title}</div>
        {description && <div className="mt-0.5 text-text-muted">{description}</div>}
      </div>
      {action && (
        <button
          type="button"
          onClick={action.onClick}
          className={cn(
            'inline-flex h-11 shrink-0 items-center rounded-md px-2 text-sm font-medium text-text underline underline-offset-4 outline-none desk:h-8',
            'focus-visible:ring-2 focus-visible:ring-ring',
          )}
        >
          {action.label}
        </button>
      )}
      {onDismiss && (
        <button
          type="button"
          onClick={onDismiss}
          aria-label={dismissLabel}
          className={cn(
            'inline-flex size-11 shrink-0 items-center justify-center rounded-md text-text-subtle outline-none desk:size-8',
            'transition-colors duration-press hover:text-text focus-visible:ring-2 focus-visible:ring-ring',
          )}
        >
          <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.75} strokeLinecap="round" aria-hidden="true">
            <path d="M6 6l12 12M18 6L6 18" />
          </svg>
        </button>
      )}
    </div>
  ),
)
ToastCard.displayName = 'ToastCard'

function LiveToast({ t, paused, onDismiss }: { t: ToastRecord; paused: boolean; onDismiss: (id: number) => void }) {
  const [shown, setShown] = React.useState(false)
  const remaining = React.useRef(t.duration ?? (t.tone === 'danger' ? Infinity : 5000))
  React.useEffect(() => {
    const raf = requestAnimationFrame(() => setShown(true))
    return () => cancelAnimationFrame(raf)
  }, [])
  React.useEffect(() => {
    if (paused || !Number.isFinite(remaining.current)) return
    const started = Date.now()
    const timer = window.setTimeout(() => onDismiss(t.id), remaining.current)
    return () => {
      window.clearTimeout(timer)
      remaining.current -= Date.now() - started
    }
  }, [paused, onDismiss, t.id])
  return (
    <li
      role={t.tone === 'danger' ? 'alert' : 'status'}
      className="transition-[opacity,transform] duration-dropdown ease-out"
      style={{ opacity: shown ? 1 : 0, transform: shown ? 'none' : 'translateY(var(--enter-offset))' }}
    >
      <ToastCard
        title={t.title}
        description={t.description}
        tone={t.tone}
        action={
          t.action && {
            label: t.action.label,
            onClick: () => {
              t.action?.onClick()
              onDismiss(t.id)
            },
          }
        }
        onDismiss={() => onDismiss(t.id)}
      />
    </li>
  )
}

export interface ToastProviderProps {
  children: React.ReactNode
  /** Most toasts on screen at once; older ones leave first. */
  max?: number
  label?: string
}

export function ToastProvider({ children, max = 3, label = 'Notifications' }: ToastProviderProps) {
  const [items, setItems] = React.useState<ToastRecord[]>([])
  const [paused, setPaused] = React.useState(false)
  const next = React.useRef(1)

  const dismiss = React.useCallback((id: number) => setItems((list) => list.filter((t) => t.id !== id)), [])
  const toast = React.useCallback(
    (o: ToastOptions) => {
      const id = next.current++
      setItems((list) => [...list, { ...o, id }].slice(-max))
      return id
    },
    [max],
  )
  const api = React.useMemo(() => ({ toast, dismiss }), [toast, dismiss])

  return (
    <ToastContext.Provider value={api}>
      {children}
      <section aria-label={label} className="pointer-events-none fixed inset-x-4 bottom-4 z-50 sm:inset-x-auto sm:right-6 sm:w-96">
        <ol
          className="flex flex-col gap-2"
          onMouseEnter={() => setPaused(true)}
          onMouseLeave={() => setPaused(false)}
          onFocus={() => setPaused(true)}
          onBlur={(e) => {
            if (!e.currentTarget.contains(e.relatedTarget as Node)) setPaused(false)
          }}
        >
          {items.map((t) => (
            <LiveToast key={t.id} t={t} paused={paused} onDismiss={dismiss} />
          ))}
        </ol>
      </section>
    </ToastContext.Provider>
  )
}