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

react-accent-color

v0.1.1

Published

Accent color management for React components

Downloads

4

Readme

Accent color palette for React

Build Status npm version

Inspired by UWP color design, with light and dark theme.

Introduction

Theming is tricky.

We believe designers should have ultimate controls on overall design, like margin, padding, and roundness. But sometimes, they may want to delegate some customizations to web developers, like accent color. We made this library to make the job a little bit easier.

This library is designed to play nice with glamor and Redux.

How to use

Alternatively, you can find our demo at react-accent-color-testbed repository.

Do npm install react-accent-color --save.

Then, in your code:

Hoist colors to props using withPalette()

Like Redux connect(), we use Higher-Order Components pattern to hoist colors to props. So you are in control of which colors should be hoisted.

In this example, accent color is extracted from withPalette() and passed into the component as fillColor prop.

You can see list of colors in the palette here.

import React from 'react';

class MyButton extends React.Component {
  render() {
    return (
      <button style={{ backgroundColor: this.props.fillColor }}>
        { this.props.children }
      </button>
    );
  }
}

export default withPalette(({ palette }) => ({
  fillColor: palette.accent
}))(MyButton)

Tips: You can also hoist on stateless component.

Add <PaletteProvider> to the root of your app

Select your accent color and theme, then wrap your app with <PaletteProvider>.

See list of accent colors for your project from UWP color design article.

import React               from 'react';
import { PaletteProvider } from 'react-prime-ui';

import MyButton from './UI/MyButton';

export default class App extends React.Component {
  render() {
    return (
      <PaletteProvider accent="#0078D7" theme="light">
        <MyButton>Woohoo!</MyButton>
      </PaletteProvider>
    )
  }
}

You can choose between "light" or "dark" theme. We prefer it "light", just like GitHub.

What's next?

Now you can start using accent colors in your app and components. There are few things you will want to try out.

Overriding color for some components

If you provide accent or theme props to the hoisted component, you can override the accent color provided from <PaletteProvider>.

The following example added accent="#E81123" to <MyButton> and will fill it red.

<PaletteProvider accent="#0078D7" theme="light">
  <MyButton accent="#E81123">I am red, instead of blue.</MyButton>
</PaletteProvider>

Tips: You can also override accent color by adding another layer of <PaletteProvider>.

Mixing colors with props

In addition to our predefined palette, you can also create new colors from props.

In this example, we set the opacity of the background color by using color and varied by passing an opacity prop.

import color from 'color';

// ...

export default withPalette(({ palette }, props) => ({
  fillColor: color(palette.accent).alpha(props.opacity)
}))(MyButton)

And in your app,

<PaletteProvider accent="#0078D7" theme="light">
  <MyButton opacity={ 0.5 }>I am transparent</MyButton>
</PaletteProvider>

Creating your color based on theme

In addition to the color palette, you can also create your own colors using theme from <PaletteProvider>.

export default withPalette(({ palette, theme }, props) => ({
  fillColor: theme === 'light' ? palette.accentDark1 : palette.accentLight1
}))(MyButton)

Non-color customization

In addition to palette, all props from <PaletteProvider> are passed as first argument to your props factory. In the following example, we are passing roundness of 0.5 to <PaletteProvider> and it will be piped to withPalette().

export default withPalette(({ roundness }) => ({
  borderRadius: 10 * roundness
}))(MyButton)

And in your app,

<PaletteProvider roundness={ 0.5 }>
  <MyButton>This button is round.</MyButton>
</PaletteProvider>

Tips: You should always consult your designer before customizing any UI designs, they may have designed it intentionally. Don't over-customize!

Working with glamor

react-accent-color is designed to play nice with glamor.

Instead of using style props, we can use glamor to create CSS style when accent color has updated.

import React   from 'react';
import { css } from 'glamor';

const createCSS = palette => css({
  backgroundColor: palette.backgroundColor
});

class MyButton extends React.Component {
  render() {
    return (
      <button { ...this.props.css }>
        { this.props.children }
      </button>
    );
  }
}

export default withPalette(({ palette }) => ({
  css: createCSS(palette)
}))(MyButton)

Making it render fast with glamor

Tips: glamor.css() calls should be done outside of render() and avoid calling it more than you need.

The factory function passed to withPalette will be called when:

  • Any props on <PaletteProvider> has changed
  • Any props on your component has changed

This could means, every time a prop on MyButton has changed, we will call glamor.css(). If the props are updated but not leading to any visible change, it could be saved to improve performance.

