# 단계형 설정

설정을 두 단계로 보여 주는 블록이에요. 처음에는 소리, 화면, 언어 같은 묶음 목록이 나오고, 하나를 고르면 그 묶음의 화면이 오른쪽에서 밀려 들어와요.

- Type: block
- Theme: base
- Tags: arc, block, settings, preferences, steps, navigation, drill-down
- English title: Settings steps
- URL: https://dsui.vercel.app/items/settings-steps

## Used items

- [설정 패널](https://dsui.vercel.app/items/settings-panel): 소리, 화면 모드, 시즌 테마, 글자 크기, 움직임을 한 서랍에 모은 설정 블록이에요. 언어나 계정 같은 묶음도 더할 수 있어요.
- [스위치](https://dsui.vercel.app/items/switch): 켜고 끄는 값을 바꾸는 스위치로, 누르는 동안 손잡이가 늘어나요.
- [슬라이더](https://dsui.vercel.app/items/slider): 끌거나 키보드로 값이나 범위를 고르는 슬라이더예요.
- [세그먼트 컨트롤](https://dsui.vercel.app/items/segmented-control): 한 번에 하나만 고르는 짧은 선택지를 나란히 보여 주는 컨트롤이에요.

## Controls

| Key | Type | Label | Default |
| --- | --- | --- | --- |
| showHead | boolean | 제목 보이기 | true |
| showReset | boolean | 초기화 버튼 | true |
| title | text | 제목 | 설정 |

## Source

### settings-steps.module.css

```css
/* Settings in two steps: a list of groups, then the group's own page that slides in from the right. Arc tokens only. */
.stage { position: relative; display: grid; }
.pane { display: grid; align-content: start; gap: var(--space-5); padding: var(--space-1) 0 var(--space-2); }
/* One bar for both steps: the back button at the left, the title in the middle. The title is the same text as the back button's label, so it can fly between them. */
.header { display: grid; padding: var(--space-1) 0 var(--space-3); }
.bar { position: relative; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; min-height: 2.5rem; }
.lead { display: flex; min-width: 0; align-items: center; }
.center { display: grid; justify-items: center; align-items: center; text-align: center; }
.center > * { grid-area: 1 / 1; }
.title { margin: 0; font-size: var(--text-base); font-weight: 600; line-height: 1.3; }
.body { position: relative; overflow: hidden; }
.noteBox { overflow: hidden; }
.note { margin: 0; padding-top: var(--space-1); color: var(--text-secondary); font-size: var(--text-sm); line-height: var(--leading-body); text-align: center; text-wrap: balance; }
.card { display: grid; overflow: hidden; border-radius: var(--radius-surface); background: var(--surface); padding: var(--space-1) 0; }
.card > * + * { position: relative; }
/* The hairline starts at the text, after the round icon. */
.card > * + *::before { position: absolute; top: 0; right: 0; left: calc(var(--space-5) + 2.5rem); height: 1px; background: var(--border-subtle); content: ""; }
.line { display: flex; width: 100%; min-height: 3.5rem; align-items: center; gap: var(--space-3); margin: 0; padding: 0 var(--space-5); border: 0; background: none; color: var(--foreground); font: inherit; text-align: left; cursor: pointer; -webkit-tap-highlight-color: transparent; transition: background var(--duration-fast) var(--ease-standard), scale var(--duration-spring) var(--ease-spring); }
.line:focus-visible { outline-offset: -3px; }
@media (hover: hover) and (pointer: fine) { .line:hover { background: var(--surface-muted); } }
.line:active { background: var(--surface-sunken); scale: .985; }
.bubble { display: grid; flex: none; width: 2rem; height: 2rem; place-items: center; border-radius: var(--radius-pill); background: var(--accent); color: var(--accent-foreground); box-shadow: var(--shadow-resting); }
.bubble[data-tone="highlight"] { background: var(--highlight); color: var(--highlight-foreground, #1a1a1a); }
.bubble[data-tone="success"] { background: var(--success); color: var(--success-foreground, #fff); }
.bubble[data-tone="danger"] { background: var(--danger); color: var(--danger-foreground, #fff); }
.lineLabel { flex: 1 1 auto; min-width: 0; font-size: var(--text-base); font-weight: 500; }
.lineValue { max-width: 9rem; overflow: hidden; color: var(--text-secondary); font-size: var(--text-sm); text-overflow: ellipsis; white-space: nowrap; }
.chevron { flex: none; color: var(--text-secondary); }
.back { display: inline-flex; width: fit-content; align-items: center; gap: 2px; margin: 0 0 0 -6px; padding: 6px 10px 6px 4px; border: 0; border-radius: var(--radius-pill); background: none; color: var(--accent-strong); font: inherit; font-size: var(--text-base); cursor: pointer; -webkit-tap-highlight-color: transparent; transition: scale var(--duration-spring) var(--ease-spring), background var(--duration-fast) var(--ease-standard); }
@media (hover: hover) and (pointer: fine) { .back:hover { background: var(--surface); scale: 1.04; } }
.back:active { scale: .94; }
.backLabel { font-weight: 600; white-space: nowrap; }
/* The group's name is already in the bar, so headings of groups inside its page stay out of sight. */
.detail h3 { display: none; }
.reset { display: grid; min-height: 3rem; place-items: center; border: 0; background: none; color: var(--accent-strong); font: inherit; font-size: var(--text-base); font-weight: 500; cursor: pointer; -webkit-tap-highlight-color: transparent; }
@media (hover: hover) and (pointer: fine) { .reset:hover { color: var(--accent-pressed); } }
.reset:active { color: var(--accent-pressed); }
@media (prefers-reduced-motion: reduce) { .line, .back { transition: none; } .line:active, .back:active { scale: 1; } }

```

### settings-steps.tsx

```tsx
"use client";

import { useEffect, useRef, useState } from "react";
import type { ReactNode } from "react";
import { AnimatePresence, motion } from "motion/react";
import { useReducedMotion } from "../../lib/use-reduced-motion";
import { playUiSound } from "../../sound/play";
import { UiIcon } from "../../theme/ui-icon";
import styles from "./settings-steps.module.css";

export type SettingsStepsTone = "accent" | "highlight" | "success" | "danger";

/** One group of the first step: its line (round icon, name, current value) and the page that opens from it. */
export interface SettingsStepsGroup {
  id: string;
  /** Name on the line and in the bar of the page. */
  label: string;
  /** The round icon's drawing, such as `<HugeiconsIcon icon={…} size={18} />`. It is hidden from assistive technology. */
  icon: ReactNode;
  /** Colour of the round icon: the accent, the highlight, success or danger tokens. */
  tone?: SettingsStepsTone;
  /** What the group is set to now, shown at the right of the line ("켜짐", "한국어"). */
  value?: string;
  /** The group's page: rows, switches, choices. Group headings inside it are hidden, since the bar already names the group. */
  content: ReactNode;
}

export interface SettingsStepsProps {
  /** Title of the first step, also the text of the back button on a group's page. */
  title: string;
  /** One quiet sentence under the title (shown with `showHead`). */
  description?: string;
  groups: SettingsStepsGroup[];
  /** Puts every setting back to its default. Leave it out to hide the button. */
  onReset?: () => void;
  resetLabel?: string;
  /** Shows the title and description above the list (a popover); a page that has its own header leaves it off. */
  showHead?: boolean;
  /** Overrides the back button's text (the title by default). */
  backLabel?: string;
  className?: string;
}

const EASE = [0.22, 1, 0.36, 1] as const;

/**
 * Settings in two steps, like a phone's settings app. The first step lists the groups with a round icon and the current value; choosing one slides its own
 * page in from the right, and going back slides it out again. The bar above them is one piece: the title is centred on the first step, moves to the back button
 * when a group opens while the group's name fades in at the centre (and the description drifts out to the left), and comes back the same way, with the
 * description arriving from the right. Focus moves to the back button on the way in and to the group's line on the way out. With reduced motion the steps swap
 * with no slide. It keeps no settings itself: each group's `content` holds the controls and their state.
 */
export function SettingsSteps({ title, description, groups, onReset, resetLabel, showHead = false, backLabel, className }: SettingsStepsProps) {
  const reduced = useReducedMotion();
  const [stepId, setStepId] = useState<string | null>(null);
  const [dir, setDir] = useState<1 | -1>(1);
  const last = useRef<string | null>(null);
  const moved = useRef(false);
  const backRef = useRef<HTMLButtonElement>(null);
  // The height of the page being shown, so the stage grows or shrinks to it with a glide instead of jumping when a step swaps in.
  const [bodyHeight, setBodyHeight] = useState<number | null>(null);
  const measureNode = useRef<HTMLDivElement | null>(null);
  const measure = (node: HTMLDivElement | null) => {
    measureNode.current = node;
  };
  useEffect(() => {
    const node = measureNode.current;
    if (!node) return;
    const read = () => setBodyHeight(node.offsetHeight);
    read();
    const observer = new ResizeObserver(read);
    observer.observe(node);
    return () => observer.disconnect();
  }, []);
  const lineRefs = useRef<Record<string, HTMLButtonElement | null>>({});
  const step = groups.find((group) => group.id === stepId) ?? null;

  useEffect(() => {
    if (!moved.current) return;
    if (step) backRef.current?.focus({ preventScroll: true });
    else if (last.current) lineRefs.current[last.current]?.focus({ preventScroll: true });
  }, [step]);

  const open = (id: string) => {
    playUiSound("tick");
    moved.current = true;
    last.current = id;
    setDir(1);
    setStepId(id);
  };
  const back = () => {
    playUiSound("tick");
    setDir(-1);
    setStepId(null);
  };

  const slide = reduced ? 0 : 36 * dir;
  const transition = { duration: reduced ? 0 : 0.18, ease: EASE };
  const move = { duration: reduced ? 0 : 0.42, ease: EASE };
  const showBar = showHead || step !== null;
  return (
    <div className={[styles.stage, className].filter(Boolean).join(" ")}>
      {showBar ? (
        <div className={styles.header}>
          <div className={styles.bar}>
            <div className={styles.lead}>
              <AnimatePresence initial={false}>
                {step ? (
                  <motion.button key="back" type="button" ref={backRef} className={styles.back} onClick={back} initial={reduced ? false : { opacity: 0, x: 12 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: 12 }} transition={move}>
                    <UiIcon iconRole="chevron-left" size={18} strokeWidth={2} aria-hidden="true" />
                    <span className={styles.backLabel}>{backLabel ?? title}</span>
                  </motion.button>
                ) : null}
              </AnimatePresence>
            </div>
            <div className={styles.center}>
              <AnimatePresence initial={false} mode="popLayout">
                {step ? (
                  <motion.h2 key={step.id} className={styles.title} initial={reduced ? false : { opacity: 0, x: 24 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: 24 }} transition={move}>
                    {step.label}
                  </motion.h2>
                ) : showHead ? (
                  <motion.h2 key="root-title" className={styles.title} initial={reduced ? false : { opacity: 0, x: -24 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: -24 }} transition={move}>
                    {title}
                  </motion.h2>
                ) : null}
              </AnimatePresence>
            </div>
            <span aria-hidden="true" />
          </div>
          <AnimatePresence initial={false}>
            {!step && showHead && description ? (
              <motion.div key="note" className={styles.noteBox} initial={reduced ? false : { opacity: 0, x: 28, height: 0 }} animate={{ opacity: 1, x: 0, height: "auto" }} exit={{ opacity: 0, x: -28, height: 0 }} transition={move}>
                <p className={styles.note}>{description}</p>
              </motion.div>
            ) : null}
          </AnimatePresence>
        </div>
      ) : null}
      <motion.div className={styles.body} animate={{ height: bodyHeight ?? "auto" }} initial={false} transition={{ duration: reduced ? 0 : 0.3, ease: EASE }}>
        <div ref={measure}>
      <AnimatePresence initial={false} mode="wait" custom={dir}>
        {!step ? (
          <motion.div key="root" className={styles.pane} initial={{ opacity: 0, x: -slide }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: -slide }} transition={transition}>
            <SettingsStepsList groups={groups} onReset={onReset} resetLabel={resetLabel} onOpen={open} lineRef={(id, node) => { lineRefs.current[id] = node; }} />
          </motion.div>
        ) : (
          <motion.div key={step.id} className={`${styles.pane} ${styles.detail}`} initial={{ opacity: 0, x: slide }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: slide }} transition={transition}>
            {step.content}
          </motion.div>
        )}
      </AnimatePresence>
        </div>
      </motion.div>
    </div>
  );
}

/**
 * The first step's list on its own: the groups as lines (round icon, name, value, chevron) and the reset button. `SettingsSteps` draws it inside its stage; a host that
 * runs its own steps (a phone page whose bar is shared with other pages) draws it directly and opens the group's page itself in `onOpen`.
 */
export function SettingsStepsList({ groups, onReset, resetLabel, onOpen, lineRef }: { groups: SettingsStepsGroup[]; onReset?: () => void; resetLabel?: string; onOpen: (id: string) => void; /** Gives the host a handle on each line, to move focus back to it. */ lineRef?: (id: string, node: HTMLButtonElement | null) => void }) {
  return (
    <>
      <div className={styles.card}>
        {groups.map((group) => (
          <button key={group.id} type="button" className={styles.line} data-step-id={group.id} ref={(node) => lineRef?.(group.id, node)} onClick={() => onOpen(group.id)}>
            <span className={styles.bubble} data-tone={group.tone ?? "accent"} aria-hidden="true">{group.icon}</span>
            <span className={styles.lineLabel}>{group.label}</span>
            {group.value ? <span className={styles.lineValue}>{group.value}</span> : null}
            <UiIcon iconRole="chevron-right" size={16} strokeWidth={1.75} className={styles.chevron} aria-hidden="true" />
          </button>
        ))}
      </div>
      {onReset && resetLabel ? <button type="button" className={styles.reset} onClick={onReset}>{resetLabel}</button> : null}
    </>
  );
}

export default SettingsSteps;

```
