Refactor dark mode handling and update UI components

Extract dark mode logic into a reusable helper function to improve code clarity and maintainability. Updated the UI layout for better structure and styling, including replacing placeholder logos and enhancing card design.
This commit is contained in:
2025-02-08 00:22:20 +01:00
parent 9b45752d1a
commit 7b1c4c27fe
3 changed files with 54 additions and 55 deletions

View File

@@ -1,8 +1,7 @@
import {useEffect, useState} from 'react' import {useEffect, useState} from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css' // Ensure a declaration for CSS is added in a .d.ts file import './App.css' // Ensure a declaration for CSS is added in a .d.ts file
import {ColumnDirective, ColumnsDirective, GridComponent} from '@syncfusion/ej2-react-grids'; import {ColumnDirective, ColumnsDirective, GridComponent} from '@syncfusion/ej2-react-grids';
import {handleDarkMode} from "./theme_helpers/ThemeHelpers.ts";
function App() { function App() {
@@ -10,26 +9,7 @@ function App() {
useEffect(() => { useEffect(() => {
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); return handleDarkMode();
// Function to update dark mode for Syncfusion controls
const updateDarkMode = (e: MediaQueryListEvent) => {
if (e.matches) {
// If the system is in dark mode, add the class to your container or body
document.body.classList.add("e-dark-mode");
} else {
// Otherwise, remove it
document.body.classList.remove("e-dark-mode");
}
};
// Set initial mode based on system preference:
updateDarkMode(mediaQuery as unknown as MediaQueryListEvent);
// Listen for changes in the system preference:
mediaQuery.addEventListener("change", updateDarkMode);
return () => mediaQuery.removeEventListener("change", updateDarkMode);
}, []); }, []);
@@ -48,19 +28,14 @@ function App() {
return ( return (
<> <>
<div> <h1>Vite, React, Syncfusion, Daisyui Demo</h1>
<a href="https://vite.dev" target="_blank"> <div className="card shadow-md w-2/3 mt-5 bg-auto-50">
<img src={viteLogo} className="logo" alt="Vite logo"/> <div className="card-body">
</a> <button className="btn w-full m-2" onClick={() => setCount((count) => count + 1)}>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo"/>
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button className="btn w-56 m-2" onClick={() => setCount((count) => count + 1)}>
count is {count} count is {count}
</button> </button>
</div>
</div>
<div className="badge badge-secondary m-4">primary</div> <div className="badge badge-secondary m-4">primary</div>
@@ -81,7 +56,6 @@ function App() {
<p> <p>
Edit <code>src/App.tsx</code> and save to test HMR Edit <code>src/App.tsx</code> and save to test HMR
</p> </p>
</div>
<p className="font-bold underline"> <p className="font-bold underline">
Click on the Vite and React logos to learn more Click on the Vite and React logos to learn more
</p> </p>

View File

@@ -13,7 +13,7 @@
@plugin "daisyui" { @plugin "daisyui" {
themes: light --default, dracula --prefersdark; themes: light --default, dark --prefersdark;
} }

View File

@@ -0,0 +1,25 @@
export function handleDarkMode(): () => void {
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
// Function to update dark mode for Syncfusion controls
const updateDarkMode = (e: MediaQueryList | MediaQueryListEvent) => {
if (e.matches) {
// If the system is in dark mode, add the class
document.body.classList.add("e-dark-mode");
} else {
// Otherwise, remove it
document.body.classList.remove("e-dark-mode");
}
};
// Set initial mode based on system preference:
updateDarkMode(mediaQuery); // Pass the initial `mediaQuery` directly
// Listen for changes in system preference:
mediaQuery.addEventListener("change", updateDarkMode);
// Cleanup function to remove the event listener
return () => {
mediaQuery.removeEventListener("change", updateDarkMode);
};
}