@buildy-ui/theme
Version:
React theme state management library for utility and semantic CSS approaches
91 lines (82 loc) • 2.27 kB
TypeScript
import React from 'react';
/**
* Theme constants
*/
declare const THEME_TYPES: {
readonly SEMANTIC: "semantic";
readonly UI8KIT: "ui8kit";
};
declare const DEFAULT_THEME: "ui8kit";
declare const STORAGE_KEYS: {
readonly THEME: "app-theme";
};
declare const LOCAL_STORAGE_KEY: "app-theme";
type ThemeType = typeof THEME_TYPES[keyof typeof THEME_TYPES];
/**
* Theme state interface
*/
interface ThemeState {
current: ThemeType;
isLoading: boolean;
}
/**
* Theme action types
*/
type ThemeAction = {
type: 'SET_THEME';
payload: ThemeType;
} | {
type: 'TOGGLE_THEME';
} | {
type: 'SET_LOADING';
payload: boolean;
};
/**
* Gets current theme
*/
declare function getTheme(): ThemeType;
/**
* Sets current theme
*/
declare function setTheme(theme: string): void;
/**
* Gets theme from storage (localStorage on client, url param on server)
*/
declare const getStoredTheme: () => ThemeType | null;
/**
* Stores theme in localStorage
*/
declare const storeTheme: (theme: ThemeType) => void;
/**
* Gets theme from URL parameter
*/
declare const getThemeFromUrl: () => ThemeType | null;
/**
* Removes theme parameter from URL
*/
declare const removeThemeParamFromUrl: () => void;
/**
* Gets current theme from all available sources
* Priority: URL param > localStorage > default
*/
declare const getInitialTheme: () => ThemeType;
/**
* Toggles between available themes
*/
declare const toggleTheme: (currentTheme: ThemeType) => ThemeType;
/**
* Theme context
*/
interface ThemeContextType extends ThemeState {
setTheme: (theme: ThemeType) => void;
toggleTheme: () => void;
}
interface ThemeProviderProps {
children: React.ReactNode;
initialTheme?: ThemeType;
}
declare const ThemeProvider: React.FC<ThemeProviderProps>;
declare const useTheme: () => ThemeContextType;
declare const initialThemeState: ThemeState;
declare function themeReducer(state: ThemeState, action: ThemeAction): ThemeState;
export { DEFAULT_THEME, LOCAL_STORAGE_KEY, STORAGE_KEYS, THEME_TYPES, type ThemeAction, ThemeProvider, type ThemeState, type ThemeType, getInitialTheme, getStoredTheme, getTheme, getThemeFromUrl, initialThemeState, removeThemeParamFromUrl, setTheme, storeTheme, themeReducer, toggleTheme, useTheme };