You can use a memoizer to call css() only when there are "meaningful" changes, i.e. changes that would lead to style update. In the following example, css() will only be called when either palette.accent or props.opacity has changed.

For your convenience, we exported our shallow memoizer with FIFO = 1, inspired by reselect.

import { memoize } from 'react-accent-color';

const createCSS = memoize((accent, opacity) => css({
  backgroundColor: color(accent).alpha(opacity)
}));

export default withPalette(({ palette }, props) => ({
  css: createCSS(palette.accent, props.opacity)
}))(MyButton)

My component has already connect-ed with Redux

No worries. HOC pattern is designed to play nice with each other, like connect() from Redux.

export default connect(state => ({
  name: state.userProfile.name
}))(withPalette(({ palette }) => ({
  fillColor: palette.accent
}))(MyButton))

Tips: You can also pass accent prop from connect() to make your accent color reactive to Redux.

Colors

We follow UWP color design and exposed the following colors:

| Color name | Light theme | Dark theme | | - | - | - | | accentDark1 | { accentColor.darken(.2) } | (Same as light) | | accentDark2 | { accentColor.darken(.4) } | (Same as light) | | accentDark3 | { accentColor.darken(.6) } | (Same as light) | | accentLight1 | { accentColor.lighten(.2) } | (Same as light) | | accentLight2 | { accentColor.lighten(.4) } | (Same as light) | | accentLight3 | { accentColor.lighten(.6) } | (Same as light) | | background | #FFF | #000 | | foreground | #000 | #FFF | | baseLow | rgba(0, 0, 0, .2) | rgba(255, 255, 255, .2) | | baseMediumLow | rgba(0, 0, 0, .4) | rgba(255, 255, 255, .4) | | baseMedium | rgba(0, 0, 0, .6) | rgba(255, 255, 255, .6) | | baseMediumHigh | rgba(0, 0, 0, .8) | rgba(255, 255, 255, .8) | | baseHigh | #000 | #FFF | | altLow | rgba(255, 255, 255, .2) | rgba(0, 0, 0, .2) | | altMediumLow | rgba(255, 255, 255, .4) | rgba(0, 0, 0, .4) | | altMedium | rgba(255, 255, 255, .6) | rgba(0, 0, 0, .6) | | altMediumHigh | rgba(255, 255, 255, .8) | rgba(0, 0, 0, .8) | | altHigh | #FFF | #000 | | listLow | rgba(0, 0, 0, .1) | rgba(255, 255, 255, .1) | | listMedium | rgba(0, 0, 0, .2) | rgba(255, 255, 255, .2) | | listAccentLow | { accentColor.fade(.4) } | (Same as light) | | listAccentMedium | { accentColor.fade(.6) } | (Same as light) | | listAccentHigh | { accentColor.fade(.7) } | (Same as light) | | chromeLow | #F2F2F2 | #171717 | | chromeMediumLow | #F2F2F2 | #2B2B2B | | chromeMedium | #E6E6E6 | #1F1F1F | | chromeHigh | #CCC | #767676 | | chromeAltLow | #171717 | #F2F2F2 | | chromeDisabledLow | #7A7A7A | #858585 | | chromeDisabledHigh | #CCC | #333 | | chromeBlackLow | rgba(0, 0, 0, .2) | (Same as light) | | chromeBlackMediumLow | rgba(0, 0, 0, .4) | (Same as light) | | chromeBlackMedium | rgba(0, 0, 0, .8) | (Same as light) | | chromeBlackHigh | #000 | (Same as light) | | chromeWhite | #FFF | (Same as light) | | primaryText | (Same as baseHigh) | (Same as baseHigh) | | secondaryText | (Same as baseMedium) | (Same as baseMedium) | | disabledUI | (Same as baseMediumLow) | (Same as baseMediumLow) |

Foreground color

Finding the right foreground color can be tricky because the fill color can be too bright for white text, and vice versa. What's more, for accessibility, it should have contrast ratio of 4.5:1.

We provide foreground colors from palette.textOn maps. For example, to get the foreground color for listLow color, you can get it from palette.textOn.listLow. For example,

export default withPalette(({ palette }) => ({
  fillColor: palette.listLow,
  color    : palette.textOn.listLow
}))(MyButton)

The foreground color is by calculating if the fill color is "dark", using the following algorithm (inspired from UWP):

function isDark(color) {
  return color.green() * 5 + color.red() * 2 + color.blue() <= 8 * 128;
}

Foreground color is either "white" or "black".

Contribution

Like us? Star us.

Doesn't like something? File us an issue.