-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
resolves #21
- Loading branch information
There are no files selected for viewing
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> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect, useState } from "react"; | ||
Check failure on line 1 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest
Check failure on line 1 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
|
||
export default function useColorScheme() { | ||
const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; | ||
Check failure on line 4 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest
Check failure on line 4 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
|
||
const [colorScheme, setColorScheme] = useState<'dark' | 'light'>(isDarkMode ? 'dark' : 'light'); | ||
Check failure on line 6 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest
Check failure on line 6 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
|
||
useEffect(() => { | ||
|
||
Check failure on line 9 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest
Check failure on line 9 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
Check failure on line 9 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and macOS-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 18.x and ubuntu-latest
Check failure on line 11 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
Check failure on line 11 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and macOS-latest
|
||
setColorScheme(newColorScheme); | ||
Check failure on line 12 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest
Check failure on line 12 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
}); | ||
Check failure on line 13 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest
Check failure on line 13 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
|
||
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 18.x and ubuntu-latest
Check failure on line 16 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
Check failure on line 16 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and macOS-latest
|
||
setColorScheme(event.matches ? "dark" : "light"); | ||
Check failure on line 17 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest
Check failure on line 17 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
}); | ||
Check failure on line 18 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest
Check failure on line 18 in src/useColorScheme/index.tsx GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest
|
||
} | ||
}, []); | ||
|
||
return colorScheme; | ||
} |