-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add color indicator component #10209
Changes from 2 commits
2a7db8d
1a63ff0
872caac
d4a2f20
f70f9e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react' | ||
import classnames from 'classnames' | ||
import PropTypes from 'prop-types' | ||
import { COLORS } from '../../../helpers/constants/design-system' | ||
|
||
export default function ColorIndicator({ | ||
size = 'small', | ||
type = 'outlined', | ||
color = COLORS.UI4, | ||
borderColor, | ||
iconClassName, | ||
}) { | ||
const colorIndicatorClassName = classnames('color-indicator', { | ||
'color-indicator--filled': type === 'filled' || Boolean(iconClassName), | ||
'color-indicator--partial-filled': type === 'partial-filled', | ||
[`color-indicator--border-color-${borderColor}`]: Boolean(borderColor), | ||
[`color-indicator--color-${color}`]: true, | ||
[`color-indicator--size-${size}`]: true, | ||
}) | ||
|
||
return ( | ||
<div className={colorIndicatorClassName}> | ||
{iconClassName ? ( | ||
<i className={classnames('color-indicator__icon', iconClassName)} /> | ||
) : ( | ||
<span className="color-indicator__inner-circle" /> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
ColorIndicator.propTypes = { | ||
color: PropTypes.oneOf(Object.values(COLORS)), | ||
borderColor: PropTypes.oneOf(Object.values(COLORS)), | ||
size: PropTypes.oneOf(['small', 'medium', 'large']), | ||
iconClassName: PropTypes.string, | ||
type: PropTypes.oneOf(['filled', 'partial-filled', 'outline']), | ||
} | ||
|
||
ColorIndicator.TYPES = { | ||
FILLED: 'filled', | ||
PARTIAL: 'partial-filled', | ||
OUTLINE: 'outline', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
@use "utilities"; | ||
@use "design-system"; | ||
|
||
$sizes: ( | ||
'large': 6, | ||
'medium': 5, | ||
'small': 4, | ||
); | ||
|
||
.color-indicator { | ||
$self: &; | ||
|
||
border: 1px solid transparent; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
|
||
&__inner-circle { | ||
background-color: transparent; | ||
} | ||
|
||
@each $variant, $size in $sizes { | ||
&--size-#{$variant} { | ||
height: #{2 * $size}px; | ||
width: #{2 * $size}px; | ||
border-radius: #{$size}px; | ||
|
||
#{$self}__inner-circle { | ||
border-radius: #{$size}px; | ||
height: #{$size}px; | ||
width: #{$size}px; | ||
} | ||
|
||
#{$self}__icon { | ||
font-size: #{1.25 * $size}px; | ||
} | ||
} | ||
} | ||
|
||
@each $variant, $color in design-system.$color-map { | ||
&--color-#{$variant} { | ||
border-color: $color; | ||
&#{$self}--partial-filled #{$self}__inner-circle { | ||
background-color: $color; | ||
} | ||
&#{$self}--filled { | ||
background-color: $color; | ||
} | ||
& #{$self}__icon { | ||
color: #{utilities.choose-contrast-color($color)}; | ||
} | ||
} | ||
} | ||
|
||
// separate iterator to ensure borderColor takes precedence | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When testing this locally in storybook, it seems that this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, no, it's only broken when I set the border colour to one of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see - there's a mistake in the design system constants. I can fix that in a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@each $variant, $color in design-system.$color-map { | ||
&--border-color-#{$variant} { | ||
border-color: $color; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
import { select } from '@storybook/addon-knobs' | ||
import { COLORS } from '../../../helpers/constants/design-system' | ||
import ColorIndicator from './color-indicator' | ||
|
||
export default { | ||
title: 'ColorIndicator', | ||
} | ||
|
||
export const colorIndicator = () => ( | ||
<ColorIndicator | ||
size={select( | ||
'size', | ||
{ large: 'large', medium: 'medium', small: 'small' }, | ||
'large', | ||
)} | ||
type={select('type', ColorIndicator.TYPES, ColorIndicator.TYPES.FILLED)} | ||
color={select('color', COLORS, COLORS.PRIMARY1)} | ||
borderColor={select('borderColor', { NONE: undefined, ...COLORS })} | ||
/> | ||
) | ||
|
||
export const withIcon = () => ( | ||
<ColorIndicator | ||
size={select( | ||
'size', | ||
{ large: 'large', medium: 'medium', small: 'small' }, | ||
'large', | ||
)} | ||
type={select('type', ColorIndicator.TYPES, ColorIndicator.TYPES.FILLED)} | ||
color={select('color', COLORS, COLORS.PRIMARY1)} | ||
iconClassName="fa fa-question" | ||
borderColor={select('borderColor', { NONE: undefined, ...COLORS })} | ||
/> | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './color-indicator' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
@use "sass:math"; | ||
@use "sass:map"; | ||
|
||
/** | ||
* Calculate the luminance for a color. | ||
* See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests | ||
*/ | ||
@function luminance($color) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, this is a pretty complex function! I did some Google-ing about this and stumbled across this interesting post: https://css-tricks.com/programming-sass-to-create-accessible-color-combinations/
But they managed to solve it more performantly in the end using a lookup table. Not saying we should optimize this now before it becomes a problem, but... this is something to consider if we notice a slowdown. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm going to leave a note in the code about this. I think that the expensive part of the operation was the custom There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah neat, TIL. |
||
$channels: ( | ||
'red': red($color), | ||
'green': green($color), | ||
'blue': blue($color), | ||
); | ||
|
||
$values: ( | ||
'red': map.get($channels, 'red') / 255, | ||
'blue': map.get($channels, 'red') / 255, | ||
'green': map.get($channels, 'red') / 255, | ||
); | ||
|
||
@each $name, $value in $values { | ||
@if $value < 0.03928 { | ||
brad-decker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$value: $value / 12.92; | ||
} | ||
|
||
@else { | ||
$value: ($value + 0.055) / 1.055; | ||
$value: math.pow($value, 2.4); | ||
} | ||
|
||
$values: map.merge($values, ($name: $value)); | ||
} | ||
|
||
@return (0.2126 * map.get($values, 'red')) | ||
+ (0.7152 * map.get($values, 'green')) | ||
+ (0.0722 * map.get($values, 'blue')); | ||
} | ||
|
||
/** | ||
* Calculate the contrast ratio between two colors. | ||
* See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests | ||
*/ | ||
@function contrast($back, $front) { | ||
$backLum: luminance($back) + 0.05; | ||
$foreLum: luminance($front) + 0.05; | ||
|
||
@return max($backLum, $foreLum) / min($backLum, $foreLum); | ||
} | ||
|
||
/** | ||
* Determine whether to use dark or light text on top of given color. | ||
* Returns black for dark text and white for light text. | ||
*/ | ||
@function choose-contrast-color($color) { | ||
$lightContrast: contrast($color, white); | ||
$darkContrast: contrast($color, black); | ||
|
||
@if ($lightContrast > $darkContrast) { | ||
@return white; | ||
} | ||
|
||
@else { | ||
@return black; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@forward 'colors'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It'd be nice to use constants for these as well, and for the
type
PropType declaration.