react-css-themes
Theme provider for React components using CSS modules.
Key Features
- Themable components expose API to allow external theming
- Easy to change themes of deeply nested themable components
Table of contents
Installation
npm i --save react-css-themes
Usage [API]
withThemes(themeTypes, [themes], [defaultTheme])
Arguments:
themeTypes
(Object
) - Theme types definitionthemes
(Object
) - The available themesdefaultTheme
(Object
) - The default theme
Returns:
Returns a decorator `function to make Component themeable.
Example:
const ThemableComponent = <div className=themecontainer> <div className=themeheading>Heading</div> </div> ThemableComponentthemeTypes = container: ThemeTypesclassName heading: ThemeTypesclassName light dark lightThemableComponent
ThemeTypes
ThemeTypes
are used to define theme's property types. It's simillar to React PropTypes
, there are 2 avaiable types:
ThemeTypes.className
: Defines a property as a classNameThemeTypes.themeOf(ThemableComponent)
: Define a property as a theme of another themeable compoenent
Example:
; { // ... do things with the props} MyComponentthemeTypes = themeProp: ThemeTypesclassName childComponent: ThemeTypes
Component.themes
The compoenent will expose a themes
object property with the themes defined as keys. These themes are set by the compoenent but also expose an API to create derivated themes.
add(fragment)
Arguments:
fragment
(Object
) - ClassNames to add to the theme
Returns:
A new theme derived from the original theme plus the fragment
Example:
let <div className=themecontainer /> ThemableComponent = ThemableComponent /* HTML output */ // <div class=".container"></div> // Derived themeconst theme = ThemableComponentthemeslight // <div class=".container .foobar"></div>
Examples
Basic theming
/* ThemableComponent.js */ const ThemableComponent = <div className=themecontainer> <div className=themeheading>Heading</div> </div> ThemableComponentthemeTypes = container: ThemeTypesclassName heading: ThemeTypesclassName light dark lightThemableComponent
/* light.css */
/* dark.css */
/* View.js */ const View = <div> <ThemableComponent theme=ThemableComponentthemeslight /> <ThemableComponent theme=ThemableComponentthemesdark /> <ThemableComponent /> /* Default theme (light) will be used */ </div>
Composing themable components
/* AnotherThemableComponent.js */ const THEMES = themeA: ...themeA child1: ThemableComponentthemeslight child2: ThemableComponentthemesdark themeB: ...themeB child1: ThemableComponentthemesdark child2: ThemableComponentthemeslight const AnotherThemableComponent = <div className=themecontainer> <ThemableComponent theme=themechild1 /> <ThemableComponent theme=themechild2 /> </div> AnotherThemableComponentthemeTypes = container: ThemeTypesclassName child1: ThemeTypes child2: ThemeTypes THEMES THEMESthemeAAnotherThemableComponent
classNames
to a deeply nested themeable child
Adding /* AnotherThemableComponent.js */ const THEMES = themeA: ...themeA child: ThemableComponentthemeslight const AnotherThemableComponent = <div className=themecontainer> <ThemableComponent theme=themechild /> </div>
About
The project is authored by Diogo Cunha (@diffcunha) and Daniel Hayes (@daniel-hayes).
Comments, improvements or feedback are highly appreciated.
License
This project is licensed under the terms of the ISC license.