UNPKG

sveltekit-superforms

Version:

Making SvelteKit forms a pleasure to use!

30 lines (29 loc) 1.38 kB
// https://stackoverflow.com/a/7557433/70894 export const isElementInViewport = (el, topOffset = 0) => { const rect = el.getBoundingClientRect(); return (rect.top >= topOffset && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) /* or $(window).height() */ && rect.right <= (window.innerWidth || document.documentElement.clientWidth) /* or $(window).width() */); }; // https://stackoverflow.com/a/36499256/70894 export const scrollToAndCenter = (el, offset = 1.125, behavior = 'smooth') => { const elementRect = el.getBoundingClientRect(); const absoluteElementTop = elementRect.top + window.pageYOffset; const top = absoluteElementTop - window.innerHeight / (2 * offset); window.scrollTo({ left: 0, top, behavior }); }; const immediateInputTypes = ['checkbox', 'radio', 'range', 'file']; /** * Information about a HTML element, for determining when to display errors. */ export function inputInfo(el) { const immediate = !!el && (el instanceof HTMLSelectElement || (el instanceof HTMLInputElement && immediateInputTypes.includes(el.type))); const multiple = !!el && el instanceof HTMLSelectElement && el.multiple; const file = !!el && el instanceof HTMLInputElement && el.type == 'file'; return { immediate, multiple, file }; }