feat: Add spacing scale tokens and token-aware layout primitives

Adds a named spacing scale (xxs=2 … xxxl=32) in shared/styles/spacing.ts
following the depths.ts pattern, with space() and resolveSpacing() helpers.

Flex gains token support for gap plus new optional p/px/py/m/mx/my props
emitting logical properties; HStack/VStack spacing accepts tokens. All
existing numeric call sites remain valid and no CSS is emitted unless the
new props are passed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014Hpjg2EbSwW1RTqkMdWQyT
This commit is contained in:
Claude committed 2026-07-08 10:58:04 +00:00
1 parent fcfcb4fd93
commit cb06ab471d
8 files changed
+274 -9

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
import { renderToString } from "react-dom/server";
import { ServerStyleSheet } from "styled-components";
import { HStack } from "./HStack";
import { VStack } from "./VStack";
/**
* Renders an element and collects the styled-components CSS generated for it.
*
* @param element the element to render.
* @returns the rendered html and collected css.
*/
function render(element: React.ReactElement) {
const sheet = new ServerStyleSheet();
try {
const html = renderToString(sheet.collectStyles(element));
return { html, css: sheet.getStyleTags() };
} finally {
sheet.seal();
}
}
describe("HStack", () => {
it("defaults to an 8px gap", () => {
const { css } = render(<HStack />);
expect(css).toContain("gap:8px");
});
it("accepts spacing tokens", () => {
const { css } = render(<HStack spacing="lg" />);
expect(css).toContain("gap:12px");
});
it("accepts raw pixel numbers", () => {
const { css } = render(<HStack spacing={0} />);
expect(css).toContain("gap:0px");
});
it("does not forward the spacing prop to the DOM", () => {
const { html } = render(<HStack spacing="lg" />);
expect(html).not.toContain("spacing=");
});
});
describe("VStack", () => {
it("stacks vertically with a default 8px gap", () => {
const { css } = render(<VStack />);
expect(css).toContain("flex-direction:column");
expect(css).toContain("gap:8px");
});
it("accepts spacing tokens", () => {
const { css } = render(<VStack spacing="xs" />);
expect(css).toContain("gap:4px");
});
});
+4 -3
View File
@@ -1,10 +1,11 @@
import type { CSSProperties } from "react";
import styled from "styled-components";
import type { SpacingValue } from "@shared/styles";
import Flex from "~/components/Flex";
interface HStackProps {
/** The spacing between children in pixels. Defaults to 8. */
spacing?: number;
/** The spacing between children — spacing token or raw pixels. Defaults to "md" (8px). */
spacing?: SpacingValue;
/** The alignment of children along the cross axis. Defaults to "center". */
align?: CSSProperties["alignItems"];
/** The justification of children along the main axis. */
@@ -24,7 +25,7 @@ export const HStack = styled(Flex)
})
.attrs<HStackProps>((props) => ({
align: props.align ?? "center",
gap: props.spacing !== undefined ? props.spacing : 8,
gap: props.spacing !== undefined ? props.spacing : "md",
}))<HStackProps>``;
HStack.displayName = "HStack";
+4 -3
View File
@@ -1,10 +1,11 @@
import type { CSSProperties } from "react";
import styled from "styled-components";
import type { SpacingValue } from "@shared/styles";
import Flex from "~/components/Flex";
interface VStackProps {
/** The spacing between children in pixels. Defaults to 8. */
spacing?: number;
/** The spacing between children — spacing token or raw pixels. Defaults to "md" (8px). */
spacing?: SpacingValue;
/** The alignment of children along the cross axis. Defaults to "center". */
align?: CSSProperties["alignItems"];
/** The justification of children along the main axis. */
@@ -25,7 +26,7 @@ export const VStack = styled(Flex)
.attrs<VStackProps>((props) => ({
column: true,
align: props.align ?? "center",
gap: props.spacing !== undefined ? props.spacing : 8,
gap: props.spacing !== undefined ? props.spacing : "md",
}))<VStackProps>``;
VStack.displayName = "VStack";
+61
View File
@@ -0,0 +1,61 @@
import { renderToString } from "react-dom/server";
import { ServerStyleSheet } from "styled-components";
import Flex from "./Flex";
/**
* Renders an element and collects the styled-components CSS generated for it.
*
* @param element the element to render.
* @returns the rendered html and collected css.
*/
function render(element: React.ReactElement) {
const sheet = new ServerStyleSheet();
try {
const html = renderToString(sheet.collectStyles(element));
return { html, css: sheet.getStyleTags() };
} finally {
sheet.seal();
}
}
describe("Flex", () => {
it("supports numeric gap values", () => {
const { css } = render(<Flex gap={8} />);
expect(css).toContain("gap:8px");
});
it("resolves gap tokens to the same pixels as numbers", () => {
const { css } = render(<Flex gap="md" />);
expect(css).toContain("gap:8px");
});
it("supports padding and margin token props", () => {
const { css } = render(<Flex p="md" px="lg" m="sm" my={4} />);
expect(css).toContain("padding:8px");
expect(css).toContain("padding-inline:12px");
expect(css).toContain("margin:6px");
expect(css).toContain("margin-block:4px");
});
it("emits shorthand before axis longhands", () => {
const { css } = render(<Flex p="md" px="lg" />);
expect(css.indexOf("padding:8px")).toBeGreaterThan(-1);
expect(css.indexOf("padding:8px")).toBeLessThan(
css.indexOf("padding-inline:12px")
);
});
it("emits no padding or margin declarations when props are absent", () => {
const { css } = render(<Flex column gap={4} />);
expect(css).not.toContain("padding");
expect(css).not.toContain("margin");
});
it("does not forward spacing props to the DOM", () => {
const { html } = render(<Flex gap="md" p="md" m="sm" column />);
expect(html).not.toContain("gap=");
expect(html).not.toContain("p=");
expect(html).not.toContain("m=");
expect(html).not.toContain("column=");
});
});
+34 -3
View File
@@ -1,5 +1,7 @@
import type { CSSProperties } from "react";
import styled from "styled-components";
import type { SpacingValue } from "../styles/spacing";
import { resolveSpacing } from "../styles/spacing";
type JustifyValues = CSSProperties["justifyContent"];
@@ -20,6 +22,12 @@ const Flex = styled.div.withConfig({
"shrink",
"reverse",
"gap",
"p",
"px",
"py",
"m",
"mx",
"my",
].includes(prop),
})<{
/** Makes the component grow to fill available space */
@@ -36,8 +44,20 @@ const Flex = styled.div.withConfig({
shrink?: boolean;
/** Reverses the direction (row-reverse or column-reverse) */
reverse?: boolean;
/** Sets gap between flex items in pixels */
gap?: number;
/** Sets gap between flex items — spacing token or raw pixels */
gap?: SpacingValue;
/** Padding on all sides — spacing token or raw pixels */
p?: SpacingValue;
/** Inline-axis (horizontal in LTR) padding — spacing token or raw pixels */
px?: SpacingValue;
/** Block-axis (vertical) padding — spacing token or raw pixels */
py?: SpacingValue;
/** Margin on all sides — spacing token or raw pixels */
m?: SpacingValue;
/** Inline-axis (horizontal in LTR) margin — spacing token or raw pixels */
mx?: SpacingValue;
/** Block-axis (vertical) margin — spacing token or raw pixels */
my?: SpacingValue;
}>`
display: flex;
flex: ${({ auto }) => (auto ? "1 1 auto" : "initial")};
@@ -54,7 +74,18 @@ const Flex = styled.div.withConfig({
flex-wrap: ${({ wrap }) => (wrap ? "wrap" : "initial")};
flex-shrink: ${({ shrink }) =>
shrink === true ? 1 : shrink === false ? 0 : "initial"};
gap: ${({ gap }) => (gap !== undefined ? `${gap}px` : "initial")};
gap: ${({ gap }) =>
gap !== undefined ? `${resolveSpacing(gap)}px` : "initial"};
${({ p }) => (p !== undefined ? `padding: ${resolveSpacing(p)}px;` : "")}
${({ px }) =>
px !== undefined ? `padding-inline: ${resolveSpacing(px)}px;` : ""}
${({ py }) =>
py !== undefined ? `padding-block: ${resolveSpacing(py)}px;` : ""}
${({ m }) => (m !== undefined ? `margin: ${resolveSpacing(m)}px;` : "")}
${({ mx }) =>
mx !== undefined ? `margin-inline: ${resolveSpacing(mx)}px;` : ""}
${({ my }) =>
my !== undefined ? `margin-block: ${resolveSpacing(my)}px;` : ""}
min-height: 0;
min-width: 0;
`;
+4
View File
@@ -5,6 +5,10 @@ export { default as depths } from "./depths";
export { default as breakpoints } from "./breakpoints";
export { default as spacing, space, resolveSpacing } from "./spacing";
export type { SpacingToken, SpacingValue } from "./spacing";
/**
* Returns "hover" on a non-touch device and "active" on a touch device. To
* avoid "sticky" hover on mobile. Use `&:${hover} {...}` instead of
+52
View File
@@ -0,0 +1,52 @@
import spacing, { resolveSpacing, space } from "./spacing";
describe("spacing", () => {
it("matches the expected scale exactly", () => {
// Changing any of these values is a visual change to every component
// using the token and must be reviewed as such.
expect(spacing).toEqual({
xxs: 2,
xs: 4,
sm: 6,
md: 8,
lg: 12,
xl: 16,
xxl: 24,
xxxl: 32,
});
});
it("is strictly ascending", () => {
const values = Object.values(spacing);
for (let i = 1; i < values.length; i++) {
expect(values[i]).toBeGreaterThan(values[i - 1]);
}
});
});
describe("resolveSpacing", () => {
it("resolves token names to pixels", () => {
expect(resolveSpacing("md")).toBe(8);
expect(resolveSpacing("xxs")).toBe(2);
});
it("passes raw numbers through", () => {
expect(resolveSpacing(10)).toBe(10);
expect(resolveSpacing(0)).toBe(0);
});
});
describe("space", () => {
it("builds CSS shorthand values from tokens", () => {
expect(space("md", "xxl", "xxl")).toBe("8px 24px 24px");
});
it("renders zero without a unit", () => {
expect(space(0, "md")).toBe("0 8px");
});
it("accepts raw numbers", () => {
expect(space(10)).toBe("10px");
expect(space("xs", 10)).toBe("4px 10px");
});
});
+60
View File
@@ -0,0 +1,60 @@
/**
* Spacing scale for margin, padding, and gap values, in pixels.
*
* Use in styled-components template literals as `${spacing.md}px`, via the
* `space()` helper for shorthands, or by token name on layout primitives,
* e.g. `<Flex gap="sm" p="md">` and `<HStack spacing="lg">`.
*
* Note: this is unrelated to the sidebar width values that are spread into
* the theme from an internal `spacing` object in shared/styles/theme.ts.
*/
const spacing = {
/** 2px */
xxs: 2,
/** 4px */
xs: 4,
/** 6px */
sm: 6,
/** 8px */
md: 8,
/** 12px */
lg: 12,
/** 16px */
xl: 16,
/** 24px */
xxl: 24,
/** 32px */
xxxl: 32,
} as const;
/** A named step on the spacing scale. */
export type SpacingToken = keyof typeof spacing;
/** A named step on the spacing scale, or raw pixels for off-scale values. */
export type SpacingValue = SpacingToken | number;
/**
* Resolves a spacing token or raw pixel number to pixels.
*
* @param value a spacing token name or raw pixel number.
* @returns the resolved value in pixels.
*/
export const resolveSpacing = (value: SpacingValue): number =>
typeof value === "number" ? value : spacing[value];
/**
* Builds a CSS shorthand value from spacing tokens or raw pixel numbers,
* e.g. `padding: ${space(0, "md")};` becomes `padding: 0 8px;`.
*
* @param values spacing token names or raw pixel numbers.
* @returns a space-separated CSS value string.
*/
export const space = (...values: SpacingValue[]): string =>
values
.map((value) => {
const resolved = resolveSpacing(value);
return resolved === 0 ? "0" : `${resolved}px`;
})
.join(" ");
export default spacing;