Become a member
to unlock all features

Level Up!

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


    Cycle Through Multiple Theme UI Color Modes

    John OtanderJohn Otander

    If you want to offer more than two color modes in your Theme UI-based site you can create a button that can activate all color modes. In this lesson we'll add two new color modes, for a total of four, and then use the useThemeUI hook to create an array of color modes to cycle through.



    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: add more color modes. The first thing we'll need to do is open up the theme file, then we can add in a couple new color modes. Let's add in tomato and purple.

    It's time to open up the layout component. In addition to the useColorMode hook, theme-ui also offers the useThemeUI hook. We can invoke that and get the theme. Now, instead of alternating between light and dark, we'll need to be able to cycle through all the existing modes.

    First, we'll need to grab the initial color mode name from theme, then we can pull out the other names of the color palates from the color modes key.

    We'll create a new array with initial color mode name at the start. Let's go ahead and log that to make sure the output's correct.

    Now we need to determine what the index of the next color mode will be. We can start by grabbing the index of the current color mode. We can then add 1 to the current index.

    However, we'll need to also use the modular operator, so we don't walk out the back of our array. Now we can access all modes to get the new color mode.

    Lastly, we can clean up our old logic. Now, we have a color mode toggle that cycles through all possible color modes.