Skip to content

⚠ Early release: v0.3.2.sophistry is not ready for production use. Until 1.0, any minor release may break the API; expect missing features and bugs too.Stability and versioning

Boxes and flexbox

Cards, captions, legends and title slides are easier to describe the way a web page does: a box with padding, a row of things with a gap between them, text that wraps at the width it is given. <Box> does that inside the canvas. It lays out with flexbox, paints a background and a border, and holds text, other boxes and any scene node, which keeps working as it does outside.

Boxes and layout

A card laid out with flexbox in design pixels: a heading, wrapped rich text, a row of tags and a circle in scene units, written on in order.

BoxLayout.tsx
3 collapsed lines
import { BLUE, lagged, track, valueTo, YELLOW } from "@sophistry/core";
import { Box, Circle, Stage, Text, usePlayer } from "@sophistry/solidjs";
import { onSettled } from "solid-js";
export default function BoxLayout() {
const { play } = usePlayer();
const heading = track(0);
const body = track(0);
const shape = track(0);
onSettled(() => {
void play(
lagged(
[heading, body, shape].map((t) =>
valueTo(t, 1, { duration: 1.2, easing: "linear" }),
),
{ lagRatio: 0.7 },
),
);
});
const tag = (label: string) => (
<Box
style={{
paddingTop: 8,
paddingBottom: 8,
paddingLeft: 20,
paddingRight: 20,
borderRadius: 999,
background: "#2b3a52",
fontSize: 28,
}}
>
<Text text={label} progress={body} />
</Box>
);
return (
<Stage>
<Box
style={{
width: 1400,
flexDirection: "row",
gap: 56,
padding: 64,
background: "#16202e",
borderWidth: 3,
borderColor: "#3d5170",
borderRadius: 40,
color: "#e6edf5",
fontSize: 36,
alignItems: "center",
}}
>
<Box style={{ flex: 1, flexDirection: "column", gap: 28 }}>
<Text
text="Fourier series"
fontSize={84}
fontWeight={700}
progress={heading}
/>
<Text
text={[
"Any reasonable ",
{ text: "periodic", italic: true, color: YELLOW },
" function is a sum of sines and cosines, each a whole multiple of one ",
{ text: "fundamental frequency", fontWeight: 700, color: BLUE },
".",
]}
progress={body}
/>
<Box style={{ flexDirection: "row", gap: 16 }}>
{tag("analysis")}
{tag("signals")}
{tag("heat equation")}
</Box>
</Box>
<Circle
radius={1.6}
stroke={BLUE}
strokeWidth={0.06}
progress={shape}
/>
</Box>
</Stage>
);
}
import { Box, Circle, Stage, Text } from "@sophistry/solidjs";
export const Card = () => (
<Stage>
<Box
style={{
flexDirection: "row",
gap: 56,
padding: 64,
background: "#16202e",
borderRadius: 40,
color: "#e6edf5",
fontSize: 36,
}}
>
<Box style={{ flex: 1, flexDirection: "column", gap: 28 }}>
<Text text="Fourier series" fontSize={84} fontWeight={700} />
<Text text="Any reasonable periodic function is a sum of sines and cosines." />
</Box>
<Circle radius={1.6} />
</Box>
</Stage>
);

Box comes from @sophistry/solidjs or @sophistry/react like every other component. In the core it is box.create({ style, children }) from @sophistry/core.

Inside a box, numbers are design pixels: a scene is 1920 design pixels wide, whatever its size on screen and however many scene units it spans. A fontSize: 36 reads the same on a 4-unit stage as on a 16-unit one, and padding: 64 is a thirtieth of the frame’s width.

A style length can also be:

  • a percentage of the parent’s size, "50%";
  • scene units, "2u";
  • design pixels whatever the box’s unit, "100px";
  • "auto", where CSS allows it.

With lengthUnit="scene", plain numbers are scene units instead. Text inside a box takes the box’s unit, so fontSize follows the same rule. The same lengthUnit prop works on <Group> and <Text> outside boxes.

