@mbao01/common 0.6.5 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
1
  import { ThemeSwitchProps } from './types';
2
- export declare const ThemeSwitch: ({ name, render, className, theme: defaultTheme, ...props }: ThemeSwitchProps) => import("react/jsx-runtime").JSX.Element;
2
+ export declare const ThemeSwitch: ({ swap, render, className, theme: defaultTheme, }: ThemeSwitchProps) => import("react/jsx-runtime").JSX.Element;
@@ -1 +1,7 @@
1
- export declare const getThemeSwitchClasses: (props?: import('class-variance-authority/types').ClassProp | undefined) => string;
1
+ export declare const getThemeSwitchClasses: (props?: ({
2
+ swap?: boolean | null | undefined;
3
+ } & import('class-variance-authority/types').ClassProp) | undefined) => string;
4
+ export declare const getThemeSwitchIconClasses: (props?: ({
5
+ isActive?: boolean | null | undefined;
6
+ swap?: boolean | null | undefined;
7
+ } & import('class-variance-authority/types').ClassProp) | undefined) => string;
@@ -1,7 +1,18 @@
1
1
  import { Theme } from '../../utilities';
2
- export type ThemeSwitchProps = React.ComponentPropsWithoutRef<"label"> & {
3
- name?: string;
2
+ export type SwitchProps = {
3
+ id: string;
4
+ icon: React.ReactNode;
5
+ swap?: boolean;
6
+ label: string;
7
+ value: Theme | null;
8
+ theme: Theme | null;
9
+ className?: string;
10
+ changeTheme: (theme: Theme | null) => void;
11
+ };
12
+ export type ThemeSwitchProps = {
13
+ swap?: boolean;
4
14
  theme?: Theme;
15
+ className?: string;
5
16
  render?: (params: {
6
17
  theme: Theme | null;
7
18
  setTheme: React.Dispatch<React.SetStateAction<Theme | null>>;
@@ -1,4 +1,4 @@
1
1
  export type Theme = "dark" | "light";
2
2
  export declare const THEME_COOKIE_NAME = "data-theme";
3
3
  export declare const getTheme: () => Theme | null;
4
- export declare const saveTheme: (theme: Theme) => void;
4
+ export declare const saveTheme: (theme: Theme | null) => null | undefined;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mbao01/common",
3
3
  "private": false,
4
- "version": "0.6.5",
4
+ "version": "0.7.0",
5
5
  "type": "module",
6
6
  "author": "Ayomide Bakare",
7
7
  "license": "MIT",
@@ -168,5 +168,5 @@
168
168
  "recharts": "2",
169
169
  "typescript": "5"
170
170
  },
171
- "gitHead": "f3359c587061980729e83322e4f6330e04e0815e"
171
+ "gitHead": "412b7c47a5fe2f7fc7e8e17255b6df1dbb3428b5"
172
172
  }
@@ -1,69 +1,88 @@
1
1
  "use client";
2
2
 
3
- import type { ChangeEvent } from "react";
4
3
  import { useLayoutEffect, useState } from "react";
5
- import { cn, getTheme, saveTheme } from "../../utilities";
6
- import { getThemeSwitchClasses } from "./constants";
7
- import { type ThemeSwitchProps } from "./types";
4
+ import { MonitorIcon, MoonIcon, SunIcon } from "lucide-react";
5
+ import type { SwitchProps, ThemeSwitchProps } from "./types";
6
+ import { cn, getTheme, saveTheme, Theme } from "../../utilities";
7
+ import { getThemeSwitchClasses, getThemeSwitchIconClasses } from "./constants";
8
8
 
9
- const MoonIcon = ({ title }: { title: string }) => (
10
- <svg
11
- className="swap-on h-4 w-4 fill-current"
12
- xmlns="http://www.w3.org/2000/svg"
13
- viewBox="0 0 24 24"
14
- >
15
- {title && <title>{title}</title>}
16
- <path d="M21.64,13a1,1,0,0,0-1.05-.14,8.05,8.05,0,0,1-3.37.73A8.15,8.15,0,0,1,9.08,5.49a8.59,8.59,0,0,1,.25-2A1,1,0,0,0,8,2.36,10.14,10.14,0,1,0,22,14.05,1,1,0,0,0,21.64,13Zm-9.5,6.69A8.14,8.14,0,0,1,7.08,5.22v.27A10.15,10.15,0,0,0,17.22,15.63a9.79,9.79,0,0,0,2.1-.22A8.11,8.11,0,0,1,12.14,19.73Z" />
17
- </svg>
18
- );
9
+ const Switcher = ({ id, icon, swap, value, label, theme, changeTheme, className }: SwitchProps) => {
10
+ const isActive = theme === value;
19
11
 
20
- const SunIcon = ({ title }: { title: string }) => (
21
- <svg
22
- className="swap-off h-4 w-4 fill-current"
23
- xmlns="http://www.w3.org/2000/svg"
24
- viewBox="0 0 24 24"
25
- xlinkTitle="sun-icon"
26
- >
27
- {title && <title>{title}</title>}
28
- <path d="M5.64,17l-.71.71a1,1,0,0,0,0,1.41,1,1,0,0,0,1.41,0l.71-.71A1,1,0,0,0,5.64,17ZM5,12a1,1,0,0,0-1-1H3a1,1,0,0,0,0,2H4A1,1,0,0,0,5,12Zm7-7a1,1,0,0,0,1-1V3a1,1,0,0,0-2,0V4A1,1,0,0,0,12,5ZM5.64,7.05a1,1,0,0,0,.7.29,1,1,0,0,0,.71-.29,1,1,0,0,0,0-1.41l-.71-.71A1,1,0,0,0,4.93,6.34Zm12,.29a1,1,0,0,0,.7-.29l.71-.71a1,1,0,1,0-1.41-1.41L17,5.64a1,1,0,0,0,0,1.41A1,1,0,0,0,17.66,7.34ZM21,11H20a1,1,0,0,0,0,2h1a1,1,0,0,0,0-2Zm-9,8a1,1,0,0,0-1,1v1a1,1,0,0,0,2,0V20A1,1,0,0,0,12,19ZM18.36,17A1,1,0,0,0,17,18.36l.71.71a1,1,0,0,0,1.41,0,1,1,0,0,0,0-1.41ZM12,6.5A5.5,5.5,0,1,0,17.5,12,5.51,5.51,0,0,0,12,6.5Zm0,9A3.5,3.5,0,1,1,15.5,12,3.5,3.5,0,0,1,12,15.5Z" />
29
- </svg>
30
- );
12
+ return (
13
+ <span
14
+ className={cn("h-full", {
15
+ hidden: swap && isActive,
16
+ })}
17
+ >
18
+ <input
19
+ hidden
20
+ id={id}
21
+ role="checkbox"
22
+ type="checkbox"
23
+ className="theme-controller"
24
+ checked={isActive}
25
+ onChange={() => changeTheme(value)}
26
+ />
27
+ <label htmlFor={id} className={cn(getThemeSwitchIconClasses({ isActive, swap }), className)}>
28
+ <span className="sr-only">{label}</span>
29
+ {icon}
30
+ </label>
31
+ </span>
32
+ );
33
+ };
31
34
 
32
35
  export const ThemeSwitch = ({
33
- name,
36
+ swap = false,
34
37
  render,
35
38
  className,
36
39
  theme: defaultTheme,
37
- ...props
38
40
  }: ThemeSwitchProps) => {
39
- const [theme, setTheme] = useState(defaultTheme ?? getTheme);
41
+ const [theme, setTheme] = useState(() => defaultTheme ?? getTheme());
40
42
 
41
43
  useLayoutEffect(() => {
42
- if (theme) saveTheme(theme);
44
+ saveTheme(theme);
43
45
  }, [theme]);
44
46
 
45
47
  if (render) {
46
48
  return render({ theme, setTheme });
47
49
  }
48
50
 
49
- const changeTheme = (e: ChangeEvent<HTMLInputElement>) => {
50
- const t = e.target.checked ? "light" : "dark";
51
- setTheme(t);
51
+ const changeTheme = (theme: Theme | null) => {
52
+ setTheme(theme);
52
53
  };
53
54
 
54
55
  return (
55
- <label className={cn(getThemeSwitchClasses(), className)} {...props}>
56
- {/* this hidden checkbox controls the state */}
57
- <input
58
- hidden
59
- name={name}
60
- type="checkbox"
61
- className="theme-controller"
62
- checked={theme === "light"}
63
- onChange={changeTheme}
56
+ <fieldset className={cn(getThemeSwitchClasses({ swap }), className)}>
57
+ {!swap && (
58
+ <Switcher
59
+ id="system"
60
+ swap={swap}
61
+ icon={<MonitorIcon className="h-3.5 w-3.5 shrink-0" />}
62
+ label="Switch to system theme"
63
+ value={null}
64
+ theme={theme}
65
+ changeTheme={changeTheme}
66
+ />
67
+ )}
68
+ <Switcher
69
+ id="dark"
70
+ swap={swap}
71
+ icon={<MoonIcon className="h-3.5 w-3.5 shrink-0" />}
72
+ label="Switch to dark theme"
73
+ value="dark"
74
+ theme={theme}
75
+ changeTheme={changeTheme}
76
+ />
77
+ <Switcher
78
+ id="light"
79
+ swap={swap}
80
+ icon={<SunIcon className="h-3.5 w-3.5 shrink-0" />}
81
+ label="Switch to light theme"
82
+ value="light"
83
+ theme={theme}
84
+ changeTheme={changeTheme}
64
85
  />
65
- <MoonIcon title="Switch to dark theme" />
66
- <SunIcon title="Switch to light theme" />
67
- </label>
86
+ </fieldset>
68
87
  );
69
88
  };
@@ -1,3 +1,36 @@
1
1
  import { cva } from "../../libs";
2
2
 
3
- export const getThemeSwitchClasses = cva("swap swap-rotate");
3
+ export const getThemeSwitchClasses = cva("", {
4
+ variants: {
5
+ swap: {
6
+ false:
7
+ "flex flex-row items-center justify-around h-6 w-18 border border-base-content/10 rounded-full",
8
+ },
9
+ },
10
+ });
11
+
12
+ export const getThemeSwitchIconClasses = cva(
13
+ "cursor-pointer text-base-content/40 hover:text-base-content transition-colors",
14
+ {
15
+ variants: {
16
+ isActive: {
17
+ true: "rounded-[9999px] outline outline-1 outline-base-content/10",
18
+ },
19
+ swap: {
20
+ false: "flex items-center justify-center w-6 h-full",
21
+ },
22
+ },
23
+ compoundVariants: [
24
+ {
25
+ swap: false,
26
+ isActive: true,
27
+ className: "text-base-content",
28
+ },
29
+ {
30
+ swap: true,
31
+ isActive: false,
32
+ className: "text-base-content",
33
+ },
34
+ ],
35
+ }
36
+ );
@@ -1,8 +1,20 @@
1
1
  import { type Theme } from "../../utilities";
2
2
 
3
- export type ThemeSwitchProps = React.ComponentPropsWithoutRef<"label"> & {
4
- name?: string;
3
+ export type SwitchProps = {
4
+ id: string;
5
+ icon: React.ReactNode;
6
+ swap?: boolean;
7
+ label: string;
8
+ value: Theme | null;
9
+ theme: Theme | null;
10
+ className?: string;
11
+ changeTheme: (theme: Theme | null) => void;
12
+ };
13
+
14
+ export type ThemeSwitchProps = {
15
+ swap?: boolean;
5
16
  theme?: Theme;
17
+ className?: string;
6
18
  render?: (params: {
7
19
  theme: Theme | null;
8
20
  setTheme: React.Dispatch<React.SetStateAction<Theme | null>>;
@@ -4,16 +4,26 @@ export type Theme = "dark" | "light";
4
4
 
5
5
  export const THEME_COOKIE_NAME = "data-theme";
6
6
 
7
+ const getSystemTheme = () => {
8
+ if (typeof window === "undefined") return null;
9
+
10
+ const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
11
+ return prefersDark ? "dark" : "light";
12
+ };
13
+
7
14
  export const getTheme = () => {
8
15
  if (typeof window === "undefined") return null;
9
16
 
10
17
  const cookies = new Cookies();
11
- const theme = cookies.get(THEME_COOKIE_NAME) as Theme;
18
+ const theme = (cookies.get(THEME_COOKIE_NAME) ?? getSystemTheme()) as Theme | null;
12
19
  return theme;
13
20
  };
14
21
 
15
- export const saveTheme = (theme: Theme) => {
22
+ export const saveTheme = (theme: Theme | null) => {
23
+ const newTheme = theme ?? getSystemTheme();
24
+ if (!newTheme) return null;
25
+
16
26
  const cookies = new Cookies();
17
- cookies.set(THEME_COOKIE_NAME, theme, { path: "/", secure: true });
18
- document.body.setAttribute(THEME_COOKIE_NAME, theme);
27
+ cookies.set(THEME_COOKIE_NAME, newTheme, { path: "/", secure: true });
28
+ document.body.setAttribute(THEME_COOKIE_NAME, newTheme);
19
29
  };