Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(tutorial): improve a11y of layout component #32539

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions docs/docs/tutorial/part-2/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -490,25 +490,27 @@ In the browser, the actual DOM elements will look something like this:

Follow the steps below to create a `Layout` component and add it to your Home and About pages.

1. Create a new file called `src/components/layout.js`. Insert the following code to define your `Layout` component. This component will render a `<main>` element that includes a dynamic page title and heading (from the `pageTitle` prop), a list of navigation links, and the contents passed in with the `children` prop.
1. Create a new file called `src/components/layout.js`. Insert the following code to define your `Layout` component. This component will render a dynamic page title and heading (from the `pageTitle` prop), a list of navigation links, and the contents passed in with the `children` prop. To improve accessibility, there's also a `<main>` element wrapping the page-specific elements (the `<h1>` heading and the contents from `children`).

```js:title=src/components/layout.js
import * as React from 'react'
import { Link } from 'gatsby'

const Layout = ({ pageTitle, children }) => {
return (
<main>
<div>
<title>{pageTitle}</title>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
</ul>
</nav>
<h1>{pageTitle}</h1>
{children}
</main>
<main>
<h1>{pageTitle}</h1>
{children}
</main>
</div>
)
}

Expand Down Expand Up @@ -652,7 +654,7 @@ Follow the steps below to style your `Layout` component using CSS Modules.
}
```

3. Then import that class into your `Layout` component `.js` file, and use the `className` prop to assign it to the `<main>` element:
3. Then import that class into your `Layout` component `.js` file, and use the `className` prop to assign it to the top-level `<div>` element:

```js:title=src/components/layout.js
import * as React from 'react'
Expand All @@ -661,17 +663,19 @@ import { container } from './layout.module.css' // highlight-line

const Layout = ({ pageTitle, children }) => {
return (
<main className={container}> // highlight-line
<div className={container}> // highlight-line
<title>{pageTitle}</title>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
</ul>
</nav>
<h1>{pageTitle}</h1>
{children}
</main>
<main>
<h1>{pageTitle}</h1>
{children}
</main>
</div>
)
}

Expand Down Expand Up @@ -737,7 +741,7 @@ import {

const Layout = ({ pageTitle, children }) => {
return (
<main className={container}>
<div className={container}>
<title>{pageTitle}</title>
<nav>
<ul className={navLinks}> // highlight-line
Expand All @@ -753,9 +757,11 @@ const Layout = ({ pageTitle, children }) => {
</li>
</ul>
</nav>
<h1 className={heading}>{pageTitle}</h1> // highlight-line
{children}
</main>
<main>
<h1 className={heading}>{pageTitle}</h1> // highlight-line
{children}
</main>
</div>
)
}

Expand Down
37 changes: 23 additions & 14 deletions docs/docs/tutorial/part-4/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,10 @@ const Layout = ({ pageTitle, children }) => {
`)

return (
<main className={container}>
<div className={container}>
{/* highlight-start */}
<title>{pageTitle} | {data.site.siteMetadata.title}</title>
<p>{data.site.siteMetadata.title}</p>
<header>{data.site.siteMetadata.title}</header>
{/* highlight-end */}
<nav>
<ul className={navLinks}>
Expand All @@ -431,9 +431,11 @@ const Layout = ({ pageTitle, children }) => {
</li>
</ul>
</nav>
<h1 className={heading}>{pageTitle}</h1>
{children}
</main>
<main>
<h1 className={heading}>{pageTitle}</h1>
{children}
</main>
</div>
)
}

Expand All @@ -451,6 +453,7 @@ export default Layout
font-size: 3rem;
color: gray;
font-weight: 700;
margin: 3rem 0;
}
```

Expand Down Expand Up @@ -480,10 +483,10 @@ const Layout = ({ pageTitle, children }) => {
`)

return (
<main className={container}>
<div className={container}>
<title>{pageTitle} | {data.site.siteMetadata.title}</title>
{/* highlight-next-line */}
<p className={siteTitle}>{data.site.siteMetadata.title}</p>
<header className={siteTitle}>{data.site.siteMetadata.title}</header>
<nav>
<ul className={navLinks}>
<li className={navLinkItem}>
Expand All @@ -498,8 +501,10 @@ const Layout = ({ pageTitle, children }) => {
</li>
</ul>
</nav>
<h1 className={heading}>{pageTitle}</h1>
{children}
<main>
<h1 className={heading}>{pageTitle}</h1>
{children}
</main>
</main>
)
}
Expand Down Expand Up @@ -560,9 +565,9 @@ const Layout = ({ pageTitle, children }) => {
`)

return (
<main className={container}>
<div className={container}>
<title>{pageTitle} | {data.site.siteMetadata.title}</title>
<p className={siteTitle}>{data.site.siteMetadata.title}</p>
<header className={siteTitle}>{data.site.siteMetadata.title}</header>
<nav>
<ul className={navLinks}>
<li className={navLinkItem}>
Expand All @@ -584,9 +589,11 @@ const Layout = ({ pageTitle, children }) => {
{/* highlight-end */}
</ul>
</nav>
<h1 className={heading}>{pageTitle}</h1>
{children}
</main>
<main>
<h1 className={heading}>{pageTitle}</h1>
{children}
</main>
</div>
)
}

Expand Down Expand Up @@ -773,6 +780,8 @@ export default BlogPage

**Note:** By default, the query you build in GraphiQL will have a query name, like `MyQuery`. You may see an error if you have more than one query with the same name, so after you copy your query over from GraphiQL to your component, delete the name (as in the code example below).

Alternatively, you can give each of your queries a unique name. Query names can be useful for debugging errors that show up in your console when Gatsby executes your queries at build time.

</Announcement>

```js:title=src/pages/blog.js
Expand Down