debian-mirror-gitlab/doc/development/fe_guide/style/scss.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

190 lines
6.8 KiB
Markdown
Raw Normal View History

2020-01-01 13:55:28 +05:30
---
2021-01-29 00:20:46 +05:30
stage: none
group: unassigned
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2020-01-01 13:55:28 +05:30
disqus_identifier: 'https://docs.gitlab.com/ee/development/fe_guide/style_guide_scss.html'
---
# SCSS style guide
This style guide recommends best practices for SCSS to make styles easy to read,
easy to maintain, and performant for the end-user.
## Rules
2022-08-27 11:52:29 +05:30
Our CSS is a mixture of current and legacy approaches. That means sometimes it may be difficult to follow this guide to the letter; it means you are likely to run into exceptions, where following the guide is difficult to impossible without major effort. In those cases, you may work with your reviewers and maintainers to identify an approach that does not fit these rules. Try to limit these cases.
2020-05-24 23:13:21 +05:30
2020-01-01 13:55:28 +05:30
### Utility Classes
2020-05-24 23:13:21 +05:30
In order to reduce the generation of more CSS as our site grows, prefer the use of utility classes over adding new CSS. In complex cases, CSS can be addressed by adding component classes.
2020-01-01 13:55:28 +05:30
#### Where are utility classes defined?
2021-03-11 19:13:27 +05:30
Prefer the use of [utility classes defined in GitLab UI](https://gitlab.com/gitlab-org/gitlab-ui/-/blob/main/doc/css.md#utilities).
2021-10-27 15:23:28 +05:30
2021-03-11 19:13:27 +05:30
<!-- vale gitlab.Spelling = NO -->
2021-10-27 15:23:28 +05:30
2021-03-11 19:13:27 +05:30
An easy list of classes can also be [seen on Unpkg](https://unpkg.com/browse/@gitlab/ui/src/scss/utilities.scss).
2021-10-27 15:23:28 +05:30
2021-03-11 19:13:27 +05:30
<!-- vale gitlab.Spelling = YES -->
2020-01-01 13:55:28 +05:30
2021-03-11 19:13:27 +05:30
Classes in [`utilities.scss`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/assets/stylesheets/utilities.scss) and [`common.scss`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/assets/stylesheets/framework/common.scss) are being deprecated.
Classes in [`common.scss`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/assets/stylesheets/framework/common.scss) that use non-design-system values should be avoided. Use classes with conforming values instead.
2020-01-01 13:55:28 +05:30
2020-05-24 23:13:21 +05:30
Avoid [Bootstrap's Utility Classes](https://getbootstrap.com/docs/4.3/utilities/).
2020-01-01 13:55:28 +05:30
2021-02-22 17:27:13 +05:30
NOTE:
2020-10-24 23:57:45 +05:30
While migrating [Bootstrap's Utility Classes](https://getbootstrap.com/docs/4.3/utilities/)
2021-03-11 19:13:27 +05:30
to the [GitLab UI](https://gitlab.com/gitlab-org/gitlab-ui/-/blob/main/doc/css.md#utilities)
2020-10-24 23:57:45 +05:30
utility classes, note both the classes for margin and padding differ. The size scale used at
GitLab differs from the scale used in the Bootstrap library. For a Bootstrap padding or margin
utility, you may need to double the size of the applied utility to achieve the same visual
result (such as `ml-1` becoming `gl-ml-2`).
2023-03-04 22:38:38 +05:30
#### Where should you put new utility classes?
2020-01-01 13:55:28 +05:30
2023-03-04 22:38:38 +05:30
If a class you need has not been added to GitLab UI, you get to add it! Follow the naming patterns documented in the [utility files](https://gitlab.com/gitlab-org/gitlab-ui/-/tree/main/src/scss/utility-mixins) and refer to the [GitLab UI CSS documentation](https://gitlab.com/gitlab-org/gitlab-ui/-/blob/main/doc/contributing/adding_css.md#adding-utility-mixins) for more details, especially about adding responsive and stateful rules.
2020-05-24 23:13:21 +05:30
2021-11-18 22:05:49 +05:30
If it is not possible to wait for a GitLab UI update (generally one day), add the class to [`utilities.scss`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/assets/stylesheets/utilities.scss) following the same naming conventions documented in GitLab UI. A follow-up issue to backport the class to GitLab UI and delete it from GitLab should be opened.
2020-01-01 13:55:28 +05:30
2023-03-04 22:38:38 +05:30
#### When should you create component classes?
2020-01-01 13:55:28 +05:30
We recommend a "utility-first" approach.
1. Start with utility classes.
1. If composing utility classes into a component class removes code duplication and encapsulates a clear responsibility, do it.
2021-09-30 23:02:18 +05:30
This encourages an organic growth of component classes and prevents the creation of one-off non-reusable classes. Also, the kind of classes that emerge from "utility-first" tend to be design-centered (for example, `.button`, `.alert`, `.card`) rather than domain-centered (for example, `.security-report-widget`, `.commit-header-icon`).
2020-01-01 13:55:28 +05:30
Inspiration:
2020-10-24 23:57:45 +05:30
- <https://tailwindcss.com/docs/utility-first>
- <https://tailwindcss.com/docs/extracting-components>
2020-01-01 13:55:28 +05:30
### Naming
Filenames should use `snake_case`.
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;
}
```
2020-05-24 23:13:21 +05:30
Class names should be used instead of tag name selectors.
2021-02-22 17:27:13 +05:30
Using tag name selectors is discouraged because they can affect
unintended elements in the hierarchy.
2020-05-24 23:13:21 +05:30
```scss
// Bad
ul {
color: #fff;
}
// Good
.class-name {
color: #fff;
}
2020-01-01 13:55:28 +05:30
2021-02-22 17:27:13 +05:30
// Best
// prefer an existing utility class over adding existing styles
2021-04-17 20:07:23 +05:30
```
2020-01-01 13:55:28 +05:30
2021-02-22 17:27:13 +05:30
Class names are also preferable to IDs. Rules that use IDs
are not-reusable, as there can only be one affected element on
the page.
2020-01-01 13:55:28 +05:30
```scss
// Bad
2021-02-22 17:27:13 +05:30
#my-element {
padding: 0;
2020-01-01 13:55:28 +05:30
}
// Good
2021-02-22 17:27:13 +05:30
.my-element {
2020-01-01 13:55:28 +05:30
padding: 0;
}
```
### Selectors with a `js-` Prefix
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
renaming without breaking styling.
### Variables
Before adding a new variable for a color or a size, guarantee:
2021-02-22 17:27:13 +05:30
- There isn't an existing one.
2020-01-01 13:55:28 +05:30
- There isn't a similar one we can use instead.
2021-12-11 22:18:48 +05:30
### Using `extend` at-rule
Usage of the `extend` at-rule is prohibited due to [memory leaks](https://gitlab.com/gitlab-org/gitlab/-/issues/323021) and [the rule doesn't work as it should to](https://sass-lang.com/documentation/breaking-changes/extend-compound). Use mixins instead:
```scss
// Bad
.gl-pt-3 {
padding-top: 12px;
}
.my-element {
@extend .gl-pt-3;
}
// compiles to
.gl-pt-3, .my-element {
padding-top: 12px;
}
// Good
2022-08-13 15:12:31 +05:30
@mixin gl-pt-3 {
2021-12-11 22:18:48 +05:30
padding-top: 12px;
}
.my-element {
@include gl-pt-3;
}
// compiles to
.my-element {
padding-top: 12px;
}
```
2020-01-01 13:55:28 +05:30
## Linting
2021-04-17 20:07:23 +05:30
We use [stylelint](https://stylelint.io) to check for style guide conformity. It uses the
ruleset in `.stylelintrc` and rules from [our SCSS configuration](https://gitlab.com/gitlab-org/frontend/gitlab-stylelint-config). `.stylelintrc` is located in the home directory of the project.
2020-01-01 13:55:28 +05:30
2021-04-17 20:07:23 +05:30
To check if any warnings are produced by your changes, run `yarn lint:stylelint` in the GitLab directory. Stylelint also runs in GitLab CI/CD to
2020-01-01 13:55:28 +05:30
catch any warnings.
If the Rake task is throwing warnings you don't understand, SCSS Lint's
2023-01-13 00:05:48 +05:30
documentation includes [a full list of their rules](https://stylelint.io/user-guide/rules/).
2020-01-01 13:55:28 +05:30
### Fixing issues
If you want to automate changing a large portion of the codebase to conform to
2020-05-24 23:13:21 +05:30
the SCSS style guide, you can use [CSSComb](https://github.com/csscomb/csscomb.js). First install
2021-03-11 19:13:27 +05:30
[Node](https://github.com/nodejs/node) and [npm](https://www.npmjs.com/), then run `npm install csscomb -g` to install
2020-01-01 13:55:28 +05:30
CSSComb globally (system-wide). Run it in the GitLab directory with
`csscomb app/assets/stylesheets` to automatically fix issues with CSS/SCSS.
2021-02-22 17:27:13 +05:30
Note that this doesn't fix every problem, but it should fix a majority.