diff --git a/CHANGELOG.md b/CHANGELOG.md index f2e9ba7..5f9bb20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ All notable changes to BotUI are recorded here. The format follows ### Changed +- **The playground opens on the CSS renderer**, as a switch on the stage beside the field it renders. It is the default because it costs zero JS per frame and SVG is the escape hatch for the motions CSS cannot express — so the field you see on arrival is the one most people will actually ship. The refusal note rides on the switch, so the reason a motion has no CSS form is where you would turn it on. +- **The picture cards scroll sideways instead of wrapping.** A wrapped grid grows taller as options are added, and the column it lives in is already taller than the viewport, so every extra shape pushed the sliders further down. A row costs the column nothing. `SHAPE` leads them, then `MOTION`, then `DOT SHAPE`. +- The playground opens at a denser dot/cell than the engine's default. At 55% a field reads as scattered pixels, and the page's subject is what a dot IS; the engine's own default is untouched, because that is the library's choice. +- The side column is not sticky again. Sticky on both columns made the sliders reachable but turned the whole right-hand side into a pane you had to scroll independently, which is worse than letting it travel with the page. + - **The playground's shape and motion controls are pictures now, not sliders.** `sides` and `silhouette` sat in one list under one "shape" heading, so a reader could not tell which moved the whole field and which moved each dot; the silhouette is now a card group whose cards change the field's outline and a second group whose cards change the dot inside it, each stating its scope in the same place. Presets were a ``,它无法表达 `spiral` 和 `ring` 的区别;现在是运行真实引擎的卡片,并且只有指针或焦点移上去时才播放,而不是十二个一起动。 - 每张卡片画的都是引擎自己的输出 —— 静帧用 `field()`、剪影用 `cellsFor()`、动效用实时点阵。卡片若自己画一个"近似"的点,那就是给它推销的东西写了第二份实现,glyph 代码一改就会漂移。相位刻意不用 0:遍历的动感在于格子之间的**差异**,而在 t=0 所有格子都处于静止,静帧只会显示一团均匀的模糊。 - 行列改成预览区上的加减步进器,`dot/cell` 也一并移了过去:它们是你盯着点阵时最常调的三个。整体 `size` 挪到最后,因为它决定占多少空间,而不是看起来是什么样。 diff --git a/apps/site/src/components/Cards.tsx b/apps/site/src/components/Cards.tsx index 9e7474a..5c00a2b 100644 --- a/apps/site/src/components/Cards.tsx +++ b/apps/site/src/components/Cards.tsx @@ -1,6 +1,9 @@ import type { ReactNode } from "react"; import { cellsFor, glyphPath, type Silhouette } from "@botharness/botui-core"; +/** the card's width in the scrolling row: enough for the picture plus two label lines */ +const CARD_W = 86; + /** * A choice you can see rather than a control you have to imagine. * @@ -33,11 +36,15 @@ export function CardGroup({ onChange: (value: T) => void; /** the card's visual — the engine's own output, not an illustration of it */ renderPreview: (value: T) => ReactNode; - /** how many cards per row; the CSS handles wrapping, this sets the target */ + /** the card's fixed width; the row scrolls horizontally past this count */ columns?: number; }) { return ( -
+
(columns ?? 4) || undefined} + > {label}

{scope}

