11.0.0 • Published 2 years ago

emotion-theming v11.0.0

Weekly downloads
2,294,274
License
MIT
Repository
github
Last release
2 years ago

emotion-theming

A CSS-in-JS theming solution for React

emotion-theming is a theming library inspired by styled-components

Table of Contents

Install

# add --save if using npm@4 or lower
npm i emotion-theming

# or
yarn add emotion-theming

Usage

TODO: Add example with the css prop

Theming is accomplished by placing the ThemeProvider component, at the top of the React component tree and wrapping descendants with the withTheme higher-order component (HOC). This HOC seamlessly acquires the current theme and injects it as a "prop" into your own component.

The theme prop is automatically injected into components created with styled.

Here is a complete example for a typical React + Emotion app (information about each piece of the theming API is listed afterward):

/** child.js */
import React from 'react'
import styled from 'react-emotion'

const Container = styled.div`
  background: whitesmoke;
  height: 100vh;
`

const Headline = styled.h1`
  color: ${props => props.theme.color};
  font: 20px/1.5 sans-serif;
`

export default class Page extends React.Component {
  render() {
    return (
      <Container>
        <Headline>I'm red!</Headline>
      </Container>
    )
  }
}

/** parent.js */
import React from 'react'
import ReactDOM from 'react-dom'
import { ThemeProvider } from 'emotion-theming'

import Page from './child.js'

const theme = {
  color: 'red'
}

class App extends React.Component {
  render() {
    return (
      <ThemeProvider theme={theme}>
        <Page />
      </ThemeProvider>
    )
  }
}

// this assumes the HTML page template has a <main> element already inside <body>
ReactDOM.render(<App />, document.querySelector('main'))

<ThemeProvider> acts as a conductor in the component hierarchy and themed components receive the theme for whatever purposes are necessary, be it styling or perhaps toggling a piece of functionality.

API

ThemeProvider: React.ComponentType

A React component that passes the theme object down the component tree via context. Additional <ThemeProvider> wrappers may be added deeper in the hierarchy to override the original theme. The theme object will be merged into its ancestor as if by Object.assign. If a function is passed instead of an object it will be called with the ancestor theme and the result will be the new theme.

Accepts:

  • children: ReactComponent - A single React component.
  • theme: Object|Function - An object or function that provides an object.
import React from 'react'
import styled from 'react-emotion'
import { ThemeProvider, withTheme } from 'emotion-theming'

// object-style theme

const theme = {
  backgroundColor: 'green',
  color: 'red'
}

// function-style theme; note that if multiple <ThemeProvider> are used,
// the parent theme will be passed as a function argument

const adjustedTheme = ancestorTheme => ({ ...ancestorTheme, color: 'blue' })

class Container extends React.Component {
  render() {
    return (
      <ThemeProvider theme={theme}>
        <ThemeProvider theme={adjustedTheme}>
          <Text>Boom shaka laka!</Text>
        </ThemeProvider>
      </ThemeProvider>
    )
  }
}

const Text = styled.div`
  background-color: ${props => props.theme.backgroundColor}; // will be green
  color: ${props => props.theme.color}; // will be blue
`

withTheme(component: React.ComponentType): React.ComponentType

A higher-order component that provides the current theme as a prop to the wrapped child and listens for changes. If the theme is updated, the child component will be re-rendered accordingly.

import PropTypes from 'prop-types'
import React from 'react'
import { withTheme } from 'emotion-theming'

class TellMeTheColor extends React.Component {
  render() {
    return <div>The color is {this.props.theme.color}.</div>
  }
}

TellMeTheColor.propTypes = {
  theme: PropTypes.shape({
    color: PropTypes.string
  })
}

const TellMeTheColorWithTheme = withTheme(TellMeTheColor)

Credits

Thanks goes to the styled-components team and their contributors who originally wrote this.

License

MIT 2017-present

