recipes/weather-week.tsximport { 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 },
]
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 ('-' , '−' )
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 >
)
}
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 >
)
}