Dynamic components
Sometimes the data decides the element: a heading whose level follows its depth in an outline, a section title that drops a level when it's 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:
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>
);
}
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
@switchwith one@caseper tag, each with the attributes and children (dynamic-component, emulated). - A default slot in the children renders in whichever tag renders, under a conditional, a teleport, a transition or another slot's fallback too, and in both branches of a conditional. Angular projects a parent's content into one
<ng-content>only, so it declares the slot once, in an<ng-template>before the@switch, and each@caserenders it withngTemplateOutlet. A fallback that reads a name the conditional narrows stays in place, in an<ng-template>that the place passes on. Under a list, the slot renders in every row on Angular too: the compiler marks itrepeated, so Angular writes it as a directive and each row renders the parent's template (ADR-0126).
An attribute that only some of the tags take renders only on those. Only <a> takes href, for example (As a component's root shows it). An attribute 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:
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). They merge into whichever tag renders, and so does the styled parent's scope attribute. This button renders a link when it has an href:
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.
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}. Vue, Svelte and Solid guard it onis. Each framework leaves out an attribute whose value isundefined. - Angular writes each
@casewith only the attributes its tag takes, and merges theclassandstyleinputs into each one. - Qwik writes one element per tag where the
<component is>has listeners, and merges theclassandstyleinto each.
A parent passes class and style as to any component, here through Primary, whose root is Base:
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's 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:
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. No other attribute or listener falls through them yet (UF1002).
On Qwik, a component whose default slot is repeated, like one that renders it in each row of a list, takes its fill as the render prop default$. One that renders it once projects the children (Slots). Where the candidates take the default fill differently, Qwik writes each candidate as a separate tag, with only the fill it takes. So default$ never reaches a component that doesn't declare it:
{Tag === Rows ? (
<Rows items={["one", "two"]} default$={() => <em>fill</em>} />
) : (
<Plain items={["one", "two"]}>
<em>fill</em>
</Plain>
)}
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, ADR-0122, ADR-0124 and ADR-0125.
Context
provide and inject share a value with every descendant under a key. Each framework gets its own context, from React's createContext to Angular's InjectionToken.
Styles
A component's styles are a plain CSS file beside it. The compiler scopes the sheet once, with Vue's algorithm, and every framework attaches the same sheet.