{"id":1485,"date":"2020-11-19T03:44:11","date_gmt":"2020-11-19T03:44:11","guid":{"rendered":"http:\/\/10.0.0.14\/?p=1485"},"modified":"2021-09-08T00:42:43","modified_gmt":"2021-09-08T00:42:43","slug":"tdd-with-gatsby-django-docker-part-2-chapter-12-using-theme-ui","status":"publish","type":"post","link":"https:\/\/tutorials.leesonresearch.com\/tutorials\/2020\/11\/19\/tdd-with-gatsby-django-docker-part-2-chapter-12-using-theme-ui\/","title":{"rendered":"TDD with Gatsby, Django &#038; Docker Part 2, Chapter 12 &#8212; Styled Components"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Styled Components<\/h1>\n\n\n\n<p>Quoting from the <a href=\"https:\/\/www.gatsbyjs.com\/docs\/how-to\/styling\/styled-components\/\" target=\"_blank\" rel=\"noreferrer noopener\">Styled Components Docs<\/a>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>styled-components is the result of wondering how we could enhance CSS for styling React component systems.<\/strong> By focusing on a single use case we managed to optimize the experience for developers as well as the output for end users<\/p><cite><a rel=\"noreferrer noopener\" href=\"https:\/\/www.gatsbyjs.com\/docs\/emotion\/\" target=\"_blank\"><\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/www.gatsbyjs.com\/docs\/emotion\/\" target=\"_blank\">Styled Components<\/a> Docs<\/cite><\/blockquote>\n\n\n\n<p>We begin&#8230;<\/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# Spin the docker project down\ndocker-compose -f docker-compose.yml -f docker-compose-dev.yml down\n\n# or\nmake dev-down\n\n# Install npm dependencies\ncd frontend\nnpm install gatsby-plugin-styled-components styled-components babel-plugin-styled-components\n<\/pre><\/div>\n\n\n<p>Add the following to the gatsby-config.js file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; highlight: [22]; 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    `gatsby-plugin-styled-components`,\n  ],\n}\n<\/pre><\/div>\n\n\n<p>Rebuild frontend:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: Double click to copy; notranslate\" title=\"Double click to copy\">\n# cd to root of project\ndocker-compose build frontend\n\n# reboot app\ndocker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d\n\n# or\nmake dev\n<\/pre><\/div>\n\n\n<p>The whole point of styled-components is to isolate a single css class from other components eliminating the problem of selector name collisions but it would useful to be able to define styles that can be applied globally. For that we use <code><strong>createGlobalStyle<\/strong><\/code>.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"has-large-font-size wp-block-heading\">Setup Global Styles<\/h2>\n\n\n\n<p>We are going to us <strong><code>createGlobalStyle<\/code><\/strong> in our layout.js component which will allows us to share our styles globally throughout the site.<\/p>\n\n\n\n<p>Start by editing our layout.js file:<\/p>\n<\/div><\/div>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; highlight: [5,6,8,9,10,11,12,13,14,15,16,17,18,20,22,23,25,26]; title: Double click to copy; notranslate\" title=\"Double click to copy\">\n\/\/ frontend\/src\/components\/layout.js\n\nimport React from &quot;react&quot;\nimport Header from &quot;.\/header&quot;\nimport styled from &quot;styled-components&quot;\nimport { createGlobalStyle } from &quot;styled-components&quot;\n\nconst GlobalStyle = createGlobalStyle`\n  div {\n    font-size: 1.8rem;\n  }\n  a {\n    color: teal;\n  }\n`\nconst BodyWrapper = styled.div`\n  padding: 2.0rem;\n`\n\nexport default function Layout({ children }) {\n  return (\n    &lt;React.Fragment&gt;\n      &lt;GlobalStyle \/&gt;\n        &lt;Header \/&gt;\n        &lt;BodyWrapper&gt;{children}&lt;\/BodyWrapper&gt;\n    &lt;\/React.Fragment&gt;\n  )\n}\n\n<\/pre><\/div>\n\n\n<p>What did we do?<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We imported styled-components and <strong>createGlobalStyle<\/strong> from styled-components<\/li><li>Using <strong>createGlobalStyle<\/strong>, define a base font size and the anchor color for the entire site and assign these style definitions to the  <strong>GlobalStyle<\/strong> component.<\/li><li>Define a BodyWrapper component adding padding to the entire site.<\/li><li>Add our <strong>GlobalStyle<\/strong> component.<\/li><li>Wrap {children} with our BodyWrapper component adding site wide padding.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add Styles to Header<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; highlight: [7,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,38,39,40,47,48,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69]; title: Double click to copy; notranslate\" title=\"Double click to copy\">\n\/\/ frontend\/src\/components\/header.js\n\nimport { Link } from &quot;gatsby&quot;\nimport PropTypes from &quot;prop-types&quot;\nimport React from &quot;react&quot;\nimport { useStaticQuery, graphql } from &quot;gatsby&quot;\nimport styled from &quot;styled-components&quot;\n\nconst HeaderWrapper = styled.div`\n  padding: 2.0rem;\n  display: flex;\n  flex-direction: row;\n  align-items = center;\n  justify-content: space-between;\n  background-color: rebeccapurple;\n  a {\n    color: white;\n    text-decoration: none;\n  }\n`\nconst SiteTitle = styled.div`\n  font-family: Arial, Helvetica, sans-serif;\n`\nconst HeaderH1 = styled.h1`\n  font-size: 2.6rem;\n`\nconst MainMenuWrapper = styled.div`\n  padding: 1.0rem 0;\n  a {\n    padding: 0 1.0rem 0 1.0rem;\n  }\n`\nconst StyledLink = styled(Link)`\n  color: white;\n`\n\nexport const PureHeader = ({ data }) =&gt; (\n  &lt;HeaderWrapper&gt;\n    &lt;SiteTitle&gt;\n      &lt;HeaderH1&gt;\n        &lt;Link\n          to=&quot;\/&quot;\n          \n        &gt;\n          {data.site.siteMetadata.title}\n        &lt;\/Link&gt;\n      &lt;\/HeaderH1&gt;\n    &lt;\/SiteTitle&gt;\n\n    &lt;MainMenuWrapper&gt;\n      &lt;Link\n        to=&quot;#&quot;\n      &gt;\n        Link 1\n      &lt;\/Link&gt;\n\n      &lt;Link\n        to=&quot;#&quot;\n      &gt;\n        Link 2\n      &lt;\/Link&gt;\n\n      &lt;Link\n        to=&quot;#&quot;\n      &gt;\n        Link 3\n      &lt;\/Link&gt;\n    &lt;\/MainMenuWrapper&gt;\n  &lt;\/HeaderWrapper&gt;\n)\n\nexport const Header = props =&gt; {\n  const data = useStaticQuery(graphql`\n    query {\n      site {\n        siteMetadata {\n          title\n        }\n      }\n    }\n  `)\n\n  return &lt;PureHeader {...props} data={data}&gt;&lt;\/PureHeader&gt;\n}\n\nHeader.propTypes = {\n  siteTitle: PropTypes.string,\n}\n\nHeader.defaultProps = {\n  siteTitle: ``,\n}\n\nexport default Header\n<\/pre><\/div>\n\n\n<p>If you hit http:\/\/localhost:8000 you should see something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"578\" src=\"http:\/\/10.0.0.14\/wp-content\/uploads\/2020\/12\/gatsby_styled_header-1024x578.jpg\" alt=\"\" class=\"wp-image-1562\" srcset=\"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2020\/12\/gatsby_styled_header-1024x578.jpg 1024w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2020\/12\/gatsby_styled_header-300x169.jpg 300w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2020\/12\/gatsby_styled_header-768x434.jpg 768w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2020\/12\/gatsby_styled_header-600x339.jpg 600w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2020\/12\/gatsby_styled_header-945x534.jpg 945w, https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-content\/uploads\/2020\/12\/gatsby_styled_header.jpg 1048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Next up in chapter 13: implement authentication for Gatsby. <\/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-11-layout-component\">&lt; Previous:  Chapter 11<\/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-13-authentication\">Next: Chapter 13 &gt;<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Styled Components Quoting from the Styled Components Docs: styled-components is the result of wondering how we could enhance CSS for styling React component systems. By focusing on a single use case we managed to optimize the experience for developers as&#8230; <a class=\"more-link\" href=\"https:\/\/tutorials.leesonresearch.com\/tutorials\/2020\/11\/19\/tdd-with-gatsby-django-docker-part-2-chapter-12-using-theme-ui\/\">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,26],"class_list":["post-1485","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured-tutorial","category-gatsby","tag-gatsby","tag-styled-components"],"_links":{"self":[{"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/posts\/1485","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=1485"}],"version-history":[{"count":62,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/posts\/1485\/revisions"}],"predecessor-version":[{"id":2523,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/posts\/1485\/revisions\/2523"}],"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=1485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/categories?post=1485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorials.leesonresearch.com\/tutorials\/wp-json\/wp\/v2\/tags?post=1485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}