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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Changed

- **The playground now opens on a hand-picked showcase state** rather than the engine's defaults: 5×5 with no gap at the largest size, `dot/cell` at 198% so the dots overlap into one surface, `grow` at 3% so a dot moves rather than pulses, `stagger` pinned at 70% so picking another motion does not move a slider under the visitor, square silhouette and square dots at radius 0. The engine's own defaults are untouched — that is the library's choice and the page is allowed to disagree — so the values live in a named `SHOWCASE` beside the component, each with the reason it was picked.

- **The site is now Astro + React islands on Cloudflare Pages.** `apps/site` was a hand-written HTML page with a
module script, served from a Worker with static assets; it is now `output: 'static'` Astro 7 with React 19.3
islands built by Vite, deployed to the `botui-site` Pages project. The demos mount
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### 变更

- **Playground 现在开在一组手工挑的 showcase 参数上**,而不是引擎默认值:最大尺寸的 5×5、无间距、`dot/cell` 198% 让点连成一整块面、`grow` 3% 让点是"移动"而不是"脉动"、`stagger` 固定在 70% 以免换个动效就把访客正看着的滑杆挪走、剪影和点都是方形且圆角为 0。引擎自己的默认值没有动 —— 那是库的选择,页面被允许不同意 —— 所以这些值放在组件旁边一个具名的 `SHOWCASE` 里,每一项都写明为什么选它。

