1280px
Install
One command installs the files and packages this item needs.
TerminalTerminalTerminalbash
npx shadcn@latest add https://dsui.vercel.app/r/settings-panel.jsonFiles
components/arc/blocks/settings-panel/settings-panel.module.csscomponents/arc/blocks/settings-panel/settings-panel.tsx
npm packages
@hugeicons/core-free-icons@^4.3.5@hugeicons/react@^1.1.10
Registry items it pulls in
- v1.0.0First release.
settings-panel.module.csssettings-panel.module.csssettings-panel.module.csscss
/* The panel is grouped cards on the muted ground: the popover and the bottom sheet both set that ground, and the cards are the surface on it. */
.panel { display: grid; align-content: start; gap: var(--space-5); padding: var(--space-1) 0 var(--space-2); }
.popover:is(div) { width: min(24rem, calc(100vw - 20px)); max-height: var(--radix-popover-content-available-height); overflow-y: auto; padding: var(--space-4); border-color: var(--float-edge, var(--surface-muted)); background: var(--surface-muted); }
.popoverHead { display: grid; gap: var(--space-1); margin-bottom: var(--space-4); }
.title { margin: 0; font-size: var(--text-base); font-weight: 600; }
.note { margin: 0; color: var(--text-secondary); font-size: var(--text-sm); line-height: var(--leading-body); }
.group { display: grid; gap: var(--space-2); }
.heading { margin: 0 0 0 var(--space-4); color: var(--text-secondary); font-size: var(--text-xs); font-weight: 500; }
.card { display: grid; overflow: hidden; border-radius: var(--radius-surface); background: var(--surface); padding: var(--space-1) 0; }
/* A hairline between rows, starting at the text edge and running out to the card's edge. The rows carry the card's side padding themselves, so a hover fill can reach the card's edges. */
.card > * + * { position: relative; }
.card > * + *::before { position: absolute; top: 0; right: 0; left: var(--space-5); height: 1px; background: var(--border-subtle); content: ""; }
.row { display: flex; min-height: 3.25rem; align-items: center; justify-content: space-between; gap: var(--space-3); padding: 0 var(--space-5); }
.rowLabel { flex: 1 1 auto; min-width: 0; font-size: var(--text-base); font-weight: 500; }
.rowControl { display: flex; flex: none; align-items: center; justify-content: flex-end; }
.block { display: grid; padding: var(--space-3) var(--space-5); }
.block > * { width: 100%; }
/* A choice that has the card to itself shares the width equally between its options. */
.block > [role="group"] > div { display: flex; width: 100%; }
.block > [role="group"] > div > button { flex: 1 1 0; justify-content: center; }
.linkRow { display: flex; width: 100%; min-height: 3.25rem; 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; text-decoration: none; cursor: pointer; -webkit-tap-highlight-color: transparent; transition: background-color var(--duration-fast) var(--ease-standard); }
.linkRow:focus-visible { outline-offset: -3px; }
.linkValue { color: var(--text-secondary); font-size: var(--text-sm); }
.linkChevron { flex: none; color: var(--text-secondary); }
@media (hover: hover) and (pointer: fine) { .linkRow:hover { background: var(--surface-muted); } }
.linkRow:active { background: var(--surface-sunken); }
@media (prefers-reduced-motion: reduce) { .linkRow { transition: none; } }
.footnote { margin: 0 var(--space-3) 0 var(--space-4); color: var(--text-secondary); font-size: var(--text-xs); line-height: 1.5; word-break: keep-all; }
.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; transition: color var(--duration-fast) var(--ease-standard); }
@media (hover: hover) and (pointer: fine) { .reset:hover { color: var(--accent-pressed); } }
.reset:active { color: var(--accent-pressed); }
@media (prefers-reduced-motion: reduce) { .reset { transition: none; } }
settings-panel.test.tssettings-panel.test.tssettings-panel.test.tsts
import { createElement } from "react";
import type { ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
import { vi } from "vitest";
// The popover renders into a portal that server rendering skips, so it is swapped for plain wrappers.
vi.mock("../../popover/popover", () => {
const Plain = ({ children }: { children?: ReactNode }) => createElement("div", null, children);
return { Popover: Plain, PopoverTrigger: Plain, PopoverContent: Plain };
});
import { SettingsPanel } from "./settings-panel";
const html = (props: Record<string, unknown>) => renderToStaticMarkup(createElement(SettingsPanel, { season: { value: "base", options: [{ value: "base", label: "기본" }], onChange() {} }, ...props }));
describe("SettingsPanel season decoration switch", () => {
it("shows a 시즌 장식 switch with its hint when asked, checked while on", () => {
const out = html({ seasonDecor: { value: true, onChange() {} }, hints: { seasonDecor: "시즌 테마의 가장자리와 반짝이를 보여 줘요. 끄면 깔끔한 기본 모양이에요." } });
expect(out).toContain("시즌 장식");
expect(out).toContain("시즌 테마의 가장자리와 반짝이를 보여 줘요. 끄면 깔끔한 기본 모양이에요.");
expect(out).toMatch(/role="switch"[^>]*aria-checked="true"/);
expect(out).toMatch(/aria-describedby="[^"]+-seasonDecor"/);
});
it("is unchecked once off", () => {
expect(html({ seasonDecor: { value: false, onChange() {} } })).toMatch(/role="switch"[^>]*aria-checked="false"/);
});
it("leaves the panel as it was when the host does not pass it (the studio)", () => {
const out = html({});
expect(out).not.toContain("시즌 장식");
expect(out).not.toContain('role="switch"');
});
});
settings-panel.tsxsettings-panel.tsxsettings-panel.tsxtsx
"use client";
import { useId, useState } from "react";
import type { ElementType, MouseEvent, ReactNode } from "react";
import { HugeiconsIcon } from "@hugeicons/react";
import { Settings02Icon } from "@hugeicons/core-free-icons";
import { Button } from "../../button/button";
import { Popover, PopoverContent, PopoverTrigger } from "../../popover/popover";
import SegmentedControl from "../../segmented-control/segmented-control";
import { Slider } from "../../slider/slider";
import { Switch } from "../../switch/switch";
import { playUiSound } from "../../sound/play";
import { UiIcon } from "../../theme/ui-icon";
import styles from "./settings-panel.module.css";
export type SettingsColorMode = "system" | "light" | "dark";
export type SettingsTextSize = "normal" | "large";
export interface SettingsPanelLabels {
triggerLabel: string;
title: string;
description: string;
sound: string;
soundSwitch: string;
volume: string;
colorMode: string;
system: string;
light: string;
dark: string;
season: string;
seasonDecor: string;
textSize: string;
textNormal: string;
textLarge: string;
reduceMotion: string;
reduceBlur: string;
reset: string;
}
/**
* One sentence under a setting that says what it does and what turning it off changes. All optional: a setting without a hint
* shows no text. Write them for people, in the voice of the rest of the panel.
*/
export interface SettingsPanelHints {
soundSwitch: string;
volume: string;
colorMode: string;
season: string;
seasonDecor: string;
textSize: string;
reduceMotion: string;
reduceBlur: string;
}
const defaultLabels: SettingsPanelLabels = {
triggerLabel: "설정 열기",
title: "설정",
description: "이 브라우저에만 저장돼요.",
sound: "소리",
soundSwitch: "효과음",
volume: "음량",
colorMode: "화면",
system: "시스템",
light: "라이트",
dark: "다크",
season: "시즌 테마",
seasonDecor: "시즌 장식",
textSize: "글자 크기",
textNormal: "보통",
textLarge: "크게",
reduceMotion: "움직임 줄이기",
reduceBlur: "흐림 효과 줄이기",
reset: "설정 초기화하기",
};
export interface SettingsPanelProps {
/** Sound on or off and the volume (0 to 1). Pass null to leave the group out. */
sound?: { enabled: boolean; volume: number; onEnabledChange: (enabled: boolean) => void; onVolumeChange: (volume: number) => void } | null;
/** System, light or dark. */
colorMode?: { value: SettingsColorMode; onChange: (value: SettingsColorMode) => void } | null;
/** A choice among season themes. The options come from the host, and the first is usually the plain site. */
season?: { value: string; options: { value: string; label: string }[]; onChange: (value: string) => void } | null;
/** The season theme's edge and sparkles on or off (a switch under the season choice). Pass null or leave it out to hide it. */
seasonDecor?: { value: boolean; onChange: (value: boolean) => void } | null;
textSize?: { value: SettingsTextSize; onChange: (value: SettingsTextSize) => void } | null;
reduceMotion?: { value: boolean; onChange: (value: boolean) => void } | null;
/** Frosted-glass blur off (backdrop-filter): cheaper to draw on slower machines. */
reduceBlur?: { value: boolean; onChange: (value: boolean) => void } | null;
/** More groups, such as language, account or a link to cookie settings. Wrap each in `SettingsGroup`. */
children?: ReactNode;
/** Puts every setting back to its default. Leave it out to hide the button. */
onReset?: () => void;
/** Replaces the default gear button. Needs to accept a ref and the Radix trigger props. */
trigger?: ReactNode;
side?: "left" | "right";
open?: boolean;
defaultOpen?: boolean;
onOpenChange?: (open: boolean) => void;
labels?: Partial<SettingsPanelLabels>;
/** A short explanation under each setting. A switch is linked to its hint with `aria-describedby`. */
hints?: Partial<SettingsPanelHints>;
/** Class of the default gear button. */
triggerClassName?: string;
}
/**
* One group of a settings panel: a quiet heading, a white card holding its rows, and the sentences that explain them under the
* card. `hint` is one sentence for the whole group (a host group such as language); `notes` are the per-setting sentences a
* setting is linked to with `aria-describedby`.
*/
export function SettingsGroup({ title, hint, notes, children }: { title: string; hint?: string; notes?: { id: string; text: string }[]; children: ReactNode }) {
const hintId = useId();
return (
<section className={styles.group} aria-label={title} aria-describedby={hint ? hintId : undefined}>
<h3 className={styles.heading}>{title}</h3>
<div className={styles.card}>{children}</div>
{hint ? <p id={hintId} className={styles.footnote}>{hint}</p> : null}
{notes?.map((note) => <p key={note.id} id={note.id} className={styles.footnote}>{note.text}</p>)}
</section>
);
}
/** One line of a card: the name on the left (after an optional `leading` icon), its control on the right. */
export function SettingsRow({ label, leading, children }: { label: string; leading?: ReactNode; children: ReactNode }) {
return (
<div className={styles.row}>
{leading}
<span className={styles.rowLabel}>{label}</span>
<div className={styles.rowControl}>{children}</div>
</div>
);
}
/**
* A line of a card that goes somewhere or does something: the whole line is the target, so its hover fill runs edge to edge of the
* card. The name is on the left, an optional `value` and a chevron on the right. It is an `a` (give it `href`, or `as` for a router's
* Link) or a `button`; inside a `form`, `type="submit"` with `chevron={false}` makes a sign-out line.
*/
export function SettingsLinkRow({
label,
value,
leading,
chevron = true,
as,
href,
type = "button",
onClick,
}: {
label: string;
value?: string;
/** Something before the name, such as an avatar. */
leading?: ReactNode;
chevron?: boolean;
as?: ElementType;
href?: string;
type?: "button" | "submit";
onClick?: (event: MouseEvent<HTMLElement>) => void;
}) {
const Tag: ElementType = href !== undefined ? (as ?? "a") : "button";
const props = href !== undefined ? { href } : { type };
return (
<Tag {...props} className={styles.linkRow} onClick={onClick}>
{leading}
<span className={styles.rowLabel}>{label}</span>
{value ? <span className={styles.linkValue}>{value}</span> : null}
{chevron ? <UiIcon iconRole="chevron-right" size={16} strokeWidth={1.75} className={styles.linkChevron} aria-hidden="true" /> : null}
</Tag>
);
}
/** A control that needs the whole width of the card (a slider, a choice among many). */
function SettingsBlock({ children }: { children: ReactNode }) {
return <div className={styles.block}>{children}</div>;
}
export type SettingsPanelBodyProps = Pick<SettingsPanelProps, "sound" | "colorMode" | "season" | "seasonDecor" | "textSize" | "reduceMotion" | "reduceBlur" | "children" | "onReset" | "labels" | "hints">;
/**
* The settings themselves, without a popover around them: grouped cards for sound with a volume, color mode, a season theme, text
* size and reduced motion, plus any groups the host adds (language, account, privacy). A host that gives them a page of their own
* (a phone) uses this directly; `SettingsPanel` puts the same thing in a popover. It owns no state: each group takes its value and
* a change handler, so the host decides where a choice is kept. A group is left out when its prop is null or missing.
*/
export function SettingsPanelBody({
sound,
colorMode,
season,
seasonDecor,
textSize,
reduceMotion,
reduceBlur,
children,
onReset,
labels: given,
hints = {},
}: SettingsPanelBodyProps) {
const labels = { ...defaultLabels, ...given };
const base = useId();
const hintId = (key: keyof SettingsPanelHints) => `${base}-${key}`;
const note = (key: keyof SettingsPanelHints) => (hints[key] ? [{ id: hintId(key), text: hints[key] as string }] : []);
const describe = (key: keyof SettingsPanelHints) => (hints[key] ? hintId(key) : undefined);
return (
<div className={styles.panel}>
{sound ? (
<SettingsGroup title={labels.sound} notes={[...note("soundSwitch"), ...note("volume")]}>
<SettingsRow label={labels.soundSwitch}>
<Switch
aria-label={labels.soundSwitch}
aria-describedby={describe("soundSwitch")}
checked={sound.enabled}
onCheckedChange={(next) => { sound.onEnabledChange(next); if (next) playUiSound("toggle-on"); }}
/>
</SettingsRow>
<SettingsBlock>
<Slider
label={labels.volume}
value={Math.round(sound.volume * 100)}
min={0}
max={100}
step={5}
disabled={!sound.enabled}
format={(value) => `${Math.round(value)}%`}
onValueChange={(value) => sound.onVolumeChange(value / 100)}
onValueCommit={() => playUiSound("tick")}
/>
</SettingsBlock>
</SettingsGroup>
) : null}
{colorMode ? (
<SettingsGroup title={labels.colorMode} notes={note("colorMode")}>
<SettingsBlock>
<SegmentedControl
label={labels.colorMode}
value={colorMode.value}
options={[{ value: "system", label: labels.system }, { value: "light", label: labels.light }, { value: "dark", label: labels.dark }]}
onValueChange={(next) => colorMode.onChange(next as SettingsColorMode)}
/>
</SettingsBlock>
</SettingsGroup>
) : null}
{season || seasonDecor ? (
<SettingsGroup title={labels.season} notes={[...note("season"), ...note("seasonDecor")]}>
{season ? (
<SettingsBlock>
<SegmentedControl label={labels.season} value={season.value} options={season.options} onValueChange={season.onChange} />
</SettingsBlock>
) : null}
{seasonDecor ? (
<SettingsRow label={labels.seasonDecor}>
<Switch aria-label={labels.seasonDecor} aria-describedby={describe("seasonDecor")} checked={seasonDecor.value} onCheckedChange={seasonDecor.onChange} />
</SettingsRow>
) : null}
</SettingsGroup>
) : null}
{textSize || reduceMotion || reduceBlur ? (
<SettingsGroup title={labels.textSize} notes={[...note("textSize"), ...note("reduceMotion"), ...note("reduceBlur")]}>
{textSize ? (
<SettingsRow label={labels.textSize}>
<SegmentedControl
label={labels.textSize}
value={textSize.value}
options={[{ value: "normal", label: labels.textNormal }, { value: "large", label: labels.textLarge }]}
onValueChange={(next) => textSize.onChange(next as SettingsTextSize)}
/>
</SettingsRow>
) : null}
{reduceMotion ? (
<SettingsRow label={labels.reduceMotion}>
<Switch aria-label={labels.reduceMotion} aria-describedby={describe("reduceMotion")} checked={reduceMotion.value} onCheckedChange={reduceMotion.onChange} />
</SettingsRow>
) : null}
{reduceBlur ? (
<SettingsRow label={labels.reduceBlur}>
<Switch aria-label={labels.reduceBlur} aria-describedby={describe("reduceBlur")} checked={reduceBlur.value} onCheckedChange={reduceBlur.onChange} />
</SettingsRow>
) : null}
</SettingsGroup>
) : null}
{children}
{onReset ? <button type="button" className={styles.reset} onClick={onReset}>{labels.reset}</button> : null}
</div>
);
}
/**
* The site's settings in a popover under the gear (`SettingsPanelBody` inside it). Give the settings a page of their own where the
* popover does not fit, such as a phone.
*/
export function SettingsPanel({
sound,
colorMode,
season,
seasonDecor,
textSize,
reduceMotion,
reduceBlur,
children,
onReset,
trigger,
side = "right",
open: openProp,
defaultOpen = false,
onOpenChange,
labels: given,
hints = {},
triggerClassName,
}: SettingsPanelProps) {
const labels = { ...defaultLabels, ...given };
const [inner, setInner] = useState(defaultOpen);
const open = openProp ?? inner;
const setOpen = (next: boolean) => { setInner(next); onOpenChange?.(next); };
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
{trigger ?? (
<Button variant="ghost" size="sm" silent aria-label={labels.triggerLabel} className={triggerClassName}>
<HugeiconsIcon icon={Settings02Icon} size={19} strokeWidth={1.75} aria-hidden="true" />
</Button>
)}
</PopoverTrigger>
<PopoverContent aria-label={labels.title} align={side === "left" ? "start" : "end"} className={styles.popover}>
<div className={styles.popoverHead}>
<h2 className={styles.title}>{labels.title}</h2>
<p className={styles.note}>{labels.description}</p>
</div>
<SettingsPanelBody sound={sound} colorMode={colorMode} season={season} seasonDecor={seasonDecor} textSize={textSize} reduceMotion={reduceMotion} reduceBlur={reduceBlur} onReset={onReset} labels={given} hints={hints}>
{children}
</SettingsPanelBody>
</PopoverContent>
</Popover>
);
}
export default SettingsPanel;
Used items
- DrawerComponent
A panel that slides in from any edge of the screen.
- Segmented controlComponent
A row of mutually exclusive options with a highlight that moves to the chosen segment.
- SliderComponent
A slider for picking a single value or a range by dragging or keyboard.
- SwitchComponent
A toggle for on and off values whose thumb stretches while pressed.
- ButtonComponent
The base button with press feedback, sound and several variants.
Used by
- Settings stepsBlock
A settings block in two steps: a list of groups first, then the chosen group's own page sliding in from the right with a back button.