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

Theming #9

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Theming #9

wants to merge 7 commits into from

Conversation

DanRader
Copy link
Contributor

– updated a number of value names
– restructured brand values to accept theming updates
– Added JS function to calculate brand colors from single colors

Dan Rader added 6 commits November 15, 2022 15:29
- removed tiers in base color names so it's green300 instead of green.300
- removed background-2 and changed to background2
- Added primary and secondary base colors
- Added Brand Primary and Brand Secondary referencing themable base tokens
- Removed Primary and Secondary from main list
- Grouped Neutral Colors
- named Neural Colors (this may be unnecessary change?)
- Removed text.action because it doesn't work with variable colors
- renamed text warning with text negative to align naming
- added text positive
- renamed text primary and text secondary to remove confusion. It's now text and text subdued
-Added Text.onPrimary and Text.onSecondary
- updated 'information' to reference static base color
- updated the positive DM value for color contrast
- updated trivial-bg to trivial.surface. Both renaming BG values to avoid confusion and nest additional values
- renamed Warning > Negative to align semantic naming
- changed "hover" to focus to align across web and mobile
- nested focus states in JSON
- renamed JSON file to Common (no effect on output)
- updated accent colors to include both lm and dm
- added blue color series
- changed JSON file name to just accent
This first release only has a function for updating CSS
I imagine we could write in other methods as conditional exports from this function
Copy link
Collaborator

@Proof757 Proof757 left a comment

Choose a reason for hiding this comment

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

Nice job man! Left some thoughts, at long last 🙏🏿

