Stack
@codeui/solid v0.3.0
A flex container that spaces its children with token gaps.
When to use
Use Stack to lay out a group of siblings in one row or one column with a gap from the space scale. It covers the layout work inside a panel: a title over its description, a row of buttons, a label and its control. Stack adds no color, padding or border, so it stays invisible and only places its children.
When to choose another component
- Use CSS grid in your own class when you need two axes, such as a form with aligned label and value columns.
Stackis one-dimensional. - Use
SegmentedControlorTabswhen the row is a set of choices.Stackhas no keyboard behavior or selection. - Use the
gaporleftIconbuilt into a component, such as the 8px gap insideButton, before you wrap its content in aStack.
Live demo
import { Button, Stack, Text } from "@codeui/solid";
export default function StackBasic() {
return (
<Stack gap="3">
<Stack gap="1">
<Text size="md" weight="semibold">
Export snippet
</Text>
<Text size="xs" tone="muted">
hello-world.ts as a 2x PNG with a transparent background.
</Text>
</Stack>
<Stack direction="row" gap="2" justify="end">
<Button variant="secondary">Cancel</Button>
<Button variant="primary">Export</Button>
</Stack>
</Stack>
);
}Anatomy
Tap a part to outline it in the demo.
- 01Stack
stackclasses.root
| Export | Renders | classes key | data-cui | Role |
|---|---|---|---|---|
Stack | div, or the element in as | root | stack | None |
The root also renders these attributes, which the stylesheet reads:
| Attribute | Values | Default |
|---|---|---|
data-direction | row, column | column |
data-gap | A Gap key | 2 |
data-align | start, center, end, stretch, baseline | stretch |
data-justify | start, center, end, between, around, evenly | start |
data-wrap | true, false | false |
Variants
Direction
import { Badge, Stack, Text } from "@codeui/solid";
export default function StackDirection() {
return (
<Stack direction="row" gap="8" wrap>
<Stack gap="2">
<Text size="xs" tone="muted">
direction="column" (default)
</Text>
<Badge>PNG</Badge>
<Badge>SVG</Badge>
<Badge>JPEG</Badge>
</Stack>
<Stack gap="2">
<Text size="xs" tone="muted">
direction="row"
</Text>
<Stack direction="row" gap="2">
<Badge>PNG</Badge>
<Badge>SVG</Badge>
<Badge>JPEG</Badge>
</Stack>
</Stack>
</Stack>
);
}direction sets the main axis. The default is column, because most stacks in a panel are vertical.
Gap
import { Badge, type Gap, Stack, Text } from "@codeui/solid";
import { For } from "solid-js";
const gaps: Gap[] = [
"0",
"px",
"0.5",
"1",
"1.5",
"2",
"3",
"4",
"6",
"8",
"10",
"12",
"16",
"20",
"24",
"32",
"40",
"48",
"56",
"64",
];
export default function StackGap() {
return (
// The widest steps are wider than a phone, so the ladder scrolls inside itself.
<Stack gap="2" style={{ "overflow-x": "auto" }}>
<For each={gaps}>
{gap => (
<Stack direction="row" gap="4" align="center">
<Text size="xs" tone="muted" style={{ width: "3rem" }}>
{gap}
</Text>
<Stack direction="row" gap={gap}>
<Badge>PNG</Badge>
<Badge>SVG</Badge>
</Stack>
</Stack>
)}
</For>
</Stack>
);
}gap takes a key of the space scale. The Gap type lists every accepted key: 0, px, 0.5, 1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40, 48, 56 and 64. Key 1 is 4px and each step follows the 4px grid, 0.5 is 2px, 1.5 is 6px and px is 1px. The default is 2, which is 8px.
Inkstone keeps space inside a group smaller than space between groups. Use 0.5 or 1 from a title to its description and 4 to 6 between groups. See Space and shape.
Align
import { Badge, Stack, Text, tokens } from "@codeui/solid";
import { For } from "solid-js";
const aligns = ["start", "center", "end", "stretch", "baseline"] as const;
export default function StackAlign() {
return (
<Stack gap="3">
<For each={aligns}>
{align => (
<Stack gap="1">
<Text size="xs" tone="muted">
align="{align}"
</Text>
<Stack
direction="row"
gap="3"
align={align}
style={{ background: tokens.surface.input, padding: tokens.space["2"], "border-radius": tokens.radius.sm }}
>
<Text size="xl" weight="semibold">
main.rs
</Text>
<Text size="xs" tone="muted">
Rust · 42 lines
</Text>
<Badge>Draft</Badge>
</Stack>
</Stack>
)}
</For>
</Stack>
);
}align sets align-items on the cross axis. The default is stretch. Use center for a row of controls, and baseline when the row mixes text sizes and the text must sit on one line.
Justify
import { Badge, Stack, Text, tokens } from "@codeui/solid";
import { For } from "solid-js";
const justifies = ["start", "center", "end", "between", "around", "evenly"] as const;
export default function StackJustify() {
return (
<Stack gap="3">
<For each={justifies}>
{justify => (
<Stack gap="1">
<Text size="xs" tone="muted">
justify="{justify}"
</Text>
<Stack
direction="row"
gap="2"
justify={justify}
style={{ background: tokens.surface.input, padding: tokens.space["2"], "border-radius": tokens.radius.sm }}
>
<Badge>PNG</Badge>
<Badge>SVG</Badge>
<Badge>JPEG</Badge>
</Stack>
</Stack>
)}
</For>
</Stack>
);
}justify sets justify-content on the main axis. between, around and evenly map to space-between, space-around and space-evenly. The default is start.
Wrap
import { Badge, Stack, Text } from "@codeui/solid";
import { For } from "solid-js";
const languages = ["TypeScript", "Rust", "Python", "Go", "Kotlin", "Swift", "Elixir", "Haskell", "Zig", "OCaml"];
export default function StackWrap() {
return (
<Stack gap="4" style={{ "max-width": "20rem" }}>
<Stack gap="1">
<Text size="xs" tone="muted">
wrap
</Text>
<Stack direction="row" gap="1.5" wrap>
<For each={languages}>{language => <Badge>{language}</Badge>}</For>
</Stack>
</Stack>
<Stack gap="1">
<Text size="xs" tone="muted">
wrap={"{false}"} (default)
</Text>
<Stack direction="row" gap="1.5" style={{ overflow: "hidden" }}>
<For each={languages}>{language => <Badge>{language}</Badge>}</For>
</Stack>
</Stack>
</Stack>
);
}wrap lets children flow onto new lines when the row runs out of space. The default is false, so children stay on one line and can overflow.
Polymorphic element
import { Link, Stack } from "@codeui/solid";
export default function StackAs() {
return (
<Stack as="nav" aria-label="Export formats" direction="row" gap="4">
<Link href="#png">PNG</Link>
<Link href="#svg">SVG</Link>
<Link href="#jpeg">JPEG</Link>
</Stack>
);
}as renders a different element or component. Use it to give the group its real meaning, such as nav, ul, ol or kbd. Every other prop goes to that element.
How Stack styles its props
The stylesheet for Stack is fixed. Vanilla Extract compiles it at build time into a base class and one class per prop value, from the gap keys of the space scale to each alignment. The props pick classes; nothing generates or injects CSS at runtime.
This design has three results that matter in an app:
Stackworks next to any styling engine. Tailwind, CSS Modules, plain CSS and Vanilla Extract all see a normal element with a few classes and attributes.- The server renders the same markup the client renders. There is no style tag to hydrate and no class that depends on render order.
stackStyles.root()gives custom markup the base class for layout controlled by attributes.
Stack also writes data-gap, data-align, data-justify, data-direction and data-wrap, so you can target any state with an attribute selector, such as [data-cui="stack"][data-direction="row"]. The kit rules sit in the cui cascade layer, so your unlayered CSS wins. To change the gap from CSS, set gap in your own class.
Ready API
Stack
Stack renders a flex container, a div by default, with the kit layout class and the data attributes listed in Anatomy.
Prop
Type
Stack takes every prop of the element in as, so there is no inherited Kobalte table. The exported types are StackProps, StackVariants, StackClasses, StackPart and Gap.
Styled parts API
Import the parts from @codeui/solid/stack/parts.
Root
Root is the same component as Stack. @codeui/solid exports it as Stack, and @codeui/solid/stack/parts exports it as Root, with RootProps equal to StackProps. It wraps no Kobalte primitive. Use the parts entry when you compose your own layout primitives and want the namespace import.
import { Badge } from "@codeui/solid";
import * as S from "@codeui/solid/stack/parts";
export default function StackParts() {
return (
<S.Root direction="row" gap="2">
<Badge>PNG</Badge>
<Badge>SVG</Badge>
</S.Root>
);
}Styles export
stackStyles
Call stackStyles.root() for the base class. Set data-direction, data-align, and data-gap to choose a row with centered items and spacing.
import { Badge, stackStyles } from "@codeui/solid";
export default function StackStylesDemo() {
return (
<ul
class={stackStyles.root()} data-direction="row" data-align="center" data-gap="2"
style={{ margin: "0", padding: "0", "list-style": "none" }}
>
<li>
<Badge>PNG</Badge>
</li>
<li>
<Badge>SVG</Badge>
</li>
</ul>
);
}Prefer <Stack as="ul">. Call stackStyles only when you cannot render the component, for example on markup that another library renders. stackParts.root holds the data-cui value, stack.
Accessibility
Stack renders a div with no role, so assistive technology ignores it. Set as when the group has meaning: ul or ol for a list, nav with an aria-label for navigation, kbd for a key combination.
Visual order follows DOM order, because Stack has no reverse direction. Keyboard focus moves in the same order that the reader sees.
SSR notes
Stack renders completely on the server: the element, its classes and the data attributes. It has no effects, IDs or portals, so the client hydrates the same markup.
Theming hooks
| Variable | Use |
|---|---|
--cui-space-* | The value of gap for each key. The default is --cui-space-2. |
The defaults are in Typography and spacing.
Composition examples
A settings row puts a title and description on the left and a control on the right:
import { Stack, Switch, Text } from "@codeui/solid";
export default function StackSettingsRow() {
return (
<Stack direction="row" gap="4" align="center" justify="between">
<Stack gap="0.5">
<Text weight="medium">Show line numbers</Text>
<Text size="xs" tone="muted">
Adds a gutter to the exported image.
</Text>
</Stack>
<Switch aria-label="Show line numbers" defaultChecked />
</Stack>
);
}For a full settings panel, see Build settings.
Related
Textsets type inside a stack.Stackplaces the elements, andTextstyles them.- Space and shape gives the Inkstone rules for which gap to use.
- Cascade layers explains why your CSS wins over the kit rules.