-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from PunitSoniME/feat/21-use-color-scheme-hook
feat(useColorScheme): new hook added
- Loading branch information
Showing
9 changed files
with
108 additions
and
26 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
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,42 @@ | ||
import { lazy, Suspense } from 'react'; | ||
import { useColorScheme } from '../../../../'; | ||
import { packageName } from '@/lib/utils'; | ||
|
||
const Block = lazy(() => import('@/common/Details/Block')); | ||
const Documentation = lazy(() => import('@/common/Documentation')); | ||
|
||
const hook = 'useColorScheme'; | ||
const info = "Use this hook to get the current color scheme either 'dark' or 'light'" | ||
|
||
const usage: string = `import { ${hook} } from '${packageName}'; | ||
export default function Component() { | ||
/* | ||
@returns - Current color scheme | ||
*/ | ||
const colorScheme = ${hook}(); | ||
}` | ||
|
||
export default function ColorSchemeComponent() { | ||
|
||
const colorScheme = useColorScheme(); | ||
|
||
return ( | ||
<Suspense fallback={<></>}> | ||
|
||
<Documentation | ||
hook={hook} | ||
info={info} | ||
usage={usage} | ||
> | ||
|
||
<Block title='Example'> | ||
|
||
Current color scheme is - <b>{colorScheme}</b> | ||
|
||
</Block> | ||
</Documentation> | ||
|
||
</Suspense> | ||
) | ||
} |
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
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,30 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export default function useColorScheme() { | ||
const isDarkMode = | ||
window.matchMedia && | ||
window.matchMedia('(prefers-color-scheme: dark)').matches; | ||
|
||
const [colorScheme, setColorScheme] = useState<'dark' | 'light'>( | ||
isDarkMode ? 'dark' : 'light' | ||
); | ||
|
||
useEffect(() => { | ||
window | ||
.matchMedia('(prefers-color-scheme: dark)') | ||
.addEventListener('change', (event) => { | ||
const newColorScheme = event.matches ? 'dark' : 'light'; | ||
setColorScheme(newColorScheme); | ||
}); | ||
|
||
return () => { | ||
window | ||
.matchMedia('(prefers-color-scheme: dark)') | ||
.removeEventListener('change', (event) => { | ||
setColorScheme(event.matches ? 'dark' : 'light'); | ||
}); | ||
}; | ||
}, []); | ||
|
||
return colorScheme; | ||
} |