Guides
Composition
Put a part on your own element or component with render, merge your props and handlers into it, and write parts of your own with the render utilities.
A part renders a default element, and you can make it yours: another tag, your own component, or
your own markup with the part's props spread on it. The props merge the same way everywhere: your
handlers run after the part's, classes join, and the part keeps its ARIA and data-* attributes.
The same machinery is public in each framework's /render entry, for parts of your own. What
stays yours: the elements, the components you compose with, and their look.
Another element: render
render takes an element, into which the part's props merge, or a function of the props and the
state.
import { Menu } from "@fragiola/primitives-react/menu";
import { Link } from "./router.tsx";
<Menu.Item render={<Link to="/profile" />}>Profile</Menu.Item>;
<Tabs.Tab
value="account"
render={(props, state) => (
<button {...props} aria-describedby={state.selected ? "account-hint" : undefined} />
)}
>
Account
</Tabs.Tab>;Your element's own props win over the part's, but the handlers compose: the part's run first,
then yours. className and style may be functions of the state:
className={(state) => (state.open ? "open" : undefined)}.
A part's element can change, but not its contract: a Trigger you render as an <a> still needs
to be something a keyboard can reach and press. Keep the role the part expects (a button for a
trigger, an input for a combobox's Input) unless its page says otherwise.
Parts of your own: the render utilities
For a part of your own that should behave as the library's (the same render, the same merge,
the same data-* attributes), each framework's /render entry exports the pieces the library
uses (Render utilities).
useRender renders your part's default element, or the consumer's render, with the props
merged in and the state as data-* attributes; mergeProps merges props as every part does.
import { type RenderProp, type UseRenderProps, mergeProps, useRender } from "@fragiola/primitives-react/render";
interface ToolbarButtonState {
pressed: boolean;
}
interface ToolbarButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
pressed?: boolean;
render?: RenderProp<ToolbarButtonState>;
}
export function ToolbarButton({ pressed = false, render, ...props }: ToolbarButtonProps) {
const own: UseRenderProps<ToolbarButtonState> = {
type: "button",
"aria-pressed": pressed,
"data-primitives-part": "toolbar-button",
};
return useRender({
render,
state: { pressed },
defaultTagName: "button",
props: mergeProps(own, props),
});
}
// <button type="button" aria-pressed="true" data-primitives-part="toolbar-button" data-pressed>
<ToolbarButton pressed>Bold</ToolbarButton>;Name your part's attribute after your own component (data-primitives-part="toolbar-button" is
an example): the library's selectors and recipes only read its own parts' names.