Comment on lines 1 to 91
const createColorList = (primaryColor, secondaryColor) => {

const primary50 = chroma.mix(primaryColor, 'white', 0.9, 'hsl').hex();
const primary100 = chroma.mix(primaryColor, 'white', 0.7, 'hsl').hex();
const primary200 = chroma.mix(primaryColor, 'white', 0.6, 'hsl').hex();
const primary300 = chroma.mix(primaryColor, 'white', 0.4, 'hsl').hex();
const primary400 = chroma.mix(primaryColor, 'white', 0.2, 'hsl').hex();
const primary500 = chroma(primaryColor).hex();
const primary600 = chroma.mix(primaryColor, 'black', 0.2, 'hsl').hex();
const primary700 = chroma.mix(primaryColor, 'black', 0.4, 'hsl').hex();
const primary800 = chroma.mix(primaryColor, 'black', 0.6, 'hsl').hex();
const primary900 = chroma.mix(primaryColor, 'black', 0.7, 'hsl').hex();
const primary1000 = chroma.mix(primaryColor, 'black', 0.9, 'hsl').hex();
const primaryDMGradStart = chroma.mix(primaryColor, 'white', 0.2, 'hsl').alpha(.2).hex();
const primaryDMGradEnd = chroma.mix(primaryColor, 'white', 0.2, 'hsl').alpha(.0).hex();

const secondary50 = chroma.mix(secondaryColor, 'white', 0.9, 'hsl').hex();
const secondary100 = chroma.mix(secondaryColor, 'white', 0.7, 'hsl').hex();
const secondary200 = chroma.mix(secondaryColor, 'white', 0.6, 'hsl').hex();
const secondary300 = chroma.mix(secondaryColor, 'white', 0.4, 'hsl').hex();
const secondary400 = chroma.mix(secondaryColor, 'white', 0.2, 'hsl').hex();
const secondary500 = chroma(secondaryColor).hex();
const secondary600 = chroma.mix(secondaryColor, 'black', 0.2, 'hsl').hex();
const secondary700 = chroma.mix(secondaryColor, 'black', 0.4, 'hsl').hex();
const secondary800 = chroma.mix(secondaryColor, 'black', 0.6, 'hsl').hex();
const secondary900 = chroma.mix(secondaryColor, 'black', 0.7, 'hsl').hex();
const secondary1000 = chroma.mix(secondaryColor, 'black', 0.9, 'hsl').hex();
const secondaryDMGradStart = chroma.mix(secondaryColor, 'white', 0.2, 'hsl').alpha(.2).hex();
const secondaryDMGradEnd = chroma.mix(secondaryColor, 'white', 0.2, 'hsl').alpha(.0).hex();

const contrastTarget = 4.5
const textColor = "#292632"
const textReverse = "#FFFFFF"
var textOnPrimary = ""
var textOnSecondary = ""

function checkContrast() {
const primaryContrast = chroma.contrast(textColor, primaryColor);
const secondaryContrast = chroma.contrast(textColor, secondaryColor);

if (primaryContrast > contrastTarget) {
textOnPrimary = textColor
} else {
textOnPrimary = textReverse
}

if (secondaryContrast > contrastTarget) {
textOnSecondary = textColor
} else {
textOnSecondary = textReverse
}
};

checkContrast();

function updateCSS() {
root.style.setProperty(`--xpl-color-primary50`, primary50);
root.style.setProperty(`--xpl-color-primary100`, primary100);
root.style.setProperty(`--xpl-color-primary100`, primary100);
root.style.setProperty(`--xpl-color-primary200`, primary200);
root.style.setProperty(`--xpl-color-primary300`, primary300);
root.style.setProperty(`--xpl-color-primary400`, primary400);
root.style.setProperty(`--xpl-color-primary500`, primary500);
root.style.setProperty(`--xpl-color-primary600`, primary600);
root.style.setProperty(`--xpl-color-primary700`, primary700);
root.style.setProperty(`--xpl-color-primary800`, primary800);
root.style.setProperty(`--xpl-color-primary900`, primary900);
root.style.setProperty(`--xpl-color-primary1000`, primary1000);
root.style.setProperty(`--xpl-color-brand-primary-gradient-start-dm`, primaryDMGradStart);
root.style.setProperty(`--xpl-color-brand-primary-gradient-start-dm`, primaryDMGradEnd);
root.style.setProperty(`--xpl-color-text-on-primary-lm`, textOnPrimary);

root.style.setProperty(`--xpl-color-secondary50`, secondary50);
root.style.setProperty(`--xpl-color-secondary100`, secondary100);
root.style.setProperty(`--xpl-color-secondary100`, secondary100);
root.style.setProperty(`--xpl-color-secondary200`, secondary200);
root.style.setProperty(`--xpl-color-secondary300`, secondary300);
root.style.setProperty(`--xpl-color-secondary400`, secondary400);
root.style.setProperty(`--xpl-color-secondary500`, secondary500);
root.style.setProperty(`--xpl-color-secondary600`, secondary600);
root.style.setProperty(`--xpl-color-secondary700`, secondary700);
root.style.setProperty(`--xpl-color-secondary800`, secondary800);
root.style.setProperty(`--xpl-color-secondary900`, secondary900);
root.style.setProperty(`--xpl-color-secondary1000`, secondary1000);
root.style.setProperty(`--xpl-color-brand-secondary-gradient-start-dm`, secondaryDMGradStart);
root.style.setProperty(`--xpl-color-brand-secondary-gradient-start-dm`, secondaryDMGradEnd);
root.style.setProperty(`--xpl-color-text-on-secondary-lm`, textOnSecondary);
}

updateCSS();
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I already spoiled the surprise but I took a stab at DRYing these functions up. Here's where I landed:

import chroma from 'chroma-js';

const { style } = document.documentElement;

function generateGradientArray(color) {
    const gradientArray = [0.9, 0.7, 0.6, 0.4, 0.2];

    const morning = gradientArray.map((val) => (
        chroma.mix(color, 'white', val, 'hsl').hex()
    ));

    const noon = chroma(color).hex();

    const evening = gradientArray.reverse().map((val) => (
        chroma.mix(color, 'black', val, 'hsl').hex()
    ));

    const gradientStart = chroma.mix(color, 'white', 0.2, 'hsl').alpha(0.2).css();
    const gradientEnd = chroma.mix(color, 'white', 0.2, 'hsl').alpha(0).css();

    return [...morning, noon, ...evening, gradientStart, gradientEnd];
}

function setTextColors(color, themeName = 'primary') {
    const textColor = '#292632';
    const textReverse = '#FFFFFF';
    const isTextColorAcceptable = chroma.contrast(textColor, color) > 4.5;

    style.setProperty(
        `--xpl-color-text-${themeName}-lm`,
        isTextColorAcceptable ? textColor : textReverse,
    );
    style.setProperty(
        `--xpl-color-text-${themeName}-dm`,
        isTextColorAcceptable ? textReverse : textColor,
    );
}

function setCustomProperties(colorSetName, gradientMode, colorSet) {
    const [gradientEnd, gradientStart, ...setArray] = colorSet.reverse();

    setArray.reverse().forEach((hexDex, idx) => {
        const int = idx === 0 ? 50 : idx * 100;
        style.setProperty(`--xpl-color-${colorSetName}${int}`, hexDex);
    });

    style.setProperty(`--xpl-color-brand-${colorSetName}-gradient-start-${gradientMode}`, gradientStart);
    style.setProperty(`--xpl-color-brand-${colorSetName}-gradient-end-${gradientMode}`, gradientEnd);
}

export {
    generateGradientArray,
    setCustomProperties,
    setTextColors,
};

If the color in question is '#6923f4', these function set value to the following variables:

'--xpl-color-text-primary-lm' '#FFFFFF'
'--xpl-color-text-primary-dm' '#292632'

'--xpl-color-primary50' '#f0e9fe'
'--xpl-color-primary100' '#d2bdfc'
'--xpl-color-primary200' '#c3a7fb'
'--xpl-color-primary300' '#a57bf8'
'--xpl-color-primary400' '#874ff6'
'--xpl-color-primary500' '#6923f4'
'--xpl-color-primary600' '#4e0bd5'
'--xpl-color-primary700' '#3b089f'
'--xpl-color-primary800' '#27056a'
'--xpl-color-primary900' '#1d0450'
'--xpl-color-primary1000' '#0a011b'

'--xpl-color-brand-primary-gradient-start-lm' 'rgba(135,79,246,0.2)'
'--xpl-color-brand-primary-gradient-end-lm' 'rgba(135,79,246,0)'

See anything wrong with these vars man?

Comment on lines 85 to 86
root.style.setProperty(`--xpl-color-brand-secondary-gradient-start-dm`, secondaryDMGradStart);
root.style.setProperty(`--xpl-color-brand-secondary-gradient-start-dm`, secondaryDMGradEnd);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should there be light-mode gradients as well?

root.style.setProperty(`--xpl-color-primary1000`, primary1000);
root.style.setProperty(`--xpl-color-brand-primary-gradient-start-dm`, primaryDMGradStart);
root.style.setProperty(`--xpl-color-brand-primary-gradient-start-dm`, primaryDMGradEnd);
root.style.setProperty(`--xpl-color-text-on-primary-lm`, textOnPrimary);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should there be a light-mode text variable as well?

npm-debug.log Outdated
@@ -0,0 +1,1091 @@
0 info it worked if it ends with ok
Copy link
Collaborator

Choose a reason for hiding this comment

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

This file shouldn't be committed. Do you mind adding this to .gitignore: npm-debug.log*, and deleting this file?

@@ -1,6 +1,6 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add chroma-js officially man 😎

- Also added base value for gradients so it can me managed in one place
-
@fernandogelin
Copy link
Collaborator

@Proof757 wondering if we should do something about this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants