Custom rendering
Every component accepts render. Pass a function to draw the root element with another component such as next/link while keeping ginga-ui's styles and behavior.
Basic usage
Pass render a function that receives the props needed for rendering and returns an element. The following example draws Anchor with the Next.js Link so that it navigates on the client side.
import { Anchor } from "@ginga-ui/core";
import NextLink from "next/link";
export function DocsLink() {
return (
<Anchor href="/docs" render={(props) => <NextLink {...props} />}>
Docs
</Anchor>
);
}A link that looks like a button works the same way. To use Button as a link, use <Anchor variant="button"> instead.
<Anchor
href="/signup"
variant="button"
render={(props) => <NextLink {...props} />}
>
Sign up
</Anchor>Requirements
- Render the same kind of element as the default, such as an
aelement forAnchoror abuttonelement forButton. A different kind of element breaks keyboard interaction and screen reader output - Pass all received props, including
ref, to the element. Dropping the ref breaks focus management and events - Return a single element, not a fragment
Merging your own props
The received props include ginga-ui's class names and data attributes. When adding your own class name, combine it instead of overwriting it.
<Box
render={({ className, ...props }) => (
<div {...props} className={`${className} my-layout`} />
)}
>
…
</Box>Reading state
Components built on React Aria Components pass state such as pressed or selected as the second argument. For components built on plain HTML elements, such as Box and Card, it is undefined.
<Button
render={(props, { isPressed }) => (
<button {...props} data-pressed-label={isPressed ? "pressed" : undefined} />
)}
>
Submit
</Button>Which element is replaced
render is passed to the element that receives className. These components made of several elements need attention.
| Component | Replaced element |
|---|---|
Anchor | The a element. Without href, render is not used and a span is drawn |
Modal | The inner modal. The backdrop overlay is not replaced |
Drawer / Popover | The outer element. Elements added inside are not replaced |
Table | The table element. The outer scroll container is not replaced |
AccordionTrigger | The inner button. The outer heading is not replaced |
ToastRegion | The region that lists toasts. Individual toasts are not replaced |
DialogTrigger / PopoverTrigger | They have no DOM element and do not accept render |