UNPKG

25.5 kBMarkdownView Raw
1# United States Web Design System
2
3[![CircleCI Build Status](https://img.shields.io/circleci/build/gh/uswds/uswds/develop?style=for-the-badge&logo=circleci)](https://circleci.com/gh/uswds/uswds/tree/develop) ![Snyk vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/uswds?style=for-the-badge) [![npm Version](https://img.shields.io/npm/v/uswds?style=for-the-badge)](https://www.npmjs.com/package/uswds) [![npm Downloads](https://img.shields.io/npm/dt/uswds?style=for-the-badge)](https://www.npmjs.com/package/uswds) [![GitHub issues](https://img.shields.io/github/issues/uswds/uswds?style=for-the-badge&logo=github)](https://github.com/uswds/uswds/issues) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4?style=for-the-badge)](https://github.com/prettier/prettier)
4
5The [United States Web Design System](https://designsystem.digital.gov) includes a library of open source UI components and a visual style guide for U.S. federal government websites.
6
7This repository is for the design system code itself. We maintain [another repository for the documentation and website](https://github.com/uswds/uswds-site). To see the design system and its documentation on the web, visit [https://designsystem.digital.gov](https://designsystem.digital.gov).
8
9## Contents
10
11- [Background](#background)
12- [Recent updates](#recent-updates)
13- [Getting started](#getting-started)
14- [Using the design system](#using-the-design-system)
15 - [Download](#download)
16 - [Install using `npm`](#install-using-npm)
17 - [Using the USWDS package](#using-the-uswds-package)
18 - [Sass and theme settings](#sass-and-theme-settings)
19 - [Sass compilation requirements](#sass-compilation-requirements)
20 - [JavaScript](#javascript)
21 - [Use another framework or package manager](#use-another-framework-or-package-manager)
22- [CSS architecture](#css-architecture)
23- [JS customization](#js-customization)
24- [Customization, theming, and tokens](#customization-theming-and-tokens)
25 - [Using tokens in theme settings](#using-tokens-in-theme-settings)
26 - [Set the base asset paths (fonts and images)](#set-the-base-asset-paths-fonts-and-images)
27 - [Main variables that can be customized](#main-variables-that-can-be-customized)
28- [Where things live](#where-things-live)
29- [Browser support](#browser-support)
30- [Accessibility](#accessibility)
31- [Fractal](#fractal)
32 - [Template compatibility](#template-compatibility)
33- [Need installation help?](#need-installation-help)
34- [Contributing to the code base](#contributing-to-the-code-base)
35- [Reuse of open-source style guides](#reuse-of-open-source-style-guides)
36- [Licenses and attribution](#licenses-and-attribution)
37
38## Background
39
40USWDS components and style guide follow industry-standard accessibility guidelines and use the best practices of existing style libraries and modern web design. The [U.S. Digital Service](https://www.usds.gov/) and [18F](https://18f.gsa.gov/) created USWDS for designers and developers. USWDS is a project of GSA’s [Technology Transformation Service](https://www.gsa.gov/about-us/organization/federal-acquisition-service/technology-transformation-services), maintained by the Office of Products and Programs. They are designed for use by government product teams who want to create beautiful, easy-to-use online experiences for the public. To learn more about the project, check out this [blog post](https://18f.gsa.gov/2015/09/28/web-design-standards/) and to view websites and applications check out our list [here](docs/WHO_IS_USING_USWDS.md).
41
42## Recent updates
43
44Information about the most recent release of the design system can always be found in the [release history](https://github.com/uswds/uswds/releases). We include details about significant updates and any backwards incompatible changes along with a list of all changes.
45
46## Getting started
47
48We’re glad you’d like to use the design system — here’s how you can get started:
49
50- Designers: [Check out our Getting Started for Designers information](https://designsystem.digital.gov/getting-started/designers/).
51 - [Design files of all the assets included in the design system are available for download](https://github.com/uswds/uswds-assets/archive/master.zip).
52- Developers: [Follow the instructions in this README to get started.](#using-the-design-system)
53 - [CSS, JavaScript, image, and font files of all the assets on this site are available for download](https://github.com/uswds/uswds/releases/latest).
54
55## Using the design system
56
57How you implement the design system depends on the needs of your project and your workstyle. We recommend implementing the design system with `npm`, but we also provide a direct download if `npm` will not work for you or your project.
58
59- **[Download the design system](#download)** if you are not familiar with `npm` and package management.
60
61- **[Use the design system `npm` package](#install-using-npm)** if you are familiar with using `npm` and package management.
62
63### Download
64
651. Download the [USWDS zip file](https://github.com/uswds/uswds/releases/download/v2.8.1/uswds-2.8.1.zip) from the latest USWDS release and open that file.
66
67 After extracting the zip file you should see the following file and folder structure:
68
69 ```
70 uswds-2.8.1/
71 ├── css/
72 │   ├── uswds.min.css.map
73 │   ├── uswds.min.css
74 │   └── uswds.css
75 ├── fonts/
76 ├── img/
77 ├── js/
78 │   ├── uswds.min.js.map
79 │   ├── uswds.min.js
80 │   └── uswds.js
81 └── scss/
82
83 ```
84
852. 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:
86
87 ```
88 example-project/
89 ├── assets/
90 │   ├── uswds-2.8.1/
91 │   ├── stylesheets/
92 │   ├── images/
93 │   └── javascript/
94 └── index.html
95 ```
96
97 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.
98
993. Reference the CSS and JavaScript files in each HTML page or dynamic templates in your project. We also provide Sass (.scss) files in the zip package which you can use to generate new CSS with project-specific settings. See [Sass and theme settings](#sass-and-theme-settings) for more information.
100
101 Here is an example of how to reference these assets in your `index.html` file:
102
103 ```html
104 <!DOCTYPE html>
105 <html>
106 <head>
107 <meta charset="utf-8" />
108 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
109 <title>My Example Project</title>
110 <link rel="stylesheet" href="assets/uswds-2.8.1/css/uswds.min.css" />
111 </head>
112 <body>
113 <script src="assets/uswds-2.8.1/js/uswds.min.js"></script>
114 </body>
115 </html>
116 ```
117
118We offer both the CSS and the JavaScript in two versions — minified and un-minified. (In the examples above, we are using the minified files.) Use the minified files in a production environment or to reduce the file size of your downloaded assets. Use the un-minified files if you are in a development environment or would like to debug the CSS or JavaScript assets in the browser.
119
120And that’s it — you should now be able to copy our code samples into your `index.html` and start using the design system.
121
122### Install using npm
123
124`npm` is a package manager for Node-based projects. USWDS maintains a [`uswds` package](https://www.npmjs.com/package/uswds) that includes both the pre-compiled and compiled files — just like the direct download. `npm` packages make it easy to update and install the design system from the command line.
125
1261. Install `Node/npm`. Below is a link to find the install method that coincides with your operating system:
127
128 - Node v12.13.0 (current LTS), [Installation guides](https://nodejs.org/en/download/)
129
130 **Note for Windows users:** If you are using Windows and are unfamiliar with `Node` or `npm`, we recommend following [Team Treehouse's tutorial](http://blog.teamtreehouse.com/install-node-js-npm-windows) for more information.
131
1322. Make sure you have installed it correctly:
133
134 ```shell
135 npm -v
136 6.13.0 # This line may vary depending on what version of Node you've installed.
137 ```
138
1393. 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.
140
1414. Add `uswds` to your project’s `package.json`:
142
143 ```shell
144 npm install --save uswds@latest
145 ```
146
147The `uswds` module is now installed as a dependency. You can use the un-compiled files found in the `node_modules/uswds/dist/` directory.
148
149```
150node_modules/uswds/
151├── dist/
152│   ├── css/
153│   ├── fonts/
154│   ├── img/
155│   ├── js/
156│ └── scss/
157```
158
159**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.
160
161#### Using the USWDS package
162
163If 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.
164
165USWDS uses the task manager [Gulp](http://gulpjs.com/) 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.
166
167The [`uswds-gulp` repo](https://github.com/uswds/uswds-gulp) is made for developers new to Gulp or those who just want a simple setup to get started quickly. The repo contains files and instructions for installing Gulp 4.0 in your project, initializing USWDS, and compiling CSS from the source files.
168
169#### Sass and theme settings
170
171The design system is customizable using the power of [Sass (Syntactically Awesome Style Sheets)](http://sass-lang.com/). The critical files you'll need in your project are those in `dist/scss/theme`:
172
173```
174scss
175├── components/
176├── core/
177├── elements/
178├── lib/
179├── packages/
180├── settings/
181├── theme/
182│   ├── _uswds-theme-color.scss
183│   ├── _uswds-theme-components.scss
184│   ├── _uswds-theme-custom-styles.scss
185│   ├── _uswds-theme-general.scss
186│   ├── _uswds-theme-spacing.scss
187│   ├── _uswds-theme-typography.scss
188│   ├── _uswds-theme-utilities.scss
189│   ├── styles.scss
190```
191
192- `_uswds-theme-color.scss`: theme color settings
193- `_uswds-theme-components.scss`: theme component settings
194- `_uswds-theme-custom-styles.scss`: additional project CSS for customizing or adding to what USWDS provides
195- `_uswds-theme-general.scss`: General theme settings
196- `_uswds-theme-spacing.scss`: Theme spacing settings
197- `_uswds-theme-typography.scss`: Theme typography settings
198- `_uswds-theme-utilities.scss`: Utility class output settings
199- `styles.scss`: The primary Sass file that you'll compile. It collects theme settings, USWDS source files, and custom CSS
200
201`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:
202
203```scss
204@import "uswds-theme-general";
205@import "uswds-theme-typography";
206@import "uswds-theme-spacing";
207@import "uswds-theme-color";
208@import "uswds-theme-utilities";
209@import "uswds";
210@import "uswds-theme-custom-styles";
211```
212
213**Technical note:** The `@import 'uswds'` statement above needs to reference the `uswds.scss` file in `node_modules/uswds/dist/scss`. The gulpfile in [`uswds-gulp`](https://github.com/uswds/uswds-gulp) is set up to do this automatically, using `gulp-sass` and `includePaths`.
214
215#### Sass compilation requirements
216
217The design system requires **autoprefixing** to work properly. This is included in the [`uswds-gulp`](https://github.com/uswds/uswds-gulp) package.
218
219**Autoprefixing** uses a service like [gulp-autoprefixer](https://github.com/sindresorhus/gulp-autoprefixer) to automatically add vendor prefixes to the precompiled stylesheets. Don't add vendor prefixes to your custom styles manually — it is more reliable to use autoprefixing. We use the following autoprefixer settings via `.browserslistrc` config:
220
221```
222> 2%
223last 2 versions
224IE 11
225not dead
226```
227
228> Note: **media query sorting** is no longer required as of USWDS 2.5.0. We stopped sorting media queries with [csso](https://github.com/css/csso) in USWDS 2.5.1 because it wasn't outputting as expected. While both the minified and unminified CSS files are modestly larger as a result: `268 KB` unsorted vs. `259 KB` sorted, our testing indicates that once the files are compressed server side with gzip, the unsorted CSS is actually smaller: `36 KB` unsorted and gzipped vs. `38 KB` sorted and gzipped. As a result, we recommend that teams do not use media query sorting at this time.
229
230We recommend using a **minifier** like [csso](https://github.com/css/csso) to compress your final compiled CSS and **sourcemaps** like [`gulp-sourcemaps`](https://www.npmjs.com/package/gulp-sourcemaps) to keep track of the location of all the source Sass for easier debugging.
231
232#### JavaScript
233
234`require('uswds')` will load all of USWDS’s JavaScript onto the page. Add this line to whatever initializer you use to load JavaScript into your application.
235
236### Use another framework or package manager
237
238If you’re using another 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 [download instructions](#download). Please note that the core team [isn’t responsible for all frameworks’ implementations](https://github.com/uswds/uswds/issues/877).
239
240If you’re interested in maintaining a package that helps us distribute USWDS, the project’s build system can help you create distribution bundles to use in your project. Please read our [contributing guidelines](CONTRIBUTING.md#building-the-project-locally-with--gulp-) to locally build distributions for your framework or package manager.
241
242## CSS architecture
243
244- The CSS foundation of this site is built with the **[Sass](https://sass-lang.com)** preprocessor language.
245- The CSS organization and naming conventions follow **[18F’s Front End Guide](https://frontend.18f.gov/css/)**.
246- We format our code with [Prettier](https://prettier.io/), per the formatting section of the **[18F Front End Guide](https://frontend.18f.gov/css/formatting/)**.
247- CSS selectors are **prefixed** with `usa` (For example: `.usa-button`). This identifier helps the design system avoid conflicts with other styles on a site which are not part of USWDS.
248- Uses a **[BEM](http://getbem.com/)** approach for naming CSS selectors. Blocks are separated from elements with two underscores (`__`). Multi-word blocks use single hyphens instead of spaces. Modifier classes are additive — proper markup requires the base class _and_ the modifier class or classes. Modifier classes consist of the base class plus a modifier suffix, separated by two hyphens (`--`) as in `.usa-button.usa-button--secondary` or `usa-accordion.usa-accordion--bordered`.
249- Uses **modular CSS** for scalable, modular, and flexible code.
250- Uses **nesting** when appropriate. Nest minimally with up to two levels of nesting.
251- Hard-coded magic numbers are avoided.
252- Media queries are built **mobile first**.
253- **Spacing units** are set with the `units()` function as described in [the USWDS 2.0 documentation](https://designsystem.digital.gov/design-tokens/spacing-units/). In general, we use spacing in multiples of `8px` — expressed as a multiple in `units([multiple])`. For instance `units(2)` is the equivalent of `2 * 8px` or `16px`. In the final, compiled CSS, this value will be expressed in rem, as a multiple of the base font size set with `$theme-base-font-size`.
254
255**For more information, visit:**
256[18F’s CSS Front End Guide](https://frontend.18f.gov/css/)
257
258## JS customization
259
260**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.**
261
262The JavaScript for the USWDS is separated into components in the same manner as the visual interface which is all initialized with event handlers when the DOM is ready. These components are accessible as CommonJS modules that can be required in other JavaScript files which then must be built for the browser. The components are currently not accessible in the global browser scope, but can be extended to be included by requiring `components` and setting it to a global scope:
263
264```js
265window.uswds = require("./components");
266```
267
268Each component has a standardized interface that can be used to extend it further. The components store a HTML class name (e.g. `.usa-accordion__button[aria-controls]`) that's used to link HTML elements with the JS component, so when a component is initialized, it will search through the current HTML DOM finding all elements that match its class and inialize the component JavaScript for those elements. The primary methods each component has are as follows:
269
270- `on`: Initialize a component's JavaScript behavior by passing the root element, such as `window.document`.
271- `off`: The opposite of `on`, de-initializes a component, removing any JavaScript event handlers on the component.
272- `hide`: Hide the whole component.
273- `show`: Shows a whole, hidden component.
274- `toggle`: Toggles the visibility of a component on and off based on the previous state.
275
276Some components have additional methods for manipulating specific aspects of them based on what they are and what they do. These can be found in the component's JS file.
277
278## Customization, theming, and tokens
279
280USWDS 2.0 provides extensive support for theming via its theme settings files introduced in [Sass and theme settings](#sass-and-theme-settings), above.
281
282Set 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.
283
284Visit the [Design tokens section](https://designsystem.digital.gov/design-tokens/) of USWDS 2.0 documentation for more on the available tokens for [color](https://designsystem.digital.gov/design-tokens/color), [spacing units](https://designsystem.digital.gov/design-tokens/spacing-units), [font size](https://designsystem.digital.gov/design-tokens/typesetting/font-size/), and more.
285
286### Using tokens in theme settings
287
288The following is an example of theme settings from `_uswds-theme-spacing.scss`:
289
290```scss
291$theme-site-max-width: "desktop";
292$theme-site-margins-breakpoint: "desktop";
293$theme-site-margins-width: 4;
294$theme-site-margins-mobile-width: 2;
295```
296
297The USWDS uses those tokens to build component styles:
298
299```scss
300.usa-example {
301 @include u-padding-x($theme-site-margins-mobile-width);
302 max-width: units($theme-site-max-width);
303
304 @include at-media($theme-site-margins-breakpoint) {
305 @include u-padding-x($theme-site-margins-width);
306 }
307}
308```
309
310This is the functional equivalent of:
311
312```scss
313.usa-example {
314 @include u-padding-x(2);
315 max-width: units("desktop");
316
317 @include at-media("desktop") {
318 @include u-padding-x(4);
319 }
320}
321```
322
323Which, if `$theme-respect-user-font-size` is set to `true` would output something like:
324
325```css
326.usa-example {
327 padding-left: 1rem;
328 padding-right: 1rem;
329 max-width: 64rem;
330}
331
332@media screen and (min-width: 64em) {
333 .usa-example {
334 padding-left: 2rem;
335 padding-right: 2rem;
336 }
337}
338```
339
340In general, USWDS sets **variables** with **tokens**, and passes those variables into **functions and mixins** in the source Sass.
341
342### Set the base asset paths (fonts and images)
343
344The values of `$theme-font-path` and `$theme-image-path` will be appended to USWDS font paths and image paths, respectively:
345
346```
347$theme-font-path: '../fonts';
348$theme-image-path: '../img';
349```
350
351## Where things live
352
353- **HTML** markup for the components is located in: `src/components` in the site root. These, however, are written in the templating language Nunjucks. It's best to get HTML source markup directly from [designsystem.digital.gov/components](https://designsystem.digital.gov/components)
354- **Sass** stylesheets are located in: `dist/scss/ (/core, /elements, /components)`. **Compiled CSS** is located in the [downloadable zip file](https://github.com/uswds/uswds/releases/latest).
355- **JS** is located in: `src/js (/components, /utils)`.
356- **Fonts** are located in: `dist/fonts`.
357- **Images** and icons are located in: `dist/img`.
358
359## Browser support
360
361We’ve designed the design system to support older and newer browsers through [progressive enhancement](https://en.wikipedia.org/wiki/Progressive_enhancement). The current major version of the design system (2.0) follows the [2% rule](https://gds.blog.gov.uk/2012/01/25/support-for-browsers/): we officially support any browser above 2% usage as observed by [analytics.usa.gov](https://analytics.usa.gov/). Currently, this means that the design system version 2.0 supports the newest versions of Chrome, Firefox, Safari, and Internet Explorer 11 and up.
362
363## Accessibility
364
365The design system also meets the [WCAG 2.0 AA accessibility guidelines](https://www.w3.org/TR/WCAG20/) and conforms to the standards of [Section 508 of the Rehabilitation Act](http://www.section508.gov/). We’re happy to answer questions about accessibility — email us for more information.
366
367## Fractal
368
369We're using [Fractal](http://fractal.build) to generate an interactive component library for the design system. You can run it locally after `npm install` with:
370
371```sh
372npm start
373```
374
375Then, visit [http://localhost:3000/](http://localhost:3000/) to see the design system in action.
376
377_**Optional**: To re-build when code changes are made, run the following command from the project directory in a separate terminal window:_
378
379```sh
380npm run watch
381```
382
383### Template compatibility
384
385Many of our Fractal view templates are compatible with [Nunjucks](https://mozilla.github.io/nunjucks/) (for JavaScript/Node), [Jinja](http://jinja.pocoo.org/docs/2.9/) (Python), and [Twig](https://twig.sensiolabs.org/) (PHP) out of the box. Components that reference other components use a Fractal-specific `{% render %}` tag that will either need to be implemented in other environments or replaced with the appropriate `{% include %}` tags.
386
387## Long-term support of v1.x
388
389[Version 1.x](https://v1.designsystem.digital.gov) is in maintenance mode. We will only make critical updates like security patches.
390
391## Need installation help?
392
393Do you have questions or need help with setup? Did you run into any weird errors while following these instructions? Feel free to open an issue here:
394
395[https://github.com/uswds/uswds/issues](https://github.com/uswds/uswds/issues).
396
397You can also email us directly at uswds@gsa.gov.
398
399## Contributing to the code base
400
401For complete instructions on how to contribute code, please read [CONTRIBUTING.md](CONTRIBUTING.md). These instructions also include guidance on how to set up your own copy of the design system style guide website for development.
402
403If you would like to learn more about our workflow process, check out the [Workflow](https://github.com/uswds/uswds/wiki/Workflow) and [Issue label Glossary](https://github.com/uswds/uswds/wiki/Issue-label-glossary) pages on the wiki.
404
405If you have questions or concerns about our contributing workflow, please contact us by [filing a GitHub issue](https://github.com/uswds/uswds/issues) or [emailing our team](mailto:uswebdesignstandards@gsa.gov).
406
407## Reuse of open-source style guides
408
409Much of the guidance in USWDS leans on open source designs, code, and patterns from other civic and government organizations, including:
410
411- Consumer Financial Protection Bureau’s [Design Manual](https://cfpb.github.io/design-manual/)
412- U.S. Patent and Trademark Office’s [Design Patterns](http://uspto.github.io/designpatterns/)
413- Healthcare.gov [Style Guide](http://styleguide.healthcare.gov/)
414- UK’s Government Digital Service’s [UI Elements](http://govuk-elements.herokuapp.com/)
415- Code for America’s Chime [Styleguide](https://github.com/chimecms/chime-starter)
416- Pivotal Labs [Component Library](http://styleguide.cfapps.io/)
417
418## Licenses and attribution
419
420A few parts of this project are not in the public domain. Attribution and licensing information for those parts are described in detail in [LICENSE.md](LICENSE.md).
421
422The rest of this project is in the worldwide public domain, released under the [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/).
423
424## Contributing
425
426All contributions to this project will be released under the CC0 dedication alongside the public domain portions of this project. For more information, see [CONTRIBUTING.md](CONTRIBUTING.md).