Skip to content

Commit 69d4dbd

Browse files
committed
calculate color for hover state
1 parent 9970a04 commit 69d4dbd

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/components/Button.svelte

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,31 @@
33
export let action_label;
44
export let color = "#00D690";
55
let contrastedColor = '';
6-
7-
onMount(async () => {
8-
const fontColorContrast = (await import('font-color-contrast')).default;
9-
contrastedColor = fontColorContrast(color);
10-
});
6+
function darkenColor(hex, percent) {
7+
const num = parseInt(hex.slice(1), 16),
8+
amt = Math.round(2.55 * percent),
9+
R = (num >> 16) - amt,
10+
G = (num >> 8 & 0x00FF) - amt,
11+
B = (num & 0x0000FF) - amt;
12+
return `#${(
13+
0x1000000 +
14+
(R < 255 ? (R < 0 ? 0 : R) : 255) * 0x10000 +
15+
(G < 255 ? (G < 0 ? 0 : G) : 255) * 0x100 +
16+
(B < 255 ? (B < 0 ? 0 : B) : 255)
17+
)
18+
.toString(16)
19+
.slice(1)}`;
20+
}
21+
let darkerColor = darkenColor(color, 7);
22+
onMount(async () => {
23+
const fontColorContrast = (await import('font-color-contrast')).default;
24+
contrastedColor = fontColorContrast(color);
25+
});
1126
</script>
1227

13-
<button class="Button min-w-[137px] h-12 px-6 py-2 rounded-3xl justify-center items-center inline-flex" style={
28+
<button class="Button min-w-[137px] h-12 px-6 py-2 rounded-3xl justify-center items-center inline-flex hover:bg-white/75 transition ease-in duration-150" style={
1429
`background-color: ${color};
15-
color: ${contrastedColor}`
16-
}>
30+
color: ${contrastedColor}; `
31+
} onMouseOver="this.style.backgroundColor='{darkerColor}'" onMouseOut="this.style.backgroundColor='{color}'">
1732
<span class="Button text-base font-['Albert Sans'] uppercase leading-normal font-bold"> {action_label}</span>
1833
</button>

0 commit comments

Comments
 (0)