API: utilities
Render utilities
The primitive contract's render, made public - React's useRender and mergeProps, Vue's renderPart, Angular's dataValue - to write parts of your own.
Every part follows the primitive contract: a default
element you can replace (render), your props merged into it, its state as data-* attributes.
The /render entry of each framework package makes that machinery public, for a part of your own
written the same way (Base UI's useRender and mergeProps). It references no component. The
composition guide shows it next to render on the library's own
parts.
Behaviour
- The element: the part's default tag, or the consumer's
render: an element its props merge into, or a function of the props and the state (React);asor therenderscoped slot (Vue); in Angular the directive sits on the consumer's own element, so there is nothing to render. - The merge: event handlers compose (the earlier first),
className/classjoin,stylemerges (the later wins), any other later prop wins; in React anundefinedleaves the earlier. - The state attributes: a boolean
truegives an empty attribute,false(ornull,undefined) none, never"false"; a string gives its value (part and state attributes).
Usage
import type { HTMLAttributes } from "react";
import { type RenderProp, type UseRenderProps, mergeProps, useRender } from "@fragiola/primitives-react/render";
interface BadgeState {
active: boolean;
}
interface BadgeProps extends HTMLAttributes<HTMLSpanElement> {
active?: boolean;
render?: RenderProp<BadgeState>;
}
function Badge({ active = false, render, ...props }: BadgeProps) {
// the part's own props: its handler runs first, then the consumer's
const own: UseRenderProps<BadgeState> = {
"data-primitives-part": "badge",
onClick: () => track("badge"),
};
return useRender({
render,
state: { active },
defaultTagName: "span",
props: mergeProps(own, props),
});
}
<Badge active>New</Badge>; // <span data-primitives-part="badge" data-active>New</span>
<Badge render={<a href="/new" />}>New</Badge>; // the same props on your <a>useRender merges className and style given as functions of the state too, and the part's
own ref (ref) with the consumer's (props.ref).
Props
useRender (React)
| option | type | default | description |
|---|---|---|---|
render | RenderProp<State> | the consumer's element, or (props, state) => ReactNode | |
props | UseRenderProps<State> | {} | the props to merge into the element; className and style may be functions of the state |
state | State | {} | its booleans and strings become data-* attributes; render receives it |
ref | Ref<HTMLElement> | the part's own ref, merged with the consumer's | |
defaultTagName | string | "div" | the element rendered without render |
mergeProps(...props) takes any number of props objects (null and undefined are skipped) and
returns them merged, left to right.
renderPart (Vue)
| argument | type | description |
|---|---|---|
tag | string | the default element |
context | { attrs, slots } | the component's setup context: the consumer's attributes, and its render and default slots |
as | string | Component | undefined | the as prop: another element or component |
state | State | given to the render slot and the default slot |
internal | Record<string, unknown> | the part's own props (ARIA, data-*, listeners) |
children | () => VNodeChild | the content, instead of the default slot |
asProp is the as prop's definition, to spread into props; stateAttributes(state) gives the
data-* attributes of a record of booleans and strings.
dataValue and dataValues (Angular)
| function | returns |
|---|---|
dataValue(value) | "" for true, the string, or null (no attribute) for false, null and undefined |
dataValues(state) | the same for every key of a record: [attr.data-open]="data().open" |
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/render | RenderProp, UseRenderOptions, UseRenderProps, mergeProps, useRender |
@fragiola/primitives-vue/render | AsProp, RenderSlotProps, asProp, renderPart, stateAttributes |
@fragiola/primitives-angular/render | dataValue, dataValues |