generated from Pettor/template-web-component-react
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
167 additions
and
16 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
24 changes: 24 additions & 0 deletions
24
src/components/library/theme-controller/ThemeSwitch.stories.tsx
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,24 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { ThemeSwitch as Component } from "./ThemeSwitch"; | ||
import type { ThemeSwitchProps as ComponentProps } from "./ThemeSwitch"; | ||
|
||
const meta = { | ||
component: Component, | ||
parameters: { | ||
layout: "centered", | ||
}, | ||
tags: ["autodocs"], | ||
title: "Library/ThemeSwitch", | ||
} satisfies Meta<typeof Component>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
const commonProps = { | ||
mode: "light", | ||
onSwitch: () => console.log("Switched"), | ||
} satisfies ComponentProps; | ||
|
||
export const Standard = { | ||
args: commonProps, | ||
} satisfies Story; |
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,50 @@ | ||
import type { ReactElement } from "react"; | ||
import type { ThemeMode } from "./ThemeSwitcherClasses"; | ||
|
||
export interface ThemeSwitchProps { | ||
mode: ThemeMode; | ||
onSwitch: () => void; | ||
} | ||
|
||
export function ThemeSwitch({ mode, onSwitch }: ThemeSwitchProps): ReactElement { | ||
return ( | ||
<label className="cursor-pointer grid place-items-center"> | ||
<input | ||
type="checkbox" | ||
checked={mode === "dark"} | ||
value={mode} | ||
onChange={onSwitch} | ||
className="toggle theme-controller bg-base-content row-start-1 col-start-1 col-span-2" | ||
/> | ||
<svg | ||
className="col-start-1 row-start-1 stroke-base-100 fill-base-100" | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="14" | ||
height="14" | ||
viewBox="0 0 24 24" | ||
fill="none" | ||
stroke="currentColor" | ||
strokeWidth="2" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
> | ||
<circle cx="12" cy="12" r="5" /> | ||
<path d="M12 1v2M12 21v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M1 12h2M21 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4" /> | ||
</svg> | ||
<svg | ||
className="col-start-2 row-start-1 stroke-base-100 fill-base-100" | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="14" | ||
height="14" | ||
viewBox="0 0 24 24" | ||
fill="none" | ||
stroke="currentColor" | ||
strokeWidth="2" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
> | ||
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path> | ||
</svg> | ||
</label> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
src/components/library/theme-controller/ThemeSwitcherClasses.ts
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 @@ | ||
export type ThemeMode = "light" | "dark"; |
46 changes: 46 additions & 0 deletions
46
src/components/library/theme-controller/UseThemeSwitcher.ts
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,46 @@ | ||
import { useEffect, useLayoutEffect, useState } from "react"; | ||
import { useLocalStorage } from "usehooks-ts"; | ||
import type { ThemeSwitchProps } from "./ThemeSwitch"; | ||
import type { ThemeMode } from "./ThemeSwitcherClasses"; | ||
|
||
export function useThemeSwitcher(): ThemeSwitchProps { | ||
const [mediaQuery] = useState(() => window.matchMedia("(prefers-color-scheme: dark)")); | ||
const [themeLocalStorage, setThemeLocalStorage] = useLocalStorage<ThemeMode>("theme", () => { | ||
if (mediaQuery.matches) { | ||
return "dark"; | ||
} else { | ||
return "light"; | ||
} | ||
}); | ||
|
||
useLayoutEffect(() => { | ||
if (themeLocalStorage) { | ||
return; | ||
} | ||
|
||
if (mediaQuery.matches) { | ||
setThemeLocalStorage("dark"); | ||
} else { | ||
setThemeLocalStorage("light"); | ||
} | ||
}, [mediaQuery, setThemeLocalStorage, themeLocalStorage]); | ||
|
||
useEffect(() => { | ||
if (themeLocalStorage === "dark") { | ||
document.documentElement.classList.add("dark"); | ||
document.querySelector("html")?.setAttribute("data-theme", "dark"); | ||
} else { | ||
document.documentElement.classList.remove("dark"); | ||
document.querySelector("html")?.setAttribute("data-theme", "light"); | ||
} | ||
}, [themeLocalStorage]); | ||
|
||
function setTheme(): void { | ||
setThemeLocalStorage((theme) => (theme === "light" ? "dark" : "light")); | ||
} | ||
|
||
return { | ||
mode: themeLocalStorage, | ||
onSwitch: setTheme, | ||
}; | ||
} |
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