react-icons/bs#BsSun JavaScript Examples

The following examples show how to use react-icons/bs#BsSun. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: darktoggle.js    From HealthBridge with GNU General Public License v3.0 5 votes vote down vote up
DarkToggle = () => {

  const [isDark, setIsDark] = useState(false);
  
  useEffect(() => {
    try {
      // console.log("checking dark ls")
      const dark = localStorage.getItem('prevState')
      // console.log('prevState', dark)
      // console.log("typeof dark",typeof(dark))
      if(dark != null) {
        setIsDark(dark==='true')
        if (dark) {
          document.documentElement.classList.add(DARK_CLASS)
        } else {
          document.documentElement.classList.remove(DARK_CLASS)
        }
      }
    }
    catch(e) {

    }
  }, [])

     useEffect(() => {
      // console.log("changin dark",isDark)
       if (isDark) {
         document.documentElement.classList.add(DARK_CLASS)
       } else {
         document.documentElement.classList.remove(DARK_CLASS)
       }
     }, [isDark]);

    //  console.log("isDark", isDark)

  return (

    <Toggle 
        className="DarkToggle"
        onChange={event => {
          setIsDark(event.target.checked)
          localStorage.setItem('prevState', event.target.checked)
        }}
        checked={isDark}
        icons={{checked: <BsMoon color="white" />, unchecked: <BsSun color="yellow"/>}}
        aria-label="Dark mode"
    />
  );
}