From d7a1e817352fe1b6742a45e8da85e1156928220a Mon Sep 17 00:00:00 2001 From: ghiscoding Date: Mon, 15 Jun 2026 20:29:54 -0400 Subject: [PATCH] refactor: simplify DOM utils with modern code logic --- .../src/MultipleSelectInstance.ts | 2 +- .../src/utils/domUtils.ts | 33 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/multiple-select-vanilla/src/MultipleSelectInstance.ts b/packages/multiple-select-vanilla/src/MultipleSelectInstance.ts index 933744e8..7a51b406 100644 --- a/packages/multiple-select-vanilla/src/MultipleSelectInstance.ts +++ b/packages/multiple-select-vanilla/src/MultipleSelectInstance.ts @@ -1341,7 +1341,7 @@ export class MultipleSelectInstance { container = this.options.container === 'body' ? document.body : (document.querySelector(this.options.container) as HTMLElement); } container!.appendChild(this.dropElm); - const { top: offsetTop = 0, left: offsetLeft = 0 } = getOffset(this.parentElm) || {}; + const { top: offsetTop, left: offsetLeft } = getOffset(this.parentElm); this.dropElm.style.top = `${offsetTop + this.parentElm.offsetHeight}px`; this.dropElm.style.left = `${offsetLeft}px`; this.dropElm.style.width = `${this.parentElm.offsetWidth}px`; diff --git a/packages/multiple-select-vanilla/src/utils/domUtils.ts b/packages/multiple-select-vanilla/src/utils/domUtils.ts index 7a0e6eb3..84108102 100644 --- a/packages/multiple-select-vanilla/src/utils/domUtils.ts +++ b/packages/multiple-select-vanilla/src/utils/domUtils.ts @@ -10,15 +10,15 @@ export interface HtmlElementPosition { /** Calculate available space for each side of the DOM element */ export function calculateAvailableSpace(elm: HTMLElement): { top: number; bottom: number; left: number; right: number } { - const { innerHeight: windowHeight = 0, innerWidth: windowWidth = 0 } = window; + const { innerHeight: vh = 0, innerWidth: vw = 0 } = window; const { top: pageScrollTop, left: pageScrollLeft } = windowScrollPosition(); - const { top = 0, left = 0 } = getOffset(elm) || {}; + const { top, left } = getOffset(elm); return { top: top - pageScrollTop, - bottom: windowHeight - (top - pageScrollTop), + bottom: vh - (top - pageScrollTop), left: left - pageScrollLeft, - right: windowWidth - (left - pageScrollLeft), + right: vw - (left - pageScrollLeft), }; } @@ -124,17 +124,18 @@ export function emptyElement(elm?: T | null): T | u } /** Get HTML element offset with pure JS */ -export function getOffset(element?: HTMLElement): HtmlElementPosition | undefined { - if (element) { - const { top, left, bottom, right } = element.getBoundingClientRect(); - return { - top: top + window.pageYOffset, - left: left + window.pageXOffset, - bottom, - right, - }; +export function getOffset(elm?: HTMLElement | null): HtmlElementPosition { + if (!elm) { + return { top: 0, bottom: 0, left: 0, right: 0 }; } - return undefined; + + const { top, left, bottom, right } = elm.getBoundingClientRect(); + return { + top: top + window.pageYOffset, + left: left + window.pageXOffset, + bottom, + right, + }; } export function getSize(elm: HTMLElement | undefined, mode: 'inner' | 'outer' | 'scroll', type: 'height' | 'width') { @@ -267,7 +268,7 @@ export function toggleElement(elm?: HTMLElement | null, display?: boolean) { */ export function windowScrollPosition(): { left: number; top: number } { return { - left: window.pageXOffset || document.documentElement.scrollLeft || 0, - top: window.pageYOffset || document.documentElement.scrollTop || 0, + left: window.pageXOffset, + top: window.pageYOffset, }; }