-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
696bf43
commit 2fd00f8
Showing
5 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React, { useCallback, useEffect, useState } from 'react' | ||
|
||
import { enable, disable } from 'darkreader' | ||
|
||
const themeStorageName = 'design-system-theme-mode' | ||
|
||
const DarkMode = () => { | ||
const [isDarkModeActive, setIsDarkModeActive] = useState<boolean>(() => { | ||
try { | ||
if (typeof localStorage !== 'undefined') { | ||
if (localStorage.getItem(themeStorageName) === 'dark') { | ||
return true | ||
} | ||
} | ||
} catch { | ||
// do nothing | ||
} | ||
|
||
return false | ||
}) | ||
|
||
const handleEnable = useCallback(() => { | ||
try { | ||
if (typeof localStorage !== 'undefined') { | ||
localStorage.setItem(themeStorageName, 'dark') | ||
} | ||
} catch { | ||
// do nothing | ||
} | ||
|
||
setIsDarkModeActive(true) | ||
}, []) | ||
|
||
const handleDisable = useCallback(() => { | ||
try { | ||
if (typeof localStorage !== 'undefined') { | ||
localStorage.setItem(themeStorageName, 'default') | ||
} | ||
} catch { | ||
// do nothing | ||
} | ||
|
||
setIsDarkModeActive(false) | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (isDarkModeActive) { | ||
enable({ | ||
brightness: 100, | ||
contrast: 100, | ||
sepia: 0, | ||
}) | ||
} else { | ||
disable() | ||
} | ||
}, [isDarkModeActive]) | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
try { | ||
if (typeof localStorage !== 'undefined') { | ||
if (localStorage.getItem(themeStorageName) === 'dark') { | ||
setIsDarkModeActive(true) | ||
} else { | ||
setIsDarkModeActive(false) | ||
} | ||
} | ||
} catch { | ||
// do nothing | ||
} | ||
}, 200) | ||
|
||
return () => { | ||
clearInterval(interval) | ||
} | ||
}, [isDarkModeActive]) | ||
|
||
if (isDarkModeActive) { | ||
return ( | ||
<button type='button' onClick={handleDisable}> | ||
Disable dark mode in render | ||
</button> | ||
) | ||
} | ||
|
||
return ( | ||
<button type='button' onClick={handleEnable}> | ||
Enable dark mode in render | ||
</button> | ||
) | ||
} | ||
|
||
export default DarkMode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters