Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions storybook/stories/design-system/patterns/settings-page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { Canvas, Markdown, Meta } from '@storybook/addon-docs/blocks';
import * as SettingsPageStories from './settings-page.story';
import SimpleSource from './settings-page/simple.tsx?raw';
import SectionsSource from './settings-page/sections.tsx?raw';
import FailedSaveSource from './settings-page/failed-save.tsx?raw';
import AdjacentPagesSource from './settings-page/adjacent-pages.tsx?raw';
import SubpagesSource from './settings-page/subpages.tsx?raw';
import AdjacentPagesAndSubpagesSource from './settings-page/adjacent-pages-and-subpages.tsx?raw';
import RecordsSubpageSource from './settings-page/records-subpage.tsx?raw';
import RecordsDialogSource from './settings-page/records-dialog.tsx?raw';
import RecordsDrawerSource from './settings-page/records-drawer.tsx?raw';

<Meta title="Design System/Patterns/Settings Page" tags={ [ 'manifest' ] } />

# Settings Page

Templates for settings screens, composed from the admin-ui `Page`, `@wordpress/ui` components, `@wordpress/theme` tokens and DataViews. Start from the template closest to your settings, then replace the fields.

These templates are starting points, not requirements. Not every settings page needs to match one exactly: adapt the structure to what your settings need, and combine or depart from the templates where that serves people better.

Several of the `@wordpress/ui` components they use, including `Button`, `Dialog`, `Drawer` and `AlertDialog`, are still marked “Use with caution”, so their APIs may change. Treat the templates as the direction settings pages are heading, not as a recommendation to migrate existing screens yet.

## Building blocks

Every template uses the same parts:

- **`Page`** from `@wordpress/admin-ui` provides the header: a title or breadcrumbs, optional section navigation, and the actions area where Save lives.
- **`Card`** from `@wordpress/ui` groups related settings. Give each card a short title, and a description when the title alone doesn't say what the settings affect. The Structure section below covers when to use one. Cap form cards at `--wpds-dimension-surface-width-xl` so fields stay a readable length, and give tables of records `--wpds-dimension-surface-width-2xl` so their columns fit.
- **`DataForm`** from `@wordpress/dataviews` renders the fields, so labels, descriptions, validation and spacing stay consistent.
- **`DataViews`** from `@wordpress/dataviews` lists records people can add and edit.
- **`Dialog`**, **`Drawer`** and **`AlertDialog`** from `@wordpress/ui` hold record editors and confirmations.
- **Snackbars** confirm saves that have no other visible effect. `SnackbarList` comes from `@wordpress/components`, since `@wordpress/ui` doesn't offer an equivalent yet.

Each example's code shows its template. The helpers it imports, such as the manual-save draft, the sample fields and the in-canvas router, live alongside it in `storybook/stories/design-system/patterns/settings-page/`.

## Structure

Plan a settings page like a document's outline before choosing how it looks. The headings decide how assistive technology reads the page and lets people jump through it, and the same kind of content should get the same treatment wherever it appears.

- **Put each group of related fields in a card.** A card bounds a group and separates it from the groups around it. Keep the card even when a page has a single group, so every settings page reads the same way.
- **Title each card after its group of fields.** The page title is the `h1`, and on a page without sections the card titles are `h2`s.
- **Add sections when cards form larger groups.** A section heading sits on the canvas above its cards, as an `h2` with an optional description, and the cards inside it take `h3` titles. Use `Text` with `variant="heading-lg"` for the section heading. Each section gets its own heading, and a page with only a few cards needs none.
- **Don't skip heading levels**, and don't choose a level for its size. The level follows the structure, so a card title looks the same whether it's an `h2` or an `h3`.

## Saving

These templates use the manual save model from [Save & Submit](?path=/docs/design-system-patterns-save-submit--docs): edits are held in a draft until the user clicks “Save changes”. Manual save is the usual choice for settings, because their fields often depend on each other and should apply together. Autosave suits a page of independent preferences that each take effect on their own, such as the block editor’s Preferences modal. Choose one model for the whole page and don't mix the two: if you choose autosave, remove the Save button. [Choose one save model per surface](?path=/docs/design-system-patterns-save-submit--docs#choose-one-save-model-per-surface) explains how to decide.

