Blocks Design System

Nutrition label

A granola product page: a blurb built from Heading, Badge, Body and Text, beside a nutrition facts panel with the printed label’s heavy rules, bold hierarchy and % Daily Value column. It is a Server Component with no client JavaScript.

Tidewater Pantry · Granola

Oat clusters, maple & sea salt

Whole grainVegan6g protein

Slow-baked rolled oats and almonds, glazed with dark maple syrup and finished with flaky sea salt. Big, crunchy clusters that hold up in cold milk.

$6.49 · 15.5 oz (440g)

Ingredients: whole grain rolled oats, almonds, maple syrup, cane sugar, sunflower oil, brown rice flour, sea salt, cinnamon. Contains: almonds.

Nutrition Facts

8 servings per container

Serving size⅔ cup (55g)

Amount per servingCalories240

Nutrients per serving
Nutrient and amount% Daily Value*
Total Fat 9g12%
Saturated Fat 1.5g8%
Trans Fat 0g
Cholesterol 0mg0%
Sodium 140mg6%
Total Carbohydrate 36g13%
Dietary Fiber 4g14%
Total Sugars 11g
Includes 9g Added Sugars 18%
Protein 6g
Vitamin D 0mcg0%
Calcium 30mg2%
Iron 1.8mg10%
Potassium 210mg4%

* The % Daily Value (DV) tells you how much a nutrient in a serving of food contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.

Source · recipes/nutrition-label.tsx
recipes/nutrition-label.tsx
import { Badge, Body, BodySmall, Heading, Stack, Text } from '@blocks/design-system'
import styles from './nutrition-label.module.css'

interface Nutrient {
  name: string
  amount: string
  dailyValue?: string
  /** Sub-nutrients sit indented under their parent, in regular weight. */
  indent?: 1 | 2
}

const macronutrients: Nutrient[] = [
  { name: 'Total Fat', amount: '9g', dailyValue: '12%' },
  { name: 'Saturated Fat', amount: '1.5g', dailyValue: '8%', indent: 1 },
  { name: 'Trans Fat', amount: '0g', indent: 1 },
  { name: 'Cholesterol', amount: '0mg', dailyValue: '0%' },
  { name: 'Sodium', amount: '140mg', dailyValue: '6%' },
  { name: 'Total Carbohydrate', amount: '36g', dailyValue: '13%' },
  { name: 'Dietary Fiber', amount: '4g', dailyValue: '14%', indent: 1 },
  { name: 'Total Sugars', amount: '11g', indent: 1 },
  { name: 'Includes 9g Added Sugars', amount: '', dailyValue: '18%', indent: 2 },
  { name: 'Protein', amount: '6g' },
]

const micronutrients: Nutrient[] = [
  { name: 'Vitamin D', amount: '0mcg', dailyValue: '0%' },
  { name: 'Calcium', amount: '30mg', dailyValue: '2%' },
  { name: 'Iron', amount: '1.8mg', dailyValue: '10%' },
  { name: 'Potassium', amount: '210mg', dailyValue: '4%' },
]

function NutrientRows({ nutrients }: { nutrients: Nutrient[] }) {
  return nutrients.map((nutrient) => (
    <tr key={nutrient.name} data-indent={nutrient.indent}>
      <th scope="row">
        <span className={styles.nutrient}>{nutrient.name}</span> {nutrient.amount}
      </th>
      <td>{nutrient.dailyValue}</td>
    </tr>
  ))
}

