ShtukbaseStandard
包页面→ /base/explore.md

pages/base/explore.md

105 行, 5.6 KB. 以页面形式查看.

这段内容尚未翻译,下面是英文原文。

  1. ---
  2. type: Page
  3. title: "Exploration pages"
  4. icon: "🎛️"
  5. 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."
  6. ---
  7. # Exploration pages
  8. An exploration page shows one design in many variants and lets a person switch them: the form of a card, its cover, a
  9. radius, a glow colour. The explore kit (package `explore-kit`) gives every such page the same panel: on the right,
  10. collapsible to a small button, split into sections that fold, with presets, a "Reset" and a "Copy link" button. The
  11. values land on the page as `data-<name>` attributes and `--x-<name>` CSS properties, so most of the page is plain CSS.
  12. Open `/design/explore-kit/demo.html` to see it: the settings of the package map, rebuilt with the kit.
  13. ## A minimal page
  14. ```html
  15. <!doctype html>
  16. <html>
  17. <head>
  18. <link rel="stylesheet" href="../design/explore-kit/explore.css">
  19. <style>
  20. .card { border-radius: var(--x-radius); padding: 16px; box-shadow: 0 0 0 1px #ddd; }
  21. [data-size="l"] .card { padding: 28px; font-size: 18px; }
  22. [data-shadow="true"] .card { box-shadow: 0 6px 20px rgb(0 0 0 / .12); }
  23. .card { background: var(--x-tint); }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="card">A card</div>
  28. <script src="../design/explore-kit/explore.js"></script>
  29. <script>
  30. Explore.panel({
  31. title: 'Card',
  32. storageKey: 'card-demo',
  33. sections: [
  34. {title: 'Shape', controls: {
  35. size: {kind: 'segment', options: [['m', 'M'], ['l', 'L']], default: 'm'},
  36. radius: {kind: 'range', min: 0, max: 24, unit: 'px', default: 8},
  37. shadow: {kind: 'bool'}}},
  38. {title: 'Colour', folded: true, controls: {
  39. tint: {kind: 'swatch', options: ['#ffffff', '#fdf1f4', '#eef5fc', 'chart-2']}}}
  40. ],
  41. presets: {soft: {radius: 16, shadow: true}, flat: {radius: 0}},
  42. onChange: (values) => console.log(values)
  43. });
  44. </script>
  45. </body>
  46. </html>
  47. ```
  48. In a lens, link the copies the lens build puts into `lib/`: `../lib/explore.css` and `../lib/explore.js` (as many `../` as
  49. the lens is deep, like `kit.js`). The lens build copies them when the project has the `explore-kit` package.
  50. ## The call
  51. `Explore.panel(options)` builds the panel, applies the start values, calls `onChange` once and returns a handle.
  52. | Option | What it is |
  53. |---|---|
  54. | `title` | the panel's title |
  55. | `target` | the element the values land on (default `document.documentElement`): `data-<name>="<value>"` and `--x-<name>: <css value>` |
  56. | `storageKey` | the localStorage key (stored as `explore:<key>`): the values, the collapsed state, the folded sections |
  57. | `sections` | `[{title, folded?, controls: {<name>: control}}]`; a section with no title has no header |
  58. | `controls` | controls outside `sections`, grouped by their `section` field |
  59. | `presets` | `{<name>: {<control>: value}}`: named bundles, shown as the first select; a preset is the defaults plus its bundle; a tweak shows "Custom" |
  60. | `open` | `false` starts collapsed when nothing is stored |
  61. | `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 |
  62. | `onChange` | `(values) => {}`, after every change and once at the start |
  63. The start values are the defaults, then the stored values, then the URL (`?size=l&radius=12`): the URL wins, so a copied
  64. link opens what its author saw. Every change writes the non-default values back to the URL (`history.replaceState`) and to
  65. storage; other URL parameters stay.
  66. The handle: `values()`, `get(name)`, `set(name, value)` or `set({...})`, `preset(name)`, `reset()`, `link()`, `open()`,
  67. `close()`, `toggle()`, `destroy()`, `el`.
  68. ## Controls
  69. The control schema of the design build contract, the same one component headers and the catalog use:
  70. `{kind, label?, options?, default?, min?, max?, step?, unit?, section?, colors?}`.
  71. | Kind | Looks like | Value | `--x-<name>` |
  72. |---|---|---|---|
  73. | `select` | a grey rounded dropdown | one of `options` | the value |
  74. | `segment` | a segmented switch, 2-4 options | one of `options` | the value |
  75. | `bool` | a toggle | `true` / `false` (`data-` is `"true"`/`"false"`) | `1` / `0` |
  76. | `range` | a black-thumb slider with a mono value | a number in `min`…`max`, snapped to `step` | the number plus `unit` |
  77. | `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 |
  78. | `text` | a text field | any text | the text |
  79. `options` are strings, `[value, label]` pairs or `{value, label}` objects. `label` defaults to the name made readable.
  80. `colors` maps option values to colours: the swatch draws them, a segment shows a dot. The schema maps one-to-one to
  81. Storybook `argTypes`: `select`/`segment` → `select`/`radio`, `bool` → `boolean`, `range` → `range`, `swatch` → `color`,
  82. `text` → `text`.
  83. ## Keyboard and look
  84. Every control is a native button, select or input, reachable with Tab; a segment and the colour dots move with the arrow
  85. keys; Esc inside the panel collapses it, the small button opens it again. The panel is styled only with the token contract
  86. (`--popover`, `--muted`, `--border`, `--foreground`, `--radius`, `--font-sans`, `--font-mono`, `--shadow-lg` …), so it follows
  87. the project's style and its dark theme (`[data-theme="dark"]`); with no style it falls back to neutral values. While it
  88. is open, `body` has the class `xp-panel-open`, if the page wants to leave room for it.