npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jfrolich/emotion-theming

v9.2.14

Published

A CSS-in-JS theming solution, inspired by styled-components

Downloads

2

Readme

emotion-theming

A CSS-in-JS theming solution for React(-like) views.

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

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 react-emotion's 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 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: ReactComponent

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: ReactComponent): Function

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)

channel: String

The emotion-theming package uses this string as the React context key by default.

If you wish to build your own components on top of this library, it is recommended to import the context key from this package instead of hardcoding its value.

import { channel } from 'emotion-theming'

console.log(channel)
;('__EMOTION_THEMING__')

createBroadcast: Function

Creates a broadcast that is used to listen to theme events via context. This is probably only useful for testing. If you have styled components that depend on theme via ThemeProvider, one option is to wrap all your components being tested in ThemeProvider. However if you're using enzyme, you'll lose the ability to call some methods that require it to be run on the root instance. Instead you can mount a component a pass the channel and broadcast as part of context.

import { channel, createBroadcast } from 'emotion-theming'
import { mount } from 'enzyme'
import PropTypes from 'prop-types'
import React from 'react'

describe('emotion-theming', function() {
  it('can use theme from a ThemeProvider', function() {
    const myTheme = { color: 'green' }
    const broadcast = createBroadcast(myTheme)
    const wrapper = mount(<MyComponent />, {
      context: {
        [channel]: broadcast
      },
      childContextTypes: {
        [channel]: PropTypes.object
      }
    })

    wrapper.setState({ foo: 'bar' })
    expect(wrapper.state('foo')).toBe('bar')
  })
})

Credits

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

License

MIT 2017-present