Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yellow-moments-follow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/source': minor
---

Add support for CSS variables in automatic button hover colours
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,21 @@ export const CustomTheme: Story = {
},
};

export const CustomThemeCssVar: Story = {
args: {
...PrimaryPriorityDefaultTheme.args,
theme: {
textPrimary: palette.neutral[100],
backgroundPrimary: 'var(--bg)',
},
},
render: (args) => {
document.documentElement.style.setProperty('--bg', '#7d0068');

return <Button {...args} />;
},
};

export const CustomTransparentTheme: Story = {
args: {
...PrimaryPriorityDefaultTheme.args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ export const calculateHoverColour = (
backgroundColour: string,
borderColour?: string,
): string => {
// A CSS variable supplied - try fetch value in browser
if (backgroundColour.startsWith('var')) {
const cssVariableName = backgroundColour
.substring(4, backgroundColour.length - 1)
.trim();

const backgroundColourFromVar = getComputedStyle(
document.body,
).getPropertyValue(cssVariableName);

if (backgroundColourFromVar === '') {
return backgroundColour;
}

return calculateHoverColour(backgroundColourFromVar, borderColour);
}

if (backgroundColour === 'transparent') {
if (borderColour !== undefined) {
return calculateTransparentBackgroundHover(borderColour);
Expand Down
Loading