Skip to content
Playground

Polymorphic components

Build semantic interfaces with Zenos components instead of recreating layout and interaction from raw HTML.

Zenos components separate an element’s meaning from its presentation. Start with the component that owns the layout, typography, surface, or behavior you need, then select the correct semantic element through as or compose behavior through render. This keeps product code aligned with Zenos without reducing the page to anonymous div and span wrappers.

Component-first markup

Build semantics into the layout

Zenos controls presentation while the as prop controls document meaning.

Recommended
  1. 01

    Use the semantic element

    Choose article, section, nav, or a real list.

  2. 02

    Keep layout declarative

    Let VStack, HStack, Cluster, and Grid own flow.

  3. 03

    Preserve native behavior

    Use Button and Link for actions and navigation.

Semantic HTML, without rebuilding the design system.

Choose an API

A polymorphic component keeps its Zenos API and styling while changing the element rendered to the DOM:

TSX
<Box as="article" elevation="container">  <Text as="h2" type="heading" size="h4">    Release notes  </Text></Box>

Box still owns the surface. article supplies document meaning. Text still owns typography, while h2 supplies the heading level. TypeScript follows the selected element, so Box as="a" accepts href and its ref resolves to an anchor.

The goal is not to hide HTML. The goal is to choose HTML semantics through a consistent Zenos component instead of rebuilding layout, tokens, states, and accessibility for every screen.

Choose the component before choosing the element. Ask what relationship the wrapper owns:

  1. Vertical flow — use VStack when children always run from top to bottom.
  2. Horizontal flow — use HStack when children stay in one row.
  3. Responsive direction — use Stack when the axis itself changes by breakpoint.
  4. Wrapping row — use Cluster for actions, tags, filters, and metadata that may wrap.
  5. Shared tracks — use Grid for rows and columns that must align.
  6. Width and region — use Container for measure and Section for a semantic region.
  7. Surface only — use Box when you need an element, elevation, or customization hook but no child-layout rule.
  8. Typography — use Text, then select the correct heading, paragraph, label-like, or inline element with as.
ComponentDefault relationshipUse it forPrefer another component when
VStackFixed vertical flex flowForms, card content, page sections, menus, and semantic vertical listsThe direction must become horizontal at a breakpoint.
HStackFixed horizontal flex flow without wrappingCompact metadata, icon-label pairs, and controls that must remain togetherItems should wrap when space runs out.
StackConfigurable flex direction; vertical by defaultResponsive or application-controlled direction, scrolling, and dimensional constraintsThe direction is permanently vertical or horizontal and a named primitive is clearer.
ClusterHorizontal flow that wraps by defaultToolbars, actions, tags, filters, and variable-width groupsItems must share grid tracks or must never wrap.

VStack and HStack expose intent directly. They also provide axis-specific aliases: VStack accepts hAlign and vAlign, while HStack accepts hAlign and vAlign. The shared align and justify props remain available when flex terminology is more useful.

TSX
<VStack as="section" gap="6" hAlign="stretch" aria-labelledby="profile-heading">  <Text id="profile-heading" as="h2" type="heading" size="h4">    Profile  </Text>   <HStack as="footer" gap="2" hAlign="end">    <Button>Cancel</Button>    <Button variant="primary">Save profile</Button>  </HStack></VStack>

Use Stack when direction is genuinely responsive:

TSX
<Stack direction={{ base: "column", md: "row" }} gap="4" align={{ md: "center" }}>  <ProjectSummary />  <ProjectActions /></Stack>

Use Cluster instead when a row should wrap naturally rather than switch to a predefined axis:

TSX
<Cluster as="nav" gap="2" aria-label="Project filters">  {filters.map((filter) => (    <Button key={filter.value} size="sm">      {filter.label}    </Button>  ))}</Cluster>

Zenos uses two composition APIs for different responsibilities.

Use as when a Zenos primitive owns presentation and you need to select its semantic element.

TSX
<Section as="section" aria-labelledby="billing-heading" padding="6">  <Stack as="header" gap="1">    <Text id="billing-heading" as="h2" type="heading" size="h4">      Billing    </Text>    <Text>Manage invoices and payment methods.</Text>  </Stack></Section>

