Sveltekit Dark Mode screenshot

Sveltekit Dark Mode

Author Avatar Theme by Captaincodeman
Updated: 31 Dec 2022
18 Stars

Dark mode toggle using SvelteKit and TailwindCSS

Categories

Overview:

The “sveltekit-dark-mode” package is a simple example of a dark mode theme toggle using SvelteKit and TailwindCSS. It provides a self-contained solution, contained within a single component, that includes a simple icon toggle and an animated switch toggle. This package also allows for the override of the system setting, but will remove the override once the system setting is returned to its original state. Additionally, the dark mode theme will dynamically switch based on the user’s system preferences.

Features:

  • Simple and self-contained: The package is contained within a single component, making it easy to implement and use.
  • Icon and switch toggles: The package includes a simple icon toggle and an animated switch toggle for easy switching between light and dark mode.
  • Override system setting: Users can choose to override the system setting and use the dark or light mode theme even if it conflicts with their system preferences.
  • Dynamic switch based on system preferences: The dark mode theme will dynamically switch based on the user’s system preferences, providing a seamless experience.

Installation:

To install the “sveltekit-dark-mode” package, follow these steps:

  1. Open your terminal and navigate to your project folder.
  2. Run the following command to install the package:
npm install sveltekit-dark-mode
  1. Import the package component into your SvelteKit project:
import DarkModeToggle from 'sveltekit-dark-mode'
  1. Use the DarkModeToggle component in your template:
<script>
  import DarkModeToggle from 'sveltekit-dark-mode'
  
  let isDarkMode = false;
  
  function toggleDarkMode() {
    isDarkMode = !isDarkMode;
  }
</script>

<main>
  <h1>Welcome to My Website</h1>
  <DarkModeToggle on:click={toggleDarkMode} isDarkMode={isDarkMode} />
</main>
  1. Customize the component as needed using the available props and styling options.

Summary:

The “sveltekit-dark-mode” package is a convenient solution for implementing a dark mode theme toggle in SvelteKit projects. Its key features include a self-contained single component, icon and switch toggles, the ability to override the system setting, and dynamic switching based on system preferences. By following the installation steps and customizing the component as needed, developers can easily add dark mode functionality to their SvelteKit applications.