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]或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 />返回音調
類型:字符串
類型:數組<TONTH>
類型:字符串
類型:字符串
類型: {}
類型: {}
類型: {}
類型:{調色板:調色板?
麻省理工學院©Diego Haz