Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
theme-change
Advanced tools
Readme
button
, toggle
or a <select>
Use CDN:
<script src="https://cdn.jsdelivr.net/npm/theme-change@2.0.2/index.js"></script>
Install: npm i theme-change --save
and use it in your js file:
import { themeChange } from 'theme-change'
themeChange()
Install: npm i theme-change --save
and use it in your js file:
import { useEffect } from 'react'
import { themeChange } from 'theme-change'
useEffect(() => {
themeChange(false)
// 👆 false parameter is required for react project
}, [])
Install: npm i theme-change --save
and use it in your js file:
import { onMounted } from 'vue'
import { themeChange } from 'theme-change'
export default {
setup() {
onMounted(() => {
themeChange(false)
})
},
}
Install: npm i theme-change --save
and use it in your js file:
import { themeChange } from 'theme-change'
export default {
mounted: function () {
themeChange(false)
},
}
Install: npm i theme-change --save
and use it in your svelte component that uses one theme-change attributes:
import { onMount } from 'svelte'
import { themeChange } from 'theme-change'
// NOTE: the element that is using one of the theme attributes must be in the DOM on mount
onMount(() => {
themeChange(false)
// 👆 false parameter is required for svelte
})
Install: npm i theme-change --save
and use it in your js/jsx/tsx file:
import { onMount } from 'solid-js'
import { themeChange } from 'theme-change'
onMount(async () => {
themeChange();
})
Install: npm i theme-change --save
and use it in your .astro file(s):
Astro is a bit tricky because of how is rendering html page as a MPA (Multiple Pages Application) Astro projects are therefore subject to FART problem. To prevent this we will use the is:inline astro directive.
If you want to apply themes on a single astro page (remember Astro is an MPA framework) :
src/pages/mypage.astro
---
---
<html lang="en">
<head>
<script is:inline>
// ☝️ This script prevent the FART effect.
if (localStorage.getItem("theme") === null) {
document.documentElement.setAttribute("data-theme", "light");
} else
document.documentElement.setAttribute("data-theme",localStorage.getItem("theme"));
// "theme" LocalStorage value is set by the package to remember user preference.
// The value is checked and applyed before rendering anything.
</script>
<script>
import { themeChange } from "theme-change";
themeChange();
// 👆 you could import the CDN directly instead of these two lines
</script>
<title>My crazy credit page</title>
</head>
<body>
<h1>Welcome to my credit page!</h1>
</body>
</html>
If you want to apply themes to all your astro pages, you need to execute both scripts in a Astro layout, it would need to wrap all your astro pages like so:
src/layouts/MyCrazyLayout.astro
---
---
<html lang="en">
<head>
<script is:inline>
// ☝️ This script prevent the FART effect.
if (localStorage.getItem("theme") === null) {
document.documentElement.setAttribute("data-theme", "light");
} else
document.documentElement.setAttribute(
"data-theme",
localStorage.getItem("theme")
);
// "theme" LocalStorage value is set by the package to remember user preference.
// The value is checked and applyed before rendering anything.
</script>
<script>
import { themeChange } from 'theme-change';
themeChange();
// 👆 you could import the CDN directly instead of these two lines
</script>
<meta charset="utf-8" />
<title>My Cool Astro Layout Wraping All My Pages</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<nav>
<a href="#">Home</a>
<a href="#">Posts</a>
<a href="#">Contact</a>
</nav>
<article>
<slot />
<!-- your content from src/pages/index.astro is injected here -->
</article>
</body>
</html>
src/pages/index.astro
---
import MyCrazyLayout from '../layouts/MyCrazyLayout.astro';
---
<MySiteLayout>
<p>My page content, wrapped in a layout!</p>
</MySiteLayout>
Set your themeable style as custom properties in CSS like this:
:root {
--my-color: #fff;
/* or any other variables/style */
}
[data-theme='dark'] {
--my-color: #000;
}
[data-theme='pink'] {
--my-color: #ffabc8;
}
then use your variables on any element
body {
background-color: var(--my-color);
}
There are 3 options:
Clicking on these buttons, sets the chosen theme and also adds the ACTIVECLASS
to the chosen button
<button data-set-theme="" data-act-class="ACTIVECLASS"></button>
<button data-set-theme="dark" data-act-class="ACTIVECLASS"></button>
<button data-set-theme="pink" data-act-class="ACTIVECLASS"></button>
Clicking on this element, toggles between dark
and light
theme and also adds the ACTIVECLASS
to the element
<button data-toggle-theme="dark,light" data-act-class="ACTIVECLASS"></button>
<select>
menu<select data-choose-theme>
<option value="">Default</option>
<option value="dark">Dark</option>
<option value="pink">Pink</option>
</select>
@media (prefers-color-scheme: dark){
:root{
--my-color: #252b30;
}
}
If you're using Purge CSS, you might need to safe list your CSS using the comments below because your secondary themes will be purged
Safelist [data-theme]
on postcss config
module.exports = {
purge: {
options: {
safelist: [/data-theme$/],
},
},
}
Safelist inside CSS file
/*! purgecss start ignore */
[data-theme='dark'] {
--my-color: #252b30;
}
/*! purgecss end ignore */
If you want to use a custom localStorage key, you can add it to the data-key
attribute like this:
<select data-choose-theme data-key="admin-panel">
<button data-key="front-page" data-set-theme="">
<span data-key="premium-user-theme" data-toggle-theme="dark">
FAQs
Change CSS theme with toggle, buttons or select using CSS Variables and localStorage
The npm package theme-change receives a total of 6,989 weekly downloads. As such, theme-change popularity was classified as popular.
We found that theme-change demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.