Pages

Settings page

A settings page: the side rail, profile fields and notification switches on cards, and the save button.

Install

React apps get the React files; every other stack gets the web component.

npx spyglass-ds add settings-page
<script type="module" src="/spyglass-ds/elements/settings-page.js"></script>
Settings

Settings

Profile

Notifications

  • Weekly summaryWhat changed across the brands you track, every Monday.
  • Launch alertsAn email the day a tracked brand ships a new ad.

Usage

Account or workspace settings; the form posts to action.

Never

  • New colors, shadows or type sizes added to a section: use the items' props
  • More than one White button in a view
  • Real people or customers in the copy: Acme only

sections lists the sections to show, in order (profile, notifications, save); a key left out is not shown.

Adds with it: button, creative-card, field, input, logo, switch

Props

The web component takes the same as attributes or properties (<spyglass-settings-page>); title is heading there.

<SettingsPage>

PropTypeDefault
contentSettingsContent
actionstring
sectionsreadonly SettingsSection[]

The sections to show, in order; all of them in the page order by default (leave one out to omit it).

classNamestring

Examples

The same example in React and as web components; both render identically.

react/examples/settings-page/Page.tsx
import { SettingsPage } from '@/react/components/pages/SettingsPage'
// The settings page with its own copy (Acme).

export default function Page() {
  return <SettingsPage />
}
elements/examples/settings-page/Page.html
<!-- The settings page with its own copy (Acme). -->
<spyglass-settings-page></spyglass-settings-page>

Source

react/components/pages/SettingsPage.tsx
import { Button } from '@/react/components/ui/Button'
import { Field } from '@/react/components/ui/Field'
import { Input } from '@/react/components/ui/Input'
import { Switch } from '@/react/components/ui/Switch'
import { Logo } from '@/react/components/visuals/Logo'
import { settingsContent, type SettingsContent, settingsSections, type SettingsSection } from '@/lib/pages/settings'
import { cn } from '@/react/lib/cn'
import { pageSections } from '@/lib/image'
import { Fragment, type ReactNode } from 'react'

// Settings in the site's look: the same side rail as the dashboard, then sections on #262626 cards: the profile fields
// and the notification switches (each row a 14/20 Medium label, a #A3A3A3 line and the switch), and the one White button
// to save. The form posts to `action`; the app wires it. The rail sits on top on phones.
export interface SettingsPageProps {
  content?: SettingsContent
  action?: string
  /** The sections to show, in order; all of them in the page order by default (leave one out to omit it). */
  sections?: readonly SettingsSection[]
  className?: string
}

const LINK = 'flex h-control-xs items-center rounded-control px-3 type-ui text-fg-soft transition-[color,background-color] duration-160 ease-out hover:bg-surface-raised hover:text-fg aria-[current=page]:bg-surface-raised aria-[current=page]:text-fg'

export function SettingsPage({ content: c = settingsContent, action, sections, className }: SettingsPageProps) {
  const parts: Record<SettingsSection, ReactNode> = {
    profile: (
      <section className="grid gap-5 rounded-container bg-surface-raised p-6">
        <h2 className="type-heading-3">{c.profile.title}</h2>
        <Field label={c.profile.name.label}>{({ id }) => <Input id={id} name="name" autoComplete="name" defaultValue={c.profile.name.value} />}</Field>
        <Field label={c.profile.email.label}>{({ id }) => <Input id={id} name="email" type="email" autoComplete="email" defaultValue={c.profile.email.value} />}</Field>
      </section>
    ),
    notifications: (
      <section className="grid gap-5 rounded-container bg-surface-raised p-6">
        <h2 className="type-heading-3">{c.notifications.title}</h2>
        <ul role="list" className="m-0 grid list-none gap-5 p-0">
          {c.notifications.items.map((n) => (
            <li key={n.label} className="flex items-start justify-between gap-6">
              <span className="grid gap-1">
                <span className="type-ui-strong text-fg">{n.label}</span>
                <span className="type-ui text-fg-muted">{n.description}</span>
              </span>
              <Switch label={n.label} defaultChecked={n.on} />
            </li>
          ))}
        </ul>
      </section>
    ),
    save: <Button type="submit" className="justify-self-start">{c.save}</Button>,
  }
  return (
    <div data-ds="settings-page" className={cn('grid min-h-dvh grid-cols-[minmax(0,1fr)] bg-surface text-fg lg:grid-cols-[256px_minmax(0,1fr)]', className)}>
      <aside className="flex min-w-0 flex-col gap-6 border-b border-edge bg-surface-subtle p-4 lg:border-r lg:border-b-0">
        <a href="/" aria-label="Home" className="self-start rounded-control px-2 py-2"><Logo height={20} /></a>
        <nav aria-label="Settings">
          <ul role="list" className="m-0 flex list-none gap-1 overflow-x-auto p-0 fade-x lg:flex-col">
            {c.nav.map((n) => <li key={n.href} className="shrink-0"><a href={n.href} aria-current={n.current ? 'page' : undefined} className={LINK}>{n.label}</a></li>)}
          </ul>
        </nav>
      </aside>
      <main className="grid max-w-200 content-start gap-8 p-6 lg:p-10">
        <h1 className="type-heading-2">{c.title}</h1>
        <form action={action} method="post" className="grid gap-6">
        {pageSections(settingsSections, sections).map((k) => <Fragment key={k}>{parts[k]}</Fragment>)}
        </form>
      </main>
    </div>
  )
}

