4.12React reference
Notes
Reference for <Recipe.Notes>, which renders a recipe's notes, each with its title, if it has one, as a heading.
<Recipe.Notes> renders the recipe’s notes, such as storage tips or how to make it ahead, each with its title, if it has one, as a heading. It must be inside <Recipe.Root>.
Notes
import { parseRecipe } from '@recipe-book/react/core'
import * as Recipe from '@recipe-book/react'
const result = parseRecipe({
name: 'Sandwich bread',
ingredients: [{ name: 'bread flour', quantity: '500 g' }],
steps: ['Knead the dough until smooth.'],
notes: [
'Keeps for 3 days in a bread box.',
{
title: 'Make ahead',
text: 'Shape the dough, cover it, and chill it overnight.',
},
],
})
if (!result.success) throw result.error
const recipe = result.data
export function BreadNotes() {
return (
<Recipe.Root recipe={recipe}>
{recipe.notes.length > 0 && <h2>Notes</h2>}
<Recipe.Notes />
</Recipe.Root>
)
}BreadNotes renders this HTML, indented here for reading:
<h2>Notes</h2>
<div data-recipe-notes="">
<div data-recipe-note=""><p>Keeps for 3 days in a bread box.</p></div>
<div data-recipe-note=""><h3>Make ahead</h3><p>Shape the dough, cover it, and chill it overnight.</p></div>
</div><Recipe.Notes> renders nothing when the recipe has no notes, so put your heading behind a check of recipe.notes.length. Notes don’t scale with servings, and they don’t convert units or temperatures either: a note such as “Rest it until it reaches 74°C” stays in Celsius on a page that shows US units, where a step’s temperature would convert.
A titled note is a <div> with its title as a heading, not a <section>. The heading carries the structure, and a <section> would change nothing: it’s a landmark only when it has an accessible name. To make the notes one region, put them in your own <section> named by your heading.
A note’s text is plain text, never HTML, so any markup in it shows as written. It keeps the line breaks it was written with, which HTML shows as spaces. To show them, set white-space on its paragraph:
[data-recipe-note] p {
white-space: pre-line;
}| Name | Type | Description |
|---|---|---|
headingLevel |
RecipeHeadingLevel | undefined |
The heading level of note titles in the default rendering. Defaults to 3, for notes under an <h2> as in the example. See Set heading levels. |
asChild |
true |
Renders the notes in their only child instead of a <div>. See Render your own element. Defaults to false. It needs exactly one element child. A value typed boolean, such as asChild={isLinked}, doesn’t type-check, so render the notes with and without asChild in separate branches. |
children |
((note: Note) => ReactNode) | ReactElement | undefined |
Renders each note instead of the default <div data-recipe-note>. The note is core’s Note. See Render your own notes. With asChild, the one element to render. |
Any other <div> prop, such as className or aria-label, passes to the outer <div>, and a ref reaches it. It renders its own content, so it doesn’t take dangerouslySetInnerHTML. Style it with className, or select its data-recipe-notes attribute, or, in the default rendering, each note’s data-recipe-note.
Render your own notes
Pass a function as children from a Client Component, as with <Recipe.Steps>. <Recipe.Notes> keys each result by the note’s id, so an element or fragment you return needs no key. Here, each note folds away:
'use client'
import type { ParsedRecipe } from '@recipe-book/react/core'
import * as Recipe from '@recipe-book/react'
export function Tips({ recipe }: { recipe: ParsedRecipe }) {
return (
<Recipe.Root recipe={recipe}>
<Recipe.Notes>
{(note) => (
<details>
<summary>{note.title ?? 'Tip'}</summary>
<p>{note.text}</p>
</details>
)}
</Recipe.Notes>
</Recipe.Root>
)
}For the recipe in the first example, Tips renders:
<div data-recipe-notes="">
<details><summary>Tip</summary><p>Keeps for 3 days in a bread box.</p></details>
<details><summary>Make ahead</summary><p>Shape the dough, cover it, and chill it overnight.</p></details>
</div>Render your own element
With asChild, <Recipe.Notes> renders its only child in place of the outer <div>, and passes data-recipe-notes, your other props, and its ref to it. Use it for a component that must hold the notes itself, such as an animated motion.div. Its props and ref merge with the child’s as asChild on the system toggle describes, along with what a function component needs on React 18. The child’s children replace the notes. A child whose children are undefined, null, true, or false gets the default notes, but any other children replace them, even '', [], or an empty Fragment, which render nothing:
import type { ParsedRecipe } from '@recipe-book/react/core'
import * as Recipe from '@recipe-book/react'
export function Sidebar({ recipe }: { recipe: ParsedRecipe }) {
return (
<Recipe.Root recipe={recipe}>
<Recipe.Notes asChild headingLevel={2}>
<aside className="notes" />
</Recipe.Notes>
</Recipe.Root>
)
}For the recipe in the first example, Sidebar renders:
<aside class="notes" data-recipe-notes="">
<div data-recipe-note=""><p>Keeps for 3 days in a bread box.</p></div>
<div data-recipe-note=""><h2>Make ahead</h2><p>Shape the dough, cover it, and chill it overnight.</p></div>
</aside>With anything but one element child, such as text, several children, a function, or a Fragment, <Recipe.Notes> renders its own <div> with those children, or with the default notes when they’re undefined, null, true, or false. In development, the first <Recipe.Notes> to do this logs a development warning.
Errors
Throws missing_parent when it isn’t inside <Recipe.Root>:
RecipeBookError: <Recipe.Notes> must be rendered inside <Recipe.Root>.
See https://recipe-book.siraj.page/errors/missing_parent/Types
@recipe-book/react exports the props type as RecipeNotesProps, to type a component that wraps the part:
type RecipeNotesProps = Omit<
ComponentPropsWithoutRef<'div'>,
'children' | 'dangerouslySetInnerHTML'
> & { headingLevel?: RecipeHeadingLevel | undefined } &
(
| {
asChild?: false | undefined
children?: ((note: Note) => ReactNode) | undefined
}
| { asChild: true; children: ReactElement }
)Don’t remove props from RecipeNotesProps with Omit, which flattens the union, so <Recipe.Notes {...props}> no longer type-checks.
Named imports
To import <Recipe.Notes> by name, import Notes. If the name clashes with one of yours, alias it, as in import { Notes as RecipeNotes } from '@recipe-book/react'.