Skip to content

Commit 5c9215c

Browse files
gillkyleMarcy Sutton
and
Marcy Sutton
authored
docs(www): 25 Workflows - Adding CSS and/or Sass (#14779)
* chore: cleanup readme for sass plugin * fix: missing guidelist components * fix: consistency between similar docs, headings, and adding beginnings of sass guide * docs: adding to sass guide * Update docs/docs/sass.md Co-Authored-By: Marcy Sutton <[email protected]> * Update packages/gatsby-plugin-sass/README.md Co-Authored-By: Marcy Sutton <[email protected]> * Apply suggestions from code review Co-Authored-By: Marcy Sutton <[email protected]>
1 parent 48a9e10 commit 5c9215c

File tree

7 files changed

+126
-48
lines changed

7 files changed

+126
-48
lines changed

docs/docs/css-in-js.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ _Note that this functionality is not a part of React or Gatsby, and requires usi
2020
2121
This section contains guides for styling your site with some of the most popular CSS-in-JS libraries, including how to set up global styles using each library.
2222

23-
[[guidelist]]
23+
<GuideList slug={props.slug} />

docs/docs/css-libraries-and-frameworks.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ There are many other CSS libraries and frameworks that you can use in your Gatsb
77

88
These are not full-on approaches to styling, and generally work no matter which styling approach you've chosen for your website. They require installing third-party libraries, often with the help of Gatsby community plugins.
99

10-
[[guidelist]]
10+
<GuideList slug={props.slug} />

docs/docs/post-css.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ title: PostCSS
44

55
PostCSS transforms extended syntaxes and features into modern, browser-friendly CSS. This guide will show you how to get started with Gatsby and PostCSS.
66

7-
### Installation and Configuration
7+
## Installing and Configuring PostCSS
88

99
This guide assumes that you have a Gatsby project set up. If you need to set up a project, head to the [**Quick Start guide**](/docs), then come back.
1010

11-
1. Install the Gatsby plugin [**gatsby-plugin-postcss**](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-postcss).
11+
1. Install the Gatsby plugin [**gatsby-plugin-postcss**](/packages/gatsby-plugin-postcss/).
1212

1313
`npm install --save gatsby-plugin-postcss`
1414

@@ -18,13 +18,11 @@ This guide assumes that you have a Gatsby project set up. If you need to set up
1818
plugins: [`gatsby-plugin-postcss`],
1919
```
2020

21-
3. Write your stylesheets using PostCSS (.css files) and require or import them as normal.
22-
23-
If you need to pass options to PostCSS use the plugins options; see [postcss-loader](https://github.com/postcss/postcss-loader) for all available options.
21+
> **Note**: If you need to pass options to PostCSS use the plugins options; see [postcss-loader](https://github.com/postcss/postcss-loader) for all available options.
2422
25-
#### Syntax example
23+
3. Write your stylesheets using PostCSS (.css files) and require or import them as normal.
2624

27-
```css
25+
```css:styles.css
2826
@custom-media --med (width <= 50rem);
2927

3028
@media (--med) {
@@ -36,6 +34,10 @@ If you need to pass options to PostCSS use the plugins options; see [postcss-loa
3634
}
3735
```
3836

37+
```javascript
38+
import "./styles.css"
39+
```
40+
3941
### With CSS Modules
4042

4143
Using CSS modules requires no additional configuration. Simply prepend `.module` to the extension. For example: `App.css -> App.module.css`. Any file with the module extension will use CSS modules.
@@ -69,6 +71,6 @@ module.exports = () => ({
6971
})
7072
```
7173

72-
### Other resources
74+
## Other resources
7375

7476
- [Introduction to postcss](https://www.smashingmagazine.com/2015/12/introduction-to-postcss/)

docs/docs/sass.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: Using Sass in Gatsby
3+
---
4+
5+
[Sass](https://sass-lang.com) is an extension of CSS, adding nested rules, variables, mixins, selector inheritance, and more. In Gatsby, Sass code can be translated to well-formatted, standard CSS using a plugin.
6+
7+
Sass will compile `.sass` and `.scss` files to `.css` files for you, so you can write your stylesheets with more advanced features.
8+
9+
> **Note**: the difference between using a `.sass` or `.scss` file is the syntax that you write your styles in. All valid CSS is valid SCSS as well so it is the easiest to use and most popular. You can read more about the differences in the [Sass documentation](https://sass-lang.com/documentation/syntax).
10+
11+
## Installing and Configuring Sass
12+
13+
This guide assumes that you have a Gatsby project set up. If you need to set up a project, head to the [**Quick Start guide**](/docs), then come back.
14+
15+
1. Install the Gatsby plugin [**gatsby-plugin-sass**](/packages/gatsby-plugin-sass/).
16+
17+
`npm install --save gatsby-plugin-sass`
18+
19+
2. Include the plugin in your `gatsby-config.js` file.
20+
21+
```javascript:title=gatsby-config.js
22+
plugins: [`gatsby-plugin-sass`],
23+
```
24+
25+
> **Note**: You can configure [additional plugin options](/packages/gatsby-plugin-sass/#other-options) like paths to include and options for `css-loader`.
26+
27+
3. Write your stylesheets as `.sass` or `.scss` files and require or import them as normal.
28+
29+
```css:styles.scss
30+
$font-stack: Helvetica, sans-serif;
31+
$primary-color: #333;
32+
33+
body {
34+
font: 100% $font-stack;
35+
color: $primary-color;
36+
}
37+
```
38+
39+
```css:styles.sass
40+
$font-stack: Helvetica, sans-serif
41+
$primary-color: #333
42+
43+
body
44+
font: 100% $font-stack
45+
color: $primary-color
46+
```
47+
48+
```javascript
49+
import "./styles.scss"
50+
import "./styles.sass"
51+
```
52+
53+
## Other resources
54+
55+
- [Introduction to Sass](https://designmodo.com/introduction-sass/)
56+
- [Sass Documentation](https://sass-lang.com/documentation)
57+
- [Gatsby starters that use Sass](/starters/?c=Styling%3ASCSS)

docs/docs/tailwind-css.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ title: Tailwind CSS
44

55
Tailwind is a utility-first CSS framework for rapidly building custom user interfaces. This guide will show you how to get started with Gatsby and [Tailwind CSS](https://tailwindcss.com/).
66

7-
This guide assumes that you have a Gatsby project set up. If you need to set up a project, head to the [**Quick Start guide**](/docs/quick-start), then come back.
8-
9-
### Overview
7+
## Overview
108

119
There are two ways you can use Tailwind with Gatsby:
1210

@@ -17,6 +15,8 @@ You have to install and configure Tailwind for both of these methods, so this gu
1715

1816
## Installing and Configuring Tailwind
1917

18+
This guide assumes that you have a Gatsby project set up. If you need to set up a project, head to the [**Quick Start guide**](/docs/quick-start), then come back.
19+
2020
1. Install Tailwind
2121

2222
```shell
@@ -31,7 +31,7 @@ To configure Tailwind, we'll need to add a Tailwind configuration file. Luckily,
3131
./node_modules/.bin/tailwind init
3232
```
3333

34-
## Option #1: PostCSS
34+
### Option #1: PostCSS
3535

3636
1. Install the Gatsby PostCSS plugin [**gatsby-plugin-postcss**](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-postcss).
3737

@@ -61,7 +61,7 @@ You can now use the `@tailwind` directives to add Tailwind's utilites, preflight
6161

6262
To learn more about how to use Tailwind in your CSS, visit the [Tailwind Documentation](https://tailwindcss.com/docs/installation#3-use-tailwind-in-your-css)
6363

64-
## Option #2: CSS-in-JS
64+
### Option #2: CSS-in-JS
6565

6666
These steps assume you have a CSS-in-JS library already installed, and the examples are based on Styled Components.
6767

@@ -82,7 +82,7 @@ const Button = styled.button`
8282
`
8383
```
8484

85-
### Other resources
85+
## Other resources
8686

8787
- [Introduction to PostCSS](https://www.smashingmagazine.com/2015/12/introduction-to-postcss/)
8888
- [Tailwind Documentation](https://tailwindcss.com/)

packages/gatsby-plugin-sass/README.md

+49-32
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,28 @@ Provides drop-in support for SASS/SCSS stylesheets
99
## How to use
1010

1111
1. Include the plugin in your `gatsby-config.js` file.
12-
2. Write your stylesheets in SASS/SCSS and require or import them as normal.
1312

14-
```javascript
15-
// in gatsby-config.js
13+
```javascript:title="gatsby-config.js"
1614
plugins: [`gatsby-plugin-sass`]
1715
```
1816

19-
By Default `node-sass` is used. To use `dart-sass`.
17+
2. Write your stylesheets in Sass/SCSS and require or import them as normal.
2018

21-
```bash
22-
npm i -D sass
19+
```css:title="src/index.sass"
20+
html {
21+
background-color: rebeccapurple;
22+
p {
23+
color: white;
24+
}
25+
}
2326
```
2427

2528
```javascript
26-
// in gatsby-config.js
27-
plugins: [
28-
{
29-
resolve: `gatsby-plugin-sass`,
30-
options: {
31-
implementation: require("sass"),
32-
},
33-
},
34-
]
29+
import("./src/index.sass")
3530
```
3631

32+
## Other options
33+
3734
If you need to pass options to Sass use the plugins options, see [node-sass](https://github.com/sass/node-sass)/[dart-sass](https://github.com/sass/dart-sass) docs
3835
for all available options.
3936

@@ -66,27 +63,27 @@ plugins: [
6663
]
6764
```
6865

69-
### With CSS Modules
66+
### Alternative Sass Implementations
7067

71-
Using CSS Modules requires no additional configuration. Simply prepend `.module` to the extension. For example: `App.scss` -> `App.module.scss`.
72-
Any file with the `module` extension will use CSS Modules.
68+
By default the node implementation of Sass (`node-sass`) is used. To use the implementation written in Dart (`dart-sass`), you can install `sass` instead of `node-sass` and pass it into the options as the implementation:
7369

74-
### PostCSS plugins
75-
76-
PostCSS is also included to handle some default optimizations like autoprefixing
77-
and common cross-browser flexbox bugs. Normally you don't need to think about it, but if
78-
you'd prefer to add additional postprocessing to your SASS output you can specify plugins
79-
in the plugin options
80-
81-
## Relative paths & url()
82-
83-
This plugin resolves `url()` paths relative to the entry SCSS/SASS file not – as might be expected – the location relative to the declaration. Under the hood, it makes use of [sass-loader](https://github.com/webpack-contrib/sass-loader/blob/master/README.md#problems-with-url) and this is documented in the [readme](https://github.com/webpack-contrib/sass-loader/blob/master/README.md#problems-with-url).
84-
85-
Using [resolve-url-loader](https://github.com/bholloway/resolve-url-loader) may provide a workaround, but at present this is not in the build and implementation would demand customisation.
70+
```bash
71+
npm install --save-dev sass
72+
```
8673

87-
TODO link to a plugin that adds resolve-url-loader
74+
```javascript
75+
// in gatsby-config.js
76+
plugins: [
77+
{
78+
resolve: `gatsby-plugin-sass`,
79+
options: {
80+
implementation: require("sass"),
81+
},
82+
},
83+
]
84+
```
8885

89-
## Other options
86+
### SASS Precision
9087

9188
SASS defaults to [5 digits of precision](https://github.com/sass/sass/issues/1122). If this is too low for you (e.g. [if you use Bootstrap](https://github.com/twbs/bootstrap-sass/blob/master/README.md#sass-number-precision)), you may configure it as follows:
9289

@@ -103,6 +100,26 @@ plugins: [
103100
]
104101
```
105102

103+
### With CSS Modules
104+
105+
Using CSS Modules requires no additional configuration. Simply prepend `.module` to the extension. For example: `App.scss` -> `App.module.scss`.
106+
Any file with the `module` extension will use CSS Modules.
107+
108+
### PostCSS plugins
109+
110+
PostCSS is also included to handle some default optimizations like autoprefixing
111+
and common cross-browser flexbox bugs. Normally you don't need to think about it, but if
112+
you'd prefer to add additional postprocessing to your Sass output you can specify plugins
113+
in the plugin options.
114+
115+
## Relative paths & url()
116+
117+
This plugin resolves `url()` paths relative to the entry SCSS/Sass file not – as might be expected – the location relative to the declaration. Under the hood, it makes use of [sass-loader](https://github.com/webpack-contrib/sass-loader/blob/master/README.md#problems-with-url) and this is documented in the [readme](https://github.com/webpack-contrib/sass-loader/blob/master/README.md#problems-with-url).
118+
119+
Using [resolve-url-loader](https://github.com/bholloway/resolve-url-loader) may provide a workaround, but at present this is not in the build and implementation would demand customization.
120+
121+
<!-- TODO link to a plugin that adds resolve-url-loader -->
122+
106123
## Breaking changes history
107124

108125
<!-- Please keep the breaking changes list ordered with the newest change at the top -->

www/src/data/sidebars/doc-links.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@
210210
items:
211211
- title: Typography.js
212212
link: /docs/typography-js/
213+
- title: Using Sass
214+
link: /docs/sass/
213215
- title: Using PostCSS
214216
link: /docs/post-css/
215217
- title: Using TailwindCSS

0 commit comments

Comments
 (0)