- **One “Save changes” button per page**, in the `Page` header actions. It covers every field on that page and nothing else.
- **Disabled only when there is nothing to save**, and busy while saving. Don't explain the disabled state with a tooltip: tooltips aren't available to screen readers or on touch devices.
- **Invalid fields don't disable Save.** The fields sit in a `<form>`, and Save submits it: invalid fields show their errors below them and the first one takes focus, and nothing is saved until they're fixed. The record editors work the same way.
- **Edits made while saving stay unsaved.** People can keep typing while a save is in progress. Once it finishes, anything they changed in the meantime is still an unsaved change, and a record editor stays open to hold it.
- **Checkboxes, not switches, for on/off settings.** A switch implies the change takes effect immediately, which isn't true until Save is clicked. Switches belong on autosave surfaces.
- **A snackbar confirms the save.** Errors belong next to the field, or in a `Notice` at the top of the form when a save fails or only partly succeeds.
- **Pages that only list records have no page-level Save.** Each record editor saves on its own, and the header holds the action that adds a record.
- **Leaving with unsaved edits asks first.** Switching pages, following a breadcrumb or closing a record editor with Escape asks “Discard unsaved changes?”, and reloading or closing the tab shows the browser’s own warning. Navigating while a save is in progress waits for it to finish, then asks only if something changed after clicking Save. Try it in any template by changing a field, then navigating away. See [Unsaved changes on exit](?path=/docs/design-system-patterns-save-submit--docs#unsaved-changes-on-exit).

### When a save fails

Keep the draft, so nothing the user entered is lost, and explain the failure in a `Notice` at the top of the form with a “Try again” action. Retrying submits the form again, so validation runs first. A retry that goes through removes the notice, and the focused button with it, so move focus to “Save changes” first. In this example the first save fails: change a field, click “Save changes”, then try again.

<Canvas
of={ SettingsPageStories.FailedSave }
source={ { code: FailedSaveSource, language: 'tsx', type: 'code' } }
/>

## Focus after navigation

Moving between adjacent pages and subpages replaces the page, including the link that was just used. Without help, keyboard focus falls back to the start of the document and screen readers don't announce the new page.

The templates' in-canvas router handles this: when a navigation removes the focused element, focus moves to the new page's `Page` region, and screen readers announce it by name. Try it by following a breadcrumb with the keyboard. `@wordpress/boot` currently updates and announces the route title, and will move focus the same way once [#86701](https://github.com/WordPress/gutenberg/pull/86701) lands. Until then, or if your app routes without `@wordpress/boot`, move focus to the `Page` region yourself after each navigation that loses focus.

The region takes its name from `title`, so give it an `ariaLabel` whenever the title doesn't identify the page: on a subpage whose breadcrumbs replace the title, name it after the page, such as “Card payments”. On adjacent pages that share a title, name each one after its page, such as “Payments settings”.

## Simple settings page

A single page with a few groups of settings. Use it when everything fits on one screen.

<Canvas
of={ SettingsPageStories.Simple }
source={ { code: SimpleSource, language: 'tsx', type: 'code' } }
/>

## Settings page with sections

A longer page whose cards fall into larger groups, here "Accepting payments" and "Payouts". Each section has an `h2` heading on the canvas, and its cards take `h3` titles. Use it when one page holds several distinct areas, but they are too closely related, or too small, to split into adjacent pages.

<Canvas
of={ SettingsPageStories.Sections }
source={ { code: SectionsSource, language: 'tsx', type: 'code' } }
/>

## Adjacent pages

Peer sections of one settings area, such as General, Payments and Emails, switched with the `Page` `navigation`. Use it when the sections are independent and each one is substantial enough to deserve a page.

- Keep the page title the same across adjacent pages; the navigation shows which page is current, so the title doesn't need to name it.
- Each page has its own draft and Save. Saving one page doesn't save another.
- Pass a router link through `components.link`, so switching pages doesn't reload the screen.

<Canvas
of={ SettingsPageStories.AdjacentPages }
source={ { code: AdjacentPagesSource, language: 'tsx', type: 'code' } }
/>

## Subpages

A subpage holds detail that would crowd the main page, such as the credentials for each payment gateway. Open it from the page body with a full-width row at the bottom of the card: a `Link` with `variant="unstyled"`, styled as a row, with a trailing chevron to signal that it navigates. A status badge in the row tells people what they will find there.

When a card leads to several subpages, put the rows in a list:

- **Use a `ul`, named by the card title.** Render the title as a heading, and point the list's `aria-labelledby` at it, so screen readers announce “Payment gateways, list, 3 items”.
- **Keep `role="list"` on the unstyled list.** Safari drops list semantics when the bullets are removed.
- **Name each link after its destination.** The row label matches the subpage's heading, so the link's name is just “Card payments”. Show the status in a `Badge` and connect it with `aria-describedby`, so screen readers announce it as a description rather than as part of the name.

On the subpage, breadcrumbs replace the title and lead back to the parent page. Repeat the row's status next to the breadcrumbs with the `Page` `badges` prop, and update it from the saved settings, not the draft, so it changes when people click Save.

<Canvas
of={ SettingsPageStories.Subpages }
source={ { code: SubpagesSource, language: 'tsx', type: 'code' } }
/>

## Adjacent pages and subpages

When a subpage belongs to one of several adjacent pages, its trail passes through that page, for example "Settings / Payments / Card payments". "Payments" leads back to the page the subpage was opened from, with its navigation item current, and "Settings" leads to the first adjacent page. The subpage drops the section navigation.

<Canvas
of={ SettingsPageStories.AdjacentPagesAndSubpages }
source={ { code: AdjacentPagesAndSubpagesSource, language: 'tsx', type: 'code' } }
/>

## Records

Some settings are lists of records, such as API keys, webhooks or redirects. These examples use API keys. Show them in a `DataViews` table, and choose how each record opens by the size of its editor.

<Markdown>{ `
| Editor | Use when |
| --- | --- |
| Subpage | The editor is long, has its own sections, or people need to link to a record. |
| Dialog | The editor is short and people should stay focused on one record. |
| Drawer | The editor is medium-sized, people benefit from seeing the table, or the record has secondary actions. |
` }</Markdown>

### Records that open a subpage

The record title links to the subpage, and the Edit action goes to the same place. The subpage uses breadcrumbs, and its Save sits in the `Page` header like any other settings page. The table goes away while a record is open, so keep its search, filters and page somewhere that outlives it, and people find their results as they left them when they come back. Real screens usually keep the view in the URL or in user preferences; this example holds it above its routes.

<Canvas
of={ SettingsPageStories.RecordsWithSubpages }
source={ { code: RecordsSubpageSource, language: 'tsx', type: 'code' } }
/>

### Records that open a dialog

The dialog saves on its own. Cancel discards the draft, and Save applies it, closes the dialog and confirms with a snackbar. The same dialog adds new records.

The footer actions are the only way out: there is no close icon, and clicking outside does nothing, so people always choose between keeping and discarding their edits. Pressing Escape closes an untouched dialog, but asks first when there are unsaved edits.

<Canvas
of={ SettingsPageStories.RecordsWithDialog }
source={ { code: RecordsDialogSource, language: 'tsx', type: 'code' } }
/>

### Records that open a drawer

The drawer works like the dialog, including closing only through its footer actions, with room for secondary actions. Revoking a key can't be undone, so it asks for confirmation in an `AlertDialog` with the `irreversible` intent, following [Destructive Actions](?path=/docs/design-system-patterns-destructive-actions--docs). Canceling returns focus to Revoke. After the key is revoked, focus moves to the next record, or to the previous one when it was last, and to Add API key when none remain.

<Canvas
of={ SettingsPageStories.RecordsWithDrawer }
source={ { code: RecordsDrawerSource, language: 'tsx', type: 'code' } }
/>
67 changes: 67 additions & 0 deletions storybook/stories/design-system/patterns/settings-page.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { AdjacentPagesExample } from './settings-page/adjacent-pages';
import { AdjacentPagesAndSubpagesExample } from './settings-page/adjacent-pages-and-subpages';
import { FailedSaveExample } from './settings-page/failed-save';
import { RecordsDialogExample } from './settings-page/records-dialog';
import { RecordsDrawerExample } from './settings-page/records-drawer';
import { RecordsSubpageExample } from './settings-page/records-subpage';
import { SectionsExample } from './settings-page/sections';
import { SimpleSettingsExample } from './settings-page/simple';
import { SubpagesExample } from './settings-page/subpages';

const meta: Meta = {
title: 'Design System/Patterns/Settings Page',
parameters: {
controls: { disable: true },
layout: 'fullscreen',
},
tags: [ 'docs-only' ],
};

export default meta;

type Story = StoryObj;

export const Simple: Story = {
name: 'Simple settings page',
render: () => <SimpleSettingsExample />,
};

export const FailedSave: Story = {
name: 'Failed save',
render: () => <FailedSaveExample />,
};

export const Sections: Story = {
name: 'Settings page with sections',
render: () => <SectionsExample />,
};

export const AdjacentPages: Story = {
name: 'Adjacent pages',
render: () => <AdjacentPagesExample />,
};

export const Subpages: Story = {
render: () => <SubpagesExample />,
};

export const AdjacentPagesAndSubpages: Story = {
name: 'Adjacent pages and subpages',
render: () => <AdjacentPagesAndSubpagesExample />,
};

export const RecordsWithSubpages: Story = {
name: 'Records that open a subpage',
render: () => <RecordsSubpageExample />,
};

export const RecordsWithDialog: Story = {
name: 'Records that open a dialog',
render: () => <RecordsDialogExample />,
};

export const RecordsWithDrawer: Story = {
name: 'Records that open a drawer',
render: () => <RecordsDrawerExample />,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { SettingsRouter, useSettingsPath } from './router';
import { SettingsExample } from './shared';
import {
adjacentPageHeader,
EmailsPage,
GatewayPage,
gatewayFromPath,
GeneralPage,
PaymentsPage,
SETTINGS_CRUMB,
subpageHeader,
useStoreSettings,
} from './store-pages';

function AdjacentPagesAndSubpagesScreen() {
const path = useSettingsPath();
const settings = useStoreSettings();
const gateway = gatewayFromPath( path );

if ( gateway ) {
return (
<GatewayPage
key={ path }
gatewayId={ gateway.id }
settings={ settings }
header={ subpageHeader( [
SETTINGS_CRUMB,
{ label: 'Payments', to: '/payments' },
{ label: gateway.name },
] ) }
/>
);
}

switch ( path ) {
case '/payments':
return (
<PaymentsPage
key={ path }
settings={ settings }
header={ adjacentPageHeader( path ) }
showGateways
/>
);
case '/emails':
return (
<EmailsPage
key={ path }
settings={ settings }
header={ adjacentPageHeader( path ) }
/>
);
default:
return (
<GeneralPage
key={ path }
settings={ settings }
header={ adjacentPageHeader( '/general' ) }
/>
);
}
}

/**
* Adjacent pages where one page leads to a subpage. The subpage drops the
* section navigation, and its breadcrumbs pass through the adjacent page it
* belongs to.
*/
export function AdjacentPagesAndSubpagesExample() {
return (
<SettingsExample>
<SettingsRouter initialPath="/payments">
<AdjacentPagesAndSubpagesScreen />
</SettingsRouter>
</SettingsExample>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { SettingsRouter, useSettingsPath } from './router';
import { SettingsExample } from './shared';
import {
adjacentPageHeader,
EmailsPage,
GeneralPage,
PaymentsPage,
useStoreSettings,
} from './store-pages';

function AdjacentPagesScreen() {
const path = useSettingsPath();
const settings = useStoreSettings();
const header = adjacentPageHeader( path );

switch ( path ) {
case '/payments':
return (
<PaymentsPage
key={ path }
settings={ settings }
header={ header }
/>
);
case '/emails':
return (
<EmailsPage
key={ path }
settings={ settings }
header={ header }
/>
);
default:
return (
<GeneralPage
key={ path }
settings={ settings }
header={ adjacentPageHeader( '/general' ) }
/>
);
}
}

/**
* Peer sections of one settings area, switched with the `Page` navigation.
* Each page keeps its own draft and Save.
*/
export function AdjacentPagesExample() {
return (
<SettingsExample>
<SettingsRouter initialPath="/general">
<AdjacentPagesScreen />
</SettingsRouter>
</SettingsExample>
);
}
Loading
Loading