export default SettingsPage
lib/pages/settings.ts
// The settings page's copy for Acme: the sections, the profile fields and the notification switches.
export const settingsContent = {
	nav: [
		{ label: 'Profile', href: '#profile', current: true },
		{ label: 'Team', href: '#team' },
		{ label: 'Billing', href: '#billing' },
		{ label: 'API keys', href: '#keys' },
	],
	title: 'Settings',
	profile: { title: 'Profile', name: { label: 'Name', value: 'Alex Kim' }, email: { label: 'Email', value: 'alex@acme.com' } },
	notifications: {
		title: 'Notifications',
		items: [
			{ label: 'Weekly summary', description: 'What changed across the brands you track, every Monday.', on: true },
			{ label: 'Launch alerts', description: 'An email the day a tracked brand ships a new ad.', on: false },
		],
	},
	save: 'Save changes',
}
export type SettingsContent = typeof settingsContent

/** The settings page's sections in order; pass `sections` to reorder them or leave some out. */
export const settingsSections = ['profile', 'notifications', 'save'] as const
export type SettingsSection = (typeof settingsSections)[number]
react/lib/cn.ts
// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]

export function cn(...values: ClassValue[]): string {
	const out: string[] = []
	for (const v of values) {
		if (!v) continue
		if (Array.isArray(v)) {
			const s = cn(...v)
			if (s) out.push(s)
		}
		else out.push(v)
	}
	return out.join(' ')
}
elements/components/pages/settings-page.ts
import { html } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import { settingsContent, type SettingsContent, settingsSections, type SettingsSection } from '@/lib/pages/settings'
import '@/elements/components/ui/button'
import '@/elements/components/ui/field'
import '@/elements/components/ui/input'
import '@/elements/components/ui/switch'
import '@/elements/components/visuals/logo'
import styles from 'virtual:tw'
import { pageSections } from '@/lib/image'

// SettingsPage.tsx as <spyglass-settings-page>: the rail, the profile fields and notification switches on cards, and
// the save button. `content` replaces the copy; the form posts to `action`.
const LINK = 'flex h-control-xs items-center rounded-control px-3 type-ui text-fg-soft transition-[color,background-color] duration-160 ease-out hover:bg-surface-raised hover:text-fg aria-[current=page]:bg-surface-raised aria-[current=page]:text-fg'

/**
 * @summary A settings page: profile fields and notification switches.
 */
export class SettingsPage extends DsElement {
	static styles = [DsElement.styles, styles]
	protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="settings-page"]'))
	static properties = { content: { type: Object }, action: {}, sections: { type: Array } }
	/** The copy (lib/pages/settings.ts by default). */
	content: SettingsContent = settingsContent
	/** Where the form posts. */
	action?: string
	/** The sections to show, in order (a list of names); all of them in the page order by default. */
	sections?: SettingsSection[]

	render() {
		const c = this.content ?? settingsContent
		const parts: Record<SettingsSection, unknown> = {
			profile: html`<section class="grid gap-5 rounded-container bg-surface-raised p-6"><h2 class="type-heading-3">${c.profile.title}</h2><spyglass-field label=${c.profile.name.label}><spyglass-input name="name" autocomplete="name" value=${c.profile.name.value}></spyglass-input></spyglass-field><spyglass-field label=${c.profile.email.label}><spyglass-input name="email" type="email" autocomplete="email" value=${c.profile.email.value}></spyglass-input></spyglass-field></section>`,
			notifications: html`<section class="grid gap-5 rounded-container bg-surface-raised p-6"><h2 class="type-heading-3">${c.notifications.title}</h2><ul role="list" class="m-0 grid list-none gap-5 p-0">${c.notifications.items.map((n) => html`<li class="flex items-start justify-between gap-6"><span class="grid gap-1"><span class="type-ui-strong text-fg">${n.label}</span><span class="type-ui text-fg-muted">${n.description}</span></span><spyglass-switch label=${n.label} ?checked=${n.on}></spyglass-switch></li>`)}</ul></section>`,
			save: html`<spyglass-button type="submit" class="justify-self-start">${c.save}</spyglass-button>`,
		}
		return html`<div data-ds="settings-page" class="grid min-h-dvh grid-cols-[minmax(0,1fr)] bg-surface text-fg lg:grid-cols-[256px_minmax(0,1fr)]"><aside class="flex min-w-0 flex-col gap-6 border-b border-edge bg-surface-subtle p-4 lg:border-r lg:border-b-0"><a href="/" aria-label="Home" class="self-start rounded-control px-2 py-2"><spyglass-logo height="20"></spyglass-logo></a><nav aria-label="Settings"><ul role="list" class="m-0 flex list-none gap-1 overflow-x-auto p-0 fade-x lg:flex-col">${c.nav.map((n) => html`<li class="shrink-0"><a href=${n.href} aria-current=${ifDefined(n.current ? 'page' : undefined)} class=${LINK}>${n.label}</a></li>`)}</ul></nav></aside><main class="grid max-w-200 content-start gap-8 p-6 lg:p-10"><h1 class="type-heading-2">${c.title}</h1><form action=${ifDefined(this.action)} method="post" class="grid gap-6">${pageSections(settingsSections, this.sections).map((k) => parts[k])}</form></main></div>`
	}
}

define('settings-page', SettingsPage)

Reference: Built from the library and the site's surfaces (cards on --surface, the rail on --raised, hairlines, the 36px links of the header).