Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Expenses Chart with editable data and dark mode using React

Azharβ€’ 600

@azhar1038

Desktop design screenshot for the Expenses chart component coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


This time I decided to go with React as I needed more control over states.

Most difficult part for me this time were:

  • Using React! I am just getting started with React, so took a very long time.
  • Animating bars. I tried animating the bars using transform but the border radius were getting squeezed. I tried dividing the bars in to 3 parts: top, middle and bottom and used scale on middle and translate on top but was not able to achieve proper positioning. So for now I have just animated height.
  • Dark mode. For this I prefer to use Adam Argyle's way and it works perfectly for normal apps but I don't know how to migrate it to react project, i.e how to load and run the script before everything else and then bind to the theme button. I ended up removing most of the functionality to be able to use it with react.

If you have any advice on how to handle bar animation and best way to integrate script for dark mode, do let me know.

OR if you know how I can create a standalone "Dark Mode Switch component" that I can just integrate in other apps, please let me know!

Thanks for checking out my solution πŸ™‚

Community feedback

Md5 daltonβ€’ 1,430

@md5dalton

Posted

Hello Azhar.

Here's a component I've reused whenever I needed the functionality of toggling Dark/Light theme:

import React, { useState, useEffect } from 'react'

export default () => {
    
    const defaultTheme = "light"
    const altTheme = "dark"

    const getTheme = () => localStorage.getItem("theme")

    const [ theme, setTheme ] = useState(getTheme())
    
    const toggleTheme = () => {

        const theme = getTheme()

        let newTheme = theme !== defaultTheme ? defaultTheme : altTheme  
        if (!theme) newTheme = altTheme
        
        localStorage.setItem("theme", newTheme)

        setTheme(newTheme)

    }

    useEffect(() => {
        document.body.classList.remove(defaultTheme, altTheme)
        document.body.classList.add(theme || defaultTheme)
    })

    return (
        <div className="theme-toggler">
            <label htmlFor="theme">{theme || defaultTheme} mode</label>
            <input type="checkbox" id="theme" checked={theme === altTheme ? true : false} onChange={toggleTheme} />
        </div>
    )
}

You can always modify the JSX to your likingπŸ‘Œ

Marked as helpful

0

Azharβ€’ 600

@azhar1038

Posted

@md5dalton Thanks!

If the theme changes after component is loaded, won't there be any flickering of screen?

0
Azharβ€’ 600

@azhar1038

Posted

@md5dalton But I do see flash in live preview πŸ˜…

I opened your site, switched to dark mode and then upon reopening I see cards starts light and then go dark.

Maybe it is because of transitions, I am not sure.

(BTW, I checked in my 5 year old phone)

0

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord