-
Notifications
You must be signed in to change notification settings - Fork 27
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
7 changed files
with
94 additions
and
131 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,67 @@ | ||
import { createContext, useContext, useEffect, useState } from "react" | ||
type Theme = "dark" | "light" | "system" | ||
import { createContext, useContext, useEffect, useState } from "react"; | ||
|
||
type Theme = "dark" | "light" | "system"; | ||
|
||
type ThemeProviderProps = { | ||
children: React.ReactNode | ||
defaultTheme?: Theme | ||
storageKey?: string | ||
} | ||
children: React.ReactNode; | ||
defaultTheme?: Theme; | ||
storageKey?: string; | ||
}; | ||
|
||
type ThemeProviderState = { | ||
theme: Theme | ||
setTheme: (theme: Theme) => void | ||
} | ||
theme: Theme; | ||
setTheme: (theme: Theme) => void; | ||
}; | ||
|
||
const initialState: ThemeProviderState = { | ||
theme: "system", | ||
setTheme: () => null, | ||
} | ||
const ThemeProviderContext = createContext<ThemeProviderState>(initialState) | ||
}; | ||
|
||
const ThemeProviderContext = createContext<ThemeProviderState>(initialState); | ||
|
||
export function ThemeProvider({ | ||
children, | ||
defaultTheme = "system", | ||
storageKey = "vite-ui-theme", | ||
...props | ||
}: ThemeProviderProps) { | ||
const [theme, setTheme] = useState<Theme>( | ||
() => (localStorage.getItem(storageKey) as Theme) || defaultTheme | ||
) | ||
|
||
const [theme, setTheme] = useState<Theme>(() => (localStorage.getItem(storageKey) as Theme) || defaultTheme); | ||
|
||
useEffect(() => { | ||
const root = window.document.documentElement | ||
root.classList.remove("light", "dark") | ||
const root = window.document.documentElement; | ||
|
||
root.classList.remove("light", "dark"); | ||
|
||
if (theme === "system") { | ||
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)") | ||
.matches | ||
? "dark" | ||
: "light" | ||
|
||
root.classList.add(systemTheme) | ||
return | ||
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; | ||
|
||
root.classList.add(systemTheme); | ||
return; | ||
} | ||
root.classList.add(theme) | ||
}, [theme]) | ||
|
||
root.classList.add(theme); | ||
}, [theme]); | ||
|
||
const value = { | ||
theme, | ||
setTheme: (theme: Theme) => { | ||
localStorage.setItem(storageKey, theme) | ||
setTheme(theme) | ||
localStorage.setItem(storageKey, theme); | ||
setTheme(theme); | ||
}, | ||
} | ||
}; | ||
|
||
return ( | ||
<ThemeProviderContext.Provider {...props} value={value}> | ||
{children} | ||
</ThemeProviderContext.Provider> | ||
) | ||
); | ||
} | ||
|
||
export const useTheme = () => { | ||
const context = useContext(ThemeProviderContext) | ||
|
||
if (context === undefined) | ||
throw new Error("useTheme must be used within a ThemeProvider") | ||
|
||
return context | ||
} | ||
const context = useContext(ThemeProviderContext); | ||
|
||
if (context === undefined) throw new Error("useTheme must be used within a ThemeProvider"); | ||
|
||
return context; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,22 @@ | ||
import * as React from "react" | ||
import * as React from "react"; | ||
|
||
import { cn } from "@/lib/utils" | ||
import { cn } from "@/lib/utils"; | ||
|
||
export interface InputProps | ||
extends React.InputHTMLAttributes<HTMLInputElement> {} | ||
export interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {} | ||
|
||
const Input = React.forwardRef<HTMLInputElement, InputProps>( | ||
({ className, type, ...props }, ref) => { | ||
return ( | ||
<input | ||
type={type} | ||
className={cn( | ||
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50", | ||
className | ||
)} | ||
ref={ref} | ||
{...props} | ||
/> | ||
) | ||
} | ||
) | ||
Input.displayName = "Input" | ||
const Input = React.forwardRef<HTMLInputElement, InputProps>(({ className, type, ...props }, ref) => { | ||
return ( | ||
<input | ||
type={type} | ||
className={cn( | ||
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50", | ||
className | ||
)} | ||
ref={ref} | ||
{...props} | ||
/> | ||
); | ||
}); | ||
Input.displayName = "Input"; | ||
|
||
export { Input } | ||
export { Input }; |
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 |
---|---|---|
@@ -1,24 +1,17 @@ | ||
import * as React from "react" | ||
import * as LabelPrimitive from "@radix-ui/react-label" | ||
import { cva, type VariantProps } from "class-variance-authority" | ||
import * as React from "react"; | ||
import * as LabelPrimitive from "@radix-ui/react-label"; | ||
import { cva, type VariantProps } from "class-variance-authority"; | ||
|
||
import { cn } from "@/lib/utils" | ||
import { cn } from "@/lib/utils"; | ||
|
||
const labelVariants = cva( | ||
"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70" | ||
) | ||
const labelVariants = cva("text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"); | ||
|
||
const Label = React.forwardRef< | ||
React.ElementRef<typeof LabelPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> & | ||
VariantProps<typeof labelVariants> | ||
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> & VariantProps<typeof labelVariants> | ||
>(({ className, ...props }, ref) => ( | ||
<LabelPrimitive.Root | ||
ref={ref} | ||
className={cn(labelVariants(), className)} | ||
{...props} | ||
/> | ||
)) | ||
Label.displayName = LabelPrimitive.Root.displayName | ||
<LabelPrimitive.Root ref={ref} className={cn(labelVariants(), className)} {...props} /> | ||
)); | ||
Label.displayName = LabelPrimitive.Root.displayName; | ||
|
||
export { Label } | ||
export { Label }; |
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 |
---|---|---|
@@ -1,15 +1,7 @@ | ||
export const Link = ({ to, children, ...props }: { | ||
to: string; | ||
children: React.ReactNode; | ||
}) => { | ||
export const Link = ({ to, children, ...props }: { to: string; children: React.ReactNode }) => { | ||
return ( | ||
<a | ||
href={to} | ||
target="_blank" | ||
className="text-blue-500 hover:text-blue-400 underline" | ||
{...props} | ||
> | ||
<a href={to} target="_blank" className="text-blue-500 hover:text-blue-400 underline" {...props}> | ||
{children} | ||
</a> | ||
); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { type ClassValue, clsx } from "clsx" | ||
import { twMerge } from "tailwind-merge" | ||
import { type ClassValue, clsx } from "clsx"; | ||
import { twMerge } from "tailwind-merge"; | ||
|
||
export function cn(...inputs: ClassValue[]) { | ||
return twMerge(clsx(inputs)) | ||
return twMerge(clsx(inputs)); | ||
} |
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