Skip to content

Commit 01e2ba3

Browse files
authored
fix: app crashing due to invalid hex (#11542)
1 parent c9b87bb commit 01e2ba3

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

packages/lib/getBrandColours.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ export function isValidHexCode(hexColor: string): boolean {
3131
return isHexColor;
3232
}
3333

34+
// credit : https://github.com/validatorjs/validator.js/blob/master/src/lib/isHexColor.js
35+
const getValidHEX = (hex: string, defaultHex: string): string => {
36+
const hexColor = /^#?([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i.test(hex.replace("##", "#"));
37+
if (hexColor) {
38+
return hex;
39+
}
40+
return defaultHex;
41+
};
42+
3443
function hexToRgb(hex: string): Rgb {
3544
const sanitizedHex = hex.replace("##", "#");
3645
const colorParts = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(sanitizedHex);
@@ -174,8 +183,8 @@ const useGetBrandingColours = ({
174183

175184
darkVal = normalizeHexCode(darkVal, true);
176185

177-
const lightColourMap = createColorMap(lightVal);
178-
const darkColourMap = createColorMap(darkVal);
186+
const lightColourMap = createColorMap(getValidHEX(lightVal, BRAND_COLOR));
187+
const darkColourMap = createColorMap(getValidHEX(darkVal, DARK_BRAND_COLOR));
179188

180189
const theme = {
181190
light: {

0 commit comments

Comments
 (0)