Skip to content

Commit fc62449

Browse files
authored
Merge pull request #7055 from alphagov/bk-replace-import-with-use-contributing
Update contributing docs for sass modules
2 parents d38d5d2 + af97b95 commit fc62449

3 files changed

Lines changed: 46 additions & 13 deletions

File tree

docs/contributing/coding-standards/components.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Generally, folder and file names should be singular, for example ‘accordion’
1111
When creating your component, you should create the following files in the component’s folder:
1212

1313
- `README.md` - Summary documentation with links to the installation instructions and component documentation on <https://design-system.service.gov.uk/>
14-
- `_[component-name].scss` - An SCSS file to generate the styles for this component only. It delegates the CSS generation to the \_index.scss file.
15-
- `_index.scss` - The actual styles for the component that you can import in 2 ways - [on their own using `[component-name].scss`](https://frontend.design-system.service.gov.uk/importing-css-assets-and-javascript/#import-specific-parts-of-the-css) or [alongside other components in `components/_index.scss`](https://frontend.design-system.service.gov.uk/importing-css-assets-and-javascript/#import-specific-parts-of-the-css)
14+
- `_index.scss` - The main Sass entry point for the component. Generates the styles by including the mixin from the `_mixin.scss` file. You can include `_index.scss` [on its own](https://frontend.design-system.service.gov.uk/importing-css-assets-and-javascript/#import-specific-parts-of-the-css), or [alongside other components in `components/_index.scss`](https://frontend.design-system.service.gov.uk/importing-css-assets-and-javascript/#import-specific-parts-of-the-css)
15+
- `_mixin.scss` - The actual styles for the component, contained within a mixin. The `_index.scss` file includes the mixin. You should not include the `_mixin.scss` file directly.
1616
- `[component-name].yaml` - Lists the component's Nunjucks macro options and includes examples using these options. Both the options and examples are used to generate component documentation in the review app. The examples are also used to test component behaviour, and to generate [fixtures for testing alternative implementations of the design system](https://frontend.design-system.service.gov.uk/testing-your-html/).
1717
- `macro.njk` - The main entry point for rendering the component. It provides a `govuk[ComponentName](params)` macro, delegating render to the `template.njk` file
1818
- `template.njk` - The template used for rendering the component using any `params` provided to the macro
@@ -24,6 +24,11 @@ If your component uses JavaScript, you must also create the following files in t
2424
- `[component-name].unit.test.mjs` - Unit tests to verify any component-specific lower-level logic.
2525
- `[component-name].test.js` - Functional tests to verify the behaviour of the whole component
2626

27+
In version 7 of GOV.UK Frontend, we'll remove support for `@import` in Sass. Before then, you must include the following files to continue supporting `@import`:
28+
29+
- `_[component-name].import.scss` - An import-only SCSS file to generate the styles for just this component. It delegates the CSS generation to the `_index.import.scss` file, and emits a warning that this usage is deprecated.
30+
- `_index.import.scss` - An import-only SCSS file which generates the styles by including the mixin from the `_mixin.scss` file.
31+
2732
## Building your components
2833

2934
To help you build and initialise your own JavaScript components, GOV.UK Frontend provides some of its internal features for you to reuse. Follow our guidance on [building JavaScript components](https://frontend.design-system.service.gov.uk/building-your-own-javascript-components/#building-your-own-javascript-components) to use these features.

docs/contributing/coding-standards/css.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
1-
# CSS Style Guide
1+
# CSS and Sass Style Guide
2+
3+
## Sass modules
4+
5+
GOV.UK Frontend supports both `@import` and `@use` to include Sass files.
6+
7+
Our public API is prefixed with `govuk-` to ensure consistent naming and avoid
8+
clashes with Sass elements in your application. You should `@use` govuk-frontend
9+
modules without a namespace:
10+
11+
Bad:
12+
13+
```scss
14+
@use "foo";
15+
16+
.bar {
17+
color: foo.govuk-function("baz")
18+
}
19+
```
20+
21+
Good:
22+
23+
```scss
24+
@use "foo" as *;
25+
26+
.bar {
27+
color: govuk-function("baz")
28+
}
29+
```
230

331
## Class naming convention
432

5-
## `govuk` namespacing
33+
### `govuk` namespacing
634

735
All class names start with a `.govuk-` namespace to reduce the likelihood of
836
conflicting with existing classes in your application. It also helps to identify
@@ -11,7 +39,7 @@ where the styling for a particular element is coming from.
1139
If you are building components for your own application or framework you should
1240
use a different prefix, for example `.app-` or the initials of your department.
1341

14-
## Block Element Modifier (BEM)
42+
### Block Element Modifier (BEM)
1543

1644
GOV.UK Frontend uses the Block Element Modifier (BEM) methodology when naming
1745
CSS classes. This is designed to help developers understand how the different
@@ -300,7 +328,7 @@ margin: 1px 2px 3px;
300328
Bad:
301329

302330
```scss
303-
@import 'foo';
331+
@use 'foo';
304332

305333
$govuk-font-family-gds-transport: 'GDS Transport', arial, sans-serif;
306334

@@ -312,7 +340,7 @@ $govuk-font-family-gds-transport: 'GDS Transport', arial, sans-serif;
312340
Good:
313341

314342
```scss
315-
@import "foo";
343+
@use "foo";
316344

317345
$govuk-font-family-gds-transport: "GDS Transport", arial, sans-serif;
318346

@@ -323,20 +351,20 @@ $govuk-font-family-gds-transport: "GDS Transport", arial, sans-serif;
323351

324352
### Files should always have a final newline
325353

326-
### The basenames of `@import`ed SCSS partials should not begin with an underscore and should not include the filename extension
354+
### The basenames of `@use`ed SCSS partials should not begin with an underscore and should not include the filename extension
327355

328356
Bad:
329357

330358
```scss
331-
@import "_foo.scss";
332-
@import "_bar/foo.scss";
359+
@use "_foo.scss";
360+
@use "_bar/foo.scss";
333361
```
334362

335363
Good:
336364

337365
```scss
338-
@import "foo";
339-
@import "bar/foo";
366+
@use "foo";
367+
@use "bar/foo";
340368
```
341369

342370
### Properties should be formatted with a single space separating the colon from the property's value

packages/govuk-frontend/src/govuk/tools/_exports.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $govuk-imported-modules: () !default;
2424
///
2525
/// @param {String} $name - Name of module - must be unique within the codebase
2626
/// @content The passed content will only be outputted if a module of the same
27-
/// $name has not already been outputted
27+
/// $name has not already been outputted. Use only with `@import`, not `@use`.
2828
/// @access public
2929

3030
@mixin govuk-exports($name) {

0 commit comments

Comments
 (0)