Giter Site home page Giter Site logo

diegohaz / styled-theme Goto Github PK

View Code? Open in Web Editor NEW
182.0 2.0 8.0 108 KB

Extensible theming system for styled-components πŸ’…

Home Page: https://diegohaz.github.io/styled-theme

License: MIT License

JavaScript 100.00%
styled-components palette theme font react sizes css-in-js

styled-theme's Introduction

styled-theme πŸ’…πŸΏ

Generated with nod NPM version Build Status Coverage Status

Theming system for styled-components πŸ’…

Install

$ npm install --save styled-theme

Usage

Play with it on WebpackBin

import styled from 'styled-components'
import { font, palette } from 'styled-theme' 

const Text = styled.span`
  font-family: ${font('primary')};
  background-color: ${palette(1)};
  color: ${palette('grayscale', 0, true)};
`

Text.defaultProps = {
  palette: 'primary'
}
<Text>Hello</Text>

image

<Text reverse>Hello</Text>

image

<Text palette="secondary">Hello</Text>

image

Provide your own theme

import { ThemeProvider } from 'styled-components'

const xmasTheme = {
  fonts: {
    primary: 'Georgia, serif'
  },
  palette: {
    // red gradient
    primary: ['#D32F2F', '#F44336', '#F8877F', '#FFCDD2']
  }
}

<ThemeProvider theme={xmasTheme}>
  <Text>Hello</Text>
</ThemeProvider>

image

Default theme structure

This is the content of src/theme.js:

import coolorsToHex from 'coolors-to-hex'
import { reversePalette } from './composer'

const theme = {}

theme.palette = {
  primary: coolorsToHex('https://coolors.co/1976d2-2196f3-71bcf7-97cef9-c2e2fb'),
  secondary: coolorsToHex('https://coolors.co/c2185b-e91e63-f06292-f48caf-f8bbd0'),
  danger: coolorsToHex('https://coolors.co/d32f2f-f44336-f8877f-f9a7a1-ffcdd2'),
  alert: coolorsToHex('https://coolors.co/ffa000-ffc107-ffd761-ffecb3-fff2ce'),
  success: coolorsToHex('https://coolors.co/388e3c-4caf50-7cc47f-9fd4a1-c8e6c9'),
  grayscale: ['#212121', '#616161', '#9e9e9e', '#bdbdbd', '#e0e0e0', '#ffffff']
}

theme.reversePalette = reversePalette(theme.palette)

theme.fonts = {
  primary: 'Helvetica Neue, Helvetica, Roboto, sans-serif',
  pre: 'Consolas, Liberation Mono, Menlo, Courier, monospace',
  quote: 'Georgia, serif'
}

theme.sizes = {
  maxWidth: '1100px'
}

export default theme

reversePalette is a helper method. Import it from styled-theme/composer.

API

reversePalette

Revert the palette

Parameters

Examples

reversePalette({ primary: ['red', 'yellow', 'green'] })
// { primary: ['green', 'yellow', 'red'] }

Returns Palette

key

Returns the value of props.theme[path] or styledTheme[path]

Parameters

Examples

const Button = styled.button`
 font-family: ${key('fonts.primary')};
 color: ${key(['colors', 'primary', 0])};
`

Returns any

font

Shorthand to key(['fonts', path])

Parameters

  • path string
  • defaultValue any

Examples

const Button = styled.button`
 font-family: ${font('primary')};
`

Returns Font

size

Shorthand to key(['sizes', path])

Parameters

  • path string
  • defaultValue any

Examples

const Button = styled.button`
 padding: ${size('padding')};
`

Returns Size

palette

Returns the value of props.theme[palette || reversePalette][path][index] or styledTheme[palette || reversePalette][path][index] (default theme)

The arguments can be passed in any order, as long as types are kept.

Parameters

  • index number The index of tone in theme palette tones array
  • path string? The key of the tones in theme palette object (optional, default props.palette)
  • exceptions Object? An object with path as key and index as value
  • reverse boolean? Flag to return tone from reversePalette or palette
  • defaultValue string? Default value
  • args ...any

Examples

// index = 1
// exception = { grayscale: 0 }
// reverse = true
const Button = styled.button`
 background-color: ${palette({ grayscale: 0 }, 1, true)};
`

// renders props.theme.reversePalette.grayscale[0]
<Button palette="grayscale" />

// renders props.theme.palette.danger[1] (nullify reverse)
<Button palette="danger" reverse />

Returns Tones

Tone

Type: string

Tones

Type: Array<Tone>

Font

Type: string

Size

Type: string

Palette

Type: {}

Fonts

Type: {}

Sizes

Type: {}

Theme

Type: {palette: Palette?, reversePalette: Palette?, fonts: Fonts?, sizes: Sizes?}

Related

  • styled-tools - Utilities for styled-components (like lodash)

License

MIT Β© Diego Haz

styled-theme's People

Contributors

diegohaz avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

styled-theme's Issues

Typescript is not supported!

It seems typescript is not supported yet.

