Timeline
Two composable patches for vertical event timelines. Apply timeline to the <ol> or <ul> container. Apply timelineItem to each <li> — it creates a 2-column grid with a colored dot (::before) and a vertical connector line (::after). Set active to highlight the dot, and last to suppress the connector on the final item.
timeline
No props. Resets list styles and arranges children in a column.
timelineItem
| Prop | Type | Default |
|---|---|---|
active | ValueOrState<boolean> | false |
last | boolean | false |
color | ThemeColor | "neutral" |
accentColor | ThemeColor | "primary" |
Customization
Must see the source of patch at the bottom of each patch page to understand the structure then code it still code as html native element.
There are four levels of customization, in increasing order of effort:
- Patch props. Each patch exposes a small, stable set of props—typically fewer than five. Lowest friction.
- Context attributes. Use
dataTone,dataSize, anddataDensityon a container to shift tone, size, or density for an entire subtree without touching individual elements. - Inline override. Native-wins merge strategy: any property set directly on the element overrides the patch value.
- Create a variant. Clone a similar patch and edit it. Use this only when you need a reusable custom version.
Formulas
Unit - U = fontSize / 4 - convert final values with themeSpacing(n).
Size - n = intrinsic text lines, w = wrapping level, d = density factor:
height = (n * 6 + 2 * d * w) * U
paddingBlock = d * w * U
paddingInline = ceil(3 / w) * d * w * U
radius = d * w * U
Base density d = 1.5:
| U | w=0 | w=1 | w=2 | w=3 |
|---|---|---|---|---|
height (n = 1) | 6 | 9 | 12 | 15 |
| paddingBlock | 0 | 1.5 | 3 | 4.5 |
| paddingInline | 3 | 4.5 | 6 | 4.5 |
| radius | 0 | 1.5 | 3 | 4.5 |
Tone - K = N / 2 where N is the palette length. For N = 18, K = 9.
| Role | Shift | n=0 |
|---|---|---|
| Background | parent +/- n | 0 |
| Text | bg + K | 6 |
| Border | bg + K/2 | 3 |
| Hover | bg + 2K/3 | 4 |
| Selected / Focus | above +/- K/3 | 2-4 |
State shift range: K/3 <= delta <= 2K/3.
import { type PartialElement, toState, type ValueOrState } from "@domphy/core";
import {
type ThemeColor,
themeColor,
themeDensity,
themeSpacing,
} from "@domphy/theme";
/**
* Container for a vertical timeline. Sets list reset styles. Apply to `<ol>` or `<ul>`.
*
* @example { ol: [...], $: [timeline()] }
*/
function timeline(): PartialElement {
return {
style: {
listStyle: "none",
margin: 0,
padding: 0,
display: "flex",
flexDirection: "column",
},
};
}
/**
* A single event row in a `timeline`. Uses a 2-column grid: the left column holds
* a dot (`::before`) and optional connector line (`::after`); the right column holds
* the user's content. Apply to `<li>`.
*
* @param props.active - Full-opacity dot (accent color). `ValueOrState<boolean>`, defaults to `false`.
* @param props.last - Suppress the vertical connector below this item. `boolean`, defaults to `false`.
* @param props.color - Dot/connector color tone. `ThemeColor`, defaults to `"neutral"`.
* @param props.accentColor - Active dot color tone. `ThemeColor`, defaults to `"primary"`.
* @example { li: [{ b: "2024" }, { p: "Event" }], $: [timelineItem({ active: true })] }
*/
function timelineItem(
props: {
active?: ValueOrState<boolean>;
last?: boolean;
color?: ThemeColor;
accentColor?: ThemeColor;
} = {},
): PartialElement {
const { last = false } = props;
const color = props.color ?? "neutral";
const accentColor = props.accentColor ?? "primary";
const activeState = toState(props.active ?? false);
return {
style: {
display: "grid",
gridTemplateColumns: "2rem 1fr",
columnGap: (listener) => themeSpacing(themeDensity(listener) * 2),
paddingBottom: (listener) =>
last ? "0" : themeSpacing(themeDensity(listener) * 4),
position: "relative",
// Dot
"&::before": {
content: '""',
display: "block",
width: "0.75rem",
height: "0.75rem",
borderRadius: "50%",
justifySelf: "center",
marginTop: "0.25em",
transition: "background-color 200ms ease, opacity 200ms ease",
backgroundColor: (listener) =>
themeColor(
listener,
"shift-8",
activeState.get(listener) ? accentColor : color,
),
opacity: (listener) => (activeState.get(listener) ? "1" : "0.4"),
},
// Vertical connector to the next item
...(last
? {}
: {
"&::after": {
content: '""',
position: "absolute",
left: "calc(1rem - 1px)",
top: "1.25rem",
bottom: (listener) =>
`-${themeSpacing(themeDensity(listener) * 4)}`,
width: "2px",
backgroundColor: (listener) =>
themeColor(listener, "shift-3", color),
},
}),
},
};
}
export { timeline, timelineItem };