debian-mirror-gitlab/doc/development/fe_guide/style_guide_scss.md

282 lines
7 KiB
Markdown
Raw Normal View History

2016-06-02 11:05:42 +05:30
# SCSS styleguide
This style guide recommends best practices for SCSS to make styles easy to read,
easy to maintain, and performant for the end-user.
## Rules
2019-07-07 11:18:12 +05:30
### Utility Classes
2019-09-30 21:07:59 +05:30
2019-07-07 11:18:12 +05:30
As part of the effort for [cleaning up our CSS and moving our components into GitLab-UI](https://gitlab.com/groups/gitlab-org/-/epics/950)
led by the [GitLab UI WG](https://gitlab.com/gitlab-com/www-gitlab-com/merge_requests/20623) we prefer the use of utility classes over adding new CSS. However, complex CSS can be addressed by adding component classes.
2019-09-04 21:01:54 +05:30
#### Where are utility classes defined?
- [Bootstrap's Utility Classes](https://getbootstrap.com/docs/4.3/utilities/)
2019-12-21 20:55:43 +05:30
- [`common.scss`](https://gitlab.com/gitlab-org/gitlab/blob/master/app/assets/stylesheets/framework/common.scss) (old)
- [`utilities.scss`](https://gitlab.com/gitlab-org/gitlab/blob/master/app/assets/stylesheets/utilities.scss) (new)
2019-09-04 21:01:54 +05:30
#### Where should I put new utility classes?
2019-07-07 11:18:12 +05:30
2019-12-21 20:55:43 +05:30
New utility classes should be added to [`utilities.scss`](https://gitlab.com/gitlab-org/gitlab/blob/master/app/assets/stylesheets/utilities.scss). Existing classes include:
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
| Name | Pattern | Example |
|------|---------|---------|
| Background color | `.bg-{variant}-{shade}` | `.bg-warning-400` |
| Text color | `.text-{variant}-{shade}` | `.text-success-500` |
| Font size | `.text-{size}` | `.text-2` |
- `{variant}` is one of 'primary', 'secondary', 'success', 'warning', 'error'
2019-12-26 22:10:19 +05:30
- `{shade}` is one of the shades listed on [colors](https://design.gitlab.com/product-foundations/colors/)
2019-12-21 20:55:43 +05:30
- `{size}` is a number from 1-6 from our [Type scale](https://design.gitlab.com/product-foundations/typography)
2019-09-04 21:01:54 +05:30
#### When should I create component classes?
2019-09-30 21:07:59 +05:30
We recommend a "utility-first" approach.
2019-09-04 21:01:54 +05:30
1. Start with utility classes.
2019-10-12 21:52:04 +05:30
1. If composing utility classes into a component class removes code duplication and encapsulates a clear responsibility, do it.
2019-09-04 21:01:54 +05:30
2019-09-30 21:07:59 +05:30
This encourages an organic growth of component classes and prevents the creation of one-off unreusable classes. Also, the kind of classes that emerge from "utility-first" tend to be design-centered (e.g. `.button`, `.alert`, `.card`) rather than domain-centered (e.g. `.security-report-widget`, `.commit-header-icon`).
2019-09-04 21:01:54 +05:30
Examples of component classes that were created using "utility-first" include:
2019-07-31 22:56:46 +05:30
2019-12-21 20:55:43 +05:30
- [`.circle-icon-container`](https://gitlab.com/gitlab-org/gitlab/blob/579fa8b8ec7eb38d40c96521f517c9dab8c3b97a/app/assets/stylesheets/framework/icons.scss#L85)
- [`.d-flex-center`](https://gitlab.com/gitlab-org/gitlab/blob/900083d89cd6af391d26ab7922b3f64fa2839bef/app/assets/stylesheets/framework/common.scss#L425)
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
Inspiration:
2019-07-31 22:56:46 +05:30
2019-12-21 20:55:43 +05:30
- <https://tailwindcss.com/docs/utility-first/>
- <https://tailwindcss.com/docs/extracting-components/>
2019-07-07 11:18:12 +05:30
2016-06-02 11:05:42 +05:30
### Naming
2018-03-27 19:54:05 +05:30
Filenames should use `snake_case`.
2016-06-02 11:05:42 +05:30
CSS classes should use the `lowercase-hyphenated` format rather than
`snake_case` or `camelCase`.
```scss
// Bad
.class_name {
color: #fff;
}
// Bad
.className {
color: #fff;
}
// Good
.class-name {
color: #fff;
}
```
### Formatting
You should always use a space before a brace, braces should be on the same
line, each property should each get its own line, and there should be a space
between the property and its value.
```scss
// Bad
2017-08-17 22:00:37 +05:30
.container-item {
2016-06-02 11:05:42 +05:30
width: 100px; height: 100px;
margin-top: 0;
}
// Bad
.container-item
{
width: 100px;
height: 100px;
margin-top: 0;
}
// Bad
.container-item{
width:100px;
height:100px;
margin-top:0;
}
// Good
.container-item {
width: 100px;
height: 100px;
margin-top: 0;
}
```
2017-08-17 22:00:37 +05:30
Note that there is an exception for single-line rulesets, although these are
2016-06-02 11:05:42 +05:30
not typically recommended.
```scss
p { margin: 0; padding: 0; }
```
### Colors
2017-08-17 22:00:37 +05:30
HEX (hexadecimal) colors should use shorthand where possible, and should use
2016-06-02 11:05:42 +05:30
lower case letters to differentiate between letters and numbers, e.g. `#E3E3E3`
vs. `#e3e3e3`.
```scss
// Bad
p {
color: #ffffff;
}
// Bad
p {
color: #FFFFFF;
}
// Good
p {
color: #fff;
}
```
### Indentation
Indentation should always use two spaces for each indentation level.
```scss
// Bad, four spaces
p {
color: #f00;
}
// Good
p {
color: #f00;
}
```
### Semicolons
2017-08-17 22:00:37 +05:30
Always include semicolons after every property. When the stylesheets are
2016-06-02 11:05:42 +05:30
minified, the semicolons will be removed automatically.
```scss
// Bad
.container-item {
width: 100px;
height: 100px
}
// Good
.container-item {
width: 100px;
height: 100px;
}
```
### Shorthand
The shorthand form should be used for properties that support it.
```scss
// Bad
margin: 10px 15px 10px 15px;
padding: 10px 10px 10px 10px;
// Good
margin: 10px 15px;
padding: 10px;
```
### Zero Units
2017-08-17 22:00:37 +05:30
Omit length units on zero values, they're unnecessary and not including them
2016-06-02 11:05:42 +05:30
is slightly more performant.
```scss
// Bad
.item-with-padding {
padding: 0px;
}
// Good
.item-with-padding {
padding: 0;
}
```
### Selectors with a `js-` Prefix
2017-08-17 22:00:37 +05:30
Do not use any selector prefixed with `js-` for styling purposes. These
selectors are intended for use only with JavaScript to allow for removal or
2016-06-02 11:05:42 +05:30
renaming without breaking styling.
2017-08-17 22:00:37 +05:30
### IDs
2019-10-12 21:52:04 +05:30
2017-08-17 22:00:37 +05:30
Don't use ID selectors in CSS.
```scss
// Bad
#my-element {
padding: 0;
}
// Good
.my-element {
padding: 0;
}
```
### Variables
2019-02-15 15:39:39 +05:30
2017-08-17 22:00:37 +05:30
Before adding a new variable for a color or a size, guarantee:
2019-02-15 15:39:39 +05:30
- There isn't already one
- There isn't a similar one we can use instead.
2017-08-17 22:00:37 +05:30
2016-06-02 11:05:42 +05:30
## Linting
2019-12-21 20:55:43 +05:30
We use [SCSS Lint](https://github.com/sds/scss-lint) to check for style guide conformity. It uses the
2017-08-17 22:00:37 +05:30
ruleset in `.scss-lint.yml`, which is located in the home directory of the
2016-06-02 11:05:42 +05:30
project.
2017-08-17 22:00:37 +05:30
To check if any warnings will be produced by your changes, you can run `rake
scss_lint` in the GitLab directory. SCSS Lint will also run in GitLab CI to
2016-06-02 11:05:42 +05:30
catch any warnings.
2017-08-17 22:00:37 +05:30
If the Rake task is throwing warnings you don't understand, SCSS Lint's
2019-12-21 20:55:43 +05:30
documentation includes [a full list of their linters][scss-lint-documentation](https://github.com/sds/scss-lint/blob/master/lib/scss_lint/linter/README.md).
2016-06-02 11:05:42 +05:30
### Fixing issues
2017-08-17 22:00:37 +05:30
If you want to automate changing a large portion of the codebase to conform to
2016-06-02 11:05:42 +05:30
the SCSS style guide, you can use [CSSComb][csscomb]. First install
2017-08-17 22:00:37 +05:30
[Node][node] and [NPM][npm], then run `npm install csscomb -g` to install
CSSComb globally (system-wide). Run it in the GitLab directory with
2016-06-02 11:05:42 +05:30
`csscomb app/assets/stylesheets` to automatically fix issues with CSS/SCSS.
Note that this won't fix every problem, but it should fix a majority.
### Ignoring issues
2017-08-17 22:00:37 +05:30
If you want a line or set of lines to be ignored by the linter, you can use
2019-12-21 20:55:43 +05:30
`// scss-lint:disable RuleName` ([more info](https://github.com/sds/scss-lint#disabling-linters-via-source)):
2016-06-02 11:05:42 +05:30
```scss
2018-11-08 19:23:39 +05:30
// This lint rule is disabled because it is supported only in Chrome/Safari
// scss-lint:disable PropertySpelling
body {
text-decoration-skip: ink;
2016-06-02 11:05:42 +05:30
}
2018-11-08 19:23:39 +05:30
// scss-lint:enable PropertySpelling
2016-06-02 11:05:42 +05:30
```
Make sure a comment is added on the line above the `disable` rule, otherwise the
2017-08-17 22:00:37 +05:30
linter will throw a warning. `DisableLinterReason` is enabled to make sure the
style guide isn't being ignored, and to communicate to others why the style
2016-06-02 11:05:42 +05:30
guide is ignored in this instance.
[csscomb]: https://github.com/csscomb/csscomb.js
[node]: https://github.com/nodejs/node
[npm]: https://www.npmjs.com/