Skip to content

feat(ui): scale a recipe by servings when it declares them - #565

Merged
LeMyst merged 4 commits into
cooklang:mainfrom
LeMyst:feat/servings-scaling
Sep 28, 2026
Merged

LeMyst merged 4 commits into
cooklang:mainfrom
LeMyst:feat/servings-scaling

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #564

What changes

On a recipe that declares a whole number of servings, the recipe page's stepper counts servings instead of a raw multiplier:

  • It starts at the recipe's own servings (servings: 4 → Servings: 4), not at 1.
  • It goes from half a serving up, with no upper limit (the multiplier's max="200" is gone there).
  • The URL carries ?servings=6; the factor applied to quantities is 6 / 4 = 1.5.
  • The 👥 pill and the print header show the chosen servings (6 servings, 0.5 servings) instead of 1.5x.
  • Add to shopping list sends the derived factor, so the list matches the page.

When the page is scaled away from the recipe's own servings, a badge next to the servings pill says what it was written for (📖 Written for 4 servings). Its hint explains that quantities are scaled but cooking times, pan sizes and seasoning may need adjusting, and it links back to the original servings. Nothing shows at the recipe's own servings. Menus with servings get the same badge. A recipe or menu without servings, scaled by the multiplier, gets the same reminder as a factor (📖 ×1.5 of the original recipe), linking back to ×1; a recipe with no metadata at all gets a row of its own for it.

Recipes without servings, or whose servings is not a whole number (4-6, 1.5, 0: cooklang only reads a u32), keep today's Scale multiplier, unchanged.

Compatibility with ?scale=

?scale= keeps working on every recipe. On one that declares servings it applies the same factor and shows it as the servings it gives: the menu links from #561 (Easy Pancakes?scale=5) open at Servings: 10. When both are present, ?servings= wins. A ?servings= on a recipe without servings is ignored.

?servings= that is zero, negative or not finite falls back to the recipe's default (or to ?scale=). Any positive value is taken as is, like ?scale=: the half-a-serving floor belongs to the stepper (what people type), while a menu can link below it (menu at ×0.1 → 0.4 servings of a 4-serving recipe). As with #561's scale field, the stepper then takes that value as its minimum and step="any", and the −/+ buttons and +/-/[/] shortcuts keep #561's 0.5 grid.

Menus

  • A menu that declares servings gets the same Servings stepper (2 Day Plan, servings: 2 → Servings: 2, ?servings=4 doubles every reference). "Add all to shopping list" sends the factor. A menu without servings keeps the multiplier.
  • Each recipe link carries servings when the target recipe declares them, and the factor otherwise: @./Breakfast/Easy Pancakes{10%servings} (a 2-serving recipe) links to ?servings=10 rather than ?scale=5, and a recipe without servings keeps ?scale=. The ×factor badge next to the link is unchanged, and a ×1 reference still has a plain link. Every reference now looks up its recipe (memoised per page), since even a {} reference needs to know whether its target counts servings.

Implementation notes

  • resolve_servings (src/web/builders.rs) reads the recipe's servings before scaling, then applies one factor. util::parse_recipe_from_entry is split so the builder can parse without scaling first.
  • The pill is set from the stepper value rather than the scaled metadata: Recipe::scale rewrites servings rounded to a whole number, so half of a 4-serving recipe would have read 1.
  • The input's id is the query parameter's name (#scale / #servings); adjustScale finds either, and treats a missing max as unbounded.
  • The static export (cook build web) is unchanged (no stepper, plain .html links); the menu JSON API still reports factors; the CLI recipe.cook:N syntax stays a multiplier (out of scope, see feat(ui): scale a recipe by number of servings when it declares them #564).
  • New strings aria-decrease-servings / aria-increase-servings, recipe-written-for (plural), recipe-scaled-from-original and their hints in all 8 locales; the label reuses label-servings. The badges are .metadata-original (accent-tinted metadata-pill), with -servings / -scale variants. Tr::tf passes a factor to Fluent as $factor, formatted like the menu badges.

Tests

  • 14 unit tests in builders.rs (servings_tests): default, factor, the "written for" badge (absent at the original servings, singular, link back), the multiplier badge (with and without metadata, once, not on a recipe with servings), half a serving, no ceiling, below-the-floor values from menus, servings over scale, ?scale= links, unusable values, recipes without/with non-integer servings, menu stepper with and without servings, and menu links by servings or scale.
  • menu_api_test.rs: the HTML-vs-API agreement test reads both ?servings= and ?scale= links, checks which recipe gets which, and skips the scaled menu's link to itself.
  • recipe-scaling.spec.ts rewritten to cover both modes explicitly (it used to click the first recipe and silently pass when #scale was missing). Shopping-list calls are intercepted to check the factor sent.
  • menu-scale.spec.ts (feat(ui): open a menu's recipes at the scale the menu asks for #561) updated: menu links to Easy Pancakes and lamb-chops now carry ?servings=; the grid-step cases moved to a recipe without servings, plus one servings case; new cases for the menu's own servings stepper, its effect on links, and the factor sent by "add all to shopping list".
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo test: clean. Full Playwright chromium suite: 243 passed, 5 skipped, 0 failed.

🤖 Generated with Claude Code

LeMyst and others added 4 commits September 28, 2026 14:15
On a recipe with a whole number of `servings`, the recipe page's stepper
now counts servings: it starts at the recipe's own servings, goes from
half a serving up with no upper limit, and navigates to `?servings=N`.
The factor is `N / servings`. The servings pill and the print header
show the chosen servings, and "add to shopping list" sends the factor.

Recipes without servings, or with servings that are not a whole number
(`4-6`, `1.5`, `0`), keep the scale multiplier. `?scale=` still works
everywhere and, on a recipe with servings, shows as the servings it
gives, so menu links keep their meaning; `?servings=` wins over it.

The pill is set from the stepper rather than the scaled metadata:
`Recipe::scale` rounds `servings` to a whole number, so half of a
4-serving recipe would have read 1.

Closes cooklang#564

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A menu that declares `servings` now has the same servings stepper as a
recipe: it starts at the menu's servings, navigates to `?servings=N`,
and "add all to shopping list" sends the factor. A menu without
servings keeps the scale multiplier.

A menu links each recipe it references by servings when that recipe
declares them (`?servings=10` for `{10%servings}` of a 2-serving
recipe), and by its factor otherwise (`?scale=`), so the link opens the
stepper at the value it counts in. A x1 reference keeps a plain link.

The recipe page now takes any positive `?servings=`, as it does
`?scale=`: a menu at x0.1 links to 0.4 of a 4-serving recipe. The half
a serving floor stays the stepper's, for what people type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Scaling a recipe away from its own servings multiplies the quantities,
but cooking times, pan sizes and seasoning do not always follow. Next
to the servings pill, a recipe or menu scaled to other servings now
shows "Written for 4 servings", with a hint saying what may need
adjusting; it links back to the original servings. Nothing shows at
the recipe's own servings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A recipe or menu without servings, scaled away from x1, now gets the
same reminder as one scaled by servings: "x1.5 of the original recipe",
with the hint about times, pan sizes and seasoning, linking back to the
original. A recipe with no metadata at all gets a row for it, since the
metadata row is not rendered then.

`Tr::tf` passes a factor to Fluent as `$factor`, formatted like the menu
badges (`1.5`, `0.333`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(ui): scale a recipe by number of servings when it declares them

1 participant