Getting Started
Set up the component library with Tailwind CSS, HTML, JavaScript, and Vite.
Setup
You’ll need Node.js and npm installed, plus some familiarity with the terminal.
Create a project folder
Terminal window mkdir my-project && cd my-projectnpm init -yInstall dependencies
Install Vite, Tailwind CSS, and Tailwind’s Vite plugin as development dependencies. Then install the USWDS + Tailwind packages.
Terminal window npm install -D vite tailwindcss @tailwindcss/vitenpm install @uswds-tailwind/compat@latest @uswds-tailwind/theme@latestNow that the dependencies are installed, we can create the project files.
Project structure
Create the HTML, CSS, and JavaScript files. Keep
index.htmlin the project root, where Vite looks for it by default. Run this frommy-project, or create the files manually.Terminal window mkdir srctouch index.html src/main.js src/styles.css vite.config.js .gitignoreYour project should look like this:
my-project/├── src/│ ├── main.js│ └── styles.css├── .gitignore├── index.html├── package-lock.json├── package.json└── vite.config.jsConfigure project
Vite
Vite will serve
index.htmland build the site intodistby default. Invite.config.js, we only need to enable Tailwind CSS:vite.config.js import tailwindcss from '@tailwindcss/vite'import { defineConfig } from 'vite'export default defineConfig({plugins: [tailwindcss()],})Add scripts to
package.jsonto run the development server and build the site:package.json {"scripts": {"dev": "vite","build": "vite build"}}CSS
Import Tailwind CSS and the USWDS theme in
src/styles.css:src/styles.css @import 'tailwindcss';@import '@uswds-tailwind/theme';JavaScript
Then import the stylesheet and component JavaScript in
src/main.js:src/main.js import './styles.css'import '@uswds-tailwind/compat/auto'The JavaScript import initializes matching interactive components when the page loads. See the component examples for their HTML markup.
HTML
Finally, fill in
index.html. The module script loadssrc/main.js, which imports the stylesheet and component JavaScript.index.html <!doctype html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>USWDS + Tailwind</title><script type="module" src="./src/main.js"></script></head><body class="font-source-sans"><main class="mx-auto max-w-prose p-6"><h1 class="mb-6 text-3xl font-bold">Hello, USWDS + Tailwind!</h1><button type="button" class="rounded-sm bg-blue-60v px-5 py-3 font-bold text-white hover:bg-blue-warm-70v focus:outline-4 focus:outline-blue-40v">Button</button></main></body></html>.gitignore
Add generated files to
.gitignore:.gitignore node_modules/dist/Complete!
The button lets you see that the styles are loading. Run
npm run devfrommy-projectand open the local URL Vite prints.When you’re ready to deploy, run
npm run buildand publish the generateddistdirectory.