From 69daa80cad181e2986ef6c5e0046e8a5130f5f04 Mon Sep 17 00:00:00 2001 From: Sergio Nicolas Lavaise Date: Mon, 20 Apr 2026 02:49:48 -0300 Subject: [PATCH] Add dark mode support and theme provider --- src/App.tsx | 7 ++- src/components/theme-provider.tsx | 73 +++++++++++++++++++++++++++++++ src/contexts/ThemeContext.tsx | 19 -------- src/sections/HistoriaClinica.tsx | 14 +++--- src/sections/Layout.tsx | 63 +++++++++++++++++--------- 5 files changed, 125 insertions(+), 51 deletions(-) create mode 100644 src/components/theme-provider.tsx delete mode 100644 src/contexts/ThemeContext.tsx diff --git a/src/App.tsx b/src/App.tsx index 88d709e..4387ac7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -12,7 +12,7 @@ import { NuevoIngreso } from '@/sections/NuevoIngreso'; import { EditIngreso } from '@/sections/EditIngreso'; import { Spinner } from '@/components/ui/spinner'; import { Button } from '@/components/ui/button'; -import { ThemeProvider } from '@/contexts/ThemeContext'; +import { ThemeProvider } from "@/components/theme-provider"; import { Toaster } from '@/components/ui/sonner'; function AppContent() { @@ -185,7 +185,7 @@ function AppContent() { getAreaName={(areaId) => store.areas.find(a => a.id === areaId)?.nombre || ''} /> ); - + case 'editaringreso': { const internacion = store.getInternacionById(store.currentInternacionId || ''); const paciente = internacion ? store.getPacienteById(internacion.pacienteId) : undefined; @@ -225,8 +225,7 @@ function AppContent() { function App() { return ( - // @ts-expect-error - next-themes types mismatch - + diff --git a/src/components/theme-provider.tsx b/src/components/theme-provider.tsx new file mode 100644 index 0000000..0b751f4 --- /dev/null +++ b/src/components/theme-provider.tsx @@ -0,0 +1,73 @@ +import { createContext, useContext, useEffect, useState } from "react" + +type Theme = "dark" | "light" | "system" + +type ThemeProviderProps = { + children: React.ReactNode + defaultTheme?: Theme + storageKey?: string +} + +type ThemeProviderState = { + theme: Theme + setTheme: (theme: Theme) => void +} + +const initialState: ThemeProviderState = { + theme: "system", + setTheme: () => null, +} + +const ThemeProviderContext = createContext(initialState) + +export function ThemeProvider({ + children, + defaultTheme = "light", + storageKey = "vite-ui-theme", + ...props +}: ThemeProviderProps) { + const [theme, setTheme] = useState( + () => (localStorage.getItem(storageKey) as Theme) || defaultTheme + ) + + useEffect(() => { + 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 + } + + root.classList.add(theme) + }, [theme]) + + const value = { + theme, + setTheme: (theme: Theme) => { + localStorage.setItem(storageKey, theme) + setTheme(theme) + }, + } + + return ( + + {children} + + ) +} + +export const useTheme = () => { + const context = useContext(ThemeProviderContext) + + if (context === undefined) + throw new Error("useTheme must be used within a ThemeProvider") + + return context +} diff --git a/src/contexts/ThemeContext.tsx b/src/contexts/ThemeContext.tsx deleted file mode 100644 index 0925d71..0000000 --- a/src/contexts/ThemeContext.tsx +++ /dev/null @@ -1,19 +0,0 @@ -import { ThemeProvider as NextThemesProvider, useTheme as useNextTheme } from 'next-themes'; -import type { ReactNode } from 'react'; - -export function ThemeProvider({ children }: { children: ReactNode }) { - return ( - - {children} - - ); -} - -export function useTheme() { - const { theme, setTheme } = useNextTheme(); - const isDarkMode = theme === 'dark'; - const toggleDarkMode = () => setTheme(theme === 'dark' ? 'light' : 'dark'); - return { isDarkMode, toggleDarkMode, theme, setTheme }; -} - -export { NextThemesProvider }; \ No newline at end of file diff --git a/src/sections/HistoriaClinica.tsx b/src/sections/HistoriaClinica.tsx index ee05321..891e46c 100644 --- a/src/sections/HistoriaClinica.tsx +++ b/src/sections/HistoriaClinica.tsx @@ -266,8 +266,8 @@ export function HistoriaClinica({
-

- +

+ {paciente.apellido}, {paciente.nombre}

@@ -284,7 +284,7 @@ export function HistoriaClinica({ {paciente.fechaNacimiento ? calcularEdad(paciente.fechaNacimiento) + ' años' : ''} - + DNI: {paciente.dni} @@ -1364,7 +1364,7 @@ function SeccionEvoluciones({ evos, internacionId, add, update, del }: {
setFecha(e.target.value)} />
setHora(e.target.value)} />

-
+

Signos Vitales

setPresionSistolica(e.target.value)} placeholder="120" />
@@ -1387,9 +1387,9 @@ function SeccionEvoluciones({ evos, internacionId, add, update, del }: {
setSoma(e.target.value)} placeholder="Estado SOMA" />
-