Skip to content
Drivn
5 min read

How to Add an Alert to a React App

Step-by-step guide to adding a copy-and-own Alert to any React app with the Drivn CLI — four variants, an icon prop, zero runtime UI dependencies.

Adding an alert to a React app is one of those tasks that looks finished after five minutes and turns out to need another hour. The first version is a styled div. Then a designer asks for an info variant and a success variant, someone files an accessibility bug because a screen reader skipped the message, and you are now maintaining a small component nobody planned for. Reaching for a headless library solves the variants but adds a runtime dependency and a layer of API to learn.

Drivn takes a different route. The CLI writes an Alert source file directly into your repo — no runtime UI package, no class-variance-authority, no wrapper components. You get a single Alert component with four variants, an icon prop, and a title prop, all in about forty lines of readable TypeScript you can edit at will. This guide adds it to an existing React app in roughly ten minutes: install the CLI, copy the component, render it on a page, and tune the variants to match your theme.

You can follow along with a Vite + React project, a Next.js App Router app, Remix, or any React framework that supports Tailwind and TypeScript. The steps are identical. For setup context see the installation page; for the App Router variant see the Next.js Alert guide; for live patterns see the Alert examples page.

Prerequisites

Before installing the Alert, confirm your React project has the three things Drivn assumes: Tailwind CSS v4 installed and processing your styles, TypeScript configured (the component ships as a .tsx file), and a @/ path alias pointing at your source directory. Projects scaffolded with create-next-app, npm create vite, or npx drivn@latest create have all three wired up already. For a custom setup, check the compilerOptions.paths entry in your tsconfig.json; the installation page lists the minimal config. The Alert reads color tokens — --primary, --success, --destructive — from your Tailwind theme, so make sure those exist or rename them in the component after install. If you plan to use the icon prop, the CLI adds lucide-react for you when it is missing.

Step 1 — Install Drivn via the CLI

Run the CLI from your project root to add the Alert source file. The command prompts once for your install directory, defaulting to src/components/ui/, then writes alert.tsx and adds lucide-react to your package.json if it is not already there. No global config file is created — the Alert is just a TypeScript file in your repo that you edit like any other component. The CLI docs cover the --cwd and --dir flags for monorepos or non-standard layouts. Once the file lands you own it; future Drivn releases will not overwrite it, so any change you make is permanent. Commit the change to keep a clean baseline before customizing.

1# add the Alert to your existing React project
2npx drivn add alert
3
4# install lucide-react if you want the icon prop
5npm install lucide-react

Step 2 — Import and render the Alert

Open the page where the Alert should appear and import it from your UI directory. The API is a single component — no AlertTitle or AlertDescription wrappers to nest. Pass variant to pick one of default, info, success, or destructive, title for the bold heading, icon for an optional Lucide icon, and the message itself as children. Because the component holds no state, it renders the same on the server and the client, which keeps it usable inside server components and static pages alike. See the Alert docs for the full prop table and the examples page for seven copy-paste patterns covering variants, icons, and titles.

1import { Alert } from '@/components/ui/alert'
2import { Info } from 'lucide-react'
3
4export function TrialBanner() {
5 return (
6 <Alert variant="info" icon={Info} title="Heads up">
7 Your trial expires in 3 days.
8 </Alert>
9 )
10}

Step 3 — Customize variants and icon

Because the Alert lives in your codebase, customization is a source edit, not a prop workaround. Open src/components/ui/alert.tsx and find the const styles object near the top — it holds every class the component uses, keyed by variant. Edit a variant string to retune its colors, change styles.base for padding or radius, or add a new key like warning and the keyof typeof styles.variants type picks it up at every call site automatically. The icon prop accepts a component reference (icon={Info}) or a pre-rendered element (icon={<Info className="size-5" />}), so per-alert icon styling is one prop away. The theming page maps every token the variants read from.

1// src/components/ui/alert.tsx — styles object
2const styles = {
3 base: 'flex gap-3 p-4 rounded-[10px] border text-sm',
4 variants: {
5 default: 'bg-accent/50 border-border text-foreground',
6 info: 'bg-primary/10 border-primary/20 text-primary-light',
7 success: 'bg-success/10 border-success/20 text-success',
8 destructive: 'bg-destructive/10 border-destructive/20 text-destructive',
9 },
10 title: 'font-semibold mb-1',
11 description: 'text-sm opacity-90',
12}

Step 4 — Add role="alert" for screen readers

Drivn's Alert renders a plain div with no ARIA role by default, which is fine for a banner that sits on the page from first render — a screen reader reads it in normal document order. For an alert that appears in response to an event, like a failed save or a successful upload, you want assistive technology to announce it immediately. Add role="alert" to the outer div in src/components/ui/alert.tsx and the message becomes a live region that announces on insertion. Because you own the source, this one-line edit is permanent. For transient, self-announcing confirmations that should disappear on their own, Toast is the better fit — it ships with a live region built in. The shadcn/ui comparison covers what the Radix-based alternative does here by default.

Get started

Install Drivn in one command

Copy the source into your project and own every line. Zero runtime dependencies, pure React + Tailwind.

Follow Drivn updates
New components, improvements, and guides every release.
Enjoying Drivn?
Star the repo on GitHub to follow new component releases.
Star →

Frequently asked questions

Yes. Set up your CRA project with TypeScript and Tailwind v4 first, then run the Drivn CLI. The Alert source has no dependency on React Router, Next.js, or any framework-specific API — it renders anywhere React and Tailwind render to the DOM. The only requirement is that the color tokens it reads, like --primary and --destructive, exist in your Tailwind theme, or that you rename them in the component after install.

Not by default. The component renders a plain div with no ARIA role, which suits a static banner read in document order. For an alert inserted dynamically after an event, add role="alert" to the outer div in src/components/ui/alert.tsx to turn it into a live region that announces on insertion. Because Drivn components live in your repo, this one-line change is permanent and never overwritten by an upgrade.

Four out of the box: default, info, success, and destructive. Each maps to a color-tuned border, background tint, and text token in the styles.variants object. Adding a fifth — warning, for example — is a matter of adding one key to that object; the keyof typeof styles.variants type powering the variant prop picks the new key up automatically, so TypeScript autocompletes it at every call site with no other edits.

Copy-and-own components avoid two problems with traditional npm packages: breaking upgrades that change internal APIs, and the lock-in of not being able to edit internals. With Drivn you can retune a variant color, add an ARIA role, or change the padding in seconds without opening an upstream PR. The trade-off is that upgrades are manual — you decide when to re-run the CLI and pull newer source, rather than receiving changes automatically.