1
1
mirror of https://github.com/primer/css.git synced 2024-11-30 19:53:11 +03:00
css/docs/color-system.js

185 lines
5.5 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
import chroma from 'chroma-js'
2018-12-06 03:31:36 +03:00
import colors from 'primer-colors'
import titleCase from 'title-case'
2018-12-06 03:31:36 +03:00
import {BorderBox, Box, Flex, Heading, Text} from '@primer/components'
const gradientHues = ['gray', 'blue', 'green', 'purple', 'yellow', 'orange', 'red']
const {black: BLACK, white: WHITE} = colors
2018-12-06 03:31:36 +03:00
export function ColorPalette(props) {
return (
<Flex mb={6} className="markdown-no-margin" {...props}>
{gradientHues.map(hue => {
const color = colors[hue][5]
return (
<Box bg={color} p={3} width={200} mr={2} key={hue}>
<Text fontWeight="bold" color={overlayColor(color)}>
{titleCase(hue)}
</Text>
</Box>
)
})}
<BorderBox bg="white" p={3} width={200} borderRadius={0}>
<Text fontWeight="bold" color="black">
White
</Text>
2018-12-06 03:31:36 +03:00
</BorderBox>
</Flex>
)
}
export function ColorVariables(props) {
return (
<>
<Flex flexWrap="wrap" className="gutter" {...props}>
2018-12-06 03:31:36 +03:00
{gradientHues.map(hue => (
<ColorVariable id={hue} hue={hue} key={hue} />
2018-12-06 03:31:36 +03:00
))}
</Flex>
<Flex flexWrap="wrap" {...props}>
<FadeVariables id="black" hue="black" bg="black" color="white">
2018-12-06 23:10:41 +03:00
<BorderBox border={0} borderRadius={0} borderTop={1} borderColor="gray.5" mt={1}>
<Text is="div" fontSize={2} pt={3} mb={0}>
2018-12-06 03:31:36 +03:00
Black fades apply alpha transparency to the <Var>$black</Var> variable. The black color value has a slight
blue hue to match our grays.
</Text>
</BorderBox>
</FadeVariables>
<FadeVariables id="white" hue="white" over={BLACK}>
2018-12-06 23:10:41 +03:00
<BorderBox border={0} borderRadius={0} borderTop={1} mt={1}>
<Text is="div" fontSize={2} pt={3} mb={0}>
2018-12-06 03:31:36 +03:00
White fades apply alpha transparency to the <Var>$white</Var> variable, below these are shown overlaid on
a dark gray background.
</Text>
</BorderBox>
</FadeVariables>
</Flex>
</>
)
}
export function ColorVariable({hue, ...rest}) {
const values = colors[hue]
return (
<Flex.Item is={Box} pr={4} mb={6} className="col-6 markdown-no-margin" {...rest}>
{/* <Heading is="div">{titleCase(hue)}</Heading> */}
2018-12-06 03:31:36 +03:00
<Box bg={`${hue}.5`} my={2} p={3} color="white">
<Heading is="div" pb={3} fontSize={56} fontWeight="light">
{titleCase(hue)}
2018-12-06 03:31:36 +03:00
</Heading>
<Flex justifyContent="space-between">
<Flex.Item flex="1 1 auto" is={Var}>
2018-12-06 03:31:36 +03:00
${hue}-500
</Flex.Item>
<Text fontFamily="mono">{values[5]}</Text>
</Flex>
</Box>
{values.map((value, i) => (
<Swatch name={`${hue}-${i}00`} value={value} key={value} />
))}
</Flex.Item>
)
}
ColorVariable.propTypes = {
hue: PropTypes.oneOf(Object.keys(colors)).isRequired
}
2018-12-06 03:31:36 +03:00
export function FadeVariables({hue, color, bg, over, children, ...rest}) {
const colorValue = colors[hue]
const alphas = [15, 30, 50, 70, 85]
const values = alphas.map(alpha => {
2018-12-08 01:36:38 +03:00
const value = chroma(colorValue)
.alpha(alpha / 100)
.css()
return {
name: `${hue}-fade-${alpha}`,
textColor: fadeTextColor(value, over),
value
}
})
2018-12-06 03:31:36 +03:00
const boxProps = {color, bg}
return (
<Flex.Item is={Box} pr={4} mb={6} width={1 / 2} className="markdown-no-margin" {...rest}>
{/* <Heading is="div">{titleCase(hue)}</Heading> */}
2018-12-06 03:31:36 +03:00
<Box my={2} p={3} {...boxProps}>
<Heading is="div" pb={3} fontSize={56} fontWeight="light">
{titleCase(hue)}
2018-12-06 03:31:36 +03:00
</Heading>
<Flex justifyContent="space-between">
<Flex.Item flex="1 1 auto" is={Var}>
2018-12-06 03:31:36 +03:00
${hue}
</Flex.Item>
<Text fontFamily="mono">
{chroma(colorValue).css()}
2018-12-06 23:10:41 +03:00
{' / '}
{colorValue}
2018-12-06 03:31:36 +03:00
</Text>
</Flex>
{children}
</Box>
<Box bg={over}>
{values.map(swatchProps => (
<Swatch {...swatchProps} key={swatchProps.name} />
2018-12-06 03:31:36 +03:00
))}
</Box>
</Flex.Item>
)
}
FadeVariables.propTypes = {
bg: Box.propTypes.color,
color: Box.propTypes.color,
hue: PropTypes.oneOf(['black', 'white']),
over: PropTypes.string
}
function Swatch(props) {
const {name, value, textColor = overlayColor(value), ...rest} = props
2018-12-06 03:31:36 +03:00
return (
2019-01-18 03:04:14 +03:00
<Box bg={value} color={textColor} {...rest}>
<Text is={Flex} fontSize={1} justifyContent="space-between">
2018-12-06 03:31:36 +03:00
<Box p={3}>
2019-01-18 03:04:14 +03:00
<Var>${name}</Var>
2018-12-06 03:31:36 +03:00
</Box>
<Box p={3}>
2019-01-18 03:04:14 +03:00
<Text fontFamily="mono">{value}</Text>
2018-12-06 03:31:36 +03:00
</Box>
</Text>
</Box>
)
}
Swatch.propTypes = {
name: PropTypes.string.isRequired,
textColor: PropTypes.string,
value: PropTypes.string.isRequired
}
2018-12-06 03:31:36 +03:00
function Var(props) {
// FIXME: fontStyle should be a prop, right?
return <Text is="var" fontWeight="bold" fontFamily="mono" style={{fontStyle: 'normal'}} {...props} />
}
function overlayColor(bg) {
2018-12-08 01:36:38 +03:00
return chroma(bg).luminance() > 0.5 ? BLACK : WHITE
}
function fadeTextColor(fg, bg = WHITE) {
const rgb = compositeRGB(fg, bg)
return overlayColor(rgb)
}
/**
* Composite ("flatten") a foreground RGBA value with an RGB background into an
* RGB color for the purposes of measuring contrast or luminance.
*/
function compositeRGB(foreground, background) {
const [fr, fg, fb, fa] = chroma(foreground).rgba()
const [br, bg, bb] = chroma(background).rgb()
2018-12-08 01:36:38 +03:00
return chroma([(1 - fa) * br + fa * fr, (1 - fa) * bg + fa * fg, (1 - fa) * bb + fa * fb]).css()
2018-12-06 03:31:36 +03:00
}