The Theme class facilitates dynamic theme switching between light and dark modes, or adapting to the system's
preferred color scheme. It manages theme
attributes, local storage, and UI updates to ensure a seamless and
consistent experience across user sessions.
localStorage
.<meta name="theme-color">
to match the applied theme.data-theme-value
– Allows buttons to explicitly set light or dark themes through data-theme-value
attributes..theme-toggler
– Toggles the theme between light and dark modes..theme-update
– Elements that sync to the active theme for real-time adjustments.import Theme from './others/theme/theme.js';
Upon initialization, the theme is set based on either the stored value or the system's preference.
Clicking the toggle button switches between light and dark themes, updating localStorage
and applying
the relevant theme attributes. Additionally, meta[name="theme-color"]
is updated to reflect the background
color of the active theme, enhancing the visual integration with browsers and mobile UIs.
The meta[name="theme-color"]
tag ensures that the browser interface reflects the active theme. If the tag is missing,
the class automatically creates and appends it to the <head>
.