Common polymorphic primitives include Box, Text, Section, VStack, HStack, Stack, Cluster, Grid, GridItem, GridSpan, Center, Container, AspectRatio, and FormLayout. Product-frame parts such as Layout.Content and SideNav also preserve their semantic defaults while accepting as when the application needs a different element.

Keep the default when it already matches the content. Add as only when the document meaning must change.

ComponentDefault elementResponsibilityCommon as values
BoxdivSurface, elevation, and a stable customization boundaryarticle, aside, a, li, span
Textp, or the element implied by its typography roleType scale, weight, color, alignment, and truncationh1–h6, span, strong, code
SectionsectionSemantic region with vertical flow and responsive paddingarticle, aside, div
VStackdivFixed vertical flow and vertical rhythmsection, header, footer, ul, ol, form
HStackdivFixed horizontal flow that stays togetherheader, footer, nav, li
StackdivResponsive direction, wrapping, dimensions, and scrollingAny structural element whose direction can change
ClusterdivWrapping horizontal groupsnav, header, footer, ul
GriddivResponsive shared tracksul, ol, section
CenterdivCentering on one or both axessection, main, aside
ContainerdivMaximum width and inline guttersmain, section, article
FormLayoutdivResponsive field layoutform, fieldset
AspectRatiodivStable media geometryfigure, a

Layout.Header, Layout.Content, Layout.Footer, and Layout.Panel already default to header, main, footer, and aside. Prefer those defaults unless the region has a different role in the page.

as accepts a React element type, not only an HTML tag. This is useful for router links or application-owned semantic wrappers:

TSX
<Box  as={RouterLink}  to="/projects/zenos"  elevation="raised"  className="rounded-lg p-4 focus-visible:ring-2 focus-visible:ring-ring">  <Text as="span" weight="medium">    Open Zenos project  </Text></Box>

The custom component must forward its ref and merge className, native accessibility attributes, and event handlers onto the final DOM element. If it drops those props, Zenos styling or behavior will also be dropped.

Base UI-backed components, including Link, use render when behavior or link styling must be applied to another component without adding a wrapper element.

TSX
<Popover.Root>  <Popover.Trigger render={<Button variant="primary" />}>Open filters</Popover.Trigger>  <Popover.Content>...</Popover.Content></Popover.Root>

The trigger remains one native button. Popover supplies focus, keyboard, and open-state behavior; Button supplies the Zenos appearance. Do not nest a Button inside a trigger that already renders a button, because nested interactive elements are invalid.

The same rule applies to field controls:

TSX
<Field.Root>  <Field.Label>Email</Field.Label>  <Field.Control render={<Input type="email" autoComplete="email" />} /></Field.Root>

Link uses render when a router link should keep Link’s color, size, underline, and focus ring:

TSX
<Link render={<RouterLink to="/workspaces/zenos" />}>View workspace</Link>

RouterLink must forward className, event handlers, and its ref to the underlying anchor.

Use the dedicated component when one exists

Section titled “Use the dedicated component when one exists”

Use Button, Link, Input, Checkbox, Table, and other purpose-built components directly. They already provide the correct native element, states, tokens, focus treatment, and public API. Polymorphism is an escape hatch for semantics, not a reason to replace a focused component with a generic Box.

Instead of rebuildingStart withWhy
<div className="flex flex-col gap-4"><VStack gap="4">VStack states that the flow is intentionally vertical.
<div className="flex items-center"><HStack align="center">HStack keeps a compact row together without wrapping.
<div className="flex flex-wrap"><Cluster align="center">Cluster owns a row that can wrap as space changes.
<div className="grid ..."><Grid columns={...} gap="4">Grid owns tracks and responsive columns.
<div className="max-w-... mx-auto"><Container size="lg">Container owns measure and gutters.
<section className="py-..."><Section as="section" paddingY="8">Section combines semantic region and token-based spacing.
<div className="rounded ..."><Box elevation="container">Box owns a semantic surface and customization hook.
<h2 className="..."><Text as="h2" type="heading" size="h4">Heading level and visual scale stay independent.
<p className="..."><Text color="subtle">Text owns typography and semantic color.
<a className="..."><Link href="...">Link preserves navigation, focus, and link styling.
<button className="..."><Button type="button">Button preserves keyboard behavior and interaction states.

