包页面→ /base/explore.md
pages/base/explore.md
105 行, 5.6 KB. 以页面形式查看.
这段内容尚未翻译,下面是英文原文。
- ---
- type: Page
- title: "Exploration pages"
- icon: "🎛️"
- description: "How to write an exploration page or a lens with the explore kit: one call, a right side panel with foldable sections and presets, values on data- attributes and CSS properties, kept in localStorage and the URL."
- ---
- # Exploration pages
- An exploration page shows one design in many variants and lets a person switch them: the form of a card, its cover, a
- radius, a glow colour. The explore kit (package `explore-kit`) gives every such page the same panel: on the right,
- collapsible to a small button, split into sections that fold, with presets, a "Reset" and a "Copy link" button. The
- values land on the page as `data-<name>` attributes and `--x-<name>` CSS properties, so most of the page is plain CSS.
- Open `/design/explore-kit/demo.html` to see it: the settings of the package map, rebuilt with the kit.
- ## A minimal page
- ```html
- <!doctype html>
- <html>
- <head>
- <link rel="stylesheet" href="../design/explore-kit/explore.css">
- <style>
- .card { border-radius: var(--x-radius); padding: 16px; box-shadow: 0 0 0 1px #ddd; }
- [data-size="l"] .card { padding: 28px; font-size: 18px; }
- [data-shadow="true"] .card { box-shadow: 0 6px 20px rgb(0 0 0 / .12); }
- .card { background: var(--x-tint); }
- </style>
- </head>
- <body>
- <div class="card">A card</div>
- <script src="../design/explore-kit/explore.js"></script>
- <script>
- Explore.panel({
- title: 'Card',
- storageKey: 'card-demo',
- sections: [
- {title: 'Shape', controls: {
- size: {kind: 'segment', options: [['m', 'M'], ['l', 'L']], default: 'm'},
- radius: {kind: 'range', min: 0, max: 24, unit: 'px', default: 8},
- shadow: {kind: 'bool'}}},
- {title: 'Colour', folded: true, controls: {
- tint: {kind: 'swatch', options: ['#ffffff', '#fdf1f4', '#eef5fc', 'chart-2']}}}
- ],
- presets: {soft: {radius: 16, shadow: true}, flat: {radius: 0}},
- onChange: (values) => console.log(values)
- });
- </script>
- </body>
- </html>
- ```
- In a lens, link the copies the lens build puts into `lib/`: `../lib/explore.css` and `../lib/explore.js` (as many `../` as
- the lens is deep, like `kit.js`). The lens build copies them when the project has the `explore-kit` package.
- ## The call
- `Explore.panel(options)` builds the panel, applies the start values, calls `onChange` once and returns a handle.
- | Option | What it is |
- |---|---|
- | `title` | the panel's title |
- | `target` | the element the values land on (default `document.documentElement`): `data-<name>="<value>"` and `--x-<name>: <css value>` |
- | `storageKey` | the localStorage key (stored as `explore:<key>`): the values, the collapsed state, the folded sections |
- | `sections` | `[{title, folded?, controls: {<name>: control}}]`; a section with no title has no header |
- | `controls` | controls outside `sections`, grouped by their `section` field |
- | `presets` | `{<name>: {<control>: value}}`: named bundles, shown as the first select; a preset is the defaults plus its bundle; a tweak shows "Custom" |
- | `open` | `false` starts collapsed when nothing is stored |
- | `container` | an element to stand the panel in instead of floating over the page; a lens passes the first section of its side panel (`ui.section()`), so the panel does not cover the side panel. The colours read the kit's own names first (`--panel`, `--ink`, `--bg3`, `--line`), since the kit's `--muted`, `--accent` and `--card` mean something else than in shadcn |
- | `onChange` | `(values) => {}`, after every change and once at the start |
- The start values are the defaults, then the stored values, then the URL (`?size=l&radius=12`): the URL wins, so a copied
- link opens what its author saw. Every change writes the non-default values back to the URL (`history.replaceState`) and to
- storage; other URL parameters stay.
- The handle: `values()`, `get(name)`, `set(name, value)` or `set({...})`, `preset(name)`, `reset()`, `link()`, `open()`,
- `close()`, `toggle()`, `destroy()`, `el`.
- ## Controls
- The control schema of the design build contract, the same one component headers and the catalog use:
- `{kind, label?, options?, default?, min?, max?, step?, unit?, section?, colors?}`.
- | Kind | Looks like | Value | `--x-<name>` |
- |---|---|---|---|
- | `select` | a grey rounded dropdown | one of `options` | the value |
- | `segment` | a segmented switch, 2-4 options | one of `options` | the value |
- | `bool` | a toggle | `true` / `false` (`data-` is `"true"`/`"false"`) | `1` / `0` |
- | `range` | a black-thumb slider with a mono value | a number in `min`…`max`, snapped to `step` | the number plus `unit` |
- | `swatch` | colour dots, a ring on the chosen one | one of `options` | the colour: `colors[value]`, a token name (`chart-1`, `--primary`) as `var(--…)`, or the CSS colour itself |
- | `text` | a text field | any text | the text |
- `options` are strings, `[value, label]` pairs or `{value, label}` objects. `label` defaults to the name made readable.
- `colors` maps option values to colours: the swatch draws them, a segment shows a dot. The schema maps one-to-one to
- Storybook `argTypes`: `select`/`segment` → `select`/`radio`, `bool` → `boolean`, `range` → `range`, `swatch` → `color`,
- `text` → `text`.
- ## Keyboard and look
- Every control is a native button, select or input, reachable with Tab; a segment and the colour dots move with the arrow
- keys; Esc inside the panel collapses it, the small button opens it again. The panel is styled only with the token contract
- (`--popover`, `--muted`, `--border`, `--foreground`, `--radius`, `--font-sans`, `--font-mono`, `--shadow-lg` …), so it follows
- the project's style and its dark theme (`[data-theme="dark"]`); with no style it falls back to neutral values. While it
- is open, `body` has the class `xp-panel-open`, if the page wants to leave room for it.