Aadi

Weather Card

Cards

About

A weather card for assistant replies: current temperature and conditions, feels like, humidity and wind, an hourly strip with chance of rain, and a few days ahead with temperature range bars.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Weather Card to your project and fits it to your theme.

Code

How Weather Card is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client"

import * as React from "react"
import { WeatherCard, WeatherCardProps } from "@/components/ui/weather-card"

const reducedMotion = () =>
  typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches

/** How long a choice holds a tour before it carries on. */
const RESUME_MS = 1800

/**
 * Steps through `values` on a timer, so a 21st preview (and its cover video) shows every option
 * without a click. Picking a value holds it, and the tour carries on from there after
 * `RESUME_MS`, so a click never leaves the preview still. Reduced motion never starts it.
 */
function useCycle<T>(values: readonly T[], ms: number) {
  const [index, setIndex] = React.useState(0)
  const [heldAt, setHeldAt] = React.useState(0)
  React.useEffect(() => {
    if (reducedMotion()) return
    let id = 0
    const start = () => {
      id = window.setInterval(() => setIndex((n) => (n + 1) % values.length), ms)
    }
    const wait = window.setTimeout(start, heldAt ? RESUME_MS : 0)
    return () => {
      window.clearTimeout(wait)
      window.clearInterval(id)
    }
  }, [heldAt, values.length, ms])
  const pick = React.useCallback(
    (value: T) => {
      setHeldAt(Date.now())
      setIndex(Math.max(0, values.indexOf(value)))
    },
    [values],
  )
  return [values[index] as T, pick] as const
}

/** Four cities an assistant might be asked about, each with its own sky. */
const CITIES: WeatherCardProps[] = [
  {
    location: "Lisbon",
    updated: "Updated 14:05",
    temp: 21,
    condition: "partly-cloudy",
    high: 23,
    low: 15,
    feelsLike: 20,
    humidity: 64,
    wind: "14 km/h",
    hourly: [
      { time: "Now", temp: 21, condition: "partly-cloudy" },
      { time: "15:00", temp: 22, condition: "clear" },
      { time: "16:00", temp: 22, condition: "clear" },
      { time: "17:00", temp: 21, condition: "partly-cloudy" },
      { time: "18:00", temp: 19, condition: "cloudy", precip: 30 },
      { time: "19:00", temp: 18, condition: "rain", precip: 60 },
    ],
    daily: [
      { day: "Today", condition: "partly-cloudy", low: 15, high: 23 },
      { day: "Thu", condition: "rain", low: 14, high: 18, precip: 80 },
      { day: "Fri", condition: "cloudy", low: 13, high: 19, precip: 30 },
      { day: "Sat", condition: "clear", low: 14, high: 24 },
    ],
  },
  {
    location: "Reykjavik",
    updated: "Updated 13:05",
    temp: -2,
    condition: "snow",
    high: 0,
    low: -5,
    feelsLike: -8,
    humidity: 81,
    wind: "32 km/h",
    hourly: [
      { time: "Now", temp: -2, condition: "snow", precip: 70 },
      { time: "14:00", temp: -2, condition: "snow", precip: 80 },
      { time: "15:00", temp: -3, condition: "wind" },
      { time: "16:00", temp: -3, condition: "cloudy" },
      { time: "17:00", temp: -4, condition: "cloudy" },
      { time: "18:00", temp: -5, condition: "clear-night" },
    ],
    daily: [
      { day: "Today", condition: "snow", low: -5, high: 0, precip: 80 },
      { day: "Thu", condition: "wind", low: -6, high: -1 },
      { day: "Fri", condition: "cloudy", low: -4, high: 1 },
      { day: "Sat", condition: "snow", low: -7, high: -2, precip: 60 },
    ],
  },
  {
    location: "Tokyo",
    updated: "Updated 22:05",
    temp: 17,
    condition: "rain",
    high: 19,
    low: 14,
    feelsLike: 16,
    humidity: 88,
    wind: "11 km/h",
    hourly: [
      { time: "Now", temp: 17, condition: "rain", precip: 90 },
      { time: "23:00", temp: 16, condition: "rain", precip: 80 },
      { time: "00:00", temp: 16, condition: "storm", precip: 70 },
      { time: "01:00", temp: 15, condition: "rain", precip: 50 },
      { time: "02:00", temp: 15, condition: "cloudy", precip: 20 },
      { time: "03:00", temp: 14, condition: "fog" },
    ],
    daily: [
      { day: "Today", condition: "rain", low: 14, high: 19, precip: 90 },
      { day: "Thu", condition: "cloudy", low: 13, high: 20 },
      { day: "Fri", condition: "clear", low: 12, high: 22 },
      { day: "Sat", condition: "partly-cloudy", low: 14, high: 21 },
    ],
  },
  {
    location: "Cape Town",
    updated: "Updated 15:05",
    temp: 27,
    condition: "clear",
    high: 29,
    low: 18,
    feelsLike: 28,
    humidity: 42,
    wind: "24 km/h",
    hourly: [
      { time: "Now", temp: 27, condition: "clear" },
      { time: "16:00", temp: 28, condition: "clear" },
      { time: "17:00", temp: 27, condition: "wind" },
      { time: "18:00", temp: 25, condition: "clear" },
      { time: "19:00", temp: 22, condition: "clear-night" },
      { time: "20:00", temp: 20, condition: "clear-night" },
    ],
    daily: [
      { day: "Today", condition: "clear", low: 18, high: 29 },
      { day: "Thu", condition: "wind", low: 17, high: 26 },
      { day: "Fri", condition: "partly-cloudy", low: 16, high: 24 },
      { day: "Sat", condition: "clear", low: 17, high: 27 },
    ],
  },
]

