{"id":1454,"date":"2020-11-19T02:03:12","date_gmt":"2020-11-19T02:03:12","guid":{"rendered":"http:\/\/10.0.0.14\/?p=1454"},"modified":"2021-09-08T00:13:27","modified_gmt":"2021-09-08T00:13:27","slug":"tdd-with-gatsby-django-docker-part-2-chapter-11-layout-component","status":"publish","type":"post","link":"https:\/\/tutorials.leesonresearch.com\/tutorials\/2020\/11\/19\/tdd-with-gatsby-django-docker-part-2-chapter-11-layout-component\/","title":{"rendered":"TDD with Gatsby, Django &#038; Docker Part 2, Chapter 11 &#8212; Layout Component &#038; Typography"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Creating the Layout Component<\/h1>\n\n\n\n<p>We start by creating a layout component in frontend\/src\/components:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: Double click to copy; notranslate\" title=\"Double click to copy\">\n\/\/ frontend\/src\/components\/layout.js\n\nimport React from 'react';\nimport Header from '.\/header';\n\nexport default function Layout({ children }) {\n  return (\n    &lt;React.Fragment&gt;\n      &lt;Header \/&gt;\n      &lt;div style={{ margin: `0 auto`, maxWidth: 650, padding: `0 1rem` }}&gt;\n        {children}\n      &lt;\/div&gt;\n    &lt;\/React.Fragment&gt;\n  )\n}\n<\/pre><\/div>\n\n\n<p>Import the layout component into the index page:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: Double click to copy; notranslate\" title=\"Double click to copy\">\n\/\/ frontend\/src\/pages\/index.js\n\nimport React from 'react';\n\nimport { useQuery } from '@apollo\/client';\nimport gql from 'graphql-tag';\nimport Layout from '..\/components\/layout';\n\nconst APOLLO_QUERY = gql`\n  query {\n    todo(id: 7) { \n      id\n      title\n      task\n    }\n  }\n`;\n\nexport default function Home() {\n  const { loading, error, data } = useQuery(APOLLO_QUERY)\n  if (loading) return &lt;p&gt;Loading...&lt;\/p&gt;;\n  if (error) return &lt;p&gt;Error!&lt;\/p&gt;;\n\n  return (\n    &lt;Layout&gt;\n      &lt;div&gt;\n        &lt;div&gt;Hello World!&lt;\/div&gt;\n        &lt;div&gt;\n          Todo Title: {data.todo.title}\n        &lt;\/div&gt;\n      &lt;\/div&gt;\n    &lt;\/Layout&gt;\n  )\n}\n<\/pre><\/div>\n\n\n<p>Spin up the development environment:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: Double click to copy; notranslate\" title=\"Double click to copy\">\ndocker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d\n\n# or with the Makefile shortcut\nmake dev\n<\/pre><\/div>\n\n\n<p>When you hit http:\/\/localhost:8000 you should see the index page something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"795\" height=\"488\" src=\"http:\/\/10.0.0.14:5557\/wp-content\/uploads\/2021\/09\/chap11_01.jpg\" alt=\"\" class=\"wp-image-2513\" srcset=\"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2021\/09\/chap11_01.jpg 795w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2021\/09\/chap11_01-300x184.jpg 300w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2021\/09\/chap11_01-768x471.jpg 768w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2021\/09\/chap11_01-600x368.jpg 600w\" sizes=\"auto, (max-width: 795px) 100vw, 795px\" \/><figcaption>http:\/\/localhost:8000<\/figcaption><\/figure>\n\n\n\n<p>As we move forward with our project we&#8217;ll wrap all our pages with our new Layout component so all the pages of the site have a consistent look and feel.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Typography.js<\/h4>\n\n\n\n<p>Quoting from the <a href=\"https:\/\/www.gatsbyjs.com\/docs\/using-typography-js\/\" target=\"_blank\" rel=\"noreferrer noopener\">Gatsby docs<\/a>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a href=\"https:\/\/github.com\/kyleamathews\/typography.js\/\">Typography.js<\/a> is a JavaScript library that allows you to explore the typographic design of your website and define beautiful custom and pre-existing typographic themes. It enables you to change the font on your website with ease. Typography.js currently maintains over 30 themes for you to use. You can also create your own custom font themes if no available themes fit your requirements.<\/p><cite><a href=\"https:\/\/www.gatsbyjs.com\/docs\/using-typography-js\/\" target=\"_blank\" rel=\"noreferrer noopener\">Gatsby Docs<\/a><\/cite><\/blockquote>\n\n\n\n<p>Sounds cool. Lets try it. We&#8217;ll start by installing the <code>gatsby-plugin-typography<\/code> plugin:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: Double click to copy; notranslate\" title=\"Double click to copy\">\n# Power down the app\nmake dev-down\n\n# or\ndocker-compose -f docker-compose.yml -f docker-compose-dev.yml down\n\n# Very important to make sure you're in frontend dir\n# Don't want to install the node modules at the root :)\ncd frontend\nnpm install gatsby-plugin-typography react-typography typography\n<\/pre><\/div>\n\n\n<p>Update gatsby-config.js with the plugin:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: Double click to copy; notranslate\" title=\"Double click to copy\">\n\/\/ frontend\/gatsby-config.js\n\n\/**\n * Configure your Gatsby site with this file.\n *\n * See: https:\/\/www.gatsbyjs.com\/docs\/gatsby-config\/\n *\/\n\nmodule.exports = {\n  siteMetadata: {\n    title: `Gatsby Todo App`,\n    description: `A fabulous description of our Todo app here...`,\n    author: `Ron Leeson`,\n  },\n  plugins: &#x5B;\n    {\n      resolve: `gatsby-plugin-typography`,\n      options: {\n        pathToConfigModule: `src\/utils\/typography`,\n      },\n    },\n  ],\n}\n<\/pre><\/div>\n\n\n<p>Note the pathToConfigModule in the options, we need to create that file with a basic typography configuration:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: Double click to copy; notranslate\" title=\"Double click to copy\">\n\/\/ frontend\/src\/utils\/typography.js\n\nimport Typography from &quot;typography&quot;\n\nconst typography = new Typography({\n  baseFontSize: &quot;18px&quot;,\n  baseLineHeight: 1.666,\n  headerFontFamily: &#x5B;\n    &quot;Avenir Next&quot;,\n    &quot;Helvetica Neue&quot;,\n    &quot;Segoe UI&quot;,\n    &quot;Helvetica&quot;,\n    &quot;Arial&quot;,\n    &quot;sans-serif&quot;,\n  ],\n  bodyFontFamily: &#x5B;&quot;Georgia&quot;, &quot;serif&quot;],\n})\n\nexport default typography\n<\/pre><\/div>\n\n\n<p>We&#8217;re ready to rebuild our frontend docker container:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: Double click to copy; notranslate\" title=\"Double click to copy\">\n# cd to root of project\ndocker-compose build frontend\n\n# After build is complete spin up the dev env\nmake dev\n<\/pre><\/div>\n\n\n<p>If all goes well you should be able to hit http:\/\/localhost:8000 and see you index page updated with cool, stylish fonts:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"962\" height=\"579\" src=\"http:\/\/10.0.0.14:5557\/wp-content\/uploads\/2021\/09\/chap11_02.jpg\" alt=\"\" class=\"wp-image-2517\" srcset=\"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2021\/09\/chap11_02.jpg 962w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2021\/09\/chap11_02-300x181.jpg 300w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2021\/09\/chap11_02-768x462.jpg 768w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2021\/09\/chap11_02-600x361.jpg 600w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2021\/09\/chap11_02-945x569.jpg 945w\" sizes=\"auto, (max-width: 962px) 100vw, 962px\" \/><figcaption>http:\/\/localhost:8000<\/figcaption><\/figure>\n\n\n\n<p>There&#8217;s loads of other Typography themes you can play with. Checkout the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.gatsbyjs.com\/docs\/using-typography-js\/\" target=\"_blank\">Gatsby Typography tutorial section<\/a> and the <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/kyleamathews\/typography.js\/\" target=\"_blank\">Typography repo<\/a>. <\/p>\n\n\n\n<p>Well, that wraps up this chapter. After all the gnarly heavy lifting of the previous chapters this one is a walk in the park.<\/p>\n\n\n\n<p>Next up in chapter 12: implement <strong>styled-components<\/strong>. <\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" href=\"tdd-with-gatsby-django-docker-part-2-chapter-10-setup-gatsby-for-tdd\">&lt; Previous:  Chapter 10<\/a><\/div>\n\n\n\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" href=\"tdd-with-gatsby-django-docker-part-2-chapter-12-using-theme-ui\">Next: Chapter 12 &gt;<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Creating the Layout Component We start by creating a layout component in frontend\/src\/components: Import the layout component into the index page: Spin up the development environment: When you hit http:\/\/localhost:8000 you should see the index page something like this: As&#8230; <a class=\"more-link\" href=\"https:\/\/tutorials.leesonresearch.com\/tutorials\/2020\/11\/19\/tdd-with-gatsby-django-docker-part-2-chapter-11-layout-component\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":277,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,12],"tags":[10,33,34],"class_list":["post-1454","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured-tutorial","category-gatsby","tag-gatsby","tag-layout-component","tag-typography"],"_links":{"self":[{"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/posts\/1454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/comments?post=1454"}],"version-history":[{"count":46,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/posts\/1454\/revisions"}],"predecessor-version":[{"id":2518,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/posts\/1454\/revisions\/2518"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/media\/277"}],"wp:attachment":[{"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/media?parent=1454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/categories?post=1454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/tags?post=1454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}