
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-css-themes
Advanced tools
Theme provider for React components using CSS modules.
Key Features
npm i --save react-css-themes
withThemes(themeTypes, [themes], [defaultTheme])
themeTypes
(Object
) - Theme types definitionthemes
(Object
) - The available themesdefaultTheme
(Object
) - The default themeReturns a decorator `function to make Component themeable.
import React from 'react'
import withThemes, { ThemeTypes } from 'react-css-themes'
import light from './light.css'
import dark from './dark.css'
const ThemableComponent = ({ theme }) => (
<div className={theme.container}>
<div className={theme.heading}>Heading</div>
</div>
)
ThemableComponent.themeTypes = {
container: ThemeTypes.className,
heading: ThemeTypes.className
}
export default withThemes({ light, dark }, light)(ThemableComponent)
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 compoenentimport React from 'react';
import { ThemeTypes } from 'react-css-themes'
function MyComponent ({ theme }) {
// ... do things with the props
}
MyComponent.themeTypes = {
themeProp: ThemeTypes.className,
childComponent: ThemeTypes.themeOf(ThemableComponent),
}
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)
fragment
(Object
) - ClassNames to add to the themeA new theme derived from the original theme plus the fragment
let ThemableComponent = ({ theme }) => (
<div className={theme.container} />
)
ThemableComponent = withThemes({
light: {
container: '.container'
}
}, light)(ThemableComponent)
/* HTML output */
renderToStaticMarkup(<ThemableComponent />)
// <div class=".container"></div>
// Derived theme
const theme = ThemableComponent.themes.light.add({
container: '.foobar'
})
renderToStaticMarkup(<ThemableComponent theme={theme} />)
// <div class=".container .foobar"></div>
/* ThemableComponent.js */
import React from 'react'
import withThemes, { ThemeTypes } from 'react-css-themes'
import light from './light.css'
import dark from './dark.css'
const ThemableComponent = ({ theme }) => (
<div className={theme.container}>
<div className={theme.heading}>Heading</div>
</div>
)
ThemableComponent.themeTypes = {
container: ThemeTypes.className,
heading: ThemeTypes.className
}
export default withThemes({ light, dark }, light)(ThemableComponent)
/* light.css */
.container {
background-color: white;
}
.heading {
color: black;
}
/* dark.css */
.container {
background-color: black;
}
.heading {
color: white;
}
/* View.js */
import React from 'react'
import ThemableComponent from './ThemableComponent'
const View = () => (
<div>
<ThemableComponent theme={ThemableComponent.themes.light} />
<ThemableComponent theme={ThemableComponent.themes.dark} />
<ThemableComponent /> /* Default theme (light) will be used */
</div>
)
export default View
/* AnotherThemableComponent.js */
import React from 'react'
import withThemes, { ThemeTypes } from 'react-css-themes'
import themeA from './themeA.css'
import themeB from './themeB.css'
const THEMES = {
themeA: {
...themeA,
child1: ThemableComponent.themes.light,
child2: ThemableComponent.themes.dark
},
themeB: {
...themeB,
child1: ThemableComponent.themes.dark,
child2: ThemableComponent.themes.light
}
}
const AnotherThemableComponent = ({ theme }) => (
<div className={theme.container}>
<ThemableComponent theme={theme.child1} />
<ThemableComponent theme={theme.child2} />
</div>
)
AnotherThemableComponent.themeTypes = {
container: ThemeTypes.className,
child1: ThemeTypes.themeOf(ThemableComponent),
child2: ThemeTypes.themeOf(ThemableComponent)
}
export default withThemes(THEMES, THEMES.themeA)(AnotherThemableComponent)
classNames
to a deeply nested themeable child/* AnotherThemableComponent.js */
import React from 'react'
import withThemes, { ThemeTypes } from 'react-css-themes'
import themeA from './themeA.css'
const THEMES = {
themeA: {
...themeA,
child: ThemableComponent.themes.light.add({
container: themeA.childContainer,
heading: themeA.childHeading
})
}
}
const AnotherThemableComponent = ({ theme }) => (
<div className={theme.container}>
<ThemableComponent theme={theme.child} />
</div>
)
The project is authored by Diogo Cunha (@diffcunha) and Daniel Hayes (@daniel-hayes).
Comments, improvements or feedback are highly appreciated.
This project is licensed under the terms of the ISC license.
FAQs
Theme provider for React components using CSS modules
The npm package react-css-themes receives a total of 7 weekly downloads. As such, react-css-themes popularity was classified as not popular.
We found that react-css-themes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.