// A Server Component: a product blurb beside a nutrition facts panel. The
// panel is semantic markup (a heading, a serving summary and a table) drawn in
// one ink, the text token.
export default function NutritionLabel() {
  return (
    <article aria-labelledby="nutrition-label-product" className={styles.product}>
      <div className={styles.layout}>
        <Stack gap="md" dangerouslySetClassName={styles.blurb}>
          <Text fontSize="xs" fontWeight="semibold" color="text-muted">
            Tidewater Pantry · Granola
          </Text>
          <Heading as="h3" id="nutrition-label-product" fontSize="xl" dangerouslySetClassName={styles.balance}>
            Oat clusters, maple & sea salt
          </Heading>
          <Stack direction="row" gap="xs" wrap="wrap">
            <Badge>Whole grain</Badge>
            <Badge>Vegan</Badge>
            <Badge>6g protein</Badge>
          </Stack>
          <Body margin="none">
            Slow-baked rolled oats and almonds, glazed with dark maple syrup and finished with flaky sea salt. Big,
            crunchy clusters that hold up in cold milk.
          </Body>
          <Text fontSize="lg" fontWeight="semibold">
            $6.49 <Text fontSize="sm" color="text-muted">· 15.5 oz (440g)</Text>
          </Text>
          <BodySmall margin="none" color="text-muted">
            <strong>Ingredients:</strong> whole grain rolled oats, almonds, maple syrup, cane sugar, sunflower oil, brown
            rice flour, sea salt, cinnamon. <strong>Contains:</strong> almonds.
          </BodySmall>
        </Stack>

        {/* Printed labels are black on white, so this one keeps the light theme in dark mode. */}
        <section aria-labelledby="nutrition-label-title" data-ds-theme="light" className={styles.label}>
          <h4 id="nutrition-label-title" className={styles.title}>
            Nutrition Facts
          </h4>
          <p className={styles.servings}>8 servings per container</p>
          <p className={styles.servingSize}>
            <span>Serving size</span>
            <span>⅔ cup (55g)</span>
          </p>
          <p className={styles.calories}>
            <span className={styles.perServing}>Amount per serving</span>
            <span>Calories</span>
            <span className={styles.calorieCount}>240</span>
          </p>
          <table className={styles.table}>
            <caption className={styles.visuallyHidden}>Nutrients per serving</caption>
            <thead>
              <tr>
                <th scope="col">
                  <span className={styles.visuallyHidden}>Nutrient and amount</span>
                </th>
                <th scope="col">% Daily Value*</th>
              </tr>
            </thead>
            <tbody>
              <NutrientRows nutrients={macronutrients} />
            </tbody>
            <tbody className={styles.micros}>
              <NutrientRows nutrients={micronutrients} />
            </tbody>
          </table>
          <p className={styles.footnote}>
            * The % Daily Value (DV) tells you how much a nutrient in a serving of food contributes to a daily diet. 2,000
            calories a day is used for general nutrition advice.
          </p>
        </section>
      </div>
    </article>
  )
}
Oat clusters, maple & sea salt

How it works

  • The panel is a section with its own heading, the serving summary as paragraphs, and the nutrients as a table: each row header holds the nutrient and its amount, and the column header names the % Daily Value. Hidden text gives the first column and the table a name for screen readers.
  • One ink does everything: --ds-color-text for type and rules on --ds-color-bg. Printed labels are black on white, so the panel sets data-ds-theme="light" and keeps the light tokens in a dark page. The three rule weights are custom properties computed from border and space tokens.
  • Indent levels are data (indent: 1 | 2) rendered as data-indent, which sets both the indent and the regular weight of sub-nutrients.
  • A container query puts the panel beside the blurb when the article is wide and under it when it isn’t. The panel is a container too: in a narrow column its rows step down a size so no line wraps.

App-owned CSS

recipes/nutrition-label.module.css
.product {
  container-type: inline-size;
}

/* The blurb and panel stack in a narrow column and sit side by side in a wide one. */
.layout {
  --label-width: calc(var(--ds-space-xxxl) * 2.375);

  display: grid;
  gap: var(--ds-space-xl);
}

@container (min-width: 34rem) {
  .layout {
    grid-template-columns: minmax(0, 1fr) var(--label-width);
  }
}

.blurb {
  align-self: start;
}

.balance {
  text-wrap: balance;
}

