Blocks Design System

Weather week

A summit forecast: current conditions in a hero, a seven-day strip whose high–low bars share one temperature scale, and the next 24 hours as a small chart for temperature, wind or snow, switched with Tabs. Heading, Badge, Text and Stack compose the rest, all from a Server Component.

Cinder Peak summit

Wind advisory

8,240 ft · Updated 6:00 AM, Monday, December 7

−6°C

Light snow
Feels like
−13 °C
Wind
W 32 km/h
gusts 55
New snow
14 cm
last 24 h
Snow depth
212 cm

7-day forecast

  1. Today, Dec 7High −4°Low −9°
  2. Tue, Dec 8High −6°Low −12°
  3. Wed, Dec 9High −8°Low −15°
  4. Thu, Dec 10High −5°Low −14°
  5. Fri, Dec 11High −1°Low −9°
  6. Sat, Dec 12High −3°Low −8°
  7. Sun, Dec 13High −7°Low −13°

Next 24 hours

Temp by hour, °C
6 AM9 AM12 PM3 PM6 PM9 PM12 AM3 AM
−6°−5°−3°−4°−6°−8°−9°−10°
Source · recipes/weather-week.tsx
recipes/weather-week.tsx
import { Badge, BodySmall, Heading, Stack, Tabs, TabsList, TabsPanel, TabsTab, Text } from '@blocks/design-system'
import { Cloud, CloudSnow, CloudSun, type LucideIcon, Snowflake, Sun, Wind } from 'lucide-react'
import type { CSSProperties } from 'react'
import styles from './weather-week.module.css'

const conditions = {
  snow: { label: 'Snow showers', icon: CloudSnow },
  heavySnow: { label: 'Heavy snow', icon: Snowflake },
  cloudy: { label: 'Cloudy', icon: Cloud },
  sunny: { label: 'Sunny', icon: Sun },
  partlySunny: { label: 'Partly sunny', icon: CloudSun },
  windy: { label: 'Windy', icon: Wind },
} satisfies Record<string, { label: string; icon: LucideIcon }>

interface Day {
  day: string
  date: string
  condition: keyof typeof conditions
  high: number
  low: number
}

const week: Day[] = [
  { day: 'Today', date: 'Dec 7', condition: 'snow', high: -4, low: -9 },
  { day: 'Tue', date: 'Dec 8', condition: 'heavySnow', high: -6, low: -12 },
  { day: 'Wed', date: 'Dec 9', condition: 'cloudy', high: -8, low: -15 },
  { day: 'Thu', date: 'Dec 10', condition: 'sunny', high: -5, low: -14 },
  { day: 'Fri', date: 'Dec 11', condition: 'partlySunny', high: -1, low: -9 },
  { day: 'Sat', date: 'Dec 12', condition: 'windy', high: -3, low: -8 },
  { day: 'Sun', date: 'Dec 13', condition: 'snow', high: -7, low: -13 },
]

// Every range bar sits on the same scale, so the days compare at a glance.
const coldest = Math.min(...week.map((day) => day.low))
const warmest = Math.max(...week.map((day) => day.high))
const span = warmest - coldest

const hours = ['6 AM', '9 AM', '12 PM', '3 PM', '6 PM', '9 PM', '12 AM', '3 AM']

const hourly = [
  { id: 'temp', label: 'Temp', unit: '°C', kind: 'line', values: [-6, -5, -3, -4, -6, -8, -9, -10] },
  { id: 'wind', label: 'Wind', unit: 'km/h', kind: 'line', values: [32, 38, 45, 52, 48, 40, 35, 30] },
  { id: 'snow', label: 'Snow', unit: 'cm', kind: 'bars', values: [1.2, 1.8, 2.5, 2, 1.4, 0.8, 0.4, 0.2] },
] as const

const minus = (value: number) => String(value).replace('-', '−')

