# CSS/Sass Coding Guidelines ## Principles We're transitioning to a component-based design system. When styling, adhere to the following principles: - Rely on the Design System components (@wordpress/components, @automattic/components and other packages), props and variants as much as possible. - If a design to be implemented doesn't match the existing components, discuss with the design team to see if the design should be adapted. - Consider whether the design system components need to be adapted/modified to accommodate the proposed designs. - Avoid writing custom CSS/Sass unless absolutely necessary. - If you do need to write custom CSS/Sass, follow the guidelines outlined on this document. For all questions related to the design system, available components or design considerations, consider asking in #components or #design-system Slack channels. ## Introduction Every stylesheet should be easy to read, scan, add to, and collaborate on. Our current system and nomenclature builds on top of _components_, where CSS files live alongside the component they are styling: `component/style.scss`. These files are all imported into the React components' JavaScript sources and then bundled by webpack to the production CSS files. This is an example of a declaration: `.component__element.is-modifier {}` Two important considerations: - The `component` fragment matches the folder name of the React component it's providing styles to. - The `.is-modifier` class should never be written on its own outside of the component class. This avoids naming collisions and provides a consistent use of modifiers. - We don't use `#ids` for style purposes. ## Example Take a component called `site/index.jsx` that renders a site item on the picker. Imagine we are going to set the color for the site title to #333 and change it to #444 when the site is a Jetpack site. Our Sass file will sit alongside `site/index.jsx` and be called `site/style.scss`. Its content will be written like: **Good** ```scss .site__title { color: #333; &.is-jetpack { color: #444; } } ``` **Bad** ```scss .site { .title { color: #333; .jetpack { color: #444; } } } ``` The modifier classes are always attached to a base element (the wrapper of a component). Since every element will have a direct class to be selected with, we avoid descendant selectors and child selectors. We also avoid indenting selectors in Sass files in favor of single selectors. The main exceptions are precisely the `is-modifier` class, and the cases where the context needs to change a given component's accidents. ```scss // Modify 'site__title' for in CurrentSite component's display .current-site .site__title { color: #444; } ``` The expressiveness of the selector above clearly conveys that there are two separate components involved (CurrentSite and Site). We also keep CSS specificity in check. Avoid using Sass indents for simple selectors — they obfuscate the cascade. It's important to note that we don't reuse classes, we reuse components. ## Components These practices mean that a component will work anywhere it is included by default, and that the developer won't need to go hunting for the relevant CSS. Any modifications will happen in the context/parents, and will be minimal in nature. Where and how changes will affect the rendering of the app will thus be clearer. (If you edit the button-component's Sass file you know you are editing it for everyone.) ## General Syntax and Writing Rules Apart from the above structure, please adhere to these guidelines: - We use [Stylelint](https://stylelint.io/) to enforce a consistent code style. You can check for style lints by running `yarn lint:css`. Please see [IDE setup](#ide-setup-auto-formatting) section for more details. - Follow the [WordPress CSS Coding Standards](https://make.wordpress.org/core/handbook/coding-standards/css/), unless it contradicts something stated in this document. - Avoid `#` selectors for style purposes. Their specificity becomes troublesome to manage fairly quickly. - Don't use the `!important` declaration. If you think you need to do it, consider fixing the root cause. - Avoid using universal selectors (`*`). - Use hyphens, not underscores or camelCase, when naming things like IDs, classes, variable names, mixins, placeholders. Good: `.site-title`, Bad: `.siteTitle` or `.site_title`. - The only exception is the `__` syntax to signal the relationship within a component. - Avoid using over-qualified selectors like `div.my-class`. ## IDE setup (auto-formatting) In order to enable auto formatting of style files, please set up your IDE. ### VS Code To get things set up in VS Code, do the following: - Run `yarn install` - Install the official Stylelint extension - [`stylelint.vscode-stylelint`](https://marketplace.visualstudio.com/items?itemName=stylelint.vscode-stylelint) - Adjust the following settings in `settings.json` - (`Cmd + Shift + P` > `Preferences: Open Settings (JSON))`: ```jsonc { "stylelint.validate": [ "css", "scss", "sass" ], "[css][scss][sass]": { "editor.formatOnSave": false, "editor.defaultFormatter": "stylelint.vscode-stylelint" }, "editor.codeActionsOnSave": { "source.fixAll.stylelint": true // other actions go here } } ``` - Reload VS Code: `Cmd + Shift + P` > `Developer: Reload Window` - Open a Sass file e.g. `client/me/security-2fa-initial-setup/style.scss` - Mess up the formatting (e.g. use spaces for indentation.) - Save the file and verify the problems are fixed. ## Classes Classes are the fundamental building block of our stylesheets. Using them appropriately and consistently is important to keep a maintainable and enjoyable codebase. - Generic class names are deemphasized in favor of component-based structures. Instead of defining generic classes in a shared stylesheet, we construct components that come with both markup and styles ready to be used. - Choose semantic class names based on hierarchy and content, not on positioning or visual appearance. (Example: `.site__meta-info`, instead of `.site-menu-small` or `.site-top-section`). - Avoid redundant bits of information that are provided by the HTML element or other contexts. (Don't do `.site__content-box` on a `div`, the `box` is not necessary.) - We keep one level of prefix for single components present in the class name: `.site__title` instead of `.site .title`. This may seem verbose but it provides more flexibility, clear direct selectors, and immediate recognizability of its role in a large codebase like Calypso. Classes are not just for style purposes, they should provide meaning to the reader parsing the document. - Keep class names lean and to the point. Name classes the same way you would describe what the content of an element is to a stranger. ## The DRY principle Calypso already provides helpers for many common solutions. Please, use them! We are transitioning towards a component-based structure where each React component will have its own stylesheet. However, there will be a few files that are by its nature shared resources across all components. (Colors, typography, some mixins, etc.) - Don't use custom colors, always utilize what `@wordpress/base-styles` provides. If you have to set a color, use lowercase hex values, and shorten them to their smallest expression (like `#aaa`). - Render icons using `@wordpress/icons`. As a fallback, you can also use ``. - Calypso runs Sass with autoprefixer, that means you DON'T need to directly use vendor specific properties. ## Sass Guidelines Currently, all component based Sass files are imported into the respective JavaScript sources (using `import './style.scss'` statements). They are compiled by webpack as part of the bundling process into CSS chunks that are then loaded into the browser at runtime. Remember that all styles, even when loaded at different times, eventually end up on one page as part of a single HTML document. Make sure you namespace your styles for the page you are working on. Under the hood, we are using webpack and its `sass-loader`, for compiling the styles with `node-sass` (a C++ implementation of the Sass compiler which is working on parity with the reference Ruby implementation) and `mini-css-extract-plugin`, for creating the CSS chunks to be loaded as `