<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Catherine Oxley on Medium]]></title>
        <description><![CDATA[Stories by Catherine Oxley on Medium]]></description>
        <link>https://medium.com/@bibiiii?source=rss-27293cfefa7a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kNG3Sijz1RkOu9T0M7GsoQ.jpeg</url>
            <title>Stories by Catherine Oxley on Medium</title>
            <link>https://medium.com/@bibiiii?source=rss-27293cfefa7a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 09:59:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bibiiii/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Themes within themes with React, TypeScript, and Styled Components ✨]]></title>
            <link>https://javascript.plainenglish.io/themes-within-themes-with-react-typescript-and-styled-components-e65491e45f5e?source=rss-27293cfefa7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e65491e45f5e</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[styled-components]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Catherine Oxley]]></dc:creator>
            <pubDate>Thu, 27 Aug 2020 09:18:26 GMT</pubDate>
            <atom:updated>2022-09-08T16:27:38.804Z</atom:updated>
            <content:encoded><![CDATA[<h3>Themes within themes with React, TypeScript and Styled Components ✨</h3><h4>How do you structure an app with multiple themes? What if these themes are <em>inside</em> another theme?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GOhd36zGnR7Navh4XP7AQA.jpeg" /><figcaption>Pencils by Jess Bailey</figcaption></figure><p>A recent project came with some big challenges. The client wanted a demo experience that would display a range of different websites within a branded ‘container’. These demo websites would be interactive within a mock desktop or mobile device, each with its own individual theme. In this article, I will dive deeper into:</p><ol><li>How we set up the project files</li><li>Theming using Styled Components</li></ol><p>This was challenging enough, but on top of this, we needed to give the user the ability to toggle between the mocked ‘desktop’ display and ‘mobile’ display modes, all within this static container, <em>without</em> using media queries. I will address this challenge in a follow up article.</p><p>Here are two illustrations of what we produced:</p><figure><img alt="Mockup design with mobile site demo in the centre" src="https://cdn-images-1.medium.com/max/874/1*IgBcQ34PJbpzrSMCN-MTqg.png" /><figcaption>Mobile view</figcaption></figure><figure><img alt="Mockup design with desktop site demo in centre" src="https://cdn-images-1.medium.com/max/874/1*CDUcwGgRBpuqt5F6qLjv0g.png" /><figcaption>Desktop View</figcaption></figure><h3>How many themes do we need? 💭</h3><p>The first step here was figuring out how many themes we were going to need, and secondly, how to set up these themes. It was <em>crucial</em> that we set this up correctly from the beginning, to save us a lot of grief in the long run.</p><ol><li>Here we need one theme for the static container, which will stay the same regardless of the active demo. This is going to have a branded dark theme to match the Client’s style guide.</li><li>We also need one theme per ‘demo’, which will change depending on which demo is active. So three demos would mean three more themes, etc.</li></ol><p>Seems simple enough. Here are some basic illustrations of how this site-within-a-site should function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D-m76HZHBPCQGA-7sgmPPQ.png" /><figcaption>Selecting a demo from the left will display a unique styled theme within the mobile frame. The outer container will keep its theme throughout.</figcaption></figure><h3>Setting up the files 📚</h3><p>The first component that we need to create is our static demo container, which is going to wrap the mock device and all the other themes.</p><p>Let’s create that as a React component called ‘DemoContainer’. Personally, I like to use the following file structure when creating components that use Styled Components (we are using TypeScript here):</p><pre>src<br>├── components<br>│   ├── atoms<br>│   ├── molecules<br>│   └── organisms<br>│       └── DemoContainer<br>│           ├── DemoContainer.tsx<br>│           ├── Demo.props.ts<br>│           └── Demo.style.ts</pre><h3>Adding the first theme</h3><p>Now we have our basic component, we need to set up its theme with Styled Components’ ThemeProvider. In this case, we created a styles/themes folder to house all of our different themes.</p><p>This is the structure we went with:</p><pre>src<br>├── styles<br>│   └── themes<br>│       ├── demoContainer<br>│       │   ├── colors.ts<br>│       │   ├── text.ts<br>│       │   ├── typography.ts<br>│       │   └── index.ts<br>│       └── index.ts</pre><h4>/colors.ts 🎨</h4><p>This file is going to contain the color variables for the theme. There is a lot of debate about the <em>correct </em>way to set up colors, and in my opinion this should be done side-by-side with a designer. However, as we’re not here to debate this, here are some dummy variable names to keep everyone happy 😎</p><pre><strong><em>//colors.ts</em></strong></pre><pre>const colors = {<br>  primary: {<br>    primary1: &#39;#172121&#39;,<br>    primary2: &#39;#444554&#39;,<br>    primary3: &#39;#7F7B82&#39;,<br>  },<br>  secondary: {<br>    secondary1: &#39;#BFACB5&#39;,<br>    secondary2: &#39;#E5D0CC&#39;,<br>  },<br>  ...<br>}</pre><pre>export default colors;</pre><h4>/text.ts 🔠</h4><p>This is where we’re going to store all our text variables for the theme. This includes:</p><ul><li>font families</li><li>sizes</li><li>letter spacing</li><li>font weight</li><li>line height</li></ul><p>and any other variables you want to store for your text. Here is an example of what you could store:</p><pre><strong><em>// text.ts</em></strong></pre><pre>const text = {<br>  font: {<br>    heading: &#39;&quot;Merriweather&quot;, Georgia, serif&#39;,<br>    body: &#39;&quot;Open Sans&quot;, Helvetica, Arial, sans-serif&#39;,<br>  }<br>  size: {<br>    heading: {<br>      mobile: {<br>        h1: 40,<br>        h2: 28,<br>        ...<br>      },<br>      desktop: {...},<br>    },<br>    body: {<br>      mobile: {<br>        regular: 16,<br>        ...<br>      },<br>      desktop: {<br>        regular: 24,<br>        ...<br>      }<br>    }<br>  },<br>  weight: {<br>    light: 200,<br>    regular: 400,<br>    bold: 700,<br>  }<br>}</pre><pre>export default text;</pre><h4>/typography.ts 📝</h4><p>Finally, typography is where we will set global typography rules for this theme. We can use Styled Components’ css to export some set styles that we would like to use throughout this theme.</p><pre><strong><em>// typography.ts</em></strong></pre><pre><em>import</em> { css } <em>from</em> &#39;styled-components&#39;;<br><em>import </em>text <em>from </em>&#39;./text&#39;;</pre><pre>const { font, size, weight, lineHeight, letterSpacing } = text;</pre><pre>const typography = css`<br>  font-family: ${font.body};<br>  font-weight: ${weight.regular};</pre><pre>  h1 {<br>    font-size: ${size.heading.mobile.h1}px;<br>  }<br>  ...<br>`;</pre><pre>export default typography;</pre><p>I think it’s much neater to import your text styles this way rather than adding them to the parent styled component. It’s effectively setting ‘global’ text styles for the theme, so it declutters your styled component and just leaves room for specific styling.</p><h4>/index.ts</h4><p>Now we have all our style variables set up for this theme, we can bring them together and export them from the index file, like so:</p><pre><strong>// <em>index.ts</em></strong></pre><pre><em>import</em> colors <em>from</em> &#39;./colors&#39;;<br><em>import</em> text <em>from</em> &#39;./text&#39;;<br><em>import</em> typography <em>from</em> &#39;./typography&#39;;</pre><pre>export default {<br>  name: &#39;demoContainer&#39;,<br>  colors,<br>  text,<br>  typography,<br>};</pre><p>There are a lot of other style variables you could include here, such as spacing and animation. The main point is that we want any reusable variables specific to each theme to be included in the theme export.</p><h4>Exporting the theme</h4><p>We know we’re going to need more than one theme here, so let’s think ahead an export these theme from the themes/index.ts file:</p><pre><strong><em>// index.ts</em></strong></pre><pre><em>import</em> demoContainer <em>from</em> &#39;./demoContainer&#39;;</pre><pre>const theme = {<br>  default: demoContainer,<br>  demoContainer,<br>};</pre><pre>export default theme;</pre><p>We’ve also added a ‘default’ theme here and set it as the demoContainer theme, just in case we need a fallback.</p><h3>Using the theme variables 🖼</h3><p>Now comes the fun part of seeing our theme in action. The first step is wrapping our DemoContainer component in a ThemeProvider from Styled Components.</p><h4>Adding the ThemeProvider</h4><p>Our DemoContainer.tsx file will look something like this:</p><pre><strong><em>// DemoContainer.tsx</em></strong></pre><pre><em>import</em> React, { FunctionComponent } <em>from</em> &#39;react&#39;;<br><em>import</em> { ThemeProvider } <em>from</em> &#39;styled-components&#39;;</pre><pre><em>import</em> { DemoContainerProps } <em>from</em> &#39;./DemoContainer.props&#39;;<br><em>import</em> theme <em>from</em> &#39;../../../styles/global/theme&#39;;</pre><pre>const <em>DemoContainer:</em> FunctionComponent&lt;DemoContainerProps&gt; = ({ children }) =&gt; {<br>  return (<br>    &lt;ThemeProvider <strong>theme={theme.demoContainer}</strong>&gt;<br>      Demo Container content here!<br>    &lt;/ThemeProvider&gt;<br>  )<br>}</pre><p>We’re now successfully passing the <em>demoContainer </em>theme to our DemoContainer component. To use the theme variables, let’s create our first styled component that will wrap all the content inside the container.</p><p>We will create a Wrapper component, which will sit within our ThemeProvider.</p><pre><strong><em>// DemoContainer.style.ts</em></strong></pre><pre><em>import</em> styled <em>from</em> &#39;styled-components&#39;;</pre><pre>export const Wrapper = styled.main``;</pre><p>⚡️</p><pre><strong><em>// DemoContainer.tsx</em></strong></pre><pre><em>import</em> React, { FunctionComponent } <em>from</em> &#39;react&#39;;<br><em>import</em> { ThemeProvider } <em>from</em> &#39;styled-components&#39;;</pre><pre><em>import</em> { DemoContainerProps } <em>from</em> &#39;./DemoContainer.props&#39;;<br><em>import</em> theme <em>from</em> &#39;../../../styles/global/theme&#39;;<br><strong><em>import</em> Wrapper <em>from</em> &#39;./DemoContainer.style.ts&#39;;</strong></pre><pre>const <em>DemoContainer:</em> FunctionComponent&lt;DemoContainerProps&gt; = () =&gt; {<br>  return (<br>    &lt;ThemeProvider theme={theme.demoContainer}&gt;<br>      &lt;<strong>Wrapper</strong>&gt;Demo Container content here!&lt;/<strong>Wrapper</strong>&gt;<br>    &lt;/ThemeProvider&gt;<br>  )<br>}</pre><h4>Using the theme variables in our Styled Components</h4><p>Now, because our Wrapper sits within the ThemeProvider, it also has access to the ‘theme’ prop automatically. So we can now use the variables that we’ve set up in our /themes folder, using object de-structuring of the ‘theme’ prop:</p><pre><strong><em>// DemoContainer.style.ts</em></strong></pre><pre><em>import</em> styled <em>from</em> &#39;styled-components&#39;;</pre><pre>export const Wrapper = styled.main`<br>  <em>// let&#39;s put all our global typography at the top</em><br>  <strong>${({theme}) =&gt; theme.typography};</strong></pre><pre>  height: 100vh;<br>  width: 100%;<br>  padding: 60px;<br>  background-color: <strong>${({theme}) =&gt; theme.colors.primary.primary1}</strong>;<br>  color: <strong>${({theme}) =&gt; theme.colors.secondary.secondary2}</strong>;</pre><pre>`;</pre><p>We’ve now got our DemoContainer styled using our variables. The theme.typography will ensure our text is styled, so no need to add extra text styling in our Wrapper. The demo should now have a nice background in our Primary Color 1, and text will be Secondary Color 2.</p><h3>Adding the second theme (and the third, and the fourth…) 👯‍♀️</h3><p>Our static container is done and styled, so all that is left is the dynamic theming of the device content, so gracefully highlighted by the arrow below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*oiZakuO5Ht3axfakaao2KA.png" /></figure><p>When the user clicks one of the buttons on the left (Demo 1, Demo 2 etc.), we need the theme within the mock device to change.</p><p>(We happened to use Redux for this state management, but you could also look at React’s useContext, which is possibly what I would use if I did this again)</p><p>Either way, to serve the correct theme we just need to access the theme’s name or ID from whatever provider is being used.</p><h4>Create a new component to wrap the dynamic themes</h4><p>Let’s create our mock device component, and call it MockDevice.</p><pre>src<br>├── components<br>│   ├── atoms<br>│   ├── molecules<br>│   └── organisms<br>│       ├── DemoContainer<br>│       └── MockDevice<br>│           ├── MockDevice.tsx<br>│           ├── MockDevice.props.ts<br>│           └── MockDevice.style.ts</pre><p>The MockDevice will have a very similar set-up to the DemoContainer, and it needs its own ThemeProvider. However, we’re going to put the ThemeProvider <em>between</em> two other StyledComponents:</p><ol><li>The Wrapper, which will provide the ‘mobile-effect’ styling to our mock device</li><li>The DeviceContent, which will contain whatever pages we want to serve for the site in question</li></ol><pre><strong><em>// </em></strong><strong><em>MockDevice.tsx</em></strong></pre><pre><em>import</em> React, { FunctionComponent } <em>from</em> &#39;react&#39;;<br><em>import</em> { ThemeProvider } <em>from</em> &#39;styled-components&#39;;</pre><pre><em>import</em> { MockDeviceProps } <em>from</em> &#39;./MockDevice.props&#39;;<br><em>import </em>{ Wrapper, DeviceContent } from &#39;./MockDevice.style.ts&#39;;</pre><pre>const MockDevice<em>:</em> FunctionComponent&lt;MockDeviceProps&gt; = () =&gt; {<br>  return (<br>    &lt;Wrapper&gt;<br>      &lt;ThemeProvider <strong>theme={???}</strong>&gt;<br>        &lt;DeviceContent&gt;<br>          Mock Device content here!<br>        &lt;/DeviceContent&gt;<br>      &lt;/ThemeProvider&gt;<br>    &lt;/Wrapper&gt;<br>  )<br>}</pre><h4>Adding new theme files</h4><p>As of yet, we don’t know what theme to pass into our new device component, but we can start setting up those themes in the same way we did the <em>demoContainer</em> theme.</p><pre>src<br>├── styles<br>│   └── themes<br>│       ├── demoContainer<br>│       ├── demo1<br>│       │   ├── colors.ts<br>│       │   ├── text.ts<br>│       │   ├── typography.ts<br>│       │   └── index.ts<br>│       ├── demo2<br>│       │   ├── colors.ts<br>│       │   ├── text.ts<br>│       │   ├── typography.ts<br>│       │   └── index.ts<br>│       └── index.ts</pre><p>don’t forget to export the themes in themes/index.ts:</p><pre><strong><em>// index.ts</em></strong></pre><pre><em>import</em> demoContainer <em>from</em> &#39;./demoContainer&#39;;<br><em>import</em> demo1 <em>from</em> &#39;./demo1&#39;;<br><em>import</em> demo2 <em>from</em> &#39;./demo2&#39;;</pre><pre>const theme = {<br>  default: demoContainer,<br>  demoContainer,<br>  demo1,<br>  demo2,<br>};</pre><pre>export default theme;</pre><h4>Using the dynamic theme 🧙‍♀️</h4><p>We can now get the active theme from our Provider and grab the correct styles. Depending on whether you’re using useContext or Redux, or another state management system, this bit might be slightly different. This is with Redux and hooks*:</p><pre><strong><em>// </em></strong><strong><em>MockDevice.tsx</em></strong></pre><pre><em>import</em> React, { FunctionComponent } <em>from</em> &#39;react&#39;;<br><em>import</em> { ThemeProvider } <em>from</em> &#39;styled-components&#39;;<br><strong><em>import</em> { useSelector } <em>from</em> &#39;react-redux&#39;;</strong></pre><pre><em>import</em> { MockDeviceProps } <em>from</em> &#39;./MockDevice.props&#39;;<br><em>import </em>{ Wrapper, DeviceContent } from &#39;./MockDevice.style.ts&#39;;<br><strong><em>import</em> themes <em>from</em> &#39;../../../styles/global/theme&#39;;</strong></pre><pre>const MockDevice<em>:</em> FunctionComponent&lt;MockDeviceProps&gt; = ({children}) =&gt; {<br>  <strong>const themeName = <em>useSelector</em>(<br>    (state<em>:</em> RootState) =&gt; state.theme,<br>  );</strong></pre><pre><strong>const theme = themes[themeName] || themes.default;</strong></pre><pre>return (<br>    &lt;Wrapper&gt;<br>      &lt;ThemeProvider <strong>theme={theme}</strong>&gt;<br>        &lt;DeviceContent&gt;<br>          {children}<br>        &lt;/DeviceContent&gt;<br>      &lt;/ThemeProvider&gt;<br>    &lt;/Wrapper&gt;<br>  )<br>}</pre><p><em>*Haven’t used hooks before or don’t want to include them? Then substitute ‘useSelector’ with a classic </em><a href="https://react-redux.js.org/using-react-redux/connect-mapstate"><em>mapStateToProps</em></a><em> within the connected component.</em></p><h3>Pulling the app together 💅</h3><p>We know that our DemoContainer component is going to wrap the whole app. This theme won’t change and it should remain static. We also know that the MockDevice needs to be within the Demo Container. So let’s add that:</p><pre><strong><em>// DemoContainer.tsx</em></strong></pre><pre><em>import</em> React, { FunctionComponent } <em>from</em> &#39;react&#39;;<br><em>import</em> { ThemeProvider } <em>from</em> &#39;styled-components&#39;;</pre><pre><em>import</em> { DemoContainerProps } <em>from</em> &#39;./DemoContainer.props&#39;;<br><em>import</em> theme <em>from</em> &#39;../../../styles/global/theme&#39;;<br><em>import</em> Wrapper <em>from</em> &#39;./DemoContainer.style.ts&#39;;<br><strong>import MockDevice from &#39;../MockDevice&#39;;</strong></pre><pre>const <em>DemoContainer:</em> FunctionComponent&lt;DemoContainerProps&gt; = ({ <strong>children</strong> }) =&gt; {<br>  return (<br>    &lt;ThemeProvider theme={theme.demoContainer}&gt;<br>      &lt;Wrapper&gt;<br>        &lt;<strong>MockDevice</strong>&gt;<br>          <strong>{children}</strong><br>        &lt;/<strong>MockDevice</strong>&gt;<br>      &lt;/Wrapper&gt;<br>    &lt;/ThemeProvider&gt;<br>  )<br>}</pre><p>Because the DemoContainer will remain completely static, we can render any React children within our MockDevice.</p><p>For this reason, we decided to have the DemoContainer wrap our Router, so when the user navigates to a new page, this page will display within our MockDevice and styled according to the chosen theme.</p><h3>Conclusion</h3><p>We now have our theme within a theme! Once you have this structure set up, you can add as many themes as you like.</p><ul><li>The DemoContainer styles will remain static, and will depend on the <em>demoContainer</em> theme variables. These style variables will all be accessible inside the DemoContainer’s Wrapper.</li><li>The MockDevice will <strong>override </strong>the <em>demoContainer</em> styles with the active theme in the app state. Any children inside MockDevice will have access to this theme only.</li><li>We can use prop de-structuring within any child styled component to access the active theme variables.</li></ul><h3>A note on Typing</h3><p>It’s important that the file structure remains identical in all new themes, to avoid any errors caused by the theme looking for styles that do not exist.</p><p>Using TypeScript really helps here, because you can define the properties that you must include in each file:</p><pre><strong><em>// themeTypes.ts</em></strong></pre><pre>export interface Text {<br>  font: {<br>    heading: string;<br>    body: string;<br>  };<br>  size: {<br>    heading: {<br>      mobile: {<br>        h1: number;<br>        h2: number;<br>      };<br>    };<br>  };<br>};</pre><pre><strong>// <em>text.ts</em></strong></pre><pre><em>import </em>{ <strong>Text</strong> } <em>from </em>&#39;./themeTypes.ts&#39;;</pre><pre>const text: <strong>Text</strong> = {...}</pre><pre>export default text;</pre><p>If you are not typing, ensure that you follow the same structure throughout.</p><h3>Thank you! 🤗</h3><p>I hope you enjoyed this article and it has helped you in some way. Have you encountered a similar challenge before? I would love to hear how you handled it in the comments below.</p><h4>Further Reading</h4><p><a href="https://bit.cloud/blog/theming-in-components-with-react-and-bit-l386casw">Theming in Components with React and Bit</a></p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><strong><em>, </em></strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><strong><em>, and</em></strong><em> </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><strong><em>.</em></strong><em> Interested in Growth Hacking? Check out </em><a href="https://circuit.ooo/"><strong><em>Circuit</em></strong></a><strong><em>.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e65491e45f5e" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/themes-within-themes-with-react-typescript-and-styled-components-e65491e45f5e">Themes within themes with React, TypeScript, and Styled Components ✨</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>