Information about the most recent release of the design system can always be found in the release history. We include details about significant updates and any backward-incompatible changes along with a list of all changes.
Anyone: Take a look at our new USWDS Tutorial. Follow the instructions in this GitHub repo to clone a sample project, install USWDS, customize it, and add USWDS components. It should take about an hour, and is a good introduction to USWDS concepts.
What's included in USWDS
The USWDS package includes compiled assets in a dist directory and component source files in a packages directory.
As of USWDS 3.0.0, our codebase is centered around functional packages, typically components. For more about how we organize packages, see our Packages documentation. In each of the following examples, we use [package] to represent a specific package. For example, component Sass is located in packages/[package]/src/styles for an accordion, this would be packages/usa-accordion/src/styles.
Fonts are located in both dist/fonts and packages/uswds-core/src/assets/fonts. The fonts in dist are simply a copy of the files in uswds-core.
Images and icons are located in: dist/img. The source for component-specific images can be found in a package's src/img directory.
JavaScript for components is located in packages/[package]/src/index.js. General JavaScript utilities and polyfills are located in the uswds-core package: packages/uswds-core/src/js
Sass component-specific stylesheets are located in: packages/[package]/src/styles. Many components also have a component entry point at packages/[package]/_index.scss that includes references to all a component's dependencies as well. Compiled CSS is located in dist/css.
Template markup for the components is located in: packages/[package]/src/[package.twig] in the site root. These, however, are written in the templating language Twig. It's best to get HTML source markup directly from designsystem.digital.gov/components
Directory structure
Here's what you can expect to find inside the USWDS package:
/packages/uswds: The package most projects include by default. This bundle includes all USWDS components and functionality.
/packages/uswds-[bundle]: Other non-component functionality is included in uswds--prefixed packages. These bundles might collect common component packages (uswds-form-controls) or important internal functionality (uswds-core).
/src: Placeholders included for backwards compatibility. Most projects should avoid using the contents of this directory.
/tasks: Internal build process files (not used in USWDS projects)
Installing the design system
There are two ways to install the design system on a project:
Installing it as a project dependency using Node and npm
Installing the package directly from GitHub
We recommend using npm to make it as straightforward as possible to install the design system and update it as we release new versions.
Install using Node and npm
npm is a package manager for Node-based projects. USWDS maintains the @uswds/uswds package that includes both the pre-compiled and compiled files. npm packages make it easy to update and install the design system from the command line.
Install Node/npm. Below is a link to find the install method that coincides with your operating system:
Note for Windows users: If you are using Windows and are unfamiliar with Node or npm, we recommend following Team Treehouse's tutorial for more information.
Make sure you have installed it correctly:
npm -v
6.13.0 # This line may vary depending on what version of Node you've installed.
Create a package.json file. You can do this manually, but an easier method is to use the npm init command. This command will prompt you with a few questions to create your package.json file.
Add @uswds/uswds to your project’s package.json:
npm install --save @uswds/uswds@latest
The @uswds/uswds module is now installed as a dependency. You can use the compiled files found in the node_modules/@uswds/uswds/dist/ directory or the source files in the node_modules/@uswds/uswds/packages/ directory.
Note: We do not recommend directly editing the design system files in node_modules. One of the benefits of using a package manager is its ease of upgrade and installation. If you make customizations to the files in the package, any upgrade or re-installation will wipe them out.
Install the package directly from GitHub
If you’re using a framework or package manager that doesn’t support npm, you can find the source files in this repository and use them in your project. Otherwise, we recommend that you follow the steps outlined in this section.
Download the USWDS package directly from the latest USWDS release and uncompress that file.
Copy these files and folders into a relevant place in your project's code base. Here is an example structure for how this might look:
You'll notice in our example above that we also outline a stylesheets, images and javascript folder in your assets folder. These folders are to help organize any assets that are unique to your project and separate from the design system assets.
Using USWDS CSS and JavaScript in your project
The three files critical to any USWDS project are the stylesheet, the JavaScript, and the initializer. Most projects require all of these to function properly.
Stylesheet: This is the compiled CSS stylesheet that describes how design system components look. To start, reference either uswds.css or uswds.min.css in the <head> of your document. Find this file in /dist/css. Most projects will want to compile their own CSS from USWDS source Sass instead of using the precompiled version. For more about this, see Compiling USWDS Sass into CSS, below.
Library: This is the compiled JavaScript that controls component interactivity. Reference either uswds.js or uswds.min.js at the end of the <body> of your document. Find this file in /dist/js.
Initializer: This small JavaScript file (less than 1 KB minified) helps the browser know if the USWDS JavaScript library is loading properly. This prevents component content from "flashing" or "shifting" while the page loads. Reference uswds-init.min.js in the <head> of your page, or inline its contents directly into the <script> tag. Find this file in /dist/js.
Reference the stylesheet, library, and initializer in each HTML page or dynamic template in your project.
Here is an example of how to reference these assets in your index.html file:
And that’s it — you should now be able to copy our code samples into your index.html and start using the design system.
Compiling USWDS Sass into CSS
If you want to take full advantage of USWDS custom settings and add build new styles and components with the USWDS toolset, you'll need a way to access the assets in the USWDS package and compile custom CSS from the USWDS source files.
USWDS uses the task manager Gulp as a way to add USWDS assets to a project and compile our CSS from the package source. Gulp is a useful and powerful tool, but it can be difficult to set up if you are new to it.
The USWDS Compile package is made for developers new to Gulp or those who just want a simple setup to compile USWDS Sass. The repo contains files and instructions for setting up the compiler, initializing USWDS, and compiling CSS from the source files.
Sass compilation requirements
USWDS Sass needs three things to compile properly:
Sass Module syntax: USWDS requires a modern Sass compiler that can parse Sass Module syntax.
Autoprefixing: USWDS requires Autoprefixing your CSS with a specific .browserslistrc.
Sass Load Paths: USWDS requires Sass compilers use Load Paths that reference the /packages directory in the USWDS package
Note: Using a compiler package like USWDS Compile is a good way to fulfill these requirements automatically.
Autoprefixing
The design system requires autoprefixing to work properly. Don't add vendor prefixes to your custom styles manually — it is more reliable to use autoprefixing. Autoprefixing services like gulp-autoprefixer automatically add vendor prefixes to CSS rules. We use the following autoprefixer settings via .browserslistrc config:
> 2%
last 2 versions
IE 11
not dead
Sass Load Paths
USWDS 3.0 and newer require the use of Sass Load Paths to compile properly.
USWDS 3.0 load paths must include a path to the /packages directory in the USWDS package, typically by updating an IncludePaths setting to include node_modules/@uswds/uswds/packages.
Here's how this might look in Gulp and in Webpack:
Minification: We recommend using a minifier like csso to compress your final compiled CSS.
Sourcemaps: We recommend using a sourcemap tool like gulp-sourcemaps to assist debugging by keeping track of source Sass locations.
Sass and theme settings
The design system is customizable using the power of Sass (Syntactically Awesome Style Sheets). The critical files you'll need in your project are those in dist/scss/theme:
_uswds-theme.scss: custom theme settings
_uswds-theme-custom-styles.scss: additional project CSS for customizing or adding to what USWDS provides
styles.scss: The Sass entry point. This is the primary Sass file that you'll compile. It collects theme settings, USWDS source files, and custom CSS
styles.scss looks something like the following code. It adds all the project theme settings, then adds USWDS source, and finally adds your project's custom styles:
Technical note: The @forward 'uswds' statement above references the uswds package in node_modules/@uswds/uswds/packages. The compile functions included in uswds-compile automatically look for USWDS packages in the proper directory using includePaths.
JS customization
Unfortunately, customizing the JavaScript for the USWDS currently requires NodeJS and a module bundler like Browserify or Webpack. We apologize for this inconvenience, and are working to resolve it in a future release of the design system.
USWDS JavaScript is separated into components (just as with the CSS and HTML) and initialized with event handlers when the DOM is ready. These components are accessible as CommonJS modules that can be required in other JavaScript files, then built for the browser. The components are not accessible in the global browser scope, but can be extended to be included by requiring components and setting it to a global scope:
window.uswds=require("./components");
Each component has a standardized interface that can be used to extend it further. The components store a HTML class (like .usa-accordion__button[aria-controls]) used to link HTML elements with the JavaScript component. When a component is initialized, it searches through the current HTML DOM to find all elements that match the class and initializes the component JavaScript for those elements. The primary methods for each component include:
on: Initialize a component's JavaScript behavior by passing the root element, such as window.document.
off: The opposite of on, de-initializes a component, removing any JavaScript event handlers on the component.
hide: Hide the whole component.
show: Shows a whole, hidden component.
toggle: Toggles the visibility of a component on and off based on the previous state.
Some components have additional methods based on that component's functionality. Any additional methods are found in that component's JavaScript file.
If you’re using a modern framework like React or Angular you can import components and initialize them in your library's DOM ready lifecycle event.
Importing a modular component.
importUSWDSfrom"@uswds/uswds/src/js";const{ characterCount, accordion }=USWDS;// deconstruct your components here// Alternativelyimportaccordionfrom"@uswds/uswds/js/usa-accordion";
⚠️Requires webpack 5+
React hooks example:
functionApp(){constref=document.body;useEffect(()=>{// initializecharacterCount.on(ref);// default ref is document.body, if you want to use// default you do not have to pass argumentsaccordion.on();// remove event listeners when the component un-mounts.return()=>{characterCount.off();accordion.off();};});}
Angular example:
exportclassAppimplementsOnInit{constructor(){this.ref=document.body;// default ref is document.body, if you want to use// default you do not have to pass arguments}ngOnInit(){// initializecharacterCount.on(this.ref);accordion.on();}// remove event listeners when the component un-mounts.ngOnDestroy(){characterCount.off();accordion.off();}}
Style theming and tokens
USWDS 3.0 provides extensive support for theming via its theme settings files introduced in Sass and theme settings, above.
Set theme settings with USWDS design tokens, not with values directly. They tend to be quoted strings like 'desktop' or 'md' or unitless numbers like 2 or -1.5. Tokens are the values passed into the USWDS functions and mixins that parse them. They are the keys that, through the mechanism of a function or mixin, unlock a value — they are not the values themselves.
请发表评论