@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
44 lines (43 loc) • 1.57 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
import { createContrastColorPair } from "../../schema";
import { createPropDefinition } from "./helpers/factory";
const HEX_REGEX = /^#((\d|[a-f]){8}|(\d|[a-f]){6}|(\d|[a-f]){3,4})$/i;
function isHexString(value) {
return HEX_REGEX.test(value);
}
function normalizer(value) {
if (typeof value === 'string' && isHexString(value)) {
const colors = createContrastColorPair(value);
return {
backgroundColor: colors.background,
foregroundColor: colors.foreground,
};
}
else if (typeof value === 'object' && value) {
const colorPair = value;
if (typeof colorPair.backgroundColor === 'string' &&
typeof colorPair.foregroundColor === 'string' &&
isHexString(colorPair.backgroundColor) &&
isHexString(colorPair.foregroundColor)) {
const colors = createContrastColorPair({
background: colorPair.backgroundColor,
foreground: colorPair.foregroundColor,
});
return {
backgroundColor: colors.background,
foregroundColor: colors.foreground,
};
}
}
throw new Error(`Invalid color ${value}`);
}
function validator(value) {
return isHexString(value.foregroundColor) && isHexString(value.backgroundColor);
}
export const colorProp = createPropDefinition('color', {
backgroundColor: '#d3d3d3',
foregroundColor: '#3f3f3f',
}, normalizer, validator);
//# sourceMappingURL=color.js.map