-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from sitevision/ENV-279-Envision-for-Dashboar…
…d-Widgets Env 279 envision for dashboard widgets
- Loading branch information
Showing
20 changed files
with
1,506 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const WCAG_CONTRAST_RATIO = { | ||
AA_LARGE: 1 / 3, | ||
AA_NORMAL: 1 / 4.5, | ||
AAA_LARGE: 1 / 4.5, | ||
AAA_NORMAL: 1 / 7, | ||
}; | ||
|
||
export const DASHBOARD_COLORS = [ | ||
'red', | ||
'pink', | ||
'purple', | ||
'deep-purple', | ||
'indigo', | ||
'blue', | ||
'light-blue', | ||
'cyan', | ||
'teal', | ||
'green', | ||
'light-green', | ||
'lime', | ||
'yellow', | ||
'orange', | ||
'brown', | ||
'gray', | ||
]; | ||
export const DASHBOARD_COLOR_VARIANTS = ['05', '20', '50', '90']; | ||
|
||
export const passesWCAG = (ratio, level) => { | ||
level = level || WCAG_CONTRAST_RATIO.AA_NORMAL; | ||
return 1 / ratio < level; | ||
}; | ||
|
||
const hexToRgb = (hex) => { | ||
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; | ||
const rgbRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i; | ||
hex = hex.replace(shorthandRegex, function (m, r, g, b) { | ||
return r + r + g + g + b + b; | ||
}); | ||
const result = rgbRegex.exec(hex); | ||
const r = parseInt(result[1], 16); | ||
const g = parseInt(result[2], 16); | ||
const b = parseInt(result[3], 16); | ||
return result ? [r, g, b] : null; | ||
}; | ||
|
||
const luminance = (rgb) => { | ||
const a = rgb.map(function (v) { | ||
v /= 255; | ||
return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); | ||
}); | ||
return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722; | ||
}; | ||
|
||
export const getContrastRatio = (hex1, hex2) => { | ||
const lum1 = luminance(hexToRgb(hex1)); | ||
const lum2 = luminance(hexToRgb(hex2)); | ||
return (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as React from 'react'; | ||
import { | ||
DASHBOARD_COLOR_VARIANTS, | ||
DASHBOARD_COLORS, | ||
getContrastRatio, | ||
passesWCAG, | ||
} from './colorUtils'; | ||
import { toTitleCase } from './demoUtils'; | ||
|
||
const TEXT_CONTRAST_ICON = | ||
'<svg class="env-icon example-dashboard-color-list-icon">' + | ||
'<use xlink:href="/images/docs-icons.svg#text"></use></svg>'; | ||
|
||
const listItem = (options) => { | ||
const o = Object.assign({}, options); | ||
return `<li class="env-dashboard-color-bg-${o.bgClassName} env-dashboard-color-txt-${o.txtClassName}"> | ||
<div><span>${o.name}</span> | ||
<span>${o.hex}</span> | ||
</div></li>`; | ||
}; | ||
|
||
export const useDashboardExample = (content) => { | ||
React.useEffect(() => { | ||
const colorList = document.getElementById('dashboard-color-list'); | ||
if (colorList) { | ||
const compStyleBody = getComputedStyle(document.body); | ||
const bodyBgColor = compStyleBody.getPropertyValue( | ||
`--env-dashboard-color-gray-05` | ||
); | ||
const blackColor = compStyleBody.getPropertyValue( | ||
`--env-dashboard-color-black` | ||
); | ||
const whiteColor = compStyleBody.getPropertyValue( | ||
`--env-dashboard-color-white` | ||
); | ||
|
||
DASHBOARD_COLORS.forEach((colorName) => { | ||
const gridEl = document.createElement('div'); | ||
gridEl.innerHTML = `<h2 class="env-ui-text-subheading">${toTitleCase( | ||
colorName.replace('-', ' ') | ||
)}</h2>`; | ||
|
||
const listEl = document.createElement('ul'); | ||
listEl.classList.add('example-dashboard-color-list'); | ||
|
||
DASHBOARD_COLOR_VARIANTS.forEach((variantName) => { | ||
let txtClassName = 'black'; | ||
let variantText = variantName; | ||
const bgClassName = `${colorName}-${variantName}`; | ||
const bgColor = compStyleBody.getPropertyValue( | ||
`--env-dashboard-color-${colorName}-${variantName}` | ||
); | ||
|
||
if (bgColor) { | ||
const contrastBody = getContrastRatio(bgColor, bodyBgColor); | ||
const contrastSection = getContrastRatio(bgColor, whiteColor); | ||
const passesAAonWhite = passesWCAG(contrastSection); | ||
const passesAAonBody = passesWCAG(contrastBody); | ||
const canHaveBlackText = passesWCAG( | ||
getContrastRatio(blackColor, bgColor) | ||
); | ||
if (!canHaveBlackText) { | ||
txtClassName = 'white'; | ||
} | ||
if (passesAAonWhite) { | ||
variantText += TEXT_CONTRAST_ICON; | ||
if (passesAAonBody) { | ||
variantText += '*'; | ||
} | ||
} | ||
} | ||
|
||
listEl.innerHTML += listItem({ | ||
bgClassName: bgClassName, | ||
txtClassName: txtClassName, | ||
name: variantText, | ||
hex: bgColor, | ||
}); | ||
}); | ||
gridEl.appendChild(listEl); | ||
colorList.appendChild(gridEl); | ||
}); | ||
} | ||
}, [content]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const toTitleCase = (str) => { | ||
return str | ||
.toLowerCase() | ||
.split(' ') | ||
.map(function (word) { | ||
return word.charAt(0).toUpperCase() + word.slice(1); | ||
}) | ||
.join(' '); | ||
}; |
Oops, something went wrong.