— 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.

@vitus-labs/elementsv2.9.1 · MIT
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.

01

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.

02

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.

03

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.

04

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.

beforeHand-rolled flex
DeployButton.tsx
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.

after@vitus-labs/elements
DeployButton.tsx
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.

tag
direction
alignX
DeployButton.tsx
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" …/>
rendered DOMlive
reading the DOM…
wrapper rulelive
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 paragraph shorthand for <p> and any text tag

  • List

    renders children, an array of primitives or an array of objects through one component

  • Positional props

    first, last, odd, even, index and position for every list item, via itemProps

  • 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.body or any DOMLocation, removed on unmount

  • Util

    adds className and style to 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

    Element is one flex row or column with three slots. For page-level columns use coolgrid; for true two-dimensional placement write CSS grid with css.

  • 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 by triggerRefName / 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 css props, or wrap them in rocketstyle or attrs components.

— Recipes

Copy, paste, ship.

01

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.

SearchField.tsx
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>
)
02

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.

Releases.tsx
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,
  })}
/>
03

A dropdown with a render-prop trigger

Render functions receive active, the ref and, for clickOutsideContent or manual control, showContent and hideContent.

Menu.tsx
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>
04

Text with a semantic tag

Text takes any text tag, and paragraph is shorthand for <p>. Both accept a css extension.

Intro.tsx
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/unistyle
Sources · 40 claims, each traced to v2.9.1 code or docs
  1. Exports Element, List, Overlay, Portal, Text plus Util, useOverlay, OverlayProvider, Providerui-system:packages/elements/src/index.ts
  2. Element renders before / content / after inside a flex wrapperui-system:packages/elements/src/Element/component.tsx
  3. Content-only Element skips the inner layerui-system:packages/elements/src/Element/component.tsx
  4. Content priority children > content > labelui-system:packages/elements/src/Element/component.tsx
  5. Per-slot css props (css, contentCss, beforeContentCss, afterContentCss)ui-system:packages/elements/src/Element/component.tsx
  6. Inline tags make slot wrappers render as spanui-system:packages/elements/src/Element/constants.ts
  7. Void tags (input, img, …) render without childrenui-system:packages/elements/src/Element/component.tsx
  8. button / fieldset / legend get a two-layer flex fixui-system:packages/elements/src/helpers/Wrapper/constants.ts
  9. Inner fix layer renders as span inside inline tags (button)ui-system:packages/elements/src/helpers/Wrapper/component.tsx
  10. Layout props are responsive via unistyle makeItResponsiveui-system:packages/elements/src/helpers/Wrapper/styled.ts
  11. Gap goes to the correct side per direction, including reverse directionsui-system:packages/elements/src/helpers/Content/styled.ts
  12. equalBeforeAfter kept in sync with a ResizeObserverui-system:packages/elements/src/Element/component.tsx
  13. Element is memoizedui-system:packages/elements/src/Element/component.tsx
  14. Text paragraph shorthand renders <p>ui-system:packages/elements/src/Text/component.tsx
  15. List / Iterator: children, primitive arrays or object arraysui-system:packages/elements/src/helpers/Iterator/component.tsx
  16. Positional metadata first, last, odd, even, index, positionui-system:packages/elements/src/helpers/Iterator/component.tsx
  17. rootElement wraps the list in an Element; default is a fragmentui-system:packages/elements/src/List/component.tsx
  18. Overlay types dropdown, tooltip, popover, modal, customui-system:packages/elements/src/Overlay/positionMath.ts
  19. Overlay defaults: click/click, dropdown, fixed, bottom, left, throttle 200 ms, ESC closesui-system:packages/elements/src/Overlay/useOverlay.tsx
  20. Default type is dropdownui-system:packages/elements/src/Overlay/useOverlay.tsx
  21. Content flips when an edge would be clippedui-system:packages/elements/src/Overlay/positionMath.ts
  22. Positions are written as top/left/bottom/right; no shift, size or arrow logicui-system:packages/elements/src/Overlay/useOverlay.tsx
  23. Trigger ARIA: aria-expanded, aria-haspopup, aria-controls; tooltip aria-describedby; dialog role + aria-modalui-system:packages/elements/src/Overlay/component.tsx
  24. Modal focus trap and scroll lockui-system:packages/elements/src/Overlay/useOverlay.tsx
  25. Only the topmost focus trap handles Tabui-system:packages/elements/src/Overlay/useFocusTrap.ts
  26. Scroll lock is reference-countedui-system:packages/elements/src/Overlay/useScrollLock.ts
  27. Nested overlay blocks its parentui-system:packages/elements/src/Overlay/context.tsx
  28. Overlay content rendered only in the browserui-system:packages/elements/src/Overlay/component.tsx
  29. Trigger must accept ref (or triggerRefName)ui-system:packages/elements/src/Overlay/component.tsx
  30. Render props receive showContent / hideContent for manual or clickOutsideContentui-system:packages/elements/src/Overlay/component.tsx
  31. Portal appends a new node to DOMLocation or document.body, removed on unmountui-system:packages/elements/src/Portal/component.ts
  32. Util adds className / style without its own DOM nodeui-system:packages/elements/src/Util/component.tsx
  33. Children-only Element: layout comes from contentDirection / contentAlignX / contentAlignY (rows / left / center), which take precedence over directionnpm:@vitus-labs/elements/README.md
  34. direction only applies when beforeContent or afterContent is presentui-system:packages/elements/src/Element/types.ts
  35. Native entry exports Element, List, Text, Util (no Overlay / Portal)ui-system:packages/elements/src/index.native.ts
  36. Styles go through core's configured engineui-system:packages/elements/src/helpers/Wrapper/styled.ts
  37. Peer dependencies: react, react-dom (optional), core, unistylenpm:@vitus-labs/elements/package.json
  38. README Overlay table: type defaults to dropdownnpm:@vitus-labs/elements/README.md
  39. This site's buttons are rocketstyle components on Elementsite:src/ui/Button.tsx
  40. Demo reads the rendered DOM and the inserted rulesite:src/components/products/demos/ElementsDemo.tsx