Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Use and Override a Theme in Gatsby with Component Shadowing

    Jason LengstorfJason Lengstorf

    Now that styles are set in theme-ui, we need to actually use them in our application.

    First, we'll override the default theme-ui config by shadowing the theme object in gatsby-theme-ui/index.js and start using the theme throughout.

    For a written version of this course, check out the Gatsby docs.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: To use the theme we've defined, we need to use something called component shadowing to override the default theme in Gatsby Theme UI.

    We do this by creating a folder with the same name as the theme in the source folder. We'll call it Gatsby Theme UI, and then we're going to shadow the index.js, which is where the theme is defined. We're going to import theme that we just defined in theme.js. We'll export default theme.

    Next, we'll refactor our layout to use theme UI. We'll start by importing the layout, but since that conflicts with our layout component, we're going to alias that as theme layout. We'll also import header, main, and container from theme UI.

    Once we have those components, we will start using them. Wrap the whole thing with theme layout. We'll set up a header. Move our h1 inside of that header. We'll set up the main component with the container inside of it, and move children inside of that.

    To see whether or not this worked, we can test it using yarn workspace site develop. On loading the site, we can see that our styles have been applied. We can also use the style import from Theme UI. To apply the styles we set, to regular imports.

    By replacing the h1 with style.h1, and doing the same for our UL and LI that we defined, we can see that our styles are now applied, including our hover styles.