-
-
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.
feat(useColorScheme): new hook added
resolves #21
- Loading branch information
1 parent
92bb6ed
commit 63dabbb
Showing
9 changed files
with
94 additions
and
23 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,23 @@ | ||
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(() => { | ||
|
||
Check failure on line 9 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { | ||
const newColorScheme = event.matches ? "dark" : "light" | ||
Check failure on line 11 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
setColorScheme(newColorScheme); | ||
}); | ||
|
||
return () => { | ||
window.matchMedia('(prefers-color-scheme: dark)').removeEventListener('change', event => { | ||
Check failure on line 16 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
setColorScheme(event.matches ? "dark" : "light"); | ||
}); | ||
} | ||
}, []); | ||
|
||
return colorScheme; | ||
} |