// A Server Component: the hero, the week and the hourly charts all render on
// the server; only the Tabs that switch charts hydrate.
export default function WeatherWeek() {
  return (
    <article aria-labelledby="weather-week-title" className={styles.forecast}>
      <header className={styles.hero}>
        <Stack gap="xs">
          <Stack direction="row" align="center" gap="sm" wrap="wrap">
            <Heading as="h3" id="weather-week-title" fontSize="lg">
              Cinder Peak summit
            </Heading>
            <Badge tone="warning">Wind advisory</Badge>
          </Stack>
          <BodySmall margin="none" color="text-muted">
            8,240 ft · Updated 6:00 AM, Monday, December 7
          </BodySmall>
        </Stack>
        <div className={styles.now}>
          <CloudSnow aria-hidden className={styles.nowIcon} />
          <p className={styles.temperature}>
            −6<span className={styles.degree}>°C</span>
          </p>
          <Text fontWeight="medium">Light snow</Text>
        </div>
        <dl className={styles.readings}>
          <div>
            <dt>Feels like</dt>
            <dd>−13 °C</dd>
          </div>
          <div>
            <dt>Wind</dt>
            <dd>W 32 km/h</dd>
            <dd className={styles.subtle}>gusts 55</dd>
          </div>
          <div>
            <dt>New snow</dt>
            <dd>14 cm</dd>
            <dd className={styles.subtle}>last 24 h</dd>
          </div>
          <div>
            <dt>Snow depth</dt>
            <dd>212 cm</dd>
          </div>
        </dl>
      </header>

      <section aria-labelledby="weather-week-days" className={styles.section}>
        <Heading as="h4" id="weather-week-days" fontSize="sm" color="text-muted">
          7-day forecast
        </Heading>
        <ol className={styles.days}>
          {week.map((day) => {
            const { label, icon: Icon } = conditions[day.condition]
            const range = {
              '--from': (day.low - coldest) / span,
              '--to': (warmest - day.high) / span,
            } as CSSProperties
            return (
              <li key={day.date} className={styles.day} data-today={day.day === 'Today' || undefined}>
                <span className={styles.dayName}>
                  {day.day}
                  <span className={styles.visuallyHidden}>, {day.date}</span>
                </span>
                <Icon role="img" aria-label={label} className={styles.dayIcon} />
                <span className={styles.high}>
                  <span className={styles.visuallyHidden}>High </span>
                  {minus(day.high)}°
                </span>
                <span className={styles.low}>
                  <span className={styles.visuallyHidden}>Low </span>
                  {minus(day.low)}°
                </span>
                <span className={styles.range} style={range} aria-hidden />
              </li>
            )
          })}
        </ol>
      </section>

      <section aria-labelledby="weather-week-hourly" className={styles.section}>
        <Heading as="h4" id="weather-week-hourly" fontSize="sm" color="text-muted">
          Next 24 hours
        </Heading>
        <Tabs defaultValue="temp">
          <TabsList aria-label="Hourly measure" activateOnFocus>
            {hourly.map((series) => (
              <TabsTab key={series.id} value={series.id}>
                {series.label}
              </TabsTab>
            ))}
          </TabsList>
          {hourly.map((series) => (
            <TabsPanel key={series.id} value={series.id} dangerouslySetClassName={styles.panel}>
              <HourlyChart kind={series.kind} values={series.values} />
              <table className={styles.hourly}>
                <caption className={styles.visuallyHidden}>
                  {series.label} by hour, {series.unit}
                </caption>
                <thead>
                  <tr>
                    {hours.map((hour) => (
                      <th key={hour} scope="col">
                        {hour}
                      </th>
                    ))}
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    {series.values.map((value, index) => (
                      <td key={hours[index]}>
                        {minus(value)}
                        {series.id === 'temp' && '°'}
                      </td>
                    ))}
                  </tr>
                </tbody>
              </table>
            </TabsPanel>
          ))}
        </Tabs>
      </section>
    </article>
  )
}

