forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Restructure styling section (gatsbyjs#13766)
* Add comment to create draft PR * Add comment to create draft PR * Draft styling section structure * Draft styling section intro * Remove guide from Glamor, poin to Emotion Keep the file to avoid any broken links, but discourage using it * Write CSS-in-JS section intro * Write Cascading and Scoped styles intro * Write Frameworks and Libraries intro * Write stubs * Remove Component CSS page This was essentially a duplicate of CSS Modules, with an intro to component-scoped CSS. Keeping the page to avoid broken links, redirecting to Scoped Styles * Structure in three sub-sections 1. Global CSS Files 2. Mudular CSS 3. Stylized Components (4. other frameworks and libraries) * Write Global CSS intro * Copy content from Global Styles to Global CSS approach * Copy content from Global Styles to Emotion guide * Rewrite Styling section intro * Add page at creating-global-styles to avoid broken links * Add links in styling approaches description * Write CSS-in-JS motivations draft * Rewrite approaches titles * Apply language and details suggestions from review Co-Authored-By: robinmetral <[email protected]> * Rename page to fit section order CSS Frameworks and Libraries -> CSS Libraries and Frameworks * Add note about stable classes for user stylesheets * Add section on importing CSS files into components * Update CSS-in-JS from suggestion Mention there are global style sections in each CSS-in-JS guide Co-Authored-By: Marcy Sutton <[email protected]> * Rewrite CSS Modules page with component CSS intro * title tweaks and content updates * Redirect Component CSS to CSS Modules * Apply suggestions from code review Co-Authored-By: gillkyle <[email protected]> * Improve links out to guides on Global Styles page * chore: format * fix heading levels
- Loading branch information
1 parent
4b45857
commit af89cf3
Showing
13 changed files
with
347 additions
and
353 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: Enhancing Styles with CSS-in-JS | ||
overview: true | ||
--- | ||
|
||
CSS-in-JS refers to an approach where styles are written in JavaScript instead of in external CSS files to easily scope styles in components, eliminate dead code, encourage faster performance and dynamic styling, and more. | ||
|
||
CSS-in-JS bridges the gap between CSS and JavaScript: | ||
|
||
1. **Components**: you'll style your site with components, which integrates well with React's "everything is a component" philosophy. | ||
2. **Scoped**: this is a side effect of the first. Just like [CSS Modules](/docs/css-modules/), CSS-in-JS is scoped to components by default. | ||
3. **Dynamic**: style your site dynamically based on component state by integrating JavaScript variables. | ||
4. **Bonuses**: many CSS-in-JS libraries generate unique class names which can help with caching, automatic vendor prefixes, timely loading of critical CSS, and implementing many other features, depending on the library you choose. | ||
|
||
CSS-in-JS, while not required in Gatsby, is very popular among JavaScript developers for the reasons listed above. For more context, read Max Stoiber's (creator of CSS-in-JS library [styled-components](/docs/styled-components/)) article [_Why I write CSS in JavaScript_](https://mxstbr.com/thoughts/css-in-js/). However, you should also consider whether CSS-in-JS is necessary, as not relying on it can encourage more inclusive front-end skill-sets. It is also more difficult to port styles from JSX to and from CSS. | ||
|
||
_Note that this functionality is not a part of React or Gatsby, and requires using any of the many [third-party CSS-in-JS libraries](https://github.com/MicheleBertoli/css-in-js#css-in-js)._ | ||
|
||
> Adding a stable CSS class to your JSX markup along with your CSS-in-JS can make it easier to users to include [User Stylesheets](https://www.viget.com/articles/inline-styles-user-style-sheets-and-accessibility/) for accessibility. See [Styled Components](/docs/styled-components#enabling-user-stylesheets-with-a-stable-class-name) example. | ||
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. | ||
|
||
[[guidelist]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
title: CSS Libraries and Frameworks | ||
overview: true | ||
--- | ||
|
||
There are many other CSS libraries and frameworks that you can use in your Gatsby project. | ||
|
||
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. | ||
|
||
[[guidelist]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,67 @@ | ||
--- | ||
title: CSS Modules | ||
title: Component-Scoped Styles with CSS Modules | ||
--- | ||
|
||
## CSS modules and Gatsby | ||
Component-scoped CSS allows you to write traditional, portable CSS with minimal side-effects: gone are the worries of selector name collisions or affecting other components' styles. | ||
|
||
Gatsby works out of the box with CSS Modules. Here is an [example site that uses CSS modules](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-css-modules). | ||
Gatsby works out of the box with [CSS Modules](https://github.com/css-modules/css-modules), a popular solution for writing component-scoped CSS. Here is an [example site that uses CSS Modules](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-css-modules). | ||
|
||
## What is a CSS module? | ||
## What is a CSS Module? | ||
|
||
Quoting from | ||
[the CSS Module homepage](https://github.com/css-modules/css-modules): | ||
Quoting from [the CSS Module homepage](https://github.com/css-modules/css-modules): | ||
|
||
> A **CSS Module** is a CSS file in which all class names and animation names | ||
> are scoped locally by default. | ||
> A **CSS Module** is a CSS file in which all class names and animation names are scoped locally by default. | ||
CSS Modules are very popular as they let 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. | ||
CSS Modules let you write styles in CSS files but consume them as JavaScript objects for additional processing and safety. CSS Modules are very popular because they automatically make class and animation names unique so you don't have to worry about selector name collisions. | ||
|
||
## When to use CSS modules | ||
### CSS Module example | ||
|
||
CSS Modules are highly recommended for those new to building with Gatsby (and | ||
React in general). | ||
The CSS in a CSS module is no different than normal CSS, but the extension of the file is different to mark that the file will be processed. | ||
|
||
## How to build a page using CSS modules | ||
```css:title=src/components/container.module.css | ||
.container { | ||
margin: 3rem auto; | ||
max-width: 600px; | ||
} | ||
``` | ||
|
||
Visit the [CSS Modules section of the tutorial](/tutorial/part-two/#css-modules) to learn how to build a page using CSS modules. | ||
```jsx:title=src/components/container.js | ||
import React from "react" | ||
// highlight-next-line | ||
import containerStyles from "./container.module.css" | ||
|
||
export default ({ children }) => ( | ||
// highlight-next-line | ||
<section className={containerStyles.container}>{children}</section> | ||
) | ||
``` | ||
|
||
In this example, a CSS module is imported and declared as a JavaScript object called `containerStyles`. Then, a CSS class from that object is referenced in the JSX `className` attribute with `containerStyles.container`, which renders into HTML with dynamic CSS class names like `container-module--container--3MbgH`. | ||
|
||
### Enabling user stylesheets with a stable class name | ||
|
||
Adding a persistent CSS `className` to your JSX markup along with your CSS Modules code can make it easier for users to take advantage of [User Stylesheets](https://www.viget.com/articles/inline-styles-user-style-sheets-and-accessibility/) for accessibility. | ||
|
||
Here's an example where the class name `container` is added to the DOM along with the module's dynamically-created class names: | ||
|
||
```jsx:title=src/components/container.js | ||
import React from "react" | ||
import containerStyles from "./container.module.css" | ||
|
||
export default ({ children }) => ( | ||
<section className={`container ${containerStyles.container}`}> | ||
{children} | ||
</section> | ||
) | ||
``` | ||
|
||
A site user could then write their own CSS styles matching HTML elements with a class name of `.container`, and it wouldn't be affected if the CSS module name or path changed. | ||
|
||
## When to use CSS Modules | ||
|
||
CSS Modules are highly recommended for those new to building with Gatsby (and React in general) as they allow you to write regular, portable CSS files while gaining | ||
performance benefits like only bundling referenced code. | ||
|
||
## How to build a page using CSS Modules | ||
|
||
Visit the [CSS Modules section of the tutorial](/tutorial/part-two/#css-modules) for a guided tour of building a page with CSS Modules. |
Oops, something went wrong.