Skip to content

Commit

Permalink
feat(frontend): use theme color based on color scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Apr 14, 2024
1 parent f2537c0 commit b4de1b3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/frontend/layouts/default.njk
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<link rel="manifest" href="{{ application.url }}/app.webmanifest">

<meta name="supported-color-schemes" content="light dark">
<meta name="theme-color" content="{{ themeColor(application.themeColor) }}" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="{{ themeColor(application.themeColor, "dark") }}" media="(prefers-color-scheme: dark)">
</head>

<body class="{{ appClasses }}">
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/lib/globals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export { fieldData } from "./field-data.js";
export { icon } from "./icon.js";
export { itemId } from "./item-id.js";
export { summaryRows } from "./summary-rows.js";
export { themeCustomProperties } from "./theme.js";
export { themeColor, themeCustomProperties } from "./theme.js";
19 changes: 17 additions & 2 deletions packages/frontend/lib/globals/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ export const _getValidatedColor = ({
return newColor;
};

/**
* Return theme color
* @param {string} string - Color (as hexadecimal)
* @param {string} colorScheme - Color scheme
* @returns {string} CSS color value (HSL)
*/
export const themeColor = (string, colorScheme = "light") => {
const primary = new Color(string).hsl().round();
const h = primary.hue();
const s = 5;
const l = colorScheme === "light" ? 99 : 10;

return new Color({ h, s, l }).hsl().toString();
};

/**
* Return theme color as CSS custom properties
* @param {string} string - Color (as hexadecimal)
Expand All @@ -49,8 +64,8 @@ export const themeCustomProperties = (string) => {
const primarySaturation = 5;

// Calculate neutral lightest and darkest hues used in custom properties
const neutral99 = new Color({ h: primaryHue, s: primarySaturation, l: 99 });
const neutral10 = new Color({ h: primaryHue, s: primarySaturation, l: 10 });
const neutral99 = new Color(themeColor(string));
const neutral10 = new Color(themeColor(string, "dark"));

// Generate a darker variant of primary colour
const primaryVariant = primary.darken(0.15).desaturate(0.1);
Expand Down
6 changes: 6 additions & 0 deletions packages/frontend/test/unit/globals/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, it } from "node:test";
import Color from "color";
import {
_getValidatedColor,
themeColor,
themeCustomProperties,
} from "../../../lib/globals/theme.js";

Expand All @@ -16,6 +17,11 @@ describe("frontend/lib/globals/icon", () => {
assert.equal(result.hex(), "#800080");
});

it("Returns theme color as HSL", () => {
assert.equal(themeColor("#f00"), "hsl(0, 5%, 99%)");
assert.equal(themeColor("#f00", "dark"), "hsl(0, 5%, 10%)");
});

it("Returns theme color as CSS custom properties", () => {
assert.equal(
themeCustomProperties("#f00"),
Expand Down

0 comments on commit b4de1b3

Please sign in to comment.