Element
Core 3-section flex layout component — the foundational building block.
Element is the primary building block of the UI system. It renders a flex container with three optional sections: beforeContent, content (children), and afterContent. When only content exists, unnecessary nesting is bypassed for optimal DOM structure.
Peer dependencies: @vitus-labs/core, @vitus-labs/unistyle
Basic Usage
import { Element } from '@vitus-labs/elements'
// Simple content
<Element tag="p" label="Hello World" />
// Button with icon and label
<Element
tag="button"
beforeContent={<Icon name="star" />}
gap={8}
alignX="center"
alignY="center"
>
Favorite
</Element>
// Three-section layout
<Element
beforeContent={<Avatar />}
afterContent={<Badge count={5} />}
gap={12}
alignX="spaceBetween"
alignY="center"
>
<span>John Doe</span>
</Element>Content Props
Content is resolved with this priority: children > content > label
| Prop | Type | Description |
|---|---|---|
children | ReactNode | Highest priority content |
content | Content | Alternative to children (supports components, elements, render functions) |
label | Content | Fallback content |
beforeContent | Content | Rendered before main content |
afterContent | Content | Rendered after main content |
The Content type accepts anything the render() utility from @vitus-labs/core handles: React elements, component types (created via createElement), render functions ((props) => ReactNode), primitives, arrays, and fragments.
DOM Structure
Simple Element (no beforeContent/afterContent)
When no side content is provided, Element renders a single wrapper with content directly inside — no unnecessary nesting:
<div class="vl-..." style="display: inline-flex; ...">
<!-- children / content / label -->
</div>The wrapper takes contentDirection / contentAlignX / contentAlignY (falling back to direction / alignX / alignY). Because contentDirection defaults to 'rows', a simple Element stacks its children in a column unless you set contentDirection.
Complex Element (has beforeContent or afterContent)
When side content is present, three inner <div> (or <span> for inline tags) slots are rendered:
<div class="vl-..." style="display: inline-flex; flex-direction: row; ...">
<div class="..." style="..."><!-- beforeContent --></div>
<div class="..." style="flex: 1; ..."><!-- children / content / label --></div>
<div class="..." style="..."><!-- afterContent --></div>
</div>The outer wrapper uses direction/alignX/alignY, while each slot has its own direction/alignment props.
gap is applied as a margin on the beforeContent / afterContent slots on the side facing the main content: margin-right / margin-left for inline, margin-bottom / margin-top for rows. Since 2.9 the sides are mirrored for reverseInline and reverseRows, so the gap stays between the slots when the direction is reversed.
Layout Props
| Prop | Type | Default | Description |
|---|---|---|---|
tag | HTMLTags | config.component ('div' on web) | HTML element to render |
block | boolean (responsive) | false | flex + width: 100% (true) vs inline-flex (false) |
direction | Direction (responsive) | 'inline' | Main flex direction (when before/after content is present) |
contentDirection | Direction (responsive) | 'rows' | Children wrapper direction |
beforeContentDirection | Direction (responsive) | 'inline' | beforeContent wrapper direction |
afterContentDirection | Direction (responsive) | 'inline' | afterContent wrapper direction |
gap | number | string (responsive) | — | Spacing between sections (applied as a margin on the before/after slots) |
alignX | AlignX (responsive) | 'left' | Horizontal alignment |
alignY | AlignY (responsive) | 'center' | Vertical alignment |
contentAlignX / contentAlignY | Alignment (responsive) | 'left' / 'center' | Children wrapper alignment |
beforeContentAlignX / beforeContentAlignY | Alignment (responsive) | 'left' / 'center' | beforeContent alignment |
afterContentAlignX / afterContentAlignY | Alignment (responsive) | 'left' / 'center' | afterContent alignment |
equalCols | boolean (responsive) | — | Equal width/height for all sections |
equalBeforeAfter | boolean | — | Set the before/after slots to the same size |
Direction Values
| Value | CSS |
|---|---|
'inline' | flex-direction: row |
'rows' | flex-direction: column |
'reverseInline' | flex-direction: row-reverse |
'reverseRows' | flex-direction: column-reverse |
Alignment Values
AlignX: 'left' | 'center' | 'right' | 'spaceBetween' | 'spaceAround' | 'block'
AlignY: 'top' | 'center' | 'bottom' | 'spaceBetween' | 'spaceAround' | 'block'
Styling Props
| Prop | Type | Description |
|---|---|---|
css | ExtendCss | Root element styles |
contentCss | ExtendCss | Children wrapper styles |
beforeContentCss | ExtendCss | beforeContent wrapper styles |
afterContentCss | ExtendCss | afterContent wrapper styles |
HTML Props
| Prop | Type | Description |
|---|---|---|
tag | string | HTML tag name |
innerRef | Ref | Alternative ref (avoids naming conflicts) |
dangerouslySetInnerHTML | { __html: string } | Raw HTML injection |
| All standard HTML attributes | — | onClick, className, style, aria-, data-, etc. |
Responsive Layout
// Stack on mobile, inline on desktop
<Element
direction={['rows', 'inline']}
alignX={['center', 'left']}
gap={[8, 16]}
>
{children}
</Element>
// Breakpoint object
<Element
gap={{ xs: 8, md: 12, lg: 16, xl: 20 }}
alignX={{ xs: 'center', md: 'left' }}
>
{children}
</Element>Special Handling
Void Elements
For self-closing tags (input, img, br, hr, embed, etc.), Element renders just the wrapper without content sections:
<Element tag="input" type="text" placeholder="Enter text" />
// Renders: <input type="text" placeholder="Enter text" />Inline Elements
When the tag is inline (span, a, button, label), content wrappers use <span> instead of <div> to maintain valid HTML nesting.
Browser Flexbox Fix
For <button>, <fieldset>, and <legend> (which don't natively support flex), Element applies a two-layer DOM structure to achieve proper flex behavior.
Development Attribute
In development builds the root element gets a data-vl-element="Element" attribute to make it easy to find in DevTools. It is not added in production.
equalBeforeAfter
Measures both side sections and sets them to the larger dimension. Useful for centered content with unequal side elements:
<Element
equalBeforeAfter
block
beforeContent={<BackButton />}
afterContent={<UserMenu />}
contentAlignX="center"
>
<Text tag="h1">Page Title</Text>
</Element>
// beforeContent and afterContent get the same width,
// so the title is centeredIt only runs when both beforeContent and afterContent are set. It clears any size it set earlier, reads offsetWidth/offsetHeight (height when direction="rows", width otherwise) on each slot, and sets both to Math.max(beforeSize, afterSize) via direct style manipulation. Because the old size is cleared first, the slots can shrink as well as grow. The initial measurement runs in useLayoutEffect when equalBeforeAfter, beforeContent, afterContent, or direction change. After that, a ResizeObserver re-balances the slots whenever the element resizes — so the values stay correct without forcing a layout flush on every re-render. Falls back to the layout-effect-only path when ResizeObserver is unavailable.
Ref Forwarding
Element accepts both ref (React 19 ref-as-prop) and the innerRef prop. When both are passed, ref wins:
const ref = useRef(null)
<Element ref={ref} tag="button">Click</Element>
// or
<Element innerRef={ref} tag="button">Click</Element>An internal ref is used for equalBeforeAfter measurement. It is merged with your ref through a callback ref. If you swap to a different ref object, the new ref receives the node and the old one is detached.
Reserved Props
These props are consumed by Element and not forwarded to the DOM:
innerRef, ref, tag, block, label, content, children, beforeContent,
afterContent, equalBeforeAfter, equalCols, gap, direction, alignX, alignY,
css, contentCss, beforeContentCss, afterContentCss, contentDirection,
contentAlignX, contentAlignY, beforeContentDirection, beforeContentAlignX,
beforeContentAlignY, afterContentDirection, afterContentAlignX,
afterContentAlignYAll other props (onClick, className, style, aria-, data-, dangerouslySetInnerHTML, etc.) are forwarded to the root DOM element. Element is wrapped in memo(), so it skips re-rendering when its props are referentially equal.
Static Properties
Element.displayName // '@vitus-labs/elements/Element'
Element.pkgName // '@vitus-labs/elements'
Element.VITUS_LABS__COMPONENT // '@vitus-labs/elements/Element'API Reference
Prop
Type
Last updated on