33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
/**
|
|
* 슬래시 메뉴 선택 항목이 보이도록 필요한 scrollTop을 계산한다.
|
|
* @param {Object} options - 계산 옵션
|
|
* @param {number} options.itemTop - 항목의 메뉴 내부 위쪽 위치
|
|
* @param {number} options.itemHeight - 항목 높이
|
|
* @param {number} options.viewTop - 현재 메뉴 scrollTop
|
|
* @param {number} options.viewHeight - 메뉴 표시 높이
|
|
* @returns {number} 적용할 scrollTop
|
|
*/
|
|
export const calculateSlashMenuScrollTop = ({
|
|
itemTop,
|
|
itemHeight,
|
|
viewTop,
|
|
viewHeight
|
|
}) => {
|
|
const safeItemTop = Number.isFinite(itemTop) ? itemTop : 0
|
|
const safeItemHeight = Number.isFinite(itemHeight) ? Math.max(0, itemHeight) : 0
|
|
const safeViewTop = Number.isFinite(viewTop) ? Math.max(0, viewTop) : 0
|
|
const safeViewHeight = Number.isFinite(viewHeight) ? Math.max(0, viewHeight) : 0
|
|
const itemBottom = safeItemTop + safeItemHeight
|
|
const viewBottom = safeViewTop + safeViewHeight
|
|
|
|
if (safeItemTop < safeViewTop) {
|
|
return Math.max(0, safeItemTop)
|
|
}
|
|
|
|
if (itemBottom > viewBottom) {
|
|
return Math.max(0, itemBottom - safeViewHeight)
|
|
}
|
|
|
|
return safeViewTop
|
|
}
|