Skip to content

Commit 07f7170

Browse files
alexnmrauchg
authored andcommitted
Added layout component example (vercel#560)
* added layout component example * coding style fixes * trailing spaces removed * updated README file * added layout example in docs * moved .babelrc so that it handles all projects from the examples folder
1 parent e363e73 commit 07f7170

File tree

8 files changed

+94
-1
lines changed

8 files changed

+94
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ export default () => (
122122

123123
<p><details>
124124
<summary><b>Examples</b></summary>
125-
<ul><li><a href="./examples/head-elements">Head elements</a></li></ul>
125+
<ul>
126+
<li><a href="./examples/head-elements">Head elements</a></li>
127+
<li><a href="./examples/layout-component">Layout component</a></li>
128+
</ul>
126129
</details></p>
127130

128131
We expose a built-in component for appending elements to the `<head>` of the page.

examples/.babelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

examples/layout-component/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
# Layout component example
3+
4+
## How to use
5+
6+
Download the example (or clone the repo)[https://github.com/zeit/next.js.git]:
7+
8+
```bash
9+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/hello-world
10+
cd hello-world
11+
```
12+
13+
Install it and run:
14+
15+
```bash
16+
npm install
17+
npm run dev
18+
```
19+
20+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
21+
22+
```bash
23+
now
24+
```
25+
26+
## The idea behind the example
27+
28+
This example shows a very common use case when building websites where you need to repeat some sort of layout for all your pages. Our pages are: `home`, `about` and `contact` and they all share the same `<head>` settings, the `<nav>` and the `<footer>`. Further more, the title (and potentially other head elements) can be sent as a prop to the layout component so that it's customizable in all pages.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Link from 'next/link'
2+
import Head from 'next/head'
3+
4+
export default ({ children, title = 'This is the default title' }) => (
5+
<div>
6+
<Head>
7+
<title>{ title }</title>
8+
<meta charSet='utf-8' />
9+
<meta name='viewport' content='initial-scale=1.0, width=device-width' />
10+
</Head>
11+
<header>
12+
<nav>
13+
<Link href='/'>Home</Link> |
14+
<Link href='/about'>About</Link> |
15+
<Link href='/contact'>Contact</Link>
16+
</nav>
17+
</header>
18+
19+
{ children }
20+
21+
<footer>
22+
I`m here to stay
23+
</footer>
24+
</div>
25+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "hello-world",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"dev": "next",
6+
"build": "next build",
7+
"start": "next start"
8+
},
9+
"dependencies": {
10+
"next": "*"
11+
},
12+
"author": "",
13+
"license": "ISC"
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Layout from '../components/layout'
2+
3+
export default () => (
4+
<Layout title='About us'>
5+
<div>About us</div>
6+
</Layout>
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Layout from '../components/layout'
2+
3+
export default () => (
4+
<Layout title='Contact us'>
5+
<div>Contact</div>
6+
</Layout>
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Layout from '../components/layout'
2+
3+
export default () => (
4+
<Layout>
5+
<div>Hello World.</div>
6+
</Layout>
7+
)

0 commit comments

Comments
 (0)