Components

Dynamic components

<component is> renders one of a set of tags or components the compiler knows, chosen by a condition or a prop. Each framework gets its own dynamic element.

Sometimes the element itself depends on the data: a heading whose level follows its depth in an outline, a section title that drops a level when it is nested, a card that collapses into a chip. <component is> renders one of a set of tags, or one of a set of components, chosen by a condition or by a prop.

Choosing a tag

is takes a conditional whose branches are tag names. The children apply to whichever tag renders, and each attribute to the tags that take it:

Section.uf.tsx
import { ref } from "@uxfront/unframework";

export default function Section({ title }: { title: string }) {
  const nested = ref(false);
  return (
    <article>
      <component is={nested.value ? "h3" : "h2"} class="title">
        {title}
      </component>
      <button type="button" onClick={() => (nested.value = !nested.value)}>
        {nested.value ? "Lift" : "Nest"}
      </button>
    </article>
  );
}
Section.tsx
import { type ComponentPropsWithRef, useRef, useState } from "react";

export type SectionAttributes = Omit<ComponentPropsWithRef<"article">, "title" | "children">;

export default function Section({
  title,
  className,
  style,
  ...attributes
}: { title: string } & SectionAttributes) {
  const [nested, setNested] = useState(false);
  const nestedRef = useRef(nested);
  const Tag = nested ? "h3" : "h2";

  return (
    <article className={className} style={style} {...attributes}>
      <Tag className="title">{title}</Tag>
      <button
        type="button"
        onClick={() => {
          nestedRef.current = !nestedRef.current;
          setNested(nestedRef.current);
        }}
      >
        {nested ? "Lift" : "Nest"}
      </button>
    </article>
  );
}
  • React, Qwik and Astro hold the chosen tag in a constant, const Tag = nested ? "h3" : "h2", and render <Tag>.
  • Vue writes <component :is>, Svelte <svelte:element this={…}>, and Solid <Dynamic component={…}>.
  • A strict Angular template can't choose an element by value, so Angular writes an @switch with one @case per tag, each with the attributes and children (dynamic-component, emulated).