I had to declare the additional module to get it working with typescript.

declare module 'styled-theme'

Is there any way to get it working properly?

theme.sizes question

does the theme.sizes support other sizes for example

theme.sizes = {
  maxWidth: '1200px',
  font: { tiny: '0.750rem', small: '0.875rem', normal: '1.000rem', large: '1.250rem', xlarge: '1.375rem', huge: '2.000rem' },
  radius: '4px',
  height: '2.40em',
  lineHeight: 1.1,
}

Palette color by index

Is there way to select palette color by index?

// my theme
theme.palette = {
  primary: [`#08C4FA`, `#34CEFA`, `#61D9FB`, `#8EE4FC`, `#BBEEFD`]
}

const Span = styled.span`
  color: ${palette({grayscale: 0}, 0)};
  font-family: ${font(`primary`)};
  font-size: ${prop(`size`, `14px`)};
`
<Span palette={{primary: 2}}>Text</Span>
// or
<Span palette="primary" tone={2}>Text</Span>

An in-range update of eslint-plugin-import is breaking the build 🚨

Version 2.4.0 of eslint-plugin-import just got published.

Branch Build failing 🚨
Dependency eslint-plugin-import
Current Version 2.3.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As eslint-plugin-import is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Commits

The new version differs by 10 commits.

  • 44ca158 update utils changelog
  • a3728d7 bump eslint-module-utils to v2.1.0
  • 3e29169 bump v2.4.0
  • ea9c92c Merge pull request #737 from kevin940726/master
  • 8f9b403 fix typos, enforce type of array of strings in allow option
  • 95315e0 update CHANGELOG.md
  • 28e1623 eslint-module-utils: filePath in parserOptions (#840)
  • 2f690b4 update CI to build on Node 6+7 (#846)
  • 7d41745 write doc, add two more tests
  • dedfb11 add allow glob for rule no-unassigned-import, fix #671

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Typescript Support

Is there a plan support for Typescript ?

I wasn't able to find definition types

An in-range update of eslint-config-airbnb-base is breaking the build 🚨

Version 11.1.2 of eslint-config-airbnb-base just got published.

Branch Build failing 🚨
Dependency eslint-config-airbnb-base
Current Version 11.1.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As eslint-config-airbnb-base is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ


Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Nested calls doesn't return the value

When I use one function inside another the font size doesn't return properly:

font-size: ${size(prop('fontSize'))};

But when I use only one of the functions the return is correct:

font-size: ${({ fontSize }) => key(['sizes', fontSize])};

This is the intended way to use the lib or its some kind of bug? I analyzed the return from prop function and see that is a function, can this be the problem that can't be used in nested calls?

An in-range update of babel-eslint is breaking the build 🚨

Version 7.2.1 of babel-eslint just got published.

Branch Build failing 🚨
Dependency babel-eslint
Current Version 7.2.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As babel-eslint is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ


Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Commits

The new version differs by 6 commits .

  • 3cda62e 7.2.1
  • 5626de1 Remove left over eslint 2 estraverse code (#452)
  • b5fb53b Fix type param and interface declaration scoping (#449)
  • f1cee0f Remove lodash dependency (#450)
  • eb05812 Format non-regression errors for legibility (#451)
  • 7972a05 Update README.md with codeFrame option (#448)

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of eslint-config-airbnb-base is breaking the build 🚨

Version 11.2.0 of eslint-config-airbnb-base just got published.

Branch Build failing 🚨
Dependency eslint-config-airbnb-base
Current Version 11.1.3
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As eslint-config-airbnb-base is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of babel-preset-env is breaking the build 🚨

Version 1.3.0 of babel-preset-env just got published.

Branch Build failing 🚨
Dependency babel-preset-env
Current Version 1.2.2
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As babel-preset-env is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ


Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details
Release Notes v1.3.0

v1.3.0 (2017-03-30)

πŸ› Bug Fix

We now properly check for Symbol.species support in ArrayBuffer and include the
polyfill if necessary. This should, as a side effect, fix ArrayBuffer-related
errors on IE9.

πŸ’… Polish

We've simplified things by adding electron as a target instead of doing a bunch of
things at runtime. Electron targets should now also be displayed in the debug output.

If you are targeting the node environment exclusively, the always-included web polyfills
(like dom.iterable, and a few others) will now no longer be included.

πŸ“ Documentation

🏠 Internal

  • npmignore: Add related to build data and codecov. (#216) (@yavorsky)
Commits

The new version differs by 8 commits .

  • 8b2dc4f 1.3.0
  • 6ebf857 Update changelog
  • 046f326 Add check for ArrayBuffer[Symbol.species] (#233)
  • aead61c Fill data with electron as a target. (#229)
  • 48a329b separate default builtins for platforms (#226)
  • a4d585c remove deprecated projects (#223) [skip ci]
  • 88cbe17 Merge pull request #216 from babel/update-npmignore
  • cf94af3 npmignore: Add related to build data and codecov.

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Convert to string

Is there a way to convert a palette() to a string for use with styled-components/polished?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.