const FIT_CSS = "html,body{overflow:hidden}[data-fit-small] [data-fit-hide]{display:none!important}"

/**
 * 21st shows a demo in a small frame: about 674 by 421 on a component page, 358 by 224 on a
 * phone and 245 by 139 on a profile card. A demo taller than its frame gets scrollbars, which
 * render white on dark pages. FitFrame fills the frame exactly instead.
 *
 * - "scale": the content shrinks until it fits, centered. For visuals such as orbs.
 * - "crop": the content stays at full size from the top and fades out at the bottom. For text
 *   that streams in, where a changing scale would make the page breathe.
 *
 * `minWidth` lays out a wide demo (a table, a two column panel) at that width and scales it down
 * to the frame, instead of letting it collapse to a narrow layout. The frame centers it, so it
 * shrinks evenly on both sides.
 *
 * Anything marked `data-fit-hide` (state buttons, the chatcn.dev line) drops out in frames
 * shorter than `hideBelow`, so a profile card shows the visual alone.
 */
function FitFrame({
  children,
  mode = "scale",
  hideBelow = 360,
  minWidth,
}: {
  children: React.ReactNode
  mode?: "scale" | "crop"
  hideBelow?: number
  minWidth?: string
}) {
  const content = React.useRef<HTMLDivElement>(null)
  const [cropped, setCropped] = React.useState(false)

  React.useLayoutEffect(() => {
    const el = content.current
    if (!el) return
    const measure = () => {
      const vw = window.innerWidth
      const vh = window.innerHeight
      el.toggleAttribute("data-fit-small", vh < hideBelow)
      // Layout sizes ignore the transform, so measuring never feeds back.
      const w = Math.max(el.offsetWidth, el.scrollWidth)
      const h = Math.max(el.offsetHeight, el.scrollHeight)
      const scale = mode === "scale" ? Math.min(1, vw / w, vh / h) : 1
      el.style.transform = scale < 1 ? `scale(${scale})` : ""
      setCropped(mode === "crop" && h > vh + 1)
    }
    measure()
    const observer = new ResizeObserver(measure)
    observer.observe(el)
    // Content that changes inside a fixed width box (a headline that cycles words) never resizes
    // the box itself, so changes below it measure again on the next frame.
    let frame = 0
    const mutations = new MutationObserver(() => {
      cancelAnimationFrame(frame)
      frame = requestAnimationFrame(measure)
    })
    mutations.observe(el, { subtree: true, childList: true, characterData: true })
    window.addEventListener("resize", measure)
    return () => {
      observer.disconnect()
      mutations.disconnect()
      cancelAnimationFrame(frame)
      window.removeEventListener("resize", measure)
    }
  }, [mode, hideBelow])

  const fade = "linear-gradient(to bottom, #000 calc(100% - 40px), transparent)"
  return (
    <div
      className="flex w-full justify-center overflow-hidden"
      style={{
        height: "100dvh",
        alignItems: mode === "crop" ? "flex-start" : "center",
        maskImage: cropped ? fade : undefined,
        WebkitMaskImage: cropped ? fade : undefined,
      }}
    >
      <style>{FIT_CSS}</style>
      <div
        ref={content}
        className="w-full shrink-0"
        style={{ transformOrigin: mode === "crop" ? "top center" : "center", minWidth }}
      >
        {children}
      </div>
    </div>
  )
}

/** The line under each 21st demo that points back to the kit. */
function MoreOn({ slug, children }: { slug: string; children: React.ReactNode }) {
  return (
    <p data-fit-hide className="text-[0.75rem]/[1rem] text-(color:--chatcn-fg-faint) text-center text-balance">
      {children}{" "}
      <a
        href={`https://chatcn.dev/docs/components/${slug}`}
        target="_blank"
        rel="noopener"
        className="text-(color:--chatcn-fg-subtle) hover:text-(color:--chatcn-fg) underline underline-offset-2"
      >
        chatcn.dev
      </a>
    </p>
  )
}

/**
 * The card an assistant answers a weather question with: current conditions, an hourly strip
 * with precipitation and a few days ahead. The demo cycles four cities, from Lisbon cloud to
 * Reykjavik snow, Tokyo rain and Cape Town sun.
 */
export default function WeatherCardPreview() {
  const [city] = useCycle(CITIES, 1800)
  return (
    <FitFrame minWidth="30rem">
      <div className="flex w-full flex-col items-center justify-center gap-4 p-6">
        <div className="w-full max-w-md">
          <WeatherCard {...city} />
        </div>
        <MoreOn slug="weather-card">Current, hourly and daily forecasts in one card. On</MoreOn>
      </div>
    </FitFrame>
  )
}

More by Aadi

View all
  • Dither Orb

    Dither Orb

    12
  • Audio Visualizer

    Audio Visualizer

    9
  • Voice Orb

    Voice Orb

    8
  • Agent Lanes

    Agent Lanes

    6
  • Computer View

    Computer View

    3
  • Waveform

    Waveform

    2
  • Reasoning

    Reasoning

    2
  • Voice Session

    Voice Session

    1

More in Cards

View all
  • Stats cards with links

    Stats cards with links

    92
  • Course Design Cards

    Course Design Cards

    1.2K
  • Statistics Card 2

    Statistics Card 2

    294
  • Statistics Card 7

    Statistics Card 7

    316
  • Interactive Tech Stack Bento Grid with Hover Effects

    Interactive Tech Stack Bento Grid with Hover Effects

    489
  • Card Studio

    Card Studio

    8
  • Item

    Item

    10
  • Statistics Card 10

    Statistics Card 10

    284

Explore more

Part of chatcn — browse the full library on 21st.dev.