Skip to main content
DrupalMaze
Free quote

Building a Drupal theme on USWDS: from setup to your first component

The practical starting point for a USWDS-based Drupal theme: where the assets live, how Sass compiles, and how a Twig or Single Directory Component wraps a USWDS pattern.

Building a Drupal theme on USWDS is mostly ordinary front-end work — the difference is that your design decisions flow through USWDS tokens and your markup wraps USWDS components instead of inventing them. This is the setup we use on government projects.

Get USWDS into the theme

Install USWDS in your theme directory rather than as a Drupal library module — you want it versioned with the theme:

cd web/themes/custom/your_theme
npm install @uswds/uswds --save

Create a Sass entry file that pulls only what you need. Importing all of USWDS produces a stylesheet north of a megabyte; importing packages keeps it closer to a tenth of that.

// src/sass/uswds-theme.scss
@use "uswds-core" as * with (
  $theme-font-path: "../fonts",
  $theme-image-path: "../img",
  $theme-show-notifications: false
);
@forward "uswds-global";
@forward "usa-banner";
@forward "usa-header";
@forward "usa-footer";

Copy USWDS fonts and images into the theme's assets so they deploy with it — the package paths above assume that layout.

Wire the build

Compile Sass with whatever the theme already uses — Dart Sass via npm scripts is enough:

"scripts": {
  "build:css": "sass --load-path=node_modules/@uswds/uswds/packages src/sass/uswds-theme.scss dist/css/uswds-theme.css",
  "watch:css": "sass --watch --load-path=node_modules/@uswds/uswds/packages src/sass:dist/css"
}

Register the compiled CSS and the USWDS JS bundle as a theme library in your_theme.libraries.yml, and attach it in the theme's libraries list.

Wrap a component instead of copying markup

USWDS markup is verbose and changes between versions, so the theme should touch it in one place per component. With Single Directory Components, a usa-alert wrapper looks like:

{# components/alert/alert.twig #}
<div class="usa-alert usa-alert--{{ type }}">
  <div class="usa-alert__body">
    {% if title %}<h4 class="usa-alert__heading">{{ title }}</h4>{% endif %}
    <p class="usa-alert__text">{{ text }}</p>
  </div>
</div>

Twig templates then include your_theme:alert with props. When USWDS updates the alert markup, you edit one file — not every template that renders a notice.

Two early mistakes to skip

  • Do not restyle USWDS classes directly. Override via theme tokens and settings; direct overrides on usa-* selectors break silently on upgrade.
  • Do not skip the JS. Header menus, banners, and accordions depend on uswds.min.js and its initialization — a page that looks right but does not open its menu is a USWDS-JS problem, not a CSS problem.

From here the pattern repeats per component: check the USWDS example markup, wrap it in an SDC, feed it from a field or render array. Our Drupal team builds and maintains USWDS themes for public-sector work — the fixed-scope packages cover exactly this kind of build.

Bring us your beautifully complicated problem.

↗