Skip to content

Add a ripple-style theme toggle animation to your Astro project with ease

License

Notifications You must be signed in to change notification settings

JackTheMico/astro-theme-toggle

 
 

Repository files navigation

NPM version

Add a ripple-style theme toggle animation to your Astro project with ease.

astro-theme-toggle.mp4

Installation

npm install astro-theme-toggle

Usage

  1. Add the ThemeScript component to your <head>. This will set the data-theme attribute and toggle the dark class on your <html> element based on the user's theme preference.

    ---
    import { ThemeScript } from 'astro-theme-toggle';
    ---
    <head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width,initial-scale=1" />
      <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
      <ThemeScript />
      <!-- other head elements -->
    </head>
  2. Add the Toggle component to your <body> to toggle the theme:

    ---
    import { Toggle } from 'astro-theme-toggle'
    ---
    
    <Toggle style="width: 32px; height: 32px;" />
  3. You can customize the icon by passing a Fragment with the icon-light and icon-dark slots.

    ---
    import { Toggle } from 'astro-theme-toggle'
    import MyLightIcon from './my-light-icon.astro'
    import MyDarkIcon from './my-dark-icon.astro'
    ---
    
    <Toggle style="width: 32px; height: 32px;">
      <Fragment slot="icon-light">
        <MyLightIcon />
      </Fragment>
      <Fragment slot="icon-dark">
        <MyDarkIcon />
      </Fragment>
    </Toggle>

Live Examples

License

MIT

About

Add a ripple-style theme toggle animation to your Astro project with ease

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Astro 69.4%
  • TypeScript 23.1%
  • JavaScript 3.7%
  • Shell 1.6%
  • Vue 1.5%
  • CSS 0.7%