Skip to content

Commit

Permalink
feat(useColorScheme): new hook added
Browse files Browse the repository at this point in the history
resolves #21
  • Loading branch information
PunitSoniME committed Jul 29, 2024
1 parent 92bb6ed commit 63dabbb
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ yarn add react-helper-hooks
- useArray - [![][documentation-demo]](https://punitsonime.github.io/react-helper-hooks/#useArray)
- useAsync - [![][documentation-demo]](https://punitsonime.github.io/react-helper-hooks/#useAsync)
- useAsyncLoop - [![][documentation-demo]](https://punitsonime.github.io/react-helper-hooks/#useAsyncLoop)
- useClickOutside - [![][documentation-demo]](https://punitsonime.github.io/react-helper-hooks/#useClickOutside)
- useColorBlend - [![][documentation-demo]](https://punitsonime.github.io/react-helper-hooks/#useColorBlend)
- useColorScheme - [![][documentation-demo]](https://punitsonime.github.io/react-helper-hooks/#useColorScheme)
- useCopyToClipboard - [![][documentation-demo]](https://punitsonime.github.io/react-helper-hooks/#useCopyToClipboard)
- useDebounce - [![][documentation-demo]](https://punitsonime.github.io/react-helper-hooks/#useDebounce)
- useEventListener - [![][documentation-demo]](https://punitsonime.github.io/react-helper-hooks/#useEventListener)
Expand Down
7 changes: 4 additions & 3 deletions example/src/common/Details/RedirectToExample.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Button } from '@/components/ui/button'
import { githubUrl } from '@/lib/utils';
import { MoveUpRightIcon } from 'lucide-react'

const githubUrl = 'https://github.com/PunitSoniME/react-helper-hooks/blob/main'
const hookCodeUrl = `${githubUrl}/src`;
const exampleCodeUrl = `${githubUrl}/example/src/hooks`;
const extendedGithubUrl = `${githubUrl}/blob/main`;
const hookCodeUrl = `${extendedGithubUrl}/src`;
const exampleCodeUrl = `${extendedGithubUrl}/example/src/hooks`;

export default function RedirectToExample({ hook }: any) {

Expand Down
10 changes: 0 additions & 10 deletions example/src/hooks/useArray/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ export default function Component() {
const { array, set, push, remove, filter, update, clear } = ${hook}([DATA]);
}`

const api = [
{ execute: 'array', type: 'array', description: 'Data on which operation to perform' },
{ execute: 'set', type: 'function', description: 'Re-initialize the array with new data' },
{ execute: 'push', type: 'function', description: 'Insert an element in array' },
{ execute: 'remove', type: 'function', description: 'Remove an element from array' },
{ execute: 'filter', type: 'function', description: 'Filter out the data from array ( This will mutate the source )' },
{ execute: 'update', type: 'function', description: 'Update any element of array' },
{ execute: 'clear', type: 'function', description: 'Clear whole array' }
];

export default function ArrayComponent() {

const { array, set, push, remove, filter, update, clear } = useArray([1, 2, 3, 4, 5, 6])
Expand Down
42 changes: 42 additions & 0 deletions example/src/hooks/useColorScheme/index.tsx
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>
)
}
5 changes: 3 additions & 2 deletions example/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { twMerge } from "tailwind-merge"
export const packageName = 'react-helper-hooks';

export const githubUrl = 'https://github.com/PunitSoniME/react-helper-hooks';
export const exampleMarkDownPrefixUrl = `${githubUrl}/blob/main/src/docs`;

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand Down Expand Up @@ -38,6 +37,7 @@ const SpeechComponent = lazy(() => import('@/hooks/useSpeech'));
const ProvidersTreeComponent = lazy(() => import('@/hooks/useProvidersTree'));
const HashComponent = lazy(() => import('@/hooks/useHash'));
const ClickOutsideComponent = lazy(() => import('@/hooks/useClickOutside'));
const ColorSchemeComponent = lazy(() => import('@/hooks/useColorScheme'));

export const hooks = [
{ key: 'useToggle', Component: ToggleComponent },
Expand Down Expand Up @@ -66,7 +66,8 @@ export const hooks = [
{ key: 'useSpeech', Component: SpeechComponent },
{ key: 'useProvidersTree', Component: ProvidersTreeComponent },
{ key: 'useHash', Component: HashComponent },
{ key: 'useClickOutside', Component: ClickOutsideComponent, isNew: true }
{ key: 'useClickOutside', Component: ClickOutsideComponent, isNew: true },
{ key: 'useColorScheme', Component: ColorSchemeComponent, isNew: true }
];

export const props = {
Expand Down
17 changes: 14 additions & 3 deletions example/src/pages/Layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { lazy, Suspense } from 'react'
import { lazy, Suspense, useMemo } from 'react'
import { useHash } from '../../../../.';
import { hooks, props } from '@/lib/utils';
import { ScrollArea } from '@/components/ui/scroll-area';
import classes from './Layout.module.css';
import RedirectToExample from '@/common/Details/RedirectToExample';
import { Sheet, SheetContent } from '@/components/ui/sheet';
import Title from '@/common/Details/Title';

const Header = lazy(() => import('../Header'));
const Sidebar = lazy(() => import('../Sidebar'));
Expand All @@ -15,7 +14,19 @@ const LandingPage = lazy(() => import('../LandingPage'));
export default function Layout() {

const [hash,] = useHash();
const selectedHook: string = hash ? (hash as string).split('#')[1] : "";
// const selectedHook: string = hash ? (hash as string).split('#')[1] : "";

const selectedHook = useMemo(() => {
if (hash) {
const splittedHookName = (hash as string).split('#')[1]?.toLowerCase();
if (splittedHookName) {
const hookDetails = hooks.find(f => f.key.toLowerCase() === splittedHookName);
return hookDetails?.key;
}
return "";
}
return "";
}, [hash]);

// @ts-ignore
const Component = hooks.find(f => f.key === selectedHook)?.Component || null;
Expand Down
10 changes: 5 additions & 5 deletions example/src/pages/Sidebar/HooksList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default function HooksList() {

const filteredComponents = components.filter(f => f.key.toLowerCase().includes(search.toLowerCase()));

const [hash, setHash] = useHash();
const selectedHook = hash ? hash.split('#')[1] : "";
const [hash, updateHash] = useHash();
const selectedHook = hash ? (hash as string).split('#')[1]?.toLowerCase() : "";

return (
<>
Expand All @@ -36,13 +36,13 @@ export default function HooksList() {
<Button
className="font-normal justify-start"
onClick={() => {
setHash(hook.key);
updateHash(hook.key);
}}
variant={selectedHook === hook.key ? 'default' : 'ghost'}
variant={selectedHook === hook.key.toLowerCase() ? 'default' : 'ghost'}
>
{hook.key} {
hook.isNew
? <span className={`ml-2 px-2 rounded-md ${selectedHook === hook.key ? 'bg-white text-blue-600' : 'bg-blue-600 text-white'}`}>New</span>
? <span className={`ml-2 px-2 rounded-md ${selectedHook === hook.key.toLowerCase() ? 'bg-white text-blue-600' : 'bg-blue-600 text-white'}`}>New</span>
: ""
}
</Button>
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ export { default as useHash } from './useHash';
export { default as useSpeech } from './useSpeech';
export { default as useProvidersTree } from './useProvidersTree';
export { default as useClickOutside } from './useClickOutside';
export { default as useColorScheme } from './useColorScheme';

export { useLocalStorage, useSessionStorage };
23 changes: 23 additions & 0 deletions src/useColorScheme/index.tsx
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

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `"react"` with `'react'`

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

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `·window.matchMedia·&&` with `⏎····window.matchMedia·&&⏎···`

const [colorScheme, setColorScheme] = useState<'dark' | 'light'>(isDarkMode ? 'dark' : 'light');

Check failure on line 6 in src/useColorScheme/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `isDarkMode·?·'dark'·:·'light'` with `⏎····isDarkMode·?·'dark'·:·'light'⏎··`

useEffect(() => {

Check failure on line 9 in src/useColorScheme/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `⏎····window.matchMedia('(prefers-color-scheme:·dark)').addEventListener('change',·event` with `····window⏎······.matchMedia('(prefers-color-scheme:·dark)')⏎······.addEventListener('change',·(event)`
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

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `······const·newColorScheme·=·event.matches·?·"dark"·:·"light"` with `········const·newColorScheme·=·event.matches·?·'dark'·:·'light';`
setColorScheme(newColorScheme);

Check failure on line 12 in src/useColorScheme/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `······` with `········`
});

Check failure on line 13 in src/useColorScheme/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `··`

return () => {
window.matchMedia('(prefers-color-scheme: dark)').removeEventListener('change', event => {

Check failure on line 16 in src/useColorScheme/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `.matchMedia('(prefers-color-scheme:·dark)').removeEventListener('change',·event` with `⏎········.matchMedia('(prefers-color-scheme:·dark)')⏎········.removeEventListener('change',·(event)`
setColorScheme(event.matches ? "dark" : "light");

Check failure on line 17 in src/useColorScheme/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `········setColorScheme(event.matches·?·"dark"·:·"light"` with `··········setColorScheme(event.matches·?·'dark'·:·'light'`
});

Check failure on line 18 in src/useColorScheme/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `··`
}
}, []);

return colorScheme;
}

0 comments on commit 63dabbb

Please sign in to comment.