The outermost box is centred on its x/y, in its parent’s units like any other node. With no size it shrinks to fit its contents; with no position it sits at the origin. Its rotation and scale turn and scale it about that centre without changing the layout.

Inside a box y runs down, as on a page, and children are placed in order. Boxes nested directly inside a box are laid out with it as one tree: a nested box’s own x, y, rotation and scale are ignored, since its parent places it.

style is one typed object per box, shaped like CSS. Nothing cascades except text properties, which are handed down to the text inside (through nested boxes too) unless the text sets its own.

Group Properties
Flex container flexDirection (default "row"), flexWrap, justifyContent, alignItems, alignContent, gap, rowGap, columnGap
Flex item flex, flexGrow, flexShrink, flexBasis, alignSelf
Size width, height, minWidth, maxWidth, minHeight, maxHeight, aspectRatio (all include padding and border)
Spacing padding, margin, and their Top/Right/Bottom/Left forms; one value, [vertical, horizontal] or [top, right, bottom, left]
Grid display: "grid", gridTemplateColumns, gridTemplateRows, gridAutoColumns, gridAutoRows, gridAutoFlow, gridColumn, gridRow, justifyItems, justifySelf
Position position: "absolute" with top, right, bottom, left; display: "none"
Paint background (a colour or a gradient), borderWidth, borderColor, borderRadius (one, or four from the top left, clockwise), opacity, overflow: "hidden"
Text, handed down color, fontFamily, fontSize, fontWeight, italic, letterSpacing, lineHeight, textAlign, underline, strikethrough

flex: 1 grows and shrinks from a basis of 0, as in CSS. There is no block layout: every box is a flex container unless it says display: "grid".

A grid’s tracks are typed values rather than CSS strings: a length (in the box’s unit, or "%", "u", "px"), "auto", "min-content", "max-content", a share of the free space { fr: 1 }, { minmax: [min, max] } or { fitContent: length }. { repeat, tracks } repeats some, a number of times or as many as fit ("auto-fill", "auto-fit").

Grid layout

A box with display: grid: a title spanning every column, three equal columns of cards, and a footer row, with tracks in design pixels and fr.

GridLayout.tsx
2 collapsed lines
import { BLUE, GREEN, RED, YELLOW } from "@sophistry/core";
import { Box, Circle, Square, Stage, Star, Text } from "@sophistry/solidjs";
const card = {
padding: 28,
gap: 16,
flexDirection: "column",
alignItems: "center",
background: "#22304a",
borderRadius: 24,
} as const;
export default function GridLayout() {
return (
<Stage>
<Box
style={{
display: "grid",
width: 1300,
gridTemplateColumns: [{ repeat: 3, tracks: [{ fr: 1 }] }],
gap: 28,
padding: 48,
background: "#16202e",
borderRadius: 36,
color: "#e6edf5",
fontSize: 36,
}}
>
<Box style={{ gridColumn: { span: 3 } }}>
<Text text="Three shapes" fontSize={64} fontWeight={700} />
</Box>
<Box style={card}>
<Circle radius={0.7} stroke={BLUE} strokeWidth={0.05} />
<Text text="circle" />
</Box>
<Box style={card}>
<Square size={1.3} stroke={GREEN} strokeWidth={0.05} />
<Text text="square" />
</Box>
<Box style={card}>
<Star outerRadius={0.8} stroke={RED} strokeWidth={0.05} />
<Text text="star" />
</Box>
<Box
style={{
gridColumn: { start: 2, span: 2 },
justifyContent: "flex-end",
color: YELLOW,
fontSize: 28,
}}
>
<Text text="the footer spans the last two columns" />
</Box>
</Box>
</Stage>
);
}
import type { BoxStyle } from "@sophistry/core";
const dashboard: BoxStyle = {
display: "grid",
gridTemplateColumns: [240, { repeat: 3, tracks: [{ fr: 1 }] }],
gridAutoRows: [{ minmax: [120, "auto"] }],
gap: 24,
};
const header: BoxStyle = { gridColumn: { span: 4 } };
const sidebar: BoxStyle = { gridRow: { start: 2, span: 2 } };

