Skip to content

Commit 8d12241

Browse files
authored
Merge pull request #68 from dreyfus92/fix/contrast-in-buttons
fix(Button): corrige contraste del botón y hover visible en fondos oscuros Revisado en local, parece correcto.
2 parents e1b569d + 5dc72fe commit 8d12241

3 files changed

Lines changed: 65 additions & 16 deletions

File tree

src/components/Button.svelte

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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
@@ -26,21 +26,46 @@
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>

src/lib/fontColorContrast.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pkg from 'font-color-contrast';
2+
3+
/**
4+
* Normalize CJS default export for Vite client + SSR.
5+
* @returns {(hex: string) => string}
6+
*/
7+
function resolve() {
8+
if (typeof pkg === 'function') return /** @type {(hex: string) => string} */ (pkg);
9+
const outer =
10+
pkg && typeof pkg === 'object' && 'default' in pkg
11+
? /** @type {{ default: unknown }} */ (pkg).default
12+
: undefined;
13+
if (typeof outer === 'function') return /** @type {(hex: string) => string} */ (outer);
14+
if (outer && typeof outer === 'object' && 'default' in outer) {
15+
const inner = /** @type {{ default: unknown }} */ (outer).default;
16+
if (typeof inner === 'function') return /** @type {(hex: string) => string} */ (inner);
17+
}
18+
throw new Error('font-color-contrast: could not resolve a callable export');
19+
}
20+
21+
export const fontColorContrast = resolve();

vite.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ import { sveltekit } from '@sveltejs/kit/vite';
22
import { defineConfig } from 'vite';
33

44
export default defineConfig({
5-
plugins: [sveltekit()]
5+
plugins: [sveltekit()],
6+
ssr: {
7+
noExternal: ['font-color-contrast']
8+
}
69
});

0 commit comments

Comments
 (0)