diff --git a/app/components/primitives/HStack.test.tsx b/app/components/primitives/HStack.test.tsx
new file mode 100644
index 0000000000..048fea44ef
--- /dev/null
+++ b/app/components/primitives/HStack.test.tsx
@@ -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();
+ expect(css).toContain("gap:8px");
+ });
+
+ it("accepts spacing tokens", () => {
+ const { css } = render();
+ expect(css).toContain("gap:12px");
+ });
+
+ it("accepts raw pixel numbers", () => {
+ const { css } = render();
+ expect(css).toContain("gap:0px");
+ });
+
+ it("does not forward the spacing prop to the DOM", () => {
+ const { html } = render();
+ expect(html).not.toContain("spacing=");
+ });
+});
+
+describe("VStack", () => {
+ it("stacks vertically with a default 8px gap", () => {
+ const { css } = render();
+ expect(css).toContain("flex-direction:column");
+ expect(css).toContain("gap:8px");
+ });
+
+ it("accepts spacing tokens", () => {
+ const { css } = render();
+ expect(css).toContain("gap:4px");
+ });
+});
diff --git a/app/components/primitives/HStack.tsx b/app/components/primitives/HStack.tsx
index a8cc721595..b3eef49e22 100644
--- a/app/components/primitives/HStack.tsx
+++ b/app/components/primitives/HStack.tsx
@@ -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((props) => ({
align: props.align ?? "center",
- gap: props.spacing !== undefined ? props.spacing : 8,
+ gap: props.spacing !== undefined ? props.spacing : "md",
}))``;
HStack.displayName = "HStack";
diff --git a/app/components/primitives/VStack.tsx b/app/components/primitives/VStack.tsx
index e99519839a..e1b672c278 100644
--- a/app/components/primitives/VStack.tsx
+++ b/app/components/primitives/VStack.tsx
@@ -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((props) => ({
column: true,
align: props.align ?? "center",
- gap: props.spacing !== undefined ? props.spacing : 8,
+ gap: props.spacing !== undefined ? props.spacing : "md",
}))``;
VStack.displayName = "VStack";
diff --git a/shared/components/Flex.test.tsx b/shared/components/Flex.test.tsx
new file mode 100644
index 0000000000..b1104cf1e1
--- /dev/null
+++ b/shared/components/Flex.test.tsx
@@ -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();
+ expect(css).toContain("gap:8px");
+ });
+
+ it("resolves gap tokens to the same pixels as numbers", () => {
+ const { css } = render();
+ expect(css).toContain("gap:8px");
+ });
+
+ it("supports padding and margin token props", () => {
+ const { css } = render();
+ 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();
+ 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();
+ expect(css).not.toContain("padding");
+ expect(css).not.toContain("margin");
+ });
+
+ it("does not forward spacing props to the DOM", () => {
+ const { html } = render();
+ expect(html).not.toContain("gap=");
+ expect(html).not.toContain("p=");
+ expect(html).not.toContain("m=");
+ expect(html).not.toContain("column=");
+ });
+});
diff --git a/shared/components/Flex.tsx b/shared/components/Flex.tsx
index f343f6eb33..75a47f1c0a 100644
--- a/shared/components/Flex.tsx
+++ b/shared/components/Flex.tsx
@@ -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;
`;
diff --git a/shared/styles/index.ts b/shared/styles/index.ts
index 6c15bcd136..859888942b 100644
--- a/shared/styles/index.ts
+++ b/shared/styles/index.ts
@@ -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
diff --git a/shared/styles/spacing.test.ts b/shared/styles/spacing.test.ts
new file mode 100644
index 0000000000..e7be3ce770
--- /dev/null
+++ b/shared/styles/spacing.test.ts
@@ -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");
+ });
+});
diff --git a/shared/styles/spacing.ts b/shared/styles/spacing.ts
new file mode 100644
index 0000000000..ccdc6dddf0
--- /dev/null
+++ b/shared/styles/spacing.ts
@@ -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. `` and ``.
+ *
+ * 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;