Skip to content
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

Merged
merged 5 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/app/components/app/account-menu/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
}

&__check-mark-icon {
background-image: url("images/check-white.svg");
background-image: url("/images/check-white.svg");
height: 18px;
width: 18px;
background-repeat: no-repeat;
Expand Down
44 changes: 44 additions & 0 deletions ui/app/components/ui/color-indicator/color-indicator.js
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']),
Copy link
Member

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.

iconClassName: PropTypes.string,
type: PropTypes.oneOf(['filled', 'partial-filled', 'outline']),
}

ColorIndicator.TYPES = {
FILLED: 'filled',
PARTIAL: 'partial-filled',
OUTLINE: 'outline',
}
61 changes: 61 additions & 0 deletions ui/app/components/ui/color-indicator/color-indicator.scss
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When testing this locally in storybook, it seems that this border-color never actually has precedence. I'm just seeing the color-indicator--color-{variant} border color 🤔

Copy link
Member

Choose a reason for hiding this comment

The 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 ERROR colours.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
}
35 changes: 35 additions & 0 deletions ui/app/components/ui/color-indicator/color-indicator.stories.js
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 })}
/>
)
1 change: 1 addition & 0 deletions ui/app/components/ui/color-indicator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './color-indicator'
1 change: 1 addition & 0 deletions ui/app/components/ui/ui-components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@import 'check-box/index';
@import 'chip/chip';
@import 'circle-icon/index';
@import 'color-indicator/color-indicator';
@import 'currency-display/index';
@import 'currency-input/index';
@import 'dialog/dialog';
Expand Down
20 changes: 10 additions & 10 deletions ui/app/css/design-system/typography.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$fa-font-path: 'fonts/fontawesome';
$fa-font-path: '/fonts/fontawesome';

@import '../../../../node_modules/@fortawesome/fontawesome-free/scss/fontawesome';
@import '../../../../node_modules/@fortawesome/fontawesome-free/scss/solid';
Expand All @@ -8,63 +8,63 @@ $fa-font-path: 'fonts/fontawesome';
font-family: 'Roboto';
font-style: normal;
font-weight: 100;
src: local('Roboto Thin'), local('Roboto-Thin'), url('fonts/Roboto/Roboto-Thin.ttf') format('truetype');
src: local('Roboto Thin'), local('Roboto-Thin'), url('/fonts/Roboto/Roboto-Thin.ttf') format('truetype');
}

@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 300;
src: local('Roboto Light'), local('Roboto-Light'), url('fonts/Roboto/Roboto-Light.ttf') format('truetype');
src: local('Roboto Light'), local('Roboto-Light'), url('/fonts/Roboto/Roboto-Light.ttf') format('truetype');
}

@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: local('Roboto'), local('Roboto-Regular'), url('fonts/Roboto/Roboto-Regular.ttf') format('truetype');
src: local('Roboto'), local('Roboto-Regular'), url('/fonts/Roboto/Roboto-Regular.ttf') format('truetype');
}

@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 500;
src: local('Roboto Medium'), local('Roboto-Medium'), url('fonts/Roboto/Roboto-Medium.ttf') format('truetype');
src: local('Roboto Medium'), local('Roboto-Medium'), url('/fonts/Roboto/Roboto-Medium.ttf') format('truetype');
}

@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 700;
src: local('Roboto Bold'), local('Roboto-Bold'), url('fonts/Roboto/Roboto-Bold.ttf') format('truetype');
src: local('Roboto Bold'), local('Roboto-Bold'), url('/fonts/Roboto/Roboto-Bold.ttf') format('truetype');
}

@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 900;
src: local('Roboto Black'), local('Roboto-Black'), url('fonts/Roboto/Roboto-Black.ttf') format('truetype');
src: local('Roboto Black'), local('Roboto-Black'), url('/fonts/Roboto/Roboto-Black.ttf') format('truetype');
}

@font-face {
font-family: 'Euclid';
font-style: normal;
font-weight: 400;
src: url('fonts/Euclid/EuclidCircularB-Regular-WebXL.ttf') format('truetype');
src: url('/fonts/Euclid/EuclidCircularB-Regular-WebXL.ttf') format('truetype');
}

@font-face {
font-family: 'Euclid';
font-style: italic;
font-weight: 400;
src: url('fonts/Euclid/EuclidCircularB-RegularItalic-WebXL.ttf') format('truetype');
src: url('/fonts/Euclid/EuclidCircularB-RegularItalic-WebXL.ttf') format('truetype');
}

@font-face {
font-family: 'Euclid';
font-style: normal;
font-weight: 700;
src: url('fonts/Euclid/EuclidCircularB-Bold-WebXL.ttf') format('truetype');
src: url('/fonts/Euclid/EuclidCircularB-Bold-WebXL.ttf') format('truetype');
}

$font-family: Euclid, Roboto, Helvetica, Arial, sans-serif;
Expand Down
65 changes: 65 additions & 0 deletions ui/app/css/utilities/_colors.scss
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) {
Copy link
Member

Choose a reason for hiding this comment

The 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/
They reported that when using this method:

Unfortunately, calculating only a handful of color contrast combinations increased Sass build times exponentially

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 pow function that didn't exist in older versions of sass. To get around this some implementations implemented functions that do many loops. Since then, math.pow is a language level feature (https://github.com/sass/dart-sass/releases/tag/1.25.0). I tried to find any mentions of it's performance, but came up empty.

Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
1 change: 1 addition & 0 deletions ui/app/css/utilities/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@forward 'colors';