neogestify-ui-components 1.1.3 → 1.2.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neogestify-ui-components",
3
- "version": "1.1.3",
3
+ "version": "1.2.1",
4
4
  "description": "Biblioteca de componentes UI reutilizables con React, Tailwind y SweetAlert",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -0,0 +1,37 @@
1
+ import { useEffect, useState } from 'react';
2
+ import type { ReactNode } from 'react';
3
+ import { ThemeContext, type Theme } from './theme.types';
4
+
5
+ export function ThemeProvider({ children }: { children: ReactNode }) {
6
+ const [theme, setThemeState] = useState<Theme>(() => {
7
+ const savedTheme = localStorage.getItem('theme');
8
+ return (savedTheme as Theme) || 'light';
9
+ });
10
+
11
+ useEffect(() => {
12
+ const root = document.documentElement
13
+ if (theme === 'dark') {
14
+ root.classList.add('dark');
15
+ } else {
16
+ root.classList.remove('dark');
17
+ }
18
+ localStorage.setItem('theme', theme);
19
+ }, [theme]);
20
+
21
+ const toggleTheme = () => {
22
+ setThemeState(prev => {
23
+ const newTheme = prev === 'light' ? 'dark' : 'light';
24
+ return newTheme;
25
+ });
26
+ };
27
+
28
+ const setTheme = (newTheme: Theme) => {
29
+ setThemeState(newTheme);
30
+ };
31
+
32
+ return (
33
+ <ThemeContext.Provider value={{ theme, toggleTheme, setTheme }}>
34
+ {children}
35
+ </ThemeContext.Provider>
36
+ );
37
+ }
@@ -0,0 +1,11 @@
1
+ import { createContext } from 'react';
2
+
3
+ export type Theme = 'light' | 'dark';
4
+
5
+ export interface ThemeContextType {
6
+ theme: Theme;
7
+ toggleTheme: () => void;
8
+ setTheme: (theme: Theme) => void;
9
+ }
10
+
11
+ export const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
@@ -0,0 +1,10 @@
1
+ import { useContext } from 'react';
2
+ import { ThemeContext } from './theme.types';
3
+
4
+ export function useTheme() {
5
+ const context = useContext(ThemeContext);
6
+ if (context === undefined) {
7
+ throw new Error('useTheme must be used within a ThemeProvider');
8
+ }
9
+ return context;
10
+ }