Weather Forecast
A forecast card: scrub the hourly temperature line to read any hour, or switch to a 7-day list with high/low range bars.
$
View as Markdownnpx shadcn@latest add https://sundaeui.dev/r/weather-forecast.json'use client'
import { useRef, useState } from 'react'
import './weather-forecast.css'
export interface Hour { t: number; rain: number; wind: number; icon: string }
export interface Day { day: string; lo: number; hi: number; icon: string }
export interface WeatherForecastProps {
place?: string
hours: Hour[]
days: Day[]
unit?: 'c' | 'f'
view?: 'hourly' | 'week'
startHour?: number
fill?: 'dark' | 'light'
className?: string
}
/** Hourly scrubbable line and 7-day range bars. */
export function WeatherForecast({ place = 'Lisbon', hours, days, unit: u0 = 'c', view: v0 = 'hourly', startHour = 8, fill = 'dark', className }: WeatherForecastProps) {
const [unit, setUnit] = useState(u0)
const [view, setView] = useState(v0)
const [i, setI] = useState(0)
const box = useRef<HTMLDivElement>(null)
const cv = (c: number) => Math.round(unit === 'c' ? c : c * 9 / 5 + 32)
const ts = hours.map((h) => h.t), lo = Math.min(...ts) - 1, hi = Math.max(...ts) + 1
const X = (k: number) => (k / (hours.length - 1)) * 100, Y = (t: number) => 8 + (1 - (t - lo) / (hi - lo)) * 92
const d = hours.map((h, k) => `${k ? 'L' : 'M'}${X(k)} ${Y(h.t)}`).join(' ')
const h = hours[i]
const hr = (k: number) => { const x = (startHour + k) % 24; return `${x % 12 || 12}${x < 12 ? 'am' : 'pm'}` }
const at = (e: React.PointerEvent) => { const r = box.current!.getBoundingClientRect(); setI(Math.max(0, Math.min(hours.length - 1, Math.round(((e.clientX - r.left) / r.width) * (hours.length - 1))))) }
const wl = Math.min(...days.map((x) => x.lo)), wh = Math.max(...days.map((x) => x.hi))
return (
<div className={['sd-wth', className].filter(Boolean).join(' ')} data-fill={fill}>
<div className="sd-wth__top">
<div>
<div className="sd-wth__place">{place} · {view === 'hourly' ? (i ? hr(i) : 'Now') : 'This week'}</div>
<div className="sd-wth__now"><b aria-live="polite">{cv(view === 'hourly' ? h.t : days[0].hi)}°</b><span className="sd-wth__ic" aria-hidden>{view === 'hourly' ? h.icon : days[0].icon}</span></div>
<div className="sd-wth__meta">{view === 'hourly' ? `Rain ${h.rain}% · Wind ${h.wind} km/h` : `H ${cv(days[0].hi)}° · L ${cv(days[0].lo)}°`}</div>
</div>
<div className="sd-wth__seg">
<div role="radiogroup" aria-label="Unit">{(['c', 'f'] as const).map((x) => <button key={x} type="button" role="radio" aria-checked={unit === x} onClick={() => setUnit(x)}>°{x.toUpperCase()}</button>)}</div>
<div role="radiogroup" aria-label="View">{(['hourly', 'week'] as const).map((x) => <button key={x} type="button" role="radio" aria-checked={view === x} onClick={() => setView(x)}>{x === 'hourly' ? 'Hourly' : '7 days'}</button>)}</div>
</div>
</div>
{view === 'hourly' ? (
<div ref={box} className="sd-wth__chart" role="slider" tabIndex={0} aria-label="Hour" aria-valuemin={0} aria-valuemax={hours.length - 1} aria-valuenow={i}
aria-valuetext={`${i ? hr(i) : 'Now'}: ${cv(h.t)} degrees, ${h.rain}% rain`}
onPointerMove={at} onPointerDown={(e) => { e.currentTarget.setPointerCapture(e.pointerId); at(e) }}
onKeyDown={(e) => { const k = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0; if (k) { e.preventDefault(); setI((x) => Math.max(0, Math.min(hours.length - 1, x + k))) } }}>
<svg viewBox="0 0 100 130" preserveAspectRatio="none" aria-hidden>
<g className="sd-wth__rain">{hours.map((x, k) => <rect key={k} x={X(k) - 1.2} y={112 - x.rain * 0.3} width="2.4" height={x.rain * 0.3} />)}</g>
<path className="sd-wth__line" d={d} vectorEffect="non-scaling-stroke" transform="scale(1 1)" />
</svg>
<span className="sd-wth__cur" style={{ left: `${X(i)}%` }} />
<span className="sd-wth__dot" style={{ left: `${X(i)}%`, top: `${(Y(h.t) / 130) * 100}%` }} />
<div className="sd-wth__hx" aria-hidden>{[0, 6, 12, 18, hours.length - 1].map((k) => <span key={k}>{k ? hr(k) : 'Now'}</span>)}</div>
</div>
) : (
<ul className="sd-wth__wk" aria-label="7-day forecast">
{days.map((x, k) => (
<li key={x.day} className="sd-wth__day" style={{ animationDelay: `${k * 30}ms` }} aria-label={`${x.day}: low ${cv(x.lo)}, high ${cv(x.hi)}`}>
<span>{x.day}</span><span aria-hidden>{x.icon}</span><span>{cv(x.lo)}°</span>
<span className="sd-wth__rng" aria-hidden><i style={{ left: `${((x.lo - wl) / (wh - wl)) * 100}%`, right: `${100 - ((x.hi - wl) / (wh - wl)) * 100}%` }} /></span>
<span>{cv(x.hi)}°</span>
</li>
))}
</ul>
)}
</div>
)
}.sd-wth { --bg: #1c1c1c; --fg: #f6f6f6; --mute: #8e8e8e; --line: rgb(255 255 255 / 0.1); --soft: rgb(255 255 255 / 0.07); --strong: #fafafa; --on: #111;
color: var(--fg); font-size: 14px; line-height: 1.4; -webkit-tap-highlight-color: transparent; }
.sd-wth[data-fill='light'] { --bg: #fff; --fg: #121212; --mute: #6f6f6f; --line: rgb(0 0 0 / 0.09); --soft: rgb(0 0 0 / 0.05); --strong: #111; --on: #fff; }
.sd-wth :where(button) { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
.sd-wth :focus-visible { outline: 2px solid var(--fg); outline-offset: 2px; }
.sd-wth .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.sd-wth { width: min(100%, 330px); padding: 16px; border-radius: 22px; background: var(--bg); box-shadow: inset 0 0 0 1px var(--line); }
.sd-wth__top { display: flex; align-items: flex-start; justify-content: space-between; }
.sd-wth__place { font-size: 13px; color: var(--mute); }
.sd-wth__now { display: flex; align-items: center; gap: 10px; margin-top: 2px; }
.sd-wth__now b { font-size: 46px; font-weight: 300; letter-spacing: -0.04em; line-height: 1; font-variant-numeric: tabular-nums; }
.sd-wth__ic { font-size: 30px; line-height: 1; filter: grayscale(1); }
.sd-wth__meta { margin-top: 6px; font-size: 12.5px; color: var(--mute); font-variant-numeric: tabular-nums; }
.sd-wth__seg { display: flex; flex-direction: column; gap: 4px; align-items: flex-end; }
.sd-wth__seg > div { display: flex; padding: 2px; border-radius: 8px; background: var(--soft); }
.sd-wth__seg button { padding: 4px 9px; border-radius: 6px; font-size: 12px; color: var(--mute) !important; }
.sd-wth__seg button[aria-checked='true'] { background: var(--bg); color: var(--fg) !important; box-shadow: 0 1px 2px rgb(0 0 0 / 0.25); }
.sd-wth__chart { position: relative; height: 130px; margin-top: 14px; touch-action: none; cursor: crosshair; outline: 0; border-radius: 10px; }
.sd-wth__chart:focus-visible { box-shadow: 0 0 0 2px var(--fg); }
.sd-wth__chart svg { position: absolute; left: 0; top: 0; width: 100%; height: 100%; overflow: visible; }
.sd-wth__line { fill: none; stroke: var(--fg); stroke-width: 2; stroke-linejoin: round; }
.sd-wth__rain rect { fill: var(--soft); }
.sd-wth__cur { position: absolute; top: 0; bottom: 18px; width: 1px; background: var(--fg); pointer-events: none; }
.sd-wth__dot { position: absolute; width: 10px; height: 10px; margin: -5px 0 0 -5px; border-radius: 99px; background: var(--bg); box-shadow: inset 0 0 0 2.5px var(--fg); pointer-events: none; }
.sd-wth__hx { position: absolute; left: 0; right: 0; bottom: 0; display: flex; justify-content: space-between; font-size: 10.5px; color: var(--mute); }
.sd-wth__wk { list-style: none; margin: 14px 0 0; padding: 0; }
.sd-wth__day { display: grid; grid-template-columns: 38px 24px 30px 1fr 30px; align-items: center; gap: 8px; height: 36px; font-size: 13.5px; font-variant-numeric: tabular-nums; animation: sd-wth-in 240ms ease-out backwards; }
.sd-wth__day span:nth-child(2) { filter: grayscale(1); text-align: center; }
.sd-wth__day span:nth-child(3) { color: var(--mute); text-align: right; }
.sd-wth__rng { position: relative; height: 5px; border-radius: 99px; background: var(--soft); }
.sd-wth__rng i { position: absolute; top: 0; bottom: 0; border-radius: 99px; background: var(--fg); }
@keyframes sd-wth-in { from { opacity: 0; transform: translateX(-6px); } }
@media (prefers-reduced-motion: reduce) { .sd-wth *, .sd-wth *::before, .sd-wth *::after { animation: none !important; transition: none !important; } }Overview
Hourly view draws temperature as a line; drag or hover to move a cursor and the header shows that hour’s temperature, rain chance and wind.
7-day view lines up each day’s low–high as a bar on a shared scale so warm and cold days read at a glance. °C/°F converts everything.
When to use
- Travel, events, outdoor activity apps, dashboards.
When not to use
- Severe-weather alerts: lead with the warning.
Installation
$
pnpm dlx shadcn@latest add https://sundaeui.dev/r/weather-forecast.json$
npx shadcn@latest add https://sundaeui.dev/r/weather-forecast.json$
yarn dlx shadcn@latest add https://sundaeui.dev/r/weather-forecast.json$
bunx --bun shadcn@latest add https://sundaeui.dev/r/weather-forecast.jsonWorks in any React project. Files are written to components/sundae/. Read the source in the stage’s Code view.
- No dependencies beyond React 18 or later.
- Copy weather-forecast.tsx and weather-forecast.css into components/sundae/.
- The CSS file is imported by the component, so no extra configuration is needed.
- Import the component and use it as shown below.
Usage
import { WeatherForecast } from '@/components/sundae/weather-forecast'
<WeatherForecast />Props
| Prop | Type | Default |
|---|---|---|
hours*Next 24 hours. | { t: number; rain: number; wind: number; icon: string }[] | — |
days*7-day. | { day: string; lo: number; hi: number; icon: string }[] | — |
unitDisplay unit. | 'c' | 'f' | 'c' |
fillSurface: dark or light. | 'dark' | 'light' | 'dark' |
Accessibility
- The hourly chart is a slider (one step per hour) with full readings in aria-valuetext.
- The 7-day list is a table-like list with highs and lows spelled out.
- Reduced motion removes the movement and keeps every state change.
Customization
- Colours are CSS custom properties on .sd-wth; override them in your own stylesheet.
- fill="light" switches the built-in light palette.
Keyboard
- ← / →
- Previous / next hour
FAQ
- Is Weather Forecast free?
- Yes. Weather Forecast is free for personal and commercial projects, with full source on this page.
- How do I install Weather Forecast?
- Run npx shadcn@latest add https://sundaeui.dev/r/weather-forecast.json. The files are written to components/sundae/. It has no dependencies beyond React 18 or later.
- Does Weather Forecast support keyboard and screen readers?
- The hourly chart is a slider (one step per hour) with full readings in aria-valuetext. The 7-day list is a table-like list with highs and lows spelled out.
- Does Weather Forecast respect reduced motion?
- Reduced motion removes the movement and keeps every state change.
More like this
All data →- Pro
Donut Breakdown
Data · Refined Stacked Bars
Data · Refined- Pro
Scrub Sparkline
Data · Refined - Pro
Tick Gauge
Data · Refined - Pro
Range Line Chart
Data · Refined - Pro
Weekly Bars
Data · Refined