A child is placed by gridColumn and gridRow: a line number (negative counts from the end), or { start, end, span }. Children without a place fill the grid in order, row by row unless gridAutoFlow says "column", and "dense" backfills earlier gaps. Named lines and areas are not supported.

  • Text wraps at the width its cell gets and grows the box to fit. Rich text works as it does anywhere; see text.
  • Boxes nest, as above.
  • Any other node (a circle, an axes, a LaTeX formula) keeps scene units and its own y direction. The box measures it and gives it a cell of that size, scaled from scene units to the box’s pixels, so a node means the same inside a box as outside.

Changing a nested box’s style, a text’s content, the children, or the size of a scene node inside lays the tree out again. To animate between layouts, see layout transitions.

layoutTo from @sophistry/core is an animation that changes boxes’ styles or children and moves everything to its new place. Each box’s size and each node’s position blend from where it was to where the new layout puts it, so reordered children slide past each other and a row can fold into a column.

Layout transitions

layoutTo changes boxes' styles and children and moves everything to its new place: tiles reorder, then the row turns into a column, each tile gliding there.

LayoutTransition.tsx
3 collapsed lines
import { type BoxNode, hold, layoutTo, sequence } from "@sophistry/core";
import { Box, Stage, Text, usePlayer } from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const tiles = [
{ label: "sin", color: "#58c4dd" },
{ label: "cos", color: "#83c167" },
{ label: "tan", color: "#fc6255" },
{ label: "exp", color: "#f0ac5f" },
];
const row = {
flexDirection: "row",
gap: 32,
padding: 40,
background: "#16202e",
borderRadius: 32,
color: "#0b1018",
fontSize: 56,
fontWeight: 700,
} as const;
const column = { ...row, flexDirection: "column", gap: 20 } as const;
export default function LayoutTransition() {
const { play, signal } = usePlayer();
let container: BoxNode | undefined;
const nodes: BoxNode[] = [];
onSettled(() => {
void (async () => {
while (!signal.aborted && container) {
const [a, b, c, d] = nodes as [BoxNode, BoxNode, BoxNode, BoxNode];
await play(
sequence(
hold(0.6),
layoutTo(container, { children: [d, b, a, c] }, { duration: 1 }),
hold(0.6),
layoutTo(container, { style: column }, { duration: 1 }),
hold(0.6),
layoutTo(
container,
{ style: row, children: [a, b, c, d] },
{ duration: 1 },
),
),
);
}
})();
});
return (
<Stage>
<Box style={row} ref={(node) => (container = node)}>
{tiles.map((tile, index) => (
<Box
ref={(node) => (nodes[index] = node)}
style={{
width: 200,
paddingTop: 24,
paddingBottom: 24,
borderRadius: 20,
background: tile.color,
justifyContent: "center",
}}
>
<Text text={tile.label} />
</Box>
))}
</Box>
</Stage>
);
}
import { box, layoutTo, sequence, text } from "@sophistry/core";
const [a, b, c] = ["sin", "cos", "tan"].map((label) => text.create({ text: label }));
const row = box.create({ style: { gap: 24 }, children: [a, b, c] });
const caption = box.create({ style: { width: 400 } });
sequence(
// One box: a new order and a wider gap.
layoutTo(row, { children: [c, a, b], style: { gap: 48 } }, { duration: 1 }),
// Several boxes in one tree, moving together.
layoutTo([[row, { style: { flexDirection: "column" } }], [caption, { style: { width: 600 } }]], { duration: 0.8 }),
);

It is an ordinary animation: it composes with sequence and parallel, scrubs, and puts the old styles and children back when rewound to its start. Nodes are matched by identity across the whole tree, so a node handed to another box in the same tree travels there. Styles themselves switch at the start: colours don’t blend, and text wraps at its new width straight away. A node that only appears in the new layout is there from the start, and one that is dropped goes at once.

Block and inline layout, shadows, blur, blend modes and scrolling are not supported. See the design notes for how layout, text and units fit together.