styled theme
1.0.0
スタイルのコンポーネントのテーマシステム?
$ npm install --save styled-theme
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 > < Text reverse > Hello </ Text > < Text palette = "secondary" > Hello </ Text > 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>これは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はヘルパーメソッドです。 styled-theme/composerからインポートします。
パレットを元に戻します
パラメーター
paletteパレット例
reversePalette ( { primary : [ 'red' , 'yellow' , 'green' ] } )
// { primary: ['green', 'yellow', 'red'] }パレットを返します
props.theme[path]の価値を返しますstyledTheme[path]
パラメーター
path (文字列|配列<String>)defaultValue任意例
const Button = styled . button `
font-family: ${ key ( 'fonts.primary' ) } ;
color: ${ key ( [ 'colors' , 'primary' , 0 ] ) } ;
`任意のものを返します
key(['fonts', path])
パラメーター
path文字列defaultValue任意例
const Button = styled . button `
font-family: ${ font ( 'primary' ) } ;
`フォントを返します
key(['sizes', path])
パラメーター
path文字列defaultValue任意例
const Button = styled . button `
padding: ${ size ( 'padding' ) } ;
`サイズを返します
props.theme[palette || reversePalette][path][index]の値を返しますprops.theme[palette || reversePalette][path][index]またはstyledTheme[palette || reversePalette][path][index] (デフォルトテーマ)
タイプが保持される限り、引数は任意の順序で渡すことができます。
パラメーター
index番号テーマパレットトーンアレイのトーンのインデックスpath文字列?テーマパレットオブジェクトのトーンのキー(オプション、デフォルトのprops.palette )exceptionsオブジェクト?キーとしてのパスと値としてのインデックスを持つオブジェクトreverseブール? reversePaletteまたはpaletteからトーンを返すフラグdefaultValue文字列?デフォルト値args ...任意例
// 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 />トーンを返します
タイプ:文字列
タイプ:配列<トーン>
タイプ:文字列
タイプ:文字列
タイプ: {}
タイプ: {}
タイプ: {}
タイプ:{パレット:パレット?、リバースパレット:パレット?、フォント:フォント?、サイズ:サイズ?}
MIT©Diego Haz