// A decorative chart over the table that carries the numbers. Points sit at
// the centre of each table column, so the two line up at any width.
function HourlyChart({ kind, values }: { kind: 'line' | 'bars'; values: readonly number[] }) {
  const width = 320
  const height = 72
  const step = width / values.length
  const low = kind === 'bars' ? 0 : Math.min(...values) - 2
  const high = Math.max(...values) + (kind === 'bars' ? 0.5 : 2)
  const x = (index: number) => step * (index + 0.5)
  const y = (value: number) => height - ((value - low) / (high - low)) * height

  if (kind === 'bars') {
    return (
      <svg viewBox={`0 0 ${width} ${height}`} preserveAspectRatio="none" aria-hidden className={styles.chart}>
        {values.map((value, index) => (
          <rect
            key={x(index)}
            x={x(index) - step * 0.3}
            width={step * 0.6}
            y={y(value)}
            height={height - y(value)}
            className={styles.bar}
          />
        ))}
      </svg>
    )
  }

  const line = values.map((value, index) => `${index === 0 ? 'M' : 'L'}${x(index)},${y(value)}`).join(' ')
  return (
    <svg viewBox={`0 0 ${width} ${height}`} preserveAspectRatio="none" aria-hidden className={styles.chart}>
      <path d={`${line} L${x(values.length - 1)},${height} L${x(0)},${height} Z`} className={styles.area} />
      <path d={line} className={styles.line} />
    </svg>
  )
}
Cinder Peak summit forecast

How it works

  • Each day’s bar is positioned with two custom properties, --from and --to: the fractions of the week’s range below the low and above the high. The same two numbers draw a horizontal bar in a row on a phone and a vertical bar in a column on a wide layout, switched with a container query.
  • The day icons have role="img" and the condition as their label, and the highs and lows carry visually hidden “High” and “Low”, so the week reads as text, not only as shapes.
  • The hourly charts are decoration over a real table of the same values. Each point sits at the centre of its table column, so the chart and the numbers line up at any width; the SVG stretches with preserveAspectRatio="none" and keeps its line weight with vector-effect: non-scaling-stroke.
  • The page imports TabsList, TabsTab and TabsPanel by name, as a Server Component must; only the tabs hydrate.

App-owned CSS

recipes/weather-week.module.css
.forecast {
  container-type: inline-size;
  display: flex;
  flex-direction: column;
  gap: var(--ds-space-xl);
}

/* Current conditions */
.hero {
  display: grid;
  gap: var(--ds-space-lg);
  padding: var(--ds-space-lg);
  background:
    linear-gradient(160deg, var(--ds-color-accent-subtle), transparent 70%),
    var(--ds-color-bg-subtle);
  border: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
  border-radius: var(--ds-border-radius-xl);
}

.now {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  column-gap: var(--ds-space-md);

  & > :last-child {
    grid-column: 2;
  }
}

.nowIcon {
  grid-row: span 2;
  width: calc(var(--ds-space-xl) * 2);
  height: calc(var(--ds-space-xl) * 2);
  color: var(--ds-color-accent-text);
  stroke-width: 1.5;
}

.temperature {
  margin: 0;
  font-size: var(--ds-font-size-xxxxl);
  font-weight: var(--ds-font-weight-semibold);
  line-height: var(--ds-line-height-heading);
  font-variant-numeric: tabular-nums;
}

.degree {
  font-size: var(--ds-font-size-xl);
  vertical-align: top;
  color: var(--ds-color-text-muted);
}

.readings {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--ds-space-md) var(--ds-space-lg);
  margin: 0;

  & dt {
    font-size: var(--ds-font-size-xs);
    color: var(--ds-color-text-muted);
  }

  & dd {
    margin: 0;
    font-weight: var(--ds-font-weight-semibold);
  }
}

.readings .subtle {
  font-size: var(--ds-font-size-xs);
  font-weight: var(--ds-font-weight-body);
  color: var(--ds-color-text-muted);
}

