From 6f6c107b06100b279d8f3a79eda6cb8c9efeca62 Mon Sep 17 00:00:00 2001 From: Kai Paysen Date: Thu, 13 Jan 2022 15:53:11 +0100 Subject: [PATCH] preventing ReferenceError --- src/Tooltip.svelte | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Tooltip.svelte b/src/Tooltip.svelte index 2c5ced41..d7fb9eeb 100644 --- a/src/Tooltip.svelte +++ b/src/Tooltip.svelte @@ -55,15 +55,31 @@ function registerEventListeners() { if (target == null || target.length == 0) { - targetEl = null; + targetEl = null; + return; } - else if (target instanceof HTMLElement) { - targetEl = target; + + // Check if target is HTMLElement + try { + if (target instanceof HTMLElement) { + targetEl = target; + } + } catch (e) { + // fails on SSR } - else { - targetEl = document.querySelector(`#${target}`); - } + // If targetEl has not been found yet + if (targetEl == null) { + // Check if target can be found via querySelector + try { + targetEl = document.querySelector(`#${target}`); + } + catch (e) { + // fails on SSR + } + } + + // If we've found targetEl if (targetEl) { targetEl.addEventListener('mouseover', open); targetEl.addEventListener('mouseleave', close);