These are starting points, not mechanical replacements. Choose the component by responsibility, then choose as by meaning.

Layout primitives can render list elements while keeping spacing out of individual items:

TSX
<VStack as="ul" gap="3" className="m-0 list-none p-0">  {projects.map((project) => (    <Box as="li" key={project.id} elevation="container" className="rounded-lg p-4">      <Text as="h3" type="heading" size="h6">        {project.name}      </Text>      <Text color="subtle">{project.description}</Text>    </Box>  ))}</VStack>

The DOM remains a real list. VStack owns the gap, each Box owns its item surface, and Text owns the content hierarchy.

The same model scales from a small list to a product page. Each component should own one structural responsibility:

TSX
<Layout>  <Layout.Header>    <Header>...</Header>  </Layout.Header>   <Layout.Content>    <Section as="section" padding={{ base: "4", lg: "8" }}>      <Container size="xl">        <VStack gap={{ base: "6", lg: "8" }}>          <Cluster as="header" align="center" justify="between" gap="4">            <VStack gap="1">              <Text as="h1" type="heading" size="h2">                Projects              </Text>              <Text color="subtle">Manage work across your organization.</Text>            </VStack>            <Button variant="primary">New project</Button>          </Cluster>           <Grid as="ul" columns={{ base: 1, md: 2, xl: 3 }} gap="4" className="list-none p-0">            {projects.map((project) => (              <Box as="li" key={project.id} elevation="container">                <ProjectCard project={project} />              </Box>            ))}          </Grid>        </VStack>      </Container>    </Section>  </Layout.Content></Layout>

In this composition, Layout owns the product frame, Section owns the page region, Container owns the maximum width, VStack owns vertical rhythm, Cluster owns the wrapping heading row, and Grid owns the repeated tracks. The rendered section, header, h1, ul, and li retain the document structure a browser and assistive technology expect.

Do not choose an element because of its browser default style. Choose it for meaning, then express the visual hierarchy with component props.

TSX
<Text as="h2" type="heading" size="h4">  Workspace members</Text>

Here h2 maintains document structure while size="h4" selects the visual scale. Keep heading levels sequential even when the design calls for a smaller heading.

Likewise, do not add onClick to a Box, Stack, or Text to make it interactive. Use a native interactive component:

TSX
<Button type="button" onClick={openWorkspace}>  Open workspace</Button> <Link href="/workspaces/zenos">View workspace</Link>

If an entire custom surface navigates, Box as="a" is valid when you also provide href, a visible focus treatment, and a clear accessible name. Prefer Link when the standard inline-link treatment is sufficient.

Native HTML is still appropriate when no Zenos component owns the responsibility and adding a primitive would make the code less clear—for example time, figure, figcaption, dl, or specialized document content. Use the native element rather than forcing an unrelated component to fit.

The practical rule is:

  1. Use a dedicated Zenos component for known controls and patterns.
  2. Use a polymorphic layout or Text primitive when Zenos owns the presentation.
  3. Use native HTML when it is the clearest semantic element and Zenos adds no useful behavior or design contract.
  • Choose semantics before styling: landmark, heading, list, link, button, then visual treatment.
  • Use as only to change the rendered element; do not use it to imitate another component’s behavior.
  • Use render when a Base UI part must share one DOM element with a Zenos control.
  • Never nest interactive elements such as a button inside another button or an anchor inside an anchor.
  • Forward native attributes such as href, type, aria-labelledby, disabled, and required to the element that owns them.
  • Let one layout parent own the gap between siblings. Avoid mixing Stack gap with child margins for the same relationship.
  • Keep className for application-specific decoration; prefer semantic component props for layout, spacing, state, and variants.
  • Preserve visible focus, keyboard behavior, and correct heading order after changing as or composing with render.
  • Use logical alignment and spacing so the composition continues to work in RTL.
  • Composition Guide explains how layout primitives share structural responsibilities.
  • Box covers the lowest-level semantic surface.
  • Text separates typography role, visual scale, and rendered element.
  • Direction explains direction-aware application structure.