diff --git a/apps/site/src/components/Playground.tsx b/apps/site/src/components/Playground.tsx index 2d8ced1..b31e3bf 100644 --- a/apps/site/src/components/Playground.tsx +++ b/apps/site/src/components/Playground.tsx @@ -25,8 +25,15 @@ import { const CARD = 44; export function Playground({ initial }: { initial?: Partial }) { - const [options, setOptions] = useState({ ...initial }); - const [cssRenderer, setCssRenderer] = useState(false); + // The page opens at a denser dot/cell than the engine's default: at 55% a field + // reads as scattered pixels, and this page's subject is what a dot IS. The engine's + // own default is untouched — that is the library's choice, and the playground is + // allowed to disagree with it. + const [options, setOptions] = useState({ dotSize: 0.78, ...initial }); + // CSS by default, because it is the cheaper renderer and this is the one a product + // page should be demonstrating: it costs zero JS per frame. SVG is the escape hatch + // for the motions CSS cannot express, so it is the one you have to ask for. + const [cssRenderer, setCssRenderer] = useState(true); const set = (patch: Partial) => setOptions((o) => ({ ...o, ...patch })); @@ -43,7 +50,10 @@ export function Playground({ initial }: { initial?: Partial }) // a motion the CSS renderer cannot express has to be refused, not approximated — the // page says so instead of showing a different animation under the same name - const cssAvailable = cssRenderable(options.preset); + // `?? DEFAULTS.preset` like every other read of the preset: `cssRenderable(undefined)` + // is false, so without the default the switch claimed the engine refused a motion it + // can express — and it was disabled on first paint for the default field. + const cssAvailable = cssRenderable(options.preset ?? DEFAULTS.preset); const sides = options.spec?.sides ?? GLYPH_DEFAULTS.sides; // the dot cards show ONE field so the only difference between them is the dot: a @@ -92,17 +102,43 @@ export function Playground({ initial }: { initial?: Partial }) compact onChange={(v) => set({ dotSize: v / 100 })} /> +
{/* Shape, as pictures. Two groups because the engine has two shapes: the field's outline and the dot inside it. A single slider list called them both "shape" and left the reader to guess which was which. */} + set({ silhouette: silhouette as Silhouette })} + options={SILHOUETTE_KEYS.map((key) => ({ value: key, label: silhouetteLabel(key) }))} + columns={6} + renderPreview={(value) => } + /> + }) )} /> - set({ silhouette: silhouette as Silhouette })} - options={SILHOUETTE_KEYS.map((key) => ({ value: key, label: silhouetteLabel(key) }))} - columns={6} - renderPreview={(value) => } - /> - }) /> - - {!cssAvailable &&

{m.css_refused()}

} -
           {[
             `${m.readout_dot_cell()}   ${Math.round((options.dotSize ?? 0) * 100)}%`,
diff --git a/apps/site/src/styles/site.css b/apps/site/src/styles/site.css
index 1715b43..f76db49 100644
--- a/apps/site/src/styles/site.css
+++ b/apps/site/src/styles/site.css
@@ -532,29 +532,11 @@ footer {
    scrolled past two card groups — which is what "there is no way to adjust these" was.
    A control you cannot see is a control you cannot find, and the whole page is about
    adjusting things while looking at them. */
-.playground-stage,
-.playground-side {
+.playground-stage {
   position: sticky;
-  /* 8px, not 20px: the side column is 817px of content and a 20px offset left a
-     773px box, so `softness`, `grow` and `floor` — the three the report was about —
-     sat just below the fold again. Both columns share the offset so they scroll as
-     one; the columns are independent scrollers, not one pane. */
   top: 8px;
 }
 
-/* A sticky element taller than its scrollport stops sticking — it scrolls with the page
-   from the top, which is the original bug. Capping the column at the viewport and
-   letting it scroll internally keeps every slider reachable however short the window
-   is. Only the side column needs this; the stage is bounded by its own content. */
-.playground-side {
-  max-height: calc(100vh - 16px);
-  overflow-y: auto;
-  padding-right: 4px;
-  /* the scrollbar was eating 20px of the column's width, which is 20px the sliders
-     did not get. `auto` lets the browser decide and most of them overlay it. */
-  scrollbar-width: thin;
-}
-
 .playground-stage {
   display: grid;
   grid-template-columns: auto minmax(0, 1fr);
@@ -578,6 +560,76 @@ footer {
   min-width: 168px;
 }
 
+/* The renderer switch, on the stage beside the thing it renders. It is a toggle
+   rather than a checkbox because the answer is a mode the field is IN, and CSS is
+   the default: it costs zero JS per frame, which is the thing worth showing. */
+.stage-toggle {
+  display: flex;
+  align-items: center;
+  gap: 8px;
+  font-size: 12px;
+  color: var(--ink);
+  cursor: pointer;
+}
+
+.stage-toggle input {
+  position: absolute;
+  opacity: 0;
+  width: 1px;
+  height: 1px;
+}
+
+.stage-toggle:has(input:disabled) {
+  cursor: not-allowed;
+  opacity: 0.55;
+}
+
+.stage-toggle:has(input:focus-visible) .stage-toggle-track {
+  outline: 2px solid var(--accent);
+  outline-offset: 1px;
+}
+
+.stage-toggle-track {
+  flex: 0 0 auto;
+  width: 30px;
+  height: 17px;
+  border-radius: 999px;
+  background: var(--line);
+  position: relative;
+  transition: background 120ms ease;
+}
+
+.stage-toggle input:checked + .stage-toggle-track {
+  background: var(--accent);
+}
+
+.stage-toggle-knob {
+  position: absolute;
+  top: 2px;
+  left: 2px;
+  width: 13px;
+  height: 13px;
+  border-radius: 999px;
+  background: var(--card);
+  transition: transform 120ms ease;
+}
+
+.stage-toggle input:checked + .stage-toggle-track .stage-toggle-knob {
+  transform: translateX(13px);
+}
+
+.stage-toggle-label {
+  display: flex;
+  flex-direction: column;
+  line-height: 1.25;
+}
+
+.stage-toggle-label em {
+  font-style: normal;
+  font-size: 10px;
+  color: var(--muted);
+}
+
 .stage-controls .playground-field[data-compact] {
   /* the label sits beside the track: the vertical space here is expensive */
   flex-direction: row;
@@ -653,6 +705,7 @@ footer {
    A radio group, so the browser supplies "3 of 14", arrow-key movement and a single
    tab stop — none of which a grid of buttons gets for free. */
 .card-group {
+  position: relative;
   border: 0;
   border-top: 1px solid var(--line);
   margin: 0;
@@ -681,13 +734,33 @@ footer {
   color: var(--muted);
 }
 
+/* One scrolling row, not a wrapped grid. A grid grows taller as options are added, and
+   the column it lives in is already taller than the viewport — so every extra shape
+   pushed the sliders further below the fold. A row grows sideways and costs the column
+   nothing, which is what lets all three groups stay on screen. */
 .card-grid {
-  display: grid;
-  grid-template-columns: repeat(var(--cards, 4), minmax(0, 1fr));
+  display: flex;
   gap: 4px;
+  overflow-x: auto;
+  scroll-snap-type: x proximity;
+  /* the scrollbar is the affordance; a fading edge says "there is more" without one */
+  padding-bottom: 2px;
+  scrollbar-width: thin;
+}
+
+.card-grid > .card {
+  flex: 0 0 var(--card-w, 86px);
+  width: var(--card-w, 86px);
+  scroll-snap-align: start;
+}
+
+.card-group[data-overflow]::after {
+  content: "";
+  position: absolute;
 }
 
 .card {
+  position: relative;
   display: flex;
   flex-direction: column;
   align-items: center;
@@ -745,11 +818,16 @@ footer {
   text-align: center;
   /* two lines max: "Rounded square" fits, a longer name truncates rather than
      pushing its neighbours out of alignment */
+  /* clamped to two lines so one long name cannot make its row taller than the others —
+     but with NO min-height: a 1.3em box plus the clamp was cropping the last row of
+     pixels off single-line names, which rendered "square" as "sqmare". */
   display: -webkit-box;
   -webkit-line-clamp: 2;
   -webkit-box-orient: vertical;
   overflow: hidden;
-  min-height: 1.3em;
+  /* reserves the two lines the clamp allows, so rows stay aligned whether the name
+     wraps or not — without cropping anything */
+  min-height: 2.5em;
 }
 
 .card[data-selected] .card-label {
@@ -766,9 +844,10 @@ footer {
     grid-template-columns: 1fr;
   }
 
-  /* the cards are the primary control on a narrow screen, so they come before the
-     tuning sliders rather than after them */
-  .card-grid {
-    grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
+  /* still one row: on a narrow screen it scrolls sideways rather than wrapping into
+     a stack that would bury the sliders again */
+  .card {
+    flex-basis: 66px;
+    width: 66px;
   }
 }
diff --git a/apps/site/test/playground.test.tsx b/apps/site/test/playground.test.tsx
index 20c1394..545622f 100644
--- a/apps/site/test/playground.test.tsx
+++ b/apps/site/test/playground.test.tsx
@@ -23,6 +23,7 @@ let container: HTMLDivElement;
 let root: Root;
 
 beforeEach(() => {
+  svgForced = false;
   container = document.createElement("div");
   document.body.appendChild(container);
   root = createRoot(container);
@@ -36,13 +37,31 @@ afterEach(() => {
   container.remove();
 });
 
-/** the rendered field, as a comparable signature */
-const field = () =>
-  [...container.querySelectorAll(".playground-stage svg path")].map((p) => ({
+/**
+ * The rendered field, as a comparable signature.
+ *
+ * Read from the SVG renderer, and the SVG renderer is switched ON first: the playground
+ * now opens on CSS — it is the cheaper renderer and the one worth demonstrating — and a
+ * CSS field paints `@keyframes`, so there are no paths to read. Every geometry
+ * assertion below is about the engine's geometry, which both renderers share, so
+ * measuring it through SVG is measuring the same thing.
+ */
+let svgForced = false;
+const useSvgRenderer = () => {
+  if (svgForced) return;
+  const toggle = container.querySelector(".stage-toggle input");
+  if (toggle?.checked) act(() => toggle.click());
+  svgForced = true;
+};
+
+const field = () => {
+  useSvgRenderer();
+  return [...container.querySelectorAll(".playground-stage svg path")].map((p) => ({
     d: p.getAttribute("d"),
     t: p.getAttribute("transform"),
     o: p.getAttribute("fill-opacity"),
   }));
+};
 
 /** a slider inside a specific column, so the stage's own controls cannot be mistaken
  *  for the side column's identically-named ones */
@@ -133,6 +152,37 @@ describe("the playground controls", () => {
     expect(field().length).toBeGreaterThan(10);
   });
 
+  it("opens on the CSS renderer, because it is the one worth demonstrating", () => {
+    // CSS costs zero JS per frame and SVG is the escape hatch for the motions CSS
+    // cannot express, so the cheaper one is the default and the other has to be asked
+    // for. It also means the field on arrival is the one most people will actually ship.
+    const toggle = container.querySelector(".stage-toggle input");
+    expect(toggle, "the renderer switch lives on the stage").toBeTruthy();
+    expect(toggle!.checked, "CSS renderer is the default").toBe(true);
+    expect(
+      toggle!.disabled,
+      "the default preset is expressible in CSS, so the switch must be usable",
+    ).toBe(false);
+    // and the field really is CSS: no SVG paths in the stage
+    expect(container.querySelectorAll(".playground-stage svg path").length).toBe(0);
+  });
+
+  it("refuses the CSS renderer only for a motion it cannot express", () => {
+    // the inverse of the above: `columns` staggers each column's entry, which a
+    // per-dot animation-delay does not model, so the engine refuses rather than
+    // approximating. The toggle must disable AND say why, rather than showing a
+    // different animation under the same name.
+    const toggle = container.querySelector(".stage-toggle input")!;
+    pick(card(/motion|动效/i, "columns"));
+    expect(toggle.disabled, "a per-dot delay cannot move a highlight").toBe(true);
+    expect(
+      container.querySelector(".stage-toggle")?.textContent,
+      "and it has to say so where the switch is",
+    ).toMatch(/refuses it/);
+    pick(card(/motion|动效/i, "spiral"));
+    expect(toggle.disabled, "spiral is expressible, so the switch comes back").toBe(false);
+  });
+
   it("has a speed control, because a loading field you cannot slow down is a GIF", () => {
     const input = slider(/速度|speed/i);
     expect(input).toBeTruthy();
@@ -371,10 +421,11 @@ describe("the playground controls", () => {
   });
 
   it("refuses the CSS renderer for a motion it cannot express, and says why", () => {
-    const checkbox = container.querySelector(".playground-check input") as HTMLInputElement;
+    const checkbox = container.querySelector(".stage-toggle input") as HTMLInputElement;
     pick(card(/motion|动效/i, "columns"));
     expect(checkbox.disabled, "a per-dot delay cannot move a highlight").toBe(true);
-    expect(container.querySelector(".playground-note")?.textContent).toMatch(/refuses it/);
+    // the explanation moved onto the toggle itself when it joined the stage
+    expect(container.querySelector(".stage-toggle")?.textContent).toMatch(/refuses it/);
   });
 });