2.2.0 • Published 7 years ago

styled-theming v2.2.0

Weekly downloads
28,032
License
MIT
Repository
github
Last release
7 years ago

styled-theming

Create themes for your app using styled-components

Read the introductory blog post

Installation

yarn add styled-components styled-theming

Example

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>
  );
}

API

<ThemeProvider>

See styled-components docs

<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"/>
cra-template-buerlioa-themeligosnippetz@sellics/rankingsreact-firebase-provider@sellics/ui-kit@sellics/profiteditor-55@sellics/lerna-workshop-featurepackagemayani-ui@everything-registry/sub-chunk-2841twitch-core-ui-utilstwitch-core-ui-utils-styled-componentsionsways-frontendwonder-uiui123321ugrowth-admin-uiwasabi-kitm7kvqbe1-react-component-libraryyuntun-plugin-zhuye@hapl/componentscreative-bot-2@hacksc/sctw-ui-kitcreate-rn-redux-toolkit-boilerplategraphql-playgroundinfrastructure-scriptsint-shared-uicompact-ligo-ide-forkcomponents-library-vite@holographicio/styled-blueprintfennec-finance-appedu-formemberjelly-test-applicationemberjelly_test_app_3odeum-appoffcourse-ui-componentsnitramui@jonathansees/crown-molding@marvelousdev0/mtm-ui@mixmaxhq/theming@mikedonkers/react-uikit-example@ligolang/compact-ligo-ide@lionhat/tuesday-app@liquid-design/liquid-design-react-native@past3lle/schematicspoligonosappmonstrosity-ui@isheedo/cll-pocmicrosite-ui@infinitebrahmanuniverse/nolb-styledmerch_style@placeshakr/ui-webprey-stashqwikskill-uiqwikskills-event-trackerqwikskills-tracking-eventsqwikskills-ui@fangorn/test@fennec-finance/app@fennec-finance/base@fennec-finance/core@fennec-finance/mobile-app@fennec-finance/web-components@polkadot/ui-app@polkadot/react-components@redwallsolutions/theming-component-modulereact-clio-system-betareact-clio-system-testreact-glassmorphism-libraryreact-github-info@royalnavy/react-component-library@unnax_dev/unnax-component-lib@alidantech/styled-tsx@uuixjs/uuixweb@uuixjs/uuixweb-lib@subsocial/react-componentsreact_redux_pwa_tmp@tverse/ui@trovo/stylereact-placeholder-canvasaria-cc@axiasolar-js/react-components@baosystems/nebula@brite-inc/p5.js-web-editor-component@britishlibrary/component-library@crpt/react-theme@decisiv/ui-components@delfitz/alex-uibin-design@defencedigital/react-component-librarytb-awesome-uicent-comp
2.2.0

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.0.0

7 years ago