This style guide targets Chromium frontend features implemented with JavaScript, CSS, and HTML. Developers of these features should adhere to the following rules where possible, just like those using C++ conform to the Chromium C++ styleguide.
This guide follows and builds on:
When designing a feature with web technologies, separate the:
This highlights the concern of each part of the code and promotes looser coupling (which makes refactor easier down the road).
Another way to envision this principle is using the MVC pattern:
MVC Component | Web Component |
---|---|
Model | HTML |
View | CSS |
Controller | JS |
It's also often appropriate to separate each implementation into separate files.
DO:
<!-- missile-button.html --> <link rel="stylesheet" href="warnings.css"> <b class="warning">LAUNCH BUTTON WARNING</b> <script src="missile-button.js">
/* warnings.css */ .warning { color: red; }
// missile-button.js document.querySelector('b').onclick = fireZeeMissiles;
DON'T:
<!-- missile-button.html --> <b style="color: red;" onclick="fireZeeMissiles()">LAUNCH BUTTON WARNING</b>
on-event
-style event listener wiring and <style>
tags that live inside of .html files.See the Google HTML/CSS Style guide.
<!doctype html> <html dir="$i18n{direction}"> <head> <meta charset="utf-8"> <title>$i18n{myFeatureTitle}</title> <link rel="icon" href="feature.png"> <link rel="stylesheet" href="feature.css"> <script src="feature.js"></script> </head> … </html>
Specify <!doctype html>
.
Set the dir
attribute of the html element to the localized ‘textdirection’ value. This flips the page visually for RTL languages and allows html[dir=rtl]
selectors to work.
Specify the charset, UTF-8.
Link in image, icon and stylesheet resources.
style="..."
attributes.Include the appropriate JS scripts.
on-click
are allowed and often reduce the amount of addressing (adding an ID just to wire up event handling).<h3>$i18n{autofillAddresses}</h3> <div class="settings-list"> <list id="address-list"></list> <div> <button id="autofill-add-address">$i18n{autofillAddAddress}</button> </div> </div> <if expr="chromeos"> <a href="https://www.google.com/support/chromeos/bin/answer.py?answer=142893" target="_blank">$i18n{learnMore}</a> </if>
Element IDs use dash-form
camelCase
is allowed in Polymer code for easier this.$.idName
access.Localize all strings using $i18n{}
Use camelCase for $i18n{} keys names.
Add 2 space indentation in each new block.
Adhere to the 80-column limit.
Use double-quotes instead of single-quotes for all attributes.
Don't close single tags
<input type="radio">
<input type="radio" />
<custom-elements>
and some HTML elements like <iframe>
require closing.Use the button
element instead of <input type="button">
.
Do not use <br>
; place blocking elements (<div>
) as appropriate.
Do not use spacing-only divs; set the margins on the surrounding elements.
Only use <table>
elements when displaying tabular data.
Do not use the for
attribute of <label>
<input>
inside the <label>
<select>
, use aria-labelledby
See the Google HTML/CSS style guide (and again, browser compatibility issues are less relevant for Chrome-only code).
.raw-button, .raw-button:hover, .raw-button:active { --sky-color: blue; -webkit-margin-collapse: discard; background-color: rgb(253, 123, 42); background-repeat: no-repeat; border: none; min-width: 0; padding: 1px 6px; }
Specify one selector per line.
@keyframe
(see below).Opening brace on the same line as the last (or only) selector.
Two-space indentation for each declaration, one declaration per line, terminated by a semicolon.
Use shorthand notation when possible.
Alphabetize properties.
-webkit
properties should be listed at the top, sorted alphabetically.--variables
should be alphabetically declared when possible.Insert a space after the colon separating property and value.
Do not create a class for only one element; use the element ID instead.
When specifying length values, do not specify units for a zero value, e.g., left: 0px;
becomes left: 0;
hsl(5, 0%, 90%)
or within @keyframe directives, e.g:@keyframe animation-name { 0% { /* beginning of animation */ } 100% { /* end of animation */ } }
Use single quotes instead of double quotes for all strings.
Don't use quotes around url()
s unless needed (i.e. a data:
URI).
Class names use dash-form
.
If time lengths are less than 1 second, use millisecond granularity.
transition: height 200ms;
transition: height 0.2s;
Use two colons when addressing a pseudo-element (i.e. ::after
, ::before
, ::-webkit-scrollbar
).
Use scalable font-size
units like %
or em
to respect users' default font size
Don't use CSS Mixins (--mixin: {}
or @apply --mixin;
) in new code. We're removing them.
When possible, use named colors (i.e. white
, black
) to enhance readability.
Prefer rgb()
or rgba()
with decimal values instead of hex notation (#rrggbb
).
#333
)If the hex value is #rrggbb
, use the shorthand notation #rgb
.
background-image: url(chrome://resources/images/path/to/image.svg);
.suboption { margin-inline-start: 16px; } #save-button { color: #fff; left: 10px; } html[dir='rtl'] #save-button { right: 10px; }
Use RTL-friendly versions of things like margin
or padding
where possible:
margin-left
-> margin-inline-start
padding-right
-> padding-inline-end
text-align: left
-> text-align: start
text-align: right
-> text-align: end
left
for [dir='ltr']
and right
for [dir='rtl']
For properties that don't have an RTL-friendly alternatives, use html[dir='rtl']
as a prefix in your selectors.
New WebUI code (except for ChromeOS specific code) should be written in TypeScript.
See the Google JavaScript Style Guide as well as ECMAScript Features in Chromium.
Use $('element-id')
instead of document.getElementById
. This function can be imported from util.m.js.
Use single-quotes instead of double-quotes for all strings.
clang-format
now handles this automatically.Use ES5 getters and setters
@type
(instead of @return
or @param
) for JSDoc annotations on getters/settersFor legacy code using closure, see Annotating JavaScript for the Closure Compiler for @ directives
Prefer event.preventDefault()
to return false
from event handlers
Prefer this.addEventListener('foo-changed', this.onFooChanged_.bind(this));
instead of always using an arrow function wrapper, when it makes the code less verbose without compromising type safety (for example in TypeScript files).
Closure compiler should only be used by legacy code that has not yet been converted to use TypeScript.
Use the closure compiler to identify JS type errors and enforce correct JSDoc annotations.
Add a BUILD.gn
file to any new web UI code directory.
Ensure that your BUILD.gn
file is included in src/BUILD.gn:webui_closure_compile
(or somewhere in its deps hierarchy) so that your code is typechecked in an automated way.
Type Polymer elements by appending ‘Element’ to the element name, e.g. /** @type {IronIconElement} */
Use explicit nullability in JSDoc type information
@type {Object}
use:{!Object}
for only Object{!Object|undefined}
for an Object that may be undefined{?Object}
for Object that may be nullDon't add a .
after template types
Array<number>
Array.<number>
Don‘t specify string in template object types. That’s the only type of key Object
can possibly have.
Object<T>
Object<string, T>
Use template types for any class that supports them, for example:
Array
CustomEvent
Map
Promise
Set
Also see the Google Polymer Style Guide.
html_to_wrapper('html_wrapper_files') { in_files = [ 'my_app.html' ] }
import {PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; import {getTemplate} from './my_app.html.js'; class MyAppElement extends PolymerElement { static get is() { return 'my-app'; } static get template() { return getTemplate(); } static get properties() { return { foo: String, }; } foo: string; } customElements.define(MyAppElement.is, MyAppElement);
Use a consistent ordering for common methods (or, in legacy code, the parameters passed to Polymer()):
is
behaviors
(legacy code only)properties
(public, then private)hostAttributes
listeners
, observers
created
, ready
, attached
, detached
Use camelCase for element IDs to simplify local DOM accessors (i.e. this.$.camelCase
instead of this.$['dash-case']
).
Note: In TypeScript, the this.$.camelCase
accessor requires adding an interface:
interface MyAppElement { $: { camelCase: HTMLElement, }; }
this.foo
instead of newFoo
arguments in observers when possible. This makes changing the type of this.foo
easier (as the @type
is duplicated in less places, i.e. @param
).static get properties() { return { foo: {type: Number, observer: 'fooChanged_'}, }; } /** @private */ fooChanged_() { this.bar = this.derive(this.foo); }
Use native on-click
for click events instead of on-tap
. ‘tap’ is a synthetic event provided by Polymer for backward compatibility with some browsers and is not needed by Chrome.
Make good use of the dom-if
template:
Consider using dom-if
to lazily render parts of the DOM that are hidden by default. Also consider using cr-lazy-render
instead.
Only usedom-if
if the DOM subtree is non-trivial, defined as:
For trivial DOM subtrees using the HTML hidden
attribute yields better performance, than adding a custom dom-if
element.
Do not add iron-icons dependency to third_party/polymer/.
iron-icons
library, but importing each of the iconsets means importing hundreds of SVGs, which is unnecessary because Chrome uses only a small subset.chrome/browser/resources/settings/icons.html
.ui/webui/resources/cr_elements/icons.html
.Grit is a tool that runs at compile time to pack resources together into Chromium. Resources are packed from grd files. Most Chromium WebUI resources should be located in autogenerated grd files created by the generate_grd gn rule.
Sometimes it is helpful to selectively include or exclude code at compile-time. This is done using the preprocess_if_expr gn rule, which makes use of a subset of grit that reads and processes files for <if expr>
without running the entire grit resource packing process. Files that require preprocessing are passed to the rule as in_files. Preprocessed versions with the same names will be written to the specified out_folder and are listed in out_manifest, which can be passed to the generate_grd rule to generate entries for them in a grd file.
The following BUILD.gn example code uses preprocess_if_expr to preprocess any <if expr>
in my_app.ts and in the my_app.html.ts file that is generated by the earlier html_to_wrapper example. It then runs the TypeScript compiler on the outputs of this operation and uses the manifest from this operation and the in_files option to place both the final, preprocessed file and a separate (not preprocessed) icon into a generated grd file using generate_grd:
preprocess_folder = "preprocessed" preprocess_manifest = "preprocessed_manifest.json" preprocess_if_expr("preprocess") { in_folder = "." in_files = [ "my_app.ts" ] out_folder = "$target_gen_dir/$preprocess_folder" } # Read file from target_gen_dir, where it will be pasted by html_to_wrapper. preprocess_if_expr("preprocess_generated") { in_folder = target_gen_dir in_files = [ "my_app.html.ts" ] out_folder = "$target_gen_dir/$preprocess_folder" deps = [ ":html_wrapper_files" ] } # Run TS compiler on the two files: ts_library("build_ts") { root_dir = "$target_gen_dir/$preprocess_folder" out_dir = "$target_gen_dir/tsc" tsconfig_base = "tsconfig_base.json" in_files = [ "my_app.html.ts", "my_app.ts", ] deps = [ "//third_party/polymer/v3_0:library", "//ui/webui/resources:library", ] extra_deps = [ ":preprocess", ":preprocess_generated", ] } # Put the compiled files as well as a separate my_icon.svg file in the grd: generate_grd("build_grd") { input_files = [ "my_icon.svg" ] input_files_base_dir = rebase_path(".", "//") deps = [ ":build_ts" ] manifest_files = [ "$target_gen_dir/tsconfig.manifest" ] grd_prefix = [ "foo" ] out_grd = "$target_gen_dir/resources.grd" }
Note #1: In a few legacy resources, preprocessing is enabled by adding the preprocess="true"
attribute inside of a .grd
file on <structure>
and <include>
nodes.
Note #2: These preprocessor statements can live in places that surprise linters or formatters (for example: running clang-format on a .ts file with an <if>
in it). Generally, putting these language-invalid features inside of comments helps alleviate problems with unexpected input.
<if>
tags allow conditional logic by evaluating an expression in a compile-time environment of grit variables. These allow conditionally including or excluding code.
Example:
function isWindows(): boolean { // <if expr="win"> return true; // </if> return false; }
<include src="[path]">
reads the file at path
and replaces the <include>
tag with the file contents of [path]
. Don't use <include>
in new JS code; it is being removed. Instead, use JS imports. If there is concern about importing a large number of JS files, the optimize_webui build rule supports bundling pages using Rollup.
Some legacy UIs use Grit to read and inline resources via flattenhtml="true"
. This option should not be used in new code; instead, use JS imports and bundling as needed. Icons can also be placed in an iconset, to avoid importing them individually.