All files / apps/huella-legal/layers/newsletter/app/composables useNewsletterJump.ts

100% Statements 20/20
90% Branches 9/10
100% Functions 4/4
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 405x   5x   5x           5x 2x   2x 2x     5x 14x   14x 3x   2x   2x 1x 1x   1x     1x 1x     3x      
export const NEWSLETTER_ID = "newsletter";
 
const TINT = "color-mix(in oklab, var(--ui-primary) 20%, transparent)";
 
const WASH: Keyframe[] = [
    { boxShadow: "inset 0 0 0 100vmax transparent" },
    { boxShadow: `inset 0 0 0 100vmax ${TINT}`, offset: 0.25 },
    { boxShadow: "inset 0 0 0 100vmax transparent" },
];
 
const arrive = (): void => {
    const band = document.getElementById(NEWSLETTER_ID);
 
    band?.animate(WASH, { duration: 2100, delay: 1100, easing: "ease-out" });
    band?.querySelector<HTMLInputElement>('input[type="email"]')?.focus({ preventScroll: true });
};
 
export const useNewsletterJump = () => {
    const { hooks } = useNuxtApp();
 
    return (event: MouseEvent): void => {
        if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
 
        const band = document.getElementById(NEWSLETTER_ID);
 
        if (!band) {
            hooks.hookOnce("page:loading:end", () => {
                requestAnimationFrame(arrive);
            });
            return;
        }
 
        event.preventDefault();
        band.scrollIntoView({
            behavior: matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth",
        });
        arrive();
    };
};