11<script >
2- import { onMount } from ' svelte ' ;
2+ import { fontColorContrast } from ' $lib/fontColorContrast.js ' ;
33
44 /** @type {{ action_label: string, color?: string }} */
55 let { action_label, color = " #00D690" } = $props ();
66
7- let contrastedColor = $state ( ' ' );
7+ const labelColor = $derived ( fontColorContrast (color) );
88
99 /**
1010 * @param {string} hex
2626 .slice (1 )} ` ;
2727 }
2828
29- let darkerColor = $derived (darkenColor (color, 7 ));
29+ /**
30+ * @param {string} hex
31+ * @param {number} percent
32+ */
33+ function lightenColor (hex , percent ) {
34+ const num = parseInt (hex .slice (1 ), 16 ),
35+ amt = Math .round (2.55 * percent),
36+ R = (num >> 16 ) + amt,
37+ G = (num >> 8 & 0x00FF ) + amt,
38+ B = (num & 0x0000FF ) + amt;
39+ return ` #${ (
40+ 0x1000000 +
41+ (R < 255 ? (R < 0 ? 0 : R ) : 255 ) * 0x10000 +
42+ (G < 255 ? (G < 0 ? 0 : G ) : 255 ) * 0x100 +
43+ (B < 255 ? (B < 0 ? 0 : B ) : 255 )
44+ )
45+ .toString (16 )
46+ .slice (1 )} ` ;
47+ }
3048
31- onMount (async () => {
32- const fontColorContrast = (await import (' font-color-contrast' )).default ;
33- contrastedColor = fontColorContrast (color);
34- });
49+ const hoverColor = $derived (
50+ labelColor === ' #ffffff' ? lightenColor (color, 22 ) : darkenColor (color, 12 )
51+ );
3552 </script >
3653
3754<button
38- 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"
39- style ={` background-color: ${color }; color: ${contrastedColor }; ` }
40- onmouseover ={(e ) => { e .currentTarget .style .backgroundColor = darkerColor ; }}
41- onmouseout ={(e ) => { e .currentTarget .style .backgroundColor = color ; }}
42- onfocus ={(e ) => { e .currentTarget .style .backgroundColor = darkerColor ; }}
43- onblur ={(e ) => { e .currentTarget .style .backgroundColor = color ; }}
55+ class =" min-w-[137px] h-12 px-6 py-2 rounded-3xl justify-center items-center inline-flex transition ease-in duration-150"
56+ style ={` background-color: ${color }; ` }
57+ onmouseover ={(e ) => {
58+ e .currentTarget .style .backgroundColor = hoverColor ;
59+ }}
60+ onmouseout ={(e ) => {
61+ e .currentTarget .style .backgroundColor = color ;
62+ }}
63+ onfocus ={(e ) => {
64+ e .currentTarget .style .backgroundColor = hoverColor ;
65+ }}
66+ onblur ={(e ) => {
67+ e .currentTarget .style .backgroundColor = color ;
68+ }}
4469>
45- <span class ="Button text-base uppercase leading-normal font-bold" >{action_label }</span >
46- </button >
70+ <span class ="text-base uppercase leading-normal font-bold" style ={ ` color: ${ labelColor }; ` } >{action_label }</span >
71+ </button >
0 commit comments