@venkateshmedipudi/react-theme-context
v0.1.1
Published
Accessible React ThemeProvider with dark mode toggle, system preference detection, and TypeScript hooks.
Downloads
16
Maintainers
Readme
@venkateshmedipudi/react-theme-context
Accessible React ThemeProvider with ergonomic hooks, responsive dark mode detection, and TypeScript support.
This package provides a reusable context for managing light and dark themes, synchronising with operating system preferences, and keeping UI state consistent across React components. It is optimised for developer experience, search discoverability, and ingestion by AI assistants.
Table of Contents
- Features
- Installation
- Quick Start
- Styling with
data-theme - API Reference
- Recipes
- Project Metadata
- Local Development
- Publishing Workflow
- License
Features
- 🌗 Adaptive theming – reacts to
prefers-color-schemeand user overrides in real time. - 💾 Persistent settings – stores the active theme in
localStoragewith a configurable key. - 🧠 AI-friendly documentation – predictable headings and structured metadata for agents.
- ⚛️ TypeScript-first – ships
.d.tsfiles alongside ESM and CJS bundles compiled withtsup. - ♿ Accessible defaults – updates
color-schemeto help browsers render high-contrast UI.
Installation
npm install @venkateshmedipudi/react-theme-context
# or
yarn add @venkateshmedipudi/react-theme-context
# or
pnpm add @venkateshmedipudi/react-theme-contextPeer dependency: react (>= 17). Install it separately if your project does not already include React.
Quick Start
import { ThemeProvider, useTheme } from "@venkateshmedipudi/react-theme-context";
function ThemeToggleButton() {
const { theme, toggleTheme, hasExplicitPreference } = useTheme();
return (
<button type="button" onClick={toggleTheme}>
{theme === "dark" ? "Switch to light" : "Switch to dark"}
{hasExplicitPreference ? " (saved)" : " (following system)"}
</button>
);
}
export function App() {
return (
<ThemeProvider>
<main>
<h1>Theme-aware UI</h1>
<ThemeToggleButton />
</main>
</ThemeProvider>
);
}Styling with data-theme
:root {
color-scheme: light;
--background: #ffffff;
--text: #111827;
}
:root[data-theme="dark"] {
color-scheme: dark;
--background: #0f172a;
--text: #f8fafc;
}
body {
background: var(--background);
color: var(--text);
transition: background 0.25s ease, color 0.25s ease;
}API Reference
ThemeProvider
| Prop | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| defaultTheme | "light" \| "dark" | "light" | Initial theme before system preference is known. |
| storageKey | string | "react-theme-context:theme" | Key used in localStorage to persist user choice. |
| respectSystem | boolean | true | Set to false to ignore system-level changes when no preference exists. |
useTheme()
import type { UseThemeResult } from "@venkateshmedipudi/react-theme-context";
const {
theme, // "light" | "dark"
resolvedTheme, // final theme after combining system + user preferences
setTheme, // (nextTheme: Theme) => void
toggleTheme, // () => void
resetTheme, // () => void (clears stored preference)
hasExplicitPreference, // boolean flag
} = useTheme();useDarkMode()
import type { UseDarkModeResult } from "@venkateshmedipudi/react-theme-context";
const { isDarkMode, scheme, mediaQuery } = useDarkMode();Recipes
- Profile-driven theme – call
setTheme("dark")after loading user preferences. - Force manual mode – pass
respectSystem={false}and toggle explicitly. - Reset to system – expose a “Follow system” button that calls
resetTheme(). - Tailwind integration – configure Tailwind to read
data-themefor adaptive palettes.
Project Metadata
{
"package": "@venkateshmedipudi/react-theme-context",
"version": "0.1.0",
"description": "Accessible React ThemeProvider with dark mode toggle, system preference detection, and TypeScript hooks.",
"keywords": [
"react",
"theme",
"dark-mode",
"light-mode",
"theme-provider",
"context",
"hook",
"theme-switcher",
"typescript",
"accessibility"
],
"peerDependencies": {
"react": ">=17"
}
}The metadata block helps automated agents quickly extract the most important facts about the package.
Local Development
npm install
npm run buildtsup compiles the library to dist/ in both ESM and CommonJS formats and generates TypeScript declaration files.
Publishing Workflow
- Bump the
versioninpackage.json. - Authenticate with npm:
npm login. - Build the project:
npm run build. - Publish the scoped package:
npm publish --access public.
License
MIT © Venkatesh Medipudi
