Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
styled-theming
Advanced tools
Create themes for your app using styled-components
Read the introductory blog post
yarn add styled-components styled-theming
import React from 'react';
import styled, {ThemeProvider} from 'styled-components';
import theme from 'styled-theming';
const boxBackgroundColor = theme('mode', {
light: '#fff',
dark: '#000',
});
const Box = styled.div`
background-color: ${boxBackgroundColor};
`;
export default function App() {
return (
<ThemeProvider theme={{ mode: 'light' }}>
<Box>
Hello World
</Box>
</ThemeProvider>
);
}
<ThemeProvider>
<ThemeProvider>
is part of styled-components, but is required for styled-theming.
import {ThemeProvider} from 'styled-components';
<ThemeProvider>
accepts a single prop theme
which you should pass an object
with either strings or getter functions. For example:
<ThemeProvider theme={{ mode: 'dark', size: 'large' }}>
<ThemeProvider theme={{ mode: modes => modes.dark, size: sizes => sizes.large }}>
You should generally set up a <ThemeProvider>
at the root of your app:
function App() {
return (
<ThemeProvider theme={...}>
{/* rest of your app */}
</ThemeProvider>
);
}
theme(name, values)
Most of your theming will be done with this function.
name
should match one of the keys in your <ThemeProvider>
theme.
<ThemeProvider theme={{ whatever: '...' }}/>
theme('whatever', {...});
values
should be an object where one of the keys will be selected by the
value provided to <ThemeProvider>
theme.
<ThemeProvider theme={{ mode: 'light' }}/>
<ThemeProvider theme={{ mode: 'dark' }}/>
theme('mode', {
light: '...',
dark: '...',
});
The values of this object can be any CSS value.
theme('mode', {
light: '#fff',
dark: '#000',
});
theme('font', {
sansSerif: '"Helvetica Neue", Helvetica, Arial, sans-serif',
serif: 'Georgia, Times, "Times New Roman", serif',
monoSpaced: 'Consolas, monaco, monospace',
});
These values can also be functions that return CSS values.
theme('mode', {
light: props => props.theme.userProfileAccentColor.light,
dark: props => props.theme.userProfileAccentColor.dark,
});
theme
will create a function that you can use as a value in
styled-component's styled
function.
import styled from 'styled-components';
import theme from 'styled-theming';
const backgroundColor = theme('mode', {
light: '#fff',
dark: '#000',
});
const Box = styled.div`
background-color: ${backgroundColor}
`;
theme.variants(name, prop, themes)
It's often useful to create variants of the same component that are selected via an additional prop.
To make this easier with theming, styled-theming provides a theme.variants
function.
import styled from 'styled-components';
import theme from 'styled-theming';
const backgroundColor = theme.variants('variant', 'mode', {
default: { light: 'gray', dark: 'darkgray' },
primary: { light: 'blue', dark: 'darkblue' },
success: { light: 'green', dark: 'darkgreen' },
warning: { light: 'orange', dark: 'darkorange' },
});
const Button = styled.button`
background-color: ${backgroundColor};
`;
Button.propTypes = {
variant: PropTypes.oneOf(['default', 'primary', 'success', 'warning'])
};
Button.defaultProps = {
variant: 'default',
};
<Button/>
<Button variant="primary"/>
<Button variant="success"/>
<Button variant="warning"/>
FAQs
Create themes for your app using styled-components
We found that styled-theming demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.