### Astro Layout Example Source: https://github.com/saadeghi/theme-change/blob/master/README.md Demonstrates how to import and use a custom layout component in an Astro project. This snippet shows basic page content wrapped within the MyCrazyLayout. ```astro import MyCrazyLayout from '../layouts/MyCrazyLayout.astro'; ---

My page content, wrapped in a layout!

``` -------------------------------- ### Integrate Theme Change with NPM in SolidJS Source: https://github.com/saadeghi/theme-change/blob/master/README.md This example demonstrates integrating the theme-change library within a SolidJS project. The `onMount` function is used to initialize `themeChange()`. Note that the `false` parameter is not required for SolidJS projects. ```javascript import { onMount } from 'solid-js' import { themeChange } from 'theme-change' onMount(async () => { themeChange(); }) ``` -------------------------------- ### Integrate Theme Change with NPM in JavaScript Source: https://github.com/saadeghi/theme-change/blob/master/README.md This demonstrates how to install and import the theme-change library into a standard JavaScript project using NPM. It initializes the theme change functionality with default settings. ```javascript import { themeChange } from 'theme-change' themeChange() ``` -------------------------------- ### Astro Layout Integration for Global Theme Change Source: https://github.com/saadeghi/theme-change/blob/master/README.md This example demonstrates integrating the theme-change library into an Astro layout file to apply theme changes across all pages in an MPA project. It includes two scripts within the `` section: one to prevent FART by setting the initial theme from local storage, and another to initialize the `themeChange` function. ```html --- --- My Cool Astro Layout Wraping All My Pages
``` -------------------------------- ### Integrate Theme Change with NPM in Vue 3 (Composition API) Source: https://github.com/saadeghi/theme-change/blob/master/README.md This example shows the integration of the theme-change library within a Vue 3 project using the Composition API. The `onMounted` hook is used to call `themeChange(false)`, which is necessary for Vue projects. ```javascript import { onMounted } from 'vue' import { themeChange } from 'theme-change' export default { setup() { onMounted(() => { themeChange(false) }) }, } ``` -------------------------------- ### CSS OS Color Scheme Detection Source: https://github.com/saadeghi/theme-change/blob/master/README.md Applies specific CSS variables based on the user's operating system color scheme preference. This example sets a dark background color when the OS is in dark mode. ```css @media (prefers-color-scheme: dark){ :root{ --my-color: #252b30; } } ``` -------------------------------- ### Include Theme Change via CDN Source: https://github.com/saadeghi/theme-change/blob/master/README.md This snippet shows how to include the theme-change library directly into your HTML using a CDN link. This is the simplest way to add the functionality without any installation steps. ```html ``` -------------------------------- ### PurgeCSS Safelisting for Themes Source: https://github.com/saadeghi/theme-change/blob/master/README.md Configuration for PurgeCSS to prevent theme-specific CSS rules from being removed during the build process. It includes examples for both PostCSS configuration and inline CSS comments. ```javascript module.exports = { purge: { options: { safelist: [/data-theme$/], }, }, } ``` -------------------------------- ### Alternative Safelist Configuration Directly in CSS Source: https://context7.com/saadeghi/theme-change/llms.txt This CSS code provides an alternative method for safelisting theme-related styles directly within the CSS file, bypassing typical CSS purging. It uses special `/*! purgecss start ignore */` and `/*! purgecss end ignore */` comments to mark sections that should not be processed by PurgeCSS. Inside these blocks, styles for different themes (`dark`, `light`, `cyberpunk`) are defined using CSS custom properties. Regular CSS rules that follow this block might be purged if unused, demonstrating how to protect specific theme styles while allowing other unused styles to be removed. ```css /* Alternative: Safelist directly in CSS */ /*! purgecss start ignore */ [data-theme='dark'] { --primary-color: #1a1a2e; --secondary-color: #16213e; --accent-color: #0f3460; } [data-theme='light'] { --primary-color: #f8f9fa; --secondary-color: #e9ecef; --accent-color: #dee2e6; } [data-theme='cyberpunk'] { --primary-color: #ff00ff; --secondary-color: #00ffff; --accent-color: #ffff00; } /*! purgecss end ignore */ /* Regular CSS - will be purged if unused */ .container { background: var(--primary-color); color: var(--secondary-color); } ``` -------------------------------- ### Initialize Theme Switching with themeChange() Source: https://context7.com/saadeghi/theme-change/llms.txt Initializes theme switching functionality. It automatically detects and activates elements with theme-related data attributes. The function can be configured to attach event listeners on DOMContentLoaded or run immediately, making it suitable for both vanilla JavaScript and framework integrations. ```javascript import { themeChange } from 'theme-change' themeChange() ``` ```javascript import { useEffect } from 'react' import { themeChange } from 'theme-change' function App() { useEffect(() => { themeChange(false) // false prevents DOMContentLoaded waiting }, []) return (
) } ``` ```javascript import { onMounted } from 'vue' import { themeChange } from 'theme-change' export default { setup() { onMounted(() => { themeChange(false) }) return { /* your reactive data */ } } } ``` -------------------------------- ### Custom localStorage Key Configuration Source: https://github.com/saadeghi/theme-change/blob/master/README.md Shows how to specify a custom localStorage key for persisting theme preferences. This is achieved by adding a `data-key` attribute to the HTML elements responsible for theme control. ```html ``` -------------------------------- ### Integrate Theme Change with NPM in React Source: https://github.com/saadeghi/theme-change/blob/master/README.md This code snippet illustrates how to integrate the theme-change library into a React application. It uses the `useEffect` hook to initialize the theme change functionality, passing `false` as a required parameter for React projects to prevent conflicts. ```javascript import { useEffect } from 'react' import { themeChange } from 'theme-change' useEffect(() => { themeChange(false) // 👆 false parameter is required for react project }, []) ``` -------------------------------- ### OS Color Scheme Detection with CSS Media Query Source: https://context7.com/saadeghi/theme-change/llms.txt This CSS code defines base theme variables and automatically applies a dark theme based on the user's operating system preference using the `@media (prefers-color-scheme: dark)` query. It also includes manual theme overrides for light, dark, and custom themes using data attributes, and applies these variables to body and card elements. No external dependencies are required. ```css :root { --background: #ffffff; --foreground: #000000; --primary: #0070f3; --secondary: #7928ca; --border: #eaeaea; } /* Automatically apply dark theme based on OS preference */ @media (prefers-color-scheme: dark) { :root { --background: #000000; --foreground: #ffffff; --primary: #0070f3; --secondary: #ff0080; --border: #333333; } } /* Manual theme overrides OS preference */ [data-theme='light'] { --background: #ffffff; --foreground: #000000; --primary: #0070f3; --border: #eaeaea; } [data-theme='dark'] { --background: #000000; --foreground: #ffffff; --primary: #0070f3; --border: #333333; } [data-theme='custom'] { --background: #1a1a2e; --foreground: #eee; --primary: #16213e; --secondary: #0f3460; --border: #533483; } /* Apply variables to elements */ body { background-color: var(--background); color: var(--foreground); } .card { border: 1px solid var(--border); background: var(--background); } ``` -------------------------------- ### Integrate Theme Change with NPM in Vue 2 (Options API) Source: https://github.com/saadeghi/theme-change/blob/master/README.md This snippet details how to add the theme-change library to a Vue 2 project utilizing the Options API. The `mounted` lifecycle hook is employed to execute `themeChange(false)`, a required step for Vue projects. ```javascript import { themeChange } from 'theme-change' export default { mounted: function () { themeChange(false) }, } ``` -------------------------------- ### HTML Select Menu for Theme Choice Source: https://github.com/saadeghi/theme-change/blob/master/README.md A standard HTML select dropdown menu that allows users to choose a theme from a list of options. The `data-choose-theme` attribute enables theme selection, with each `