@container (min-width: 34rem) {
  .hero {
    grid-template-columns: auto 1fr;
    padding: var(--ds-space-xl);
  }

  .hero > :first-child {
    grid-column: 1 / -1;
  }

  .readings {
    align-self: center;
    padding-left: var(--ds-space-xl);
    border-left: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
  }
}

.section {
  display: flex;
  flex-direction: column;
  gap: var(--ds-space-md);
}

/*
 * The week. Narrow: one row per day (day, icon, low, bar, high). Wide: a strip
 * of columns with vertical bars. --from and --to are the fractions of the
 * shared scale below the low and above the high.
 */
.days {
  margin: 0;
  padding: 0;
  list-style: none;
}

.day {
  display: grid;
  grid-template-areas: "day icon low range high";
  grid-template-columns: calc(var(--ds-space-xxl) * 0.75) var(--ds-icon-size-md) calc(var(--ds-space-xl) + var(--ds-space-sm)) 1fr calc(var(--ds-space-xl) + var(--ds-space-sm));
  align-items: center;
  gap: var(--ds-space-md);
  padding: var(--ds-space-md) var(--ds-space-sm);
  font-variant-numeric: tabular-nums;

  & + & {
    border-top: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
  }

  &[data-today] {
    font-weight: var(--ds-font-weight-semibold);
  }
}

.dayName {
  grid-area: day;
}

.dayIcon {
  grid-area: icon;
  width: var(--ds-icon-size-md);
  height: var(--ds-icon-size-md);
  color: var(--ds-color-accent-text);
}

.high {
  grid-area: high;
  font-weight: var(--ds-font-weight-semibold);
}

.low {
  grid-area: low;
  color: var(--ds-color-text-muted);
  text-align: right;
}

.range {
  position: relative;
  grid-area: range;
  height: var(--ds-space-sm);
  background-color: var(--ds-color-surface-active);
  border-radius: var(--ds-border-radius-pill);

  &::after {
    content: "";
    position: absolute;
    inset: 0 calc(var(--to) * 100%) 0 calc(var(--from) * 100%);
    background: linear-gradient(to right, var(--ds-color-accent), var(--ds-color-accent-text));
    border-radius: inherit;
  }
}

@container (min-width: 34rem) {
  .days {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: var(--ds-space-sm);
  }

  .day {
    grid-template-areas: "day" "icon" "high" "range" "low";
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto calc(var(--ds-space-xxl) * 1.5) auto;
    justify-items: center;
    gap: var(--ds-space-sm);
    padding: var(--ds-space-md) var(--ds-space-sm);
    border-radius: var(--ds-border-radius-md);

    & + & {
      border-top: none;
    }

    &[data-today] {
      background-color: var(--ds-color-accent-subtle);
    }
  }

  .low {
    text-align: center;
  }

  .range {
    width: var(--ds-space-sm);
    height: 100%;

    &::after {
      inset: calc(var(--to) * 100%) 0 calc(var(--from) * 100%);
      background: linear-gradient(to top, var(--ds-color-accent), var(--ds-color-accent-text));
    }
  }
}

/* Hourly chart and the table under it */
.panel {
  padding-top: var(--ds-space-md);
}

.chart {
  display: block;
  width: 100%;
  height: calc(var(--ds-space-xxl) + var(--ds-space-md));
  overflow: visible;
}

.line {
  fill: none;
  stroke: var(--ds-color-accent);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}

.area {
  fill: color-mix(in srgb, var(--ds-color-accent) 14%, transparent);
}

.bar {
  fill: var(--ds-color-accent);
}

.hourly {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
  text-align: center;

  & th {
    padding-top: var(--ds-space-sm);
    font-size: var(--ds-font-size-xxs);
    font-weight: var(--ds-font-weight-medium);
    color: var(--ds-color-text-muted);
    border-top: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
  }

  & td {
    padding-top: var(--ds-space-xs);
    font-size: var(--ds-font-size-sm);
    font-weight: var(--ds-font-weight-semibold);
  }
}

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