- **站点改为 Astro + React islands,部署到 Cloudflare Pages。** `apps/site` 原先是一个手写 HTML + module script,
由 Worker 静态资源托管;现在是 `output: 'static'` 的 Astro 7 + React 19.3 islands,由 Vite 构建,部署到
`botui-site` Pages 项目。demo 直接挂载 `@botharness/botui-core`,而不是走已发布的 React wrapper —— 否则文档站
Expand Down
47 changes: 42 additions & 5 deletions apps/site/src/components/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,49 @@ import {

const CARD = 44;

/**
* The state the page opens on: a hand-picked look, not the engine's defaults.
*
* The engine's defaults are a library's choice and this page is allowed to disagree
* with them — what it is demonstrating is a component someone would actually ship,
* and shipped components are tuned. Every value here was picked to make one thing
* legible at a glance:
*
* · **5×5 and no gap, at the largest size.** A field you can see the shape of. At 7×7
* with a 45% gap the dots are far enough apart that the motion reads as noise.
* · **dot/cell 198%.** Over the cell, so the dots OVERLAP. This is the single number
* that makes it read as one surface rather than as separate pixels — and it is past
* the touching point, which is why the readout's own `dot / pitch` line is the one
* worth watching: 100% there means two dots touching.
* · **grow 3%.** Almost no breathing. `grow` is how far a dot changes size over the
* cycle, and at 50% a small dot is a pulsing blob rather than a moving light.
* · **stagger 70%, pinned.** A wide spread, and pinned rather than following the
* preset so the opening frame does not change under the visitor when they pick a
* different motion.
* · **radius 0.** Square dots. The rounded corners are one control away, and a
* rounded dot at 198% overlap hides the grid the size is there to show.
*/
const SHOWCASE: Partial<DotMatrixOptions> = {
size: 260,
cols: 5,
rows: 5,
fill: 1,
silhouette: "square",
dot: "square",
dotSize: 1.98,
gapX: 0,
gapY: 0,
preset: "spiral",
stagger: 0.7,
softness: 0,
grow: 0.03,
floor: 0.16,
speed: 1,
spec: { ...GLYPH_DEFAULTS },
};

export function Playground({ initial }: { initial?: Partial<DotMatrixOptions> }) {
// 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<DotMatrixOptions>({ dotSize: 0.78, ...initial });
const [options, setOptions] = useState<DotMatrixOptions>({ ...SHOWCASE, ...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.
Expand Down
91 changes: 76 additions & 15 deletions apps/site/test/playground.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,54 @@ const step = (label: RegExp, by: 1 | -1) => {
const readout = () => container.querySelector(".playground-readout")?.textContent ?? "";

describe("the playground controls", () => {
it("opens on the hand-picked showcase state, not the engine defaults", () => {
// The page demonstrates a component someone would SHIP, and shipped components are
// tuned — so the opening state is chosen rather than inherited. Asserted value by
// value because "it looks right" is not a claim a test can make, and each number is a
// decision: 198% is what makes the dots read as one surface rather than separate
// pixels, 3% grow stops a dot pulsing like a blob, and a PINNED stagger means picking
// a different motion does not silently move a slider under the visitor.
const labels: Record<string, string> = {};
for (const row of container.querySelectorAll(
".playground-side .playground-field, .stage-controls .playground-field",
)) {
const text = row.querySelector("span")?.textContent ?? "";
const name = text.slice(0, text.search(/[0-9]/)).trim();
const value =
row.querySelector("em")?.textContent?.trim() ??
row.querySelector("output")?.textContent?.trim() ??
"";
if (name) labels[name] = value;
}
expect(labels["dot / cell"], "over the cell, so the dots overlap").toBe("198%");
expect(labels.speed).toContain("1.00");
expect(labels.stagger, "pinned at 70%").toMatch(/^70%/);
expect(labels.softness).toBe("0%");
expect(labels.grow, "almost no breathing").toBe("3%");
expect(labels.floor).toBe("16%");
expect(labels["corner radius"]).toBe("0% of 71%");
expect(labels.aspect).toBe("100%");
expect(labels.size, "the largest box").toBe("260px");
expect(labels["gap x"]).toBe("0%");
expect(labels["gap y"]).toBe("0%");

expect(
[...container.querySelectorAll(".stepper")].map(
(s) => s.querySelector("output")?.textContent,
),
"5 by 5",
).toEqual(["5", "5"]);

const text = readout();
expect(text, "the readout agrees with the controls").toContain("dot / cell 198%");
expect(text).toContain("dot / pitch 198%");
expect(text).toContain("dot square");
expect(
container.querySelector<HTMLInputElement>(".stage-toggle input")?.checked,
"CSS by default",
).toBe(true);
});

it("paints a field on mount", () => {
expect(field().length).toBeGreaterThan(10);
});
Expand Down Expand Up @@ -229,7 +277,12 @@ describe("the playground controls", () => {
const square = field().length;
pick(card(/shape|形状/i, "ring"));
const ring = field().length;
expect(square, "square keeps every cell of a 7×7 lattice").toBe(49);
// read the opening grid off the steppers rather than hardcoding a cell count: the
// page opens on a showcase state, and a test about silhouettes has no business
// knowing what that state is
const cells =
Number(stepper(/cols/i).value.textContent) * Number(stepper(/rows/i).value.textContent);
expect(square, "square keeps every cell of the lattice").toBe(cells);
expect(ring, "ring keeps only its outline").toBeLessThan(square);
expect(readout()).toContain("ring");
});
Expand Down Expand Up @@ -284,9 +337,6 @@ describe("the playground controls", () => {
() => pick(card(/motion|动效/i, key)),
`selecting ${key} must not throw`,
).not.toThrow();
const checked = container.querySelector(
".card-group:nth-of-type(2) input:checked, .card-group input:checked",
);
expect(
[...container.querySelectorAll(".card-group")]
.find((g) => /motion|动效/i.test(g.querySelector("legend")?.textContent ?? ""))
Expand Down Expand Up @@ -393,9 +443,10 @@ describe("the playground controls", () => {
?.querySelector("input:checked")
?.getAttribute("value");

expect(checkedIn(/shape ·|形状/i), "the silhouette card is the engine's default").toBe(
DEFAULTS.silhouette,
);
// the opening silhouette is the showcase's `square`, not the engine's `circle`:
// this test catches the card disagreeing with what is DRAWN, and asserting
// DEFAULTS would only pass if someone reset the opening state
expect(checkedIn(/shape ·|形状/i), "the silhouette card is what is rendered").toBe("square");
expect(checkedIn(/dot shape|点的形状/i), "the dot card is the engine's default").toBe(
DEFAULTS.dot,
);
Expand All @@ -404,7 +455,11 @@ describe("the playground controls", () => {
);
const drawn = field().length;
expect(drawn, "the rendered field matches the selected silhouette").toBe(
cellsFor(DEFAULTS.cols, DEFAULTS.rows, DEFAULTS.silhouette).length,
cellsFor(
Number(stepper(/cols/i).value.textContent),
Number(stepper(/rows/i).value.textContent),
"square",
).length,
);
});

Expand Down Expand Up @@ -485,7 +540,10 @@ describe("the playground controls", () => {
// That asymmetry is deliberate and load-bearing — a dot that grew to fill the slack
// on the roomy axis would be wider than it is tall — and it is exactly the kind of
// thing a test that only checked "the number changed" would miss.
for (let i = 0; i < 4; i++) step(/^cols$|列/i, -1);
// 6×3, derived: at the opening dot/cell of 198% the old 3×5 is tight on ROWS, so
// gap x could not resize the dot and this test would have passed for the wrong reason.
while (Number(stepper(/^cols$/i).value.textContent) < 6) step(/^cols$/i, 1);
while (Number(stepper(/rows/i).value.textContent) > 3) step(/rows/i, -1);
// an SVG transform reads `translate(x y) scale(s)` — space separated, not comma
const transform = /translate\((-?[\d.]+)\s+(-?[\d.]+)\) scale\((-?[\d.]+)\)/;
const read = (dot: { t?: string | null }) => transform.exec(dot.t ?? "");
Expand All @@ -500,15 +558,15 @@ describe("the playground controls", () => {
const scaleAtWideX = scale();
drag(slider(/gap x/i), 0);
expect(axisX(), "gap x must move the dots along x").not.toEqual(xWide);
expect(scale(), "gap x must NOT resize the dot — the rows are tighter").toBe(scaleAtWideX);
expect(scale(), "gap x DOES resize the dot — it is the tighter axis").not.toBe(scaleAtWideX);

// the tighter axis (y): everything moves. Widening first, for the same reason.
drag(slider(/gap y/i), 100);
const yWide = axisY();
const scaleAtWideY = scale();
drag(slider(/gap y/i), 0);
expect(axisY(), "gap y must move the dots along y").not.toEqual(yWide);
expect(scale(), "gap y DOES resize the dot — it is the tighter axis").not.toBe(scaleAtWideY);
expect(scale(), "gap y must NOT resize the dot — the columns are tighter").toBe(scaleAtWideY);
});

it("the dot/cell slider still lands the field exactly on the box at 200%", () => {
Expand Down Expand Up @@ -698,13 +756,16 @@ describe("a dial that follows a preset must be able to return to it", () => {

it("the label states the preset’s own number, so it can never disagree with the thumb", () => {
const row = staggerRow();
const shown = row.querySelector("em")?.textContent ?? "";
const input = row.querySelector("input") as HTMLInputElement;
// spiral's spread is 0.95, and the label has to say so. The expectation comes
// from the message rather than a literal, so this test does not encode a language:
// a hardcoded "预设" here would have passed in English and failed in Chinese.
expect(shown).toContain("95%");
expect(Number(input.value)).toBe(95);
// the page opens PINNED at 70%, so this row starts as an override and the claim
// under test is what happens once it follows the preset again — the reset being the
// only way there, which is the one-way door this describe is about
act(() => (row.querySelector(".reset") as HTMLButtonElement).click());
const followed = row.querySelector("em")?.textContent ?? "";
expect(followed, "on the preset, the label states its own number").toContain("95%");
expect(Number((row.querySelector("input") as HTMLInputElement).value)).toBe(95);
});

it("following the preset is reversible", () => {
Expand Down
Loading