File tree Expand file tree Collapse file tree 6 files changed +56
-16
lines changed Expand file tree Collapse file tree 6 files changed +56
-16
lines changed Original file line number Diff line number Diff line change 1+ ``` tsx
2+ const colorScheme = useColorScheme ();
3+
4+ return <>Your system color scheme is: { colorScheme ()} </>;
5+ ```
Original file line number Diff line number Diff line change 1+ import { useColorScheme } from 'src' ;
2+ import { ExampleBase } from '../example-base' ;
3+ import Code from './use-color-scheme.code.mdx' ;
4+
5+ import { useMDXComponents } from 'solid-jsx' ;
6+
7+ export function UseColorSchemeExample ( ) {
8+ const colorScheme = useColorScheme ( ) ;
9+
10+ // @ts -ignore
11+ const components : any = useMDXComponents ( ) ;
12+
13+ return (
14+ < ExampleBase
15+ title = "useColorScheme"
16+ description = "A hook that returns system color scheme value i.e. either `dark` or `light`."
17+ code = { < Code components = { components } /> }
18+ >
19+ < div class = "flex h-full w-full items-center justify-center gap-x-1 rounded-md border p-3 py-10 text-center text-sm" >
20+ < div
21+ class = "rounded-md border px-4 py-2 text-sm"
22+ style = { {
23+ background : colorScheme ( ) === 'dark' ? '#000' : '#fff' ,
24+ color : colorScheme ( ) ? '#fff' : '#000' ,
25+ } }
26+ >
27+ Your system color scheme is: { colorScheme ( ) }
28+ </ div >
29+ </ div >
30+ </ ExampleBase >
31+ ) ;
32+ }
Original file line number Diff line number Diff line change 11``` tsx
22const id = useId ();
33
4- return (
5- <div >
6- Random ID: { id ()}
7- </div >;
8- )
4+ return <div >Random ID: { id ()} </div >;
95```
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import packageJSON from 'src/../package.json';
33// Hooks
44import { UseClickOutsideExample } from 'dev/components/examples/use-click-outside/use-click-outside.example' ;
55import { UseClipboardExample } from 'dev/components/examples/use-clipboard/use-clipboard.example' ;
6+ import { UseColorSchemeExample } from 'dev/components/examples/use-color-scheme/use-color-scheme.example' ;
67import { UseCounterExample } from 'dev/components/examples/use-counter/use-counter.example' ;
78import { UseDebouncedCallbackExample } from 'dev/components/examples/use-debounced-callback/use-debounced-callback.example' ;
89import { UseDebouncedSignalExample } from 'dev/components/examples/use-debounced-signal/use-debounced-signal.example' ;
@@ -168,6 +169,10 @@ export default function HomePage() {
168169 title : 'useDidUpdate' ,
169170 example : < UseDidUpdateExample /> ,
170171 } ,
172+ {
173+ title : 'useColorScheme' ,
174+ example : < UseColorSchemeExample /> ,
175+ } ,
171176 ] ;
172177
173178 const filteredList = createMemo ( ( ) => {
@@ -314,17 +319,6 @@ export default function HomePage() {
314319 </ div >
315320
316321 < div class = "h-20" />
317- { /* <p ref={ref}>hovered: {JSON.stringify(hovered())}</p>
318-
319- <p>idle: {JSON.stringify(idle())}</p>
320-
321- <p>networkStatus: {JSON.stringify(networkStatus())}</p>
322-
323- <button onClick={() => toggle()}>Current Toggled: {JSON.stringify(currentOption())}</button> */ }
324-
325- { /* <textarea ref={elementSizeRef} class="resize text-green-500">
326- </textarea>
327- {width()} */ }
328322 </ div >
329323 ) ;
330324}
Original file line number Diff line number Diff line change 11export * from './use-click-outside/use-click-outside' ;
22export * from './use-clipboard/use-clipboard' ;
3+ export * from './use-color-scheme/use-color-scheme' ;
34export * from './use-counter/use-counter' ;
45export * from './use-debounced-callback/use-debounced-callback' ;
56export * from './use-debounced-signal/use-debounced-signal' ;
Original file line number Diff line number Diff line change 1+ import { createMemo } from 'solid-js' ;
2+ import { useMediaQuery , UseMediaQueryOptions } from '../use-media-query/use-media-query' ;
3+
4+ export function useColorScheme ( initialValue ?: 'dark' | 'light' , options ?: UseMediaQueryOptions ) {
5+ const isDark = useMediaQuery (
6+ ( ) => '(prefers-color-scheme: dark)' ,
7+ initialValue === 'dark' ,
8+ options ,
9+ ) ;
10+
11+ return createMemo ( ( ) => ( isDark ( ) ? 'dark' : 'light' ) ) ;
12+ }
You can’t perform that action at this time.
0 commit comments