File tree Expand file tree Collapse file tree 8 files changed +58
-30
lines changed Expand file tree Collapse file tree 8 files changed +58
-30
lines changed Original file line number Diff line number Diff line change @@ -432,14 +432,7 @@ export function generateGetColorOptionsTsCode(colorOptions: ColorOption[]) {
432432 } ) ;
433433
434434 return [
435- `export function getColorOptions(colorScheme: ColorScheme) {` ,
436- ` const isDark: boolean = (() => {` ,
437- ` switch (colorScheme) {` ,
438- ` case "dark": return true;` ,
439- ` case "light": return false;` ,
440- ` }` ,
441- ` })();` ,
442- `` ,
435+ `export function getColorOptions(isDark: boolean) {` ,
443436 ` return {` ,
444437 multiReplace ( {
445438 "input" : JSON . stringify ( obj , null , 2 ) ,
Original file line number Diff line number Diff line change @@ -28,7 +28,6 @@ fs.writeFileSync(
2828 Buffer . from (
2929 [
3030 warningMessage ,
31- `import type { ColorScheme } from "../../useColorScheme";` ,
3231 `` ,
3332 generateGetColorOptionsTsCode ( colorOptions ) ,
3433 `` ,
Original file line number Diff line number Diff line change 1- export { $colorScheme , useColorScheme } from "./useColorScheme" ;
1+ export { $colorScheme , useIsDark } from "./useColorScheme" ;
22export type { ColorScheme } from "./useColorScheme" ;
33export * from "./start" ;
Original file line number Diff line number Diff line change @@ -38,6 +38,29 @@ const useColorSchemeServerSide: UseColorScheme = () => {
3838
3939export const useColorScheme = isBrowser ? useColorSchemeClientSide : useColorSchemeServerSide ;
4040
41+ //NOTE: Just because it's more convenient to have a boolean than "light" | "dark"
42+ export function useIsDark ( ) {
43+ const { colorScheme, setColorScheme } = useColorScheme ( ) ;
44+
45+ const setIsDark = useConstCallback ( ( isDark : boolean | "system" ) =>
46+ setColorScheme ( typeof isDark !== "boolean" ? isDark : isDark ? "dark" : "light" )
47+ ) ;
48+
49+ const isDark = ( ( ) => {
50+ switch ( colorScheme ) {
51+ case "dark" :
52+ return true ;
53+ case "light" :
54+ return false ;
55+ }
56+ } ) ( ) ;
57+
58+ return {
59+ isDark,
60+ setIsDark
61+ } ;
62+ }
63+
4164function getCurrentColorSchemeFromHtmlAttribute ( ) : ColorScheme {
4265 if ( ! isBrowser ) {
4366 return "light" ;
Original file line number Diff line number Diff line change 22import artworkLightSvgUrl from "@codegouvfr/react-dsfr/dsfr/artwork/light.svg" ;
33import artworkDarkSvgUrl from "@codegouvfr/react-dsfr/dsfr/artwork/dark.svg" ;
44import artworkSystemSvgUrl from "@codegouvfr/react-dsfr/dsfr/artwork/system.svg" ;
5- import { useColorScheme } from "@codegouvfr/react-dsfr" ;
5+ import { useIsDark } from "@codegouvfr/react-dsfr" ;
66
77export function App ( ) {
88
9- const { colorScheme , setColorScheme } = useColorScheme ( ) ;
9+ const { isDark , setIsDark } = useIsDark ( ) ;
1010
1111 return (
1212 < >
13- < h1 > Color Scheme: { colorScheme } </ h1 >
14- < button onClick = { ( ) => setColorScheme ( "dark" ) } > Set color scheme to dark</ button >
15- < button onClick = { ( ) => setColorScheme ( "light" ) } > Set color scheme to light</ button >
16- < button onClick = { ( ) => setColorScheme ( "system" ) } > Set color scheme to system</ button >
13+ < h1 > Color Scheme: { isDark ? "dark" : "light" } </ h1 >
14+ < button onClick = { ( ) => setIsDark ( true ) } > Set color scheme to dark</ button >
15+ < button onClick = { ( ) => setIsDark ( false ) } > Set color scheme to light</ button >
16+ < button onClick = { ( ) => setIsDark ( "system" ) } > Set color scheme to system</ button >
1717 < header role = "banner" className = "fr-header" >
1818 < div className = "fr-header__body" >
1919 < div className = "fr-container" >
Original file line number Diff line number Diff line change 22import ArtworkLightSvg from "@codegouvfr/react-dsfr/dsfr/artwork/light.svg" ;
33import ArtworkDarkSvg from "@codegouvfr/react-dsfr/dsfr/artwork/dark.svg" ;
44import ArtworkSystemSvg from "@codegouvfr/react-dsfr/dsfr/artwork/system.svg" ;
5- import { useColorScheme } from "@codegouvfr/react-dsfr" ;
5+ import { useIsDark } from "@codegouvfr/react-dsfr" ;
66
7- export default function Index ( ) {
7+ export default function App ( ) {
88
9- const { colorScheme , setColorScheme } = useColorScheme ( ) ;
9+ const { isDark , setIsDark } = useIsDark ( ) ;
1010
1111 return (
1212 < >
13- < h1 > Color Scheme: { colorScheme } </ h1 >
14- < button onClick = { ( ) => setColorScheme ( "dark" ) } > Set color scheme to dark</ button >
15- < button onClick = { ( ) => setColorScheme ( "light" ) } > Set color scheme to light</ button >
16- < button onClick = { ( ) => setColorScheme ( "system" ) } > Set color scheme to system</ button >
13+ < h1 > Color Scheme: { isDark ? "dark" : "light" } </ h1 >
14+ < button onClick = { ( ) => setIsDark ( true ) } > Set color scheme to dark</ button >
15+ < button onClick = { ( ) => setIsDark ( false ) } > Set color scheme to light</ button >
16+ < button onClick = { ( ) => setIsDark ( "system" ) } > Set color scheme to system</ button >
1717 < header role = "banner" className = "fr-header" >
1818 < div className = "fr-header__body" >
1919 < div className = "fr-container" >
Original file line number Diff line number Diff line change 3939 regenerator-runtime "^0.13.4"
4040
4141" @codegouvfr/react-dsfr@file:../../../../dist " :
42- version "0.0.0"
42+ version "0.0.6"
43+ dependencies :
44+ " @gouvfr/dsfr" " 1.7.2"
45+ tsafe "^1.0.1"
4346
4447" @eslint/eslintrc@^0.4.2 " :
4548 version "0.4.3"
5659 minimatch "^3.0.4"
5760 strip-json-comments "^3.1.1"
5861
62+ " @gouvfr/dsfr@1.7.2 " :
63+ version "1.7.2"
64+ resolved "https://registry.yarnpkg.com/@gouvfr/dsfr/-/dsfr-1.7.2.tgz#c522fce21a14989a10e30583160118c39c9c05d1"
65+ integrity sha512-hPNtz+gHcc8m7ZPANxSOFMz4Ap+M9FHOudqoMR/+Kjl5FCOqwA6u/aoYnMJ8KqedS1k5XThFMp7jiktr53qXYw==
66+
5967" @humanwhocodes/config-array@^0.5.0 " :
6068 version "0.5.0"
6169 resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.5.0.tgz#1407967d4c6eecd7388f83acf1eaf4d0c6e58ef9"
@@ -1746,6 +1754,11 @@ to-regex-range@^5.0.1:
17461754 dependencies :
17471755 is-number "^7.0.0"
17481756
1757+ tsafe@^1.0.1 :
1758+ version "1.1.1"
1759+ resolved "https://registry.yarnpkg.com/tsafe/-/tsafe-1.1.1.tgz#8d6998c726f8c63c518e1d1e283bbcd282a2b9a9"
1760+ integrity sha512-Ogblm3uh0dVupcCcC4IT641rnSQ7CW9IO0q8yIncG8OBe4DDXEqGtUE8LWf7+0MK1qZGeWPWEqSxlLzY2xzREA==
1761+
17491762tsconfig-paths@^3.14.1 :
17501763 version "3.14.1"
17511764 resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz#ba0734599e8ea36c862798e920bcf163277b137a"
Original file line number Diff line number Diff line change 22import artworkLightSvgUrl from "@codegouvfr/react-dsfr/dsfr/artwork/light.svg" ;
33import artworkDarkSvgUrl from "@codegouvfr/react-dsfr/dsfr/artwork/dark.svg" ;
44import artworkSystemSvgUrl from "@codegouvfr/react-dsfr/dsfr/artwork/system.svg" ;
5- import { useColorScheme } from "@codegouvfr/react-dsfr" ;
5+ import { useIsDark } from "@codegouvfr/react-dsfr" ;
66
77export function App ( ) {
88
9- const { colorScheme , setColorScheme } = useColorScheme ( ) ;
9+ const { isDark , setIsDark } = useIsDark ( ) ;
1010
1111 return (
1212 < >
13- < h1 > Color Scheme: { colorScheme } </ h1 >
14- < button onClick = { ( ) => setColorScheme ( "dark" ) } > Set color scheme to dark</ button >
15- < button onClick = { ( ) => setColorScheme ( "light" ) } > Set color scheme to light</ button >
16- < button onClick = { ( ) => setColorScheme ( "system" ) } > Set color scheme to system</ button >
13+ < h1 > Color Scheme: { isDark ? "dark" : "light" } </ h1 >
14+ < button onClick = { ( ) => setIsDark ( true ) } > Set color scheme to dark</ button >
15+ < button onClick = { ( ) => setIsDark ( false ) } > Set color scheme to light</ button >
16+ < button onClick = { ( ) => setIsDark ( "system" ) } > Set color scheme to system</ button >
1717 < header role = "banner" className = "fr-header" >
1818 < div className = "fr-header__body" >
1919 < div className = "fr-container" >
You can’t perform that action at this time.
0 commit comments