Package Exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-theme-kit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Theme Package (Beta)
A lightweight and flexible theme management system for React applications using CSS variables.
Installation
npm install theme-package@beta
Features
- ๐จ Streamlined theme switching
- โจ Fully customizable themes
- ๐พ Automatic theme persistence
- ๐ Immediate theme updates
- ๐ฆ TypeScript support
- ๐งช 100% test coverage
- ๐ ๏ธ Zero dependencies (except React)
- ๐ Detailed documentation
- ๐ Type-safe APIs
Quick Start
import { ThemeProvider, useTheme } from "theme-package";
const themes = {
light: {
name: "light",
variables: {
"--bg-color": "#ffffff",
"--text-color": "#000000",
},
},
dark: {
name: "dark",
variables: {
"--bg-color": "#000000",
"--text-color": "#ffffff",
},
},
};
// Wrap your app with ThemeProvider
function App() {
return (
<ThemeProvider themes={themes} initialTheme="light">
<YourApp />
</ThemeProvider>
);
}
// Use the theme in any component
function ThemeSwitcher() {
const { currentTheme, setTheme } = useTheme();
return (
<button
onClick={() => setTheme(currentTheme === "light" ? "dark" : "light")}
>
Toggle Theme
</button>
);
}
Adding Custom Themes
import { Theme, useTheme } from "theme-package";
const customTheme: Theme = {
name: "custom",
variables: {
"--bg-color": "#f0f0f0",
"--text-color": "#333333",
"--primary-color": "#0066cc",
// ... add more CSS variables
},
};
function ThemeManager() {
const { addTheme, setTheme } = useTheme();
const handleAddTheme = () => {
addTheme(customTheme); // Add the theme
setTheme("custom"); // Apply it immediately
};
return <button onClick={handleAddTheme}>Use Custom Theme</button>;
}
Theme Persistence
Themes are automatically persisted in localStorage. The ThemeProvider will:
- Check localStorage for a saved theme on initialization
- Fall back to initialTheme if no theme is saved or found
- Automatically save theme changes
- Handle missing or incorrect stored themes appropriately
API Reference
ThemeProvider Props
Prop | Type | Required | Default | Description |
---|---|---|---|---|
themes | Record<string, Theme> | Yes | - | Object containing all themes |
initialTheme | string | No | First theme key | Initial theme to use |
useTheme Hook
Returns an object with:
currentTheme
: string - Current active theme namethemes
: Record<string, Theme> - All available themessetTheme
: (themeName: string) => void - Function to switch themes (applies immediately)addTheme
: (theme: Theme) => void - Function to add a new theme
Theme Interface
interface Theme {
name: string;
variables: {
[key: string]: string; // CSS variables and their values
};
}
Development
# Install dependencies
npm install
# Start development mode
npm run dev
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Lint code
npm run lint
# Format code
npm run format
# Build package
npm run build
Testing
The package includes a comprehensive test suite with 100% coverage using Jest and React Testing Library.
Contributing
We welcome contributions! This package is in beta, and we'd love your help to improve it.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Run tests (
npm test
) - Ensure code is formatted (
npm run format
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
MIT
Todo
- Add theme transition animations
- Add theme validation
- Add more built-in themes
- Add theme export/import functionality
- Add theme preview component
- Add theme editor component
- Add CSS-in-JS integration examples
- Add Storybook documentation
- Add performance optimizations
- Add theme inheritance support
๐ Star us on GitHub if you find this package helpful!