/* The panel: one ink (the text token) on the page background, rules of three weights. */
.label {
  --ink: var(--ds-color-text);
  --rule-thin: var(--ds-border-width-sm);
  --rule-medium: calc(var(--ds-border-width-lg) + var(--ds-border-width-sm));
  --rule-heavy: calc(var(--ds-space-md) + var(--ds-space-xs));

  container-type: inline-size;
  justify-self: start;
  width: 100%;
  max-width: var(--label-width);
  padding: var(--ds-space-sm) var(--ds-space-md);
  font-family: var(--ds-font-body);
  font-size: var(--ds-font-size-sm);
  line-height: var(--ds-line-height-heading);
  color: var(--ink);
  background-color: var(--ds-color-bg);
  border: var(--ds-border-width-sm) solid var(--ink);

  & p {
    margin: 0;
  }
}

.title {
  margin: 0;
  font-size: var(--ds-font-size-xxl);
  font-weight: var(--ds-font-weight-bold);
  line-height: var(--ds-line-height-heading);
  border-bottom: var(--rule-thin) solid var(--ink);
  padding-bottom: var(--ds-space-xs);
}

.servings {
  padding-top: var(--ds-space-xs);
}

.servingSize {
  display: flex;
  justify-content: space-between;
  gap: var(--ds-space-md);
  padding-bottom: var(--ds-space-sm);
  font-size: var(--ds-font-size-md);
  font-weight: var(--ds-font-weight-bold);
  border-bottom: var(--rule-heavy) solid var(--ink);
}

.calories {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  padding-block: var(--ds-space-xs);
  font-size: var(--ds-font-size-xl);
  font-weight: var(--ds-font-weight-bold);
  border-bottom: var(--rule-medium) solid var(--ink);
}

.perServing {
  grid-column: 1 / -1;
  font-size: var(--ds-font-size-xs);
}

.calorieCount {
  font-size: var(--ds-font-size-xxl);
  line-height: var(--ds-line-height-heading);
}

.table {
  width: 100%;
  border-collapse: collapse;

  & th,
  & td {
    padding: var(--ds-space-xs) 0;
    white-space: nowrap;
    font-weight: var(--ds-font-weight-body);
    text-align: left;
    border-top: var(--rule-thin) solid var(--ink);
  }

  & td {
    font-weight: var(--ds-font-weight-bold);
    text-align: right;
    font-variant-numeric: tabular-nums;
  }

  & thead th {
    border-top: none;
    font-size: var(--ds-font-size-xs);
    font-weight: var(--ds-font-weight-bold);
    text-align: right;
  }

  & tr[data-indent="1"] th {
    padding-left: var(--ds-space-lg);
  }

  & tr[data-indent="2"] th {
    padding-left: calc(var(--ds-space-lg) + var(--ds-space-md));
  }

  /* Top-level nutrient names are bold; sub-nutrients are regular weight. */
  & tr:not([data-indent]) .nutrient {
    font-weight: var(--ds-font-weight-bold);
  }

  /* Total Sugars carries a short rule that stops at the indent, as on the printed label. */
  & tr[data-indent="2"] > * {
    border-top-color: transparent;
    background-image: linear-gradient(var(--ink), var(--ink));
    background-repeat: no-repeat;
    background-size: 100% var(--rule-thin);
  }

  & tr[data-indent="2"] th {
    background-position: calc(var(--ds-space-lg) + var(--ds-space-md)) 0;
  }

  & tbody:first-of-type tr:last-child > * {
    border-bottom: var(--rule-heavy) solid var(--ink);
  }
}

/* In a narrow column the rows step down a size so no line wraps. */
@container (max-width: 18.75rem) {
  .table {
    font-size: var(--ds-font-size-xs);
  }
}

.micros .nutrient {
  font-weight: var(--ds-font-weight-body);
}

.footnote {
  margin-top: var(--ds-space-xs);
  padding-top: var(--ds-space-xs);
  font-size: var(--ds-font-size-xs);
  line-height: var(--ds-line-height-heading);
  border-top: var(--rule-medium) solid var(--ink);
}

.visuallyHidden {
  position: absolute;
  width: var(--ds-space-xxs);
  height: var(--ds-space-xxs);
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}