@3wirebuild/styled-system@cencosud-ds/bigbangreact-ui-components-lib@koombea/authtungtbt-console-feedtailzilla-console-feed@rowsncolumns/spreadsheet@travelshift/webisnoisno-components@mantl/chameleon-sdk@waves.exchange/waves-signerrecipe-doc-site@sachinjadhav/gatsby-theme-blogard-skellig@njradford/libsflowbuilder-test@galley-eng/pricing-calculatoremotion-reproduce-external-lib@pixelmord/prestyledmychakra@trufala/uistreamline-pkg@jason.silvers/tron-uidrpika-first-npm@motive.io/motive-core-ui@motive.io/core-uicarioca-components-emotiongsystemmivo-uikitspm-subeha@recobest/plugin-chart-funneltungtb-console-feed@dloss/coregrandbois@deity/falcon-uielrond-connectdokztestskilvul-web-componentstrova-componentsreact-component-library-gatsbymymoria-uidmove_client_alon-testdmove_client_test2reactivesearchuibrugsdistrodesignsystem-alpha-v2@provablyfair/ui-kit@killerwink/ui-kit@meetsales.io/uiscreenshot-mockuppuil3.0dumi-dux-uibrawl-capped-webpage@mequedev/storybooktestgatsby-theme-utopixgatsby-theme-rayriffy-blog@infinitebrahmanuniverse/nolb-emotnimble-ndsnykaa_web_reloadedcurbo-components-library@academysports/ui-component-library@everything-registry/sub-chunk-1583ossus-componentsnlx-chat-sdknour-emam-button-tsnour-emam-special-buttonnour-emam-special-button1nour-emam-test-testnorwegian-forestnorweigian-forestnib-blurnib-corenib-editor-onblurohbetsyondash-componentspracticestorybookprestein-dantepetto_styleguide@cap3/capitano-components@campj/theme@dering-hall/ui-kitzkit@cda/button@dfds-frontend/react-components@chantelle/design-systemblauncher-app@dloss/harvestland@carecar-ds/chakra-uibrugssearchui-distro@colisweb/bs-chakra-uibuttered-chakra-example@ds-tools/primitives@cobuildlab/boost@dotdev/reactive-rewards@dotdev/reactivesearchbricks-of-sand@codesignal/console-feed@codebrahma/gatsby-theme-blog@codeanit.com/theme@csod-oss/docz-theme
10.3.0

2 years ago

11.0.0

3 years ago

11.0.0-rc.0

4 years ago

10.0.27

4 years ago

11.0.0-next.6

4 years ago

11.0.0-next.5

4 years ago

11.0.0-next.4

4 years ago

11.0.0-next.3

4 years ago

11.0.0-next.2

4 years ago

11.0.0-next.1

4 years ago

11.0.0-next.0

4 years ago

10.0.19

5 years ago

10.0.18

5 years ago

10.0.17

5 years ago

10.0.14

5 years ago

10.0.10

5 years ago

10.0.9

5 years ago

10.0.8

5 years ago

10.0.7

5 years ago

10.0.6

5 years ago

10.0.5

5 years ago

10.0.4

5 years ago

10.0.3

5 years ago

10.0.2

5 years ago

10.0.1

5 years ago

10.0.0

5 years ago

10.0.0-beta.13

5 years ago

10.0.0-beta.12

5 years ago

10.0.0-beta.11

5 years ago

10.0.0-beta.10

5 years ago

10.0.0-beta.8

5 years ago

10.0.0-beta.7

5 years ago

10.0.0-beta.6

6 years ago

10.0.0-beta.5

6 years ago

10.0.0-beta.4

6 years ago

10.0.0-beta.3

6 years ago

10.0.0-beta.2

6 years ago

10.0.0-beta.1

6 years ago

10.0.0-beta.0

6 years ago

9.2.9

6 years ago

9.2.6

6 years ago

9.2.5

6 years ago

9.2.4

6 years ago

9.2.3

6 years ago

9.2.1

6 years ago

9.2.0

6 years ago

9.1.2

6 years ago

9.0.0

6 years ago

9.0.0-1

6 years ago

9.0.0-0

6 years ago

8.0.12

6 years ago

8.0.11

6 years ago

8.0.10

6 years ago

8.0.9

7 years ago

8.0.7

7 years ago

8.0.4

7 years ago

8.0.3

7 years ago

8.0.2

7 years ago

8.0.2-11

7 years ago

8.0.2-10

7 years ago