An attribute that only some of the tags take, like href, which only <a> takes, renders only on those (As a component's root shows it). One that no tag takes is UF3006.

A prop typed as a union of tag names can choose the tag too, as a heading's level:

Heading.uf.tsx
export default function Heading({ tag, text }: { tag: "h1" | "h2" | "h3"; text: string }) {
  return (
    <component is={tag} class="heading">
      {text}
    </component>
  );
}

As a component's root

A <component is> over tags can be a component's root. It renders one element, so it takes a parent's class and style as a one-element root does (Fallthrough), merged into whichever tag renders, and so does the styled parent's scope attribute. This button renders a link when it has an href:

Base.uf.tsx
import { defineEmits } from "@uxfront/unframework";

// Its root is a `<component is>` over tags: a link with an `href`, and a button without one.
// `href` renders only on the link and `disabled` only on the button (ADR-0119).
export default function Base({
  href,
  disabled,
  text,
}: {
  href?: string;
  disabled?: boolean;
  text: string;
}) {
  const emit = defineEmits<{ press: [] }>();
  // The page handles a press, as a router would: the link does not navigate.
  function press(event: MouseEvent) {
    event.preventDefault();
    emit("press");
  }
  return (
    <component
      is={href ? "a" : "button"}
      href={href}
      disabled={disabled}
      class="base"
      style="padding: 4px 8px"
      onClick={press}
    >
      {text}
    </component>
  );
}

href renders only on the link, and disabled only on the button. An attribute that at least one tag takes is accepted, and renders only when a tag that takes it renders. One that no tag takes is still UF3006, reported once, naming every tag. A spread's keys go on every tag, so every tag must take them.

A parent's other attributes, its DOM listeners and its ref fall through to whichever tag renders too, where every tag takes them: A <component is> root shows them.

Base.tsx
import { cx } from "./_unframework/cx";
import { mergeRefs } from "./_unframework/mergeRefs";
import {
  type ComponentPropsWithoutRef,
  type DOMAttributes,
  type MouseEvent,
  type Ref,
  useMemo,
} from "react";

export interface BaseEvents {
  onPress?: () => void;
}

export type BaseAttributes = Omit<
  ComponentPropsWithoutRef<"a"> & ComponentPropsWithoutRef<"button">,
  | "href"
  | "disabled"
  | "text"
  | "onPress"
  | "children"
  | keyof DOMAttributes<HTMLAnchorElement | HTMLButtonElement>
> &
  Omit<
    DOMAttributes<HTMLAnchorElement | HTMLButtonElement>,
    "children" | "dangerouslySetInnerHTML" | "onPress"
  > & { ref?: Ref<HTMLAnchorElement | HTMLButtonElement> };

export default function Base({
  href,
  disabled,
  text,
  onPress,
  className,
  style,
  onClick,
  ref,
  ...attributes
}: { href?: string; disabled?: boolean; text: string } & BaseEvents & BaseAttributes) {
  function press(event: MouseEvent) {
    event.preventDefault();
    onPress?.();
  }

  const rootRef = useMemo(() => mergeRefs(ref), [ref]);

  const Tag = href ? "a" : "button";

  return (
    <Tag
      href={Tag === "a" ? href : undefined}
      disabled={Tag === "button" ? disabled : undefined}
      className={cx("base", className)}
      style={{ padding: "4px 8px", ...style }}
      onClick={(event) => {
        press(event);
        onClick?.(event);
      }}
      ref={rootRef}
      {...attributes}
    >
      {text}
    </Tag>
  );
}
  • React, Qwik and Astro guard an attribute on the constant, href={Tag === "a" ? href : undefined}, and Vue, Svelte and Solid on is itself. Each framework leaves out an attribute whose value is undefined.
  • Angular writes each @case with only the attributes its tag takes, and merges the class and style inputs into each one.
  • Qwik writes one element per tag where the <component is> has listeners, and merges the class and style into each.

A parent passes class and style as to any component, here through Primary, whose root is Base:

Primary.uf.tsx
import { defineEmits } from "@uxfront/unframework";

import Base from "./Base.uf.tsx";

// Its root is a component: a class passed to it passes on to whichever tag that component renders.
export default function Primary({
  href,
  disabled,
  text,
}: {
  href?: string;
  disabled?: boolean;
  text: string;
}) {
  const emit = defineEmits<{ press: [] }>();
  return (
    <Base
      href={href}
      disabled={disabled}
      text={text}
      class="primary"
      onPress={() => emit("press")}
    />
  );
}

The parent reads the tags from is when it is a literal, a conditional of literals or a prop typed as a union of tags. An is that reads a setup value, such as tag.value, gives the parent no tags to read, so a class passed to it is UF3045.

Choosing a component

The candidates can be components instead, imported or of the same module. Each one must declare every prop, model and slot you pass:

Tag.uf.tsx
import { ref } from "@uxfront/unframework";

import Card from "./Card.uf.tsx";
import Chip from "./Chip.uf.tsx";

export default function Tag({ label }: { label: string }) {
  const compact = ref(true);
  return (
    <section aria-label="Tag">
      <component is={compact.value ? Chip : Card} label={label} />
      <button type="button" onClick={() => (compact.value = !compact.value)}>
        {compact.value ? "Expand" : "Compact"}
      </button>
    </section>
  );
}

React, Qwik and Astro hold the chosen component in a constant, Vue writes <component :is="compact ? Chip : Card">, Svelte a $derived component, Solid <Dynamic>, and Angular an @switch with a @case per component. A prop or a fill that one of the candidates doesn't declare is UF3035 or UF3038, as on any component. Component candidates take class and style, but no other attribute or listener falls through them yet (UF1002).

A known set

is chooses from a set the compiler knows when it compiles: a conditional or a literal whose leaves are all tags or all components, or a prop typed as a union of tag literals. Angular writes every candidate, and the typed targets check each one, so a value only the run time knows, such as a string from an API, is UF3044, and so is a set that mixes tags and components. Write the candidates out in a conditional, or type the prop as a union of tags.

The decisions behind this page are ADR-0054, ADR-0055, ADR-0119 and ADR-0122.

Copyright © 2026