Socket
Book a DemoInstallSign in
Socket

@lonestone/usetheme

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lonestone/usetheme

React hook and provider for application theming

latest
Source
npmnpm
Version
1.0.5
Version published
Maintainers
0
Created
Source

useTheme

A React hook for implementing theme management with support for light, dark, and system themes.

Installation

npm install @lonestone/usetheme
# or
yarn add @lonestone/usetheme
# or
pnpm add @lonestone/usetheme

Features

  • 🌓 Support for light and dark themes
  • 🖥️ System theme detection and synchronization
  • 💾 Persistent theme storage
  • 🔄 Automatic theme switching
  • 🎨 CSS class-based theming
  • 💪 TypeScript support

Usage

import { ThemeProvider, useTheme } from '@lonestone/usetheme'

// Wrap your app with ThemeProvider
function App() {
  return (
    <ThemeProvider
      defaultTheme="system" // Optional: 'light' | 'dark' | 'system'
      storageKey="my-app-theme" // Optional: custom storage key
    >
      <YourApp />
    </ThemeProvider>
  )
}

// Use the hook in your components
function ThemeToggle() {
  const { theme, systemTheme, setTheme } = useTheme()

  return (
    <div>
      <p>Current theme: {theme}</p>
      <p>System theme: {systemTheme}</p>
      <button onClick={() => setTheme('light')}>Light</button>
      <button onClick={() => setTheme('dark')}>Dark</button>
      <button onClick={() => setTheme('system')}>System</button>
    </div>
  )
}

API Reference

ThemeProvider Props

PropTypeDefaultDescription
defaultTheme'light' | 'dark' | 'system''system'Default theme to use
storageKeystring'vite-ui-theme'Local storage key for persisting the theme
childrenReact.ReactNode-Child components

useTheme Hook Return Value

PropertyTypeDescription
theme'light' | 'dark' | 'system'Current theme setting
systemTheme'light' | 'dark'Current system theme (when theme='system')
setTheme(theme: Theme) => voidFunction to update the theme

How it Works

The hook automatically:

  • Adds appropriate theme classes (light or dark) to the document root
  • Syncs with system theme preferences when set to 'system'
  • Persists theme choice in localStorage
  • Provides real-time theme updates

CSS Usage

The theme provider adds either light or dark class to your document's root element. You can style your application accordingly:

:root.light {
  --background: #ffffff;
  --text: #000000;
}

:root.dark {
  --background: #000000;
  --text: #ffffff;
}

License

This project is licensed under the Unlicense.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Keywords

react

FAQs

Package last updated on 18 Feb 2025

Did you know?

Socket

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.

Install

Related posts