— Layout & primitives · @vitus-labs/elements
Five primitives. Every component.
5 building blocks for the components you actually ship: Element lays out an icon, a label and a trailing slot in one flex box; Text, List, Overlay and Portal cover type, data-driven lists and anything that floats. Every layout prop takes a value, an array or a breakpoint map, and the tag you pick is respected down to valid HTML nesting.
npm i @vitus-labs/elements @vitus-labs/core @vitus-labs/unistyle- primitives, plus the Util helper
- 5
- slots in Element: before, content, after
- 3
- Overlay types, dropdown to modal
- 5
— Why elements
The layout under the look.
One box for most components
Buttons, badges, inputs with icons, list rows and card headers are the same shape: something before, the content, something after. Element renders that shape with direction, alignment and gap per slot, and drops the inner layer entirely when there's only content.
HTML that stays valid
Pick tag="button" and the slots render as span, because a div can't sit inside a button. Void tags like input and img render without children. button, fieldset and legend get a two-layer flex fix.
Responsive without media queries
direction={{ xs: 'rows', md: 'inline' }}, gap={[8, 12]}, block={{ xs: true, lg: false }}. Layout props go through unistyle, so each breakpoint emits only what changed. The gap moves to the right side when direction flips.
Overlays with the ARIA wired
Overlay positions dropdowns, tooltips, popovers and modals against their trigger, flips at the viewport edge, and sets aria-expanded, aria-haspopup, aria-controls and roles. Modals trap focus and lock scroll.
— Before / after
Same button. No slot CSS.
A button with a leading icon and a trailing chevron that stacks vertically on small screens. On the left the slots and their spacing are CSS you maintain; on the right they're props.
import { styled } from '@vitus-labs/styler'
const Button = styled.button`
display: inline-flex;
flex-direction: column;
align-items: center;
& > .before { margin-bottom: 0.5rem; }
& > .after { margin-top: 0.5rem; }
@media (min-width: 48em) {
flex-direction: row;
& > .before { margin: 0 0.5rem 0 0; }
& > .after { margin: 0 0 0 0.5rem; }
}
`
<Button>
<span className="before"><Icon /></span>
<span>Deploy</span>
<span className="after"><Chevron /></span>
</Button>Wrapper spans, per-slot margins, and a media query that has to flip every margin when the direction changes.
import { Element } from '@vitus-labs/elements'
<Element
tag="button"
beforeContent={<Icon />}
afterContent={<Chevron />}
direction={{ xs: 'rows', md: 'inline' }}
alignX="center"
gap={8}
>
Deploy
</Element>Slots are props. The gap follows the direction at each breakpoint, and the inner wrappers render as span because the tag is a button.
— Live, not a screenshot
Change the props, read the DOM.
A real Element with before and after slots. The tree next to it is read from the rendered node: switch the tag to a and the slots become span, switch it to button and an extra flex layer appears inside.
import { Element } from '@vitus-labs/elements'
<Element
tag={tag} // 'div' | 'a' | 'button'
block
direction={direction}
alignX={alignX}
alignY="center"
gap={12}
beforeContent={<span>◆</span>}
afterContent={<span>→</span>}
css={look}
>
Deploy
</Element><Element tag="button" direction="inline" alignX="center" …/>reading the DOM…
reading the stylesheet…
— Features
What's in the box.
- Element
three-slot flex layout; each slot has its own direction, alignX, alignY and css
- Text
a text primitive with a
paragraphshorthand for<p>and any texttag - List
renders children, an array of primitives or an array of objects through one component
- Positional props
first,last,odd,even,indexandpositionfor every list item, viaitemProps - Overlay
dropdown, tooltip, popover, modal or custom; opens on click, hover or manually
- Edge flipping
content that would be clipped flips to the side that keeps it on screen
- Modal behavior
focus trap (topmost only, for nested modals), ref-counted body scroll lock, ESC to close
- Nested overlays
a child overlay blocks its parent from closing while it's open
- Portal
renders into a fresh node on
document.bodyor anyDOMLocation, removed on unmount - Util
adds
classNameandstyleto its child without a DOM node of its own - equalBeforeAfter
matches the before and after slot sizes, kept in sync with a ResizeObserver
- React Native
a native entry with Element, Text, List and Util; Overlay and Portal stay web-only
— API
Five components. Two helpers.
Every name links to its reference in the docs.
— Honest limits
When to reach past them.
Where it isn't the right fit, and what to reach for instead.
Two-dimensional layouts
Elementis one flex row or column with three slots. For page-level columns use coolgrid; for true two-dimensional placement write CSS grid withcss.Advanced floating positioning
Overlay flips to the opposite side when content would be clipped. It doesn't shift along the edge, resize to fit or draw an arrow. For those, use a dedicated positioning library inside a
Portal.Overlay content isn't server-rendered
Overlay renders its content only in the browser, when active, and both trigger and content must accept a
ref(or the prop named bytriggerRefName/contentRefName). Content that must be in the HTML belongs outside an Overlay.Overlay and Portal need react-dom
They're excluded from the React Native entry. On native, use a platform modal; Element, Text, List and Util still work.
Layout only, no look
Elements ship structure, not design: no colors, spacing scale or variants. Style them with
cssprops, or wrap them in rocketstyle or attrs components.
— Recipes
Copy, paste, ship.
An input with an icon and a clear button
Before and after slots around the content, each aligned on its own. gap spaces the slots from the content.
import { Element } from '@vitus-labs/elements'
export const SearchField = ({ value, onChange, onClear }) => (
<Element
tag="label"
block
alignY="center"
gap={8}
beforeContent={<SearchIcon />}
afterContent={value ? <ClearButton onClick={onClear} /> : null}
css="padding: 8px 12px; border: 1px solid #d0d4dc; border-radius: 8px;"
>
<input value={value} onChange={onChange} placeholder="Search" />
</Element>
)A list that knows its edges
itemProps receives each item and its position, so separators and zebra rows need no index math. rootElement wraps the list in an Element for layout.
import { List } from '@vitus-labs/elements'
<List
rootElement
direction="rows"
component={ReleaseRow}
data={releases}
itemKey="version"
itemProps={(item, { first, last, odd }) => ({
latest: first,
divider: !last,
shaded: odd,
})}
/>A dropdown with a render-prop trigger
Render functions receive active, the ref and, for clickOutsideContent or manual control, showContent and hideContent.
import { Overlay } from '@vitus-labs/elements'
<Overlay
type="dropdown"
openOn="click"
closeOn="clickOutsideContent"
align="bottom"
alignX="right"
offsetY={6}
trigger={({ active, ref }) => (
<button ref={ref}>{active ? 'Close' : 'Options'}</button>
)}
>
{({ hideContent, ref }) => (
<ul ref={ref} role="menu">
<li><button onClick={hideContent}>Rename</button></li>
<li><button onClick={hideContent}>Archive</button></li>
</ul>
)}
</Overlay>Text with a semantic tag
Text takes any text tag, and paragraph is shorthand for <p>. Both accept a css extension.
import { Text } from '@vitus-labs/elements'
<Text tag="h2" css="margin: 0;">Release notes</Text>
<Text paragraph>Everything that changed in 2.9.</Text>— FAQ
Questions, answered.
Do I need a CSS engine?
Yes. Elements style through @vitus-labs/core, so call init() with a connector (styler, styled-components, Emotion or React Native) before rendering, and wrap the app in unistyle's Provider for breakpoints.
Why doesn't `direction` change a children-only Element?
With no beforeContent or afterContent there is no inner wrapper: the outer element is the content wrapper, so its layout comes from contentDirection, contentAlignX and contentAlignY (defaults rows, left, center), which take precedence. Use contentDirection="inline" there. direction, alignX and alignY lay out the slots once one is present.
Which content prop wins?
children, then content, then label. The last two exist for inline use and for components that pass content through attrs.
Why is my Element rendering a span inside a button?
Slot wrappers inside inline tags (button, a, span, label and others) render as span so the HTML stays valid. A button also gets a second flex layer, because flexbox on a button element isn't reliable across browsers.
What does Overlay default to?
A dropdown that opens and closes on click, positioned fixed below the trigger and left-aligned, closing on ESC, with repositioning throttled to 200 ms.
Is this site built with it?
Yes. The buttons, cards and headings on this page are rocketstyle components on Element.
— @vitus-labs/elements
Start every component from Element.
npm i @vitus-labs/elements @vitus-labs/core @vitus-labs/unistyleSources · 40 claims, each traced to v2.9.1 code or docs
- Exports Element, List, Overlay, Portal, Text plus Util, useOverlay, OverlayProvider, Providerui-system:packages/elements/src/index.ts
- Element renders before / content / after inside a flex wrapperui-system:packages/elements/src/Element/component.tsx
- Content-only Element skips the inner layerui-system:packages/elements/src/Element/component.tsx
- Content priority children > content > labelui-system:packages/elements/src/Element/component.tsx
- Per-slot css props (css, contentCss, beforeContentCss, afterContentCss)ui-system:packages/elements/src/Element/component.tsx
- Inline tags make slot wrappers render as spanui-system:packages/elements/src/Element/constants.ts
- Void tags (input, img, …) render without childrenui-system:packages/elements/src/Element/component.tsx
- button / fieldset / legend get a two-layer flex fixui-system:packages/elements/src/helpers/Wrapper/constants.ts
- Inner fix layer renders as span inside inline tags (button)ui-system:packages/elements/src/helpers/Wrapper/component.tsx
- Layout props are responsive via unistyle makeItResponsiveui-system:packages/elements/src/helpers/Wrapper/styled.ts
- Gap goes to the correct side per direction, including reverse directionsui-system:packages/elements/src/helpers/Content/styled.ts
- equalBeforeAfter kept in sync with a ResizeObserverui-system:packages/elements/src/Element/component.tsx
- Element is memoizedui-system:packages/elements/src/Element/component.tsx
- Text paragraph shorthand renders <p>ui-system:packages/elements/src/Text/component.tsx
- List / Iterator: children, primitive arrays or object arraysui-system:packages/elements/src/helpers/Iterator/component.tsx
- Positional metadata first, last, odd, even, index, positionui-system:packages/elements/src/helpers/Iterator/component.tsx
- rootElement wraps the list in an Element; default is a fragmentui-system:packages/elements/src/List/component.tsx
- Overlay types dropdown, tooltip, popover, modal, customui-system:packages/elements/src/Overlay/positionMath.ts
- Overlay defaults: click/click, dropdown, fixed, bottom, left, throttle 200 ms, ESC closesui-system:packages/elements/src/Overlay/useOverlay.tsx
- Default type is dropdownui-system:packages/elements/src/Overlay/useOverlay.tsx
- Content flips when an edge would be clippedui-system:packages/elements/src/Overlay/positionMath.ts
- Positions are written as top/left/bottom/right; no shift, size or arrow logicui-system:packages/elements/src/Overlay/useOverlay.tsx
- Trigger ARIA: aria-expanded, aria-haspopup, aria-controls; tooltip aria-describedby; dialog role + aria-modalui-system:packages/elements/src/Overlay/component.tsx
- Modal focus trap and scroll lockui-system:packages/elements/src/Overlay/useOverlay.tsx
- Only the topmost focus trap handles Tabui-system:packages/elements/src/Overlay/useFocusTrap.ts
- Scroll lock is reference-countedui-system:packages/elements/src/Overlay/useScrollLock.ts
- Nested overlay blocks its parentui-system:packages/elements/src/Overlay/context.tsx
- Overlay content rendered only in the browserui-system:packages/elements/src/Overlay/component.tsx
- Trigger must accept ref (or triggerRefName)ui-system:packages/elements/src/Overlay/component.tsx
- Render props receive showContent / hideContent for manual or clickOutsideContentui-system:packages/elements/src/Overlay/component.tsx
- Portal appends a new node to DOMLocation or document.body, removed on unmountui-system:packages/elements/src/Portal/component.ts
- Util adds className / style without its own DOM nodeui-system:packages/elements/src/Util/component.tsx
- Children-only Element: layout comes from contentDirection / contentAlignX / contentAlignY (rows / left / center), which take precedence over directionnpm:@vitus-labs/elements/README.md
- direction only applies when beforeContent or afterContent is presentui-system:packages/elements/src/Element/types.ts
- Native entry exports Element, List, Text, Util (no Overlay / Portal)ui-system:packages/elements/src/index.native.ts
- Styles go through core's configured engineui-system:packages/elements/src/helpers/Wrapper/styled.ts
- Peer dependencies: react, react-dom (optional), core, unistylenpm:@vitus-labs/elements/package.json
- README Overlay table: type defaults to dropdownnpm:@vitus-labs/elements/README.md
- This site's buttons are rocketstyle components on Elementsite:src/ui/Button.tsx
- Demo reads the rendered DOM and the inserted rulesite:src/components/products/demos/ElementsDemo.tsx