` in `src/pages/index.js`.
```jsx{4}
-import React from "react"
+import React from "react";
export default () =>
@@ -229,8 +230,8 @@ Ah, this is starting to look nice!
What we're seeing here is the default CSS Typography.js produces. We can easily
customize it, however. Let's do that.
-In your site, create a new directory at `src/utils`. There create a file named
-`typography.js`. In it, add the following code.
+In your site, create a new directory at `src/utils`. In that directory, create a file named
+`typography.js`. In that file, add the following code.
```javascript
import Typography from "typography";
@@ -256,17 +257,16 @@ module.exports = {
}
```
-Stop `gatsby develop` and then restart it again for our plugin change to take
-effect.
+Stop `gatsby develop` by typing
Ctrl + c into the terminal window where the development process has been running. Then, run `gatsby develop` again to restart it. This will allow our plugin change to take effect.
-Now all the text font sizes should be slightly bigger. Try changing the
+Now, all the text font sizes should be slightly bigger. Try changing the
`baseFontSize` to `24px` then `12px`. All elements get resized as their
`font-size` is based on the `baseFontSize`.
There are
[many themes available](https://github.com/KyleAMathews/typography.js#published-typographyjs-themes)
-for Typography.js. Let's try a couple. Run in your terminal at the root of your
-site:
+for Typography.js. Let's try a couple. In your terminal at the root of your
+site, run:
```shell
npm install --save typography-theme-bootstrap typography-theme-lawton
@@ -275,8 +275,8 @@ npm install --save typography-theme-bootstrap typography-theme-lawton
To use the Bootstrap theme, change your typography code to:
```javascript{2,4}
-import Typography from "typography"
-import bootstrapTheme from "typography-theme-bootstrap"
+import Typography from "typography";
+import bootstrapTheme from "typography-theme-bootstrap";
const typography = new Typography(bootstrapTheme)
@@ -290,9 +290,9 @@ Bootstrap theme does this. Replace your typography module code with the
following, then restart the dev server (necessary to load the new Google Fonts).
```javascript{2-3,5}
-import Typography from "typography"
+import Typography from "typography";
// import bootstrapTheme from "typography-theme-bootstrap"
-import lawtonTheme from "typography-theme-lawton"
+import lawtonTheme from "typography-theme-lawton";
const typography = new Typography(lawtonTheme)
@@ -307,32 +307,20 @@ Typography.js has more than 30 themes!
## Component CSS
-Gatsby has a wealth of options available for styling components. Let's explore
-three very popular and production-ready options. We'll build the same page three
-times to explore each styling option.
-
-Each is a variant on "CSS-in-JS"—which solves many of the problems with
-traditional CSS.
+Gatsby has a wealth of options available for styling components. In this tutorial, we'll explore
+one very popular method: CSS Modules.
-One of the most important problems they solve is selector name collisions. With
-traditional CSS, you have to be careful not to overwrite CSS selectors used
-elsewhere in a site because all CSS selectors live in the same global namespace.
-This unfortunate restriction can lead to elaborate (and often confusing)
-selector naming schemes.
+### CSS-in-JS
-With CSS-in-JS, you avoid all that as CSS selectors are scoped automatically to
-their component. Styles are tightly coupled with their components. This makes it
-very easy to know how to edit a component's CSS as there's never any confusion
-about how and where CSS is being used.
+While we won't cover CSS-in-JS in this initial tutorial, we encourage you to explore CSS-in-JS libraries as these solve many of the problems with traditional CSS plus help make your React components even smarter. There are mini-tutorials for two libraries, [Glamor](/docs/glamor.md) and [Styled Components](/docs/styled-components.md). Check out the following resources for background reading on CSS-in-JS:
-For some background reading on CSS-in-JS, see
[Christopher "vjeux" Chedeau's 2014 presentation that sparked this movement](https://speakerdeck.com/vjeux/react-css-in-js)
as well as
[Mark Dalgleish's more recent post "A Unified Styling Language"](https://medium.com/seek-blog/a-unified-styling-language-d0c208de2660).
### CSS Modules
-Let's explore first **CSS Modules**.
+Let's explore **CSS Modules**.
Quoting from
[the CSS Module homepage](https://github.com/css-modules/css-modules):
@@ -340,7 +328,7 @@ Quoting from
> A **CSS Module** is a CSS file in which all class names and animation names
> are scoped locally by default.
-CSS Modules is very popular, as it lets you write CSS like normal but with a lot
+CSS Modules is very popular as it lets you write CSS like normal but with a lot
more safety. The tool automatically makes class and animation names unique so
you don't have to worry about selector name collisions.
@@ -354,7 +342,7 @@ Let's build a page using CSS Modules.
First, let's create a new `Container` component which we'll use for each of the
CSS-in-JS examples. Create a `components` directory at `src/components` and
then, in this directory, create a file named `container.js` and paste the
-following.
+following:
```javascript
import React from "react";
@@ -394,6 +382,8 @@ First, let's create the file for the CSS at
with `.module.css` instead of `.css` like normal. This is how we tell Gatsby
that this CSS file should be processed as CSS modules.
+Paste the following into the file:
+
```css
.user {
display: flex;
@@ -436,7 +426,7 @@ import styles from "./about-css-modules.module.css";
console.log(styles);
```
-If you open the developer console in your browser you'll see:
+If you open the developer console (using e.g. Firefox or Chrome's developer tools) in your browser, you'll see:
![css-modules-console](css-modules-console.png)
@@ -450,17 +440,17 @@ where some CSS is being used.
Let's use our styles to create a `User` component.
Let's create the new component inline in the `about-css-modules.js` page
-component. The general rule of thumb is if you use a component in multiple
+component. The general rule of thumb is this: if you use a component in multiple
places on a site, it should be in its own module file in the `components`
directory. But, if it's used only in one file, create it inline.
Modify `about-css-modules.js` so it looks like the following:
```jsx{6-17,23-30}
-import React from "react"
-import styles from "./about-css-modules.module.css"
+import React from "react";
+import styles from "./about-css-modules.module.css";
-import Container from "../components/container"
+import Container from "../components/container";
const User = props =>
@@ -496,204 +486,7 @@ The finished page should now look like:
![css-modules-final](css-modules-final.png)
-### Glamor
-
-Let's create the same page using
-[Glamor](https://github.com/threepointone/glamor).
-
-Glamor lets you write _real_ CSS inline in your components using the same Object
-CSS syntax React supports for the `style` prop.
-
-First install the Gatsby plugin for Glamor.
-
-```shell
-npm install --save gatsby-plugin-glamor
-```
-
-And then add it to your `gatsby-config.js`
-
-```javascript{9}
-module.exports = {
- plugins: [
- {
- resolve: `gatsby-plugin-typography`,
- options: {
- pathToConfigModule: `src/utils/typography.js`,
- },
- },
- `gatsby-plugin-glamor`,
- ],
-}
-```
-
-Restart `gatsby develop` again to enable the Glamor plugin.
-
-Now create the Glamor page at `src/pages/about-glamor.js`
-
-```jsx
-import React from "react";
-import Container from "../components/container";
-
-export default () => (
-
- About Glamor
- Glamor is cool
-
-);
-```
-
-Let's add the same inline `User` component but this time using Glamor's `css`
-prop.
-
-```jsx{5-27,33-40}
-import React from "react"
-
-import Container from "../components/container"
-
-const User = props =>
-
-
-
-
- {props.username}
-
-
- {props.excerpt}
-
-
-
-
-export default () =>
-
- About Glamor
- Glamor is cool
-
-
-
-```
-
-The final Glamor page should look identical to the CSS Modules page.
-
-![glamor-example](glamor-example.png)
-
-### Styled Components
-
-For our final CSS-in-JS example, we'll try
-[Styled Components](https://www.styled-components.com/).
-
-Styled Components lets you use actual CSS syntax inside your components.
-
-First, like normal, we'll install the Gatsby plugin for Styled Components.
-
-```sh
-npm install --save gatsby-plugin-styled-components styled-components
-```
-
-Then modify the `gatsby-config.js`. Before we can use Styled Components however,
-we'll need to remove the Glamor plugin and delete the Glamor component page we
-created. The two plugins conflict with each other as both want to take control
-during server rendering.
-
-```javascript{9}
-module.exports = {
- plugins: [
- {
- resolve: `gatsby-plugin-typography`,
- options: {
- pathToConfigModule: `src/utils/typography.js`,
- },
- },
- `gatsby-plugin-styled-components`,
- ],
-}
-```
-
-Then at `src/pages/about-styled-components.js` create:
-
-```jsx
-import React from "react";
-import styled from "styled-components";
-
-import Container from "../components/container";
-
-const UserWrapper = styled.div`
- display: flex;
- align-items: center;
- margin: 0 auto 12px auto;
- &:last-child {
- margin-bottom: 0;
- }
-`;
-
-const Avatar = styled.img`
- flex: 0 0 96px;
- width: 96px;
- height: 96px;
- margin: 0;
-`;
-
-const Description = styled.div`
- flex: 1;
- margin-left: 18px;
- padding: 12px;
-`;
-
-const Username = styled.h2`
- margin: 0 0 12px 0;
- padding: 0;
-`;
-
-const Excerpt = styled.p`
- margin: 0;
-`;
-
-const User = props => (
-
-
-
- {props.username}
- {props.excerpt}
-
-
-);
-
-export default () => (
-
- About Styled Components
- Styled Components is cool
-
-
-
-);
-```
### Other CSS options