Skip to content

Commit

Permalink
feat: account for scroll margin (#906)
Browse files Browse the repository at this point in the history
* feat: account for scroll margin

* feat: rename vars
  • Loading branch information
RostiMelk committed Sep 30, 2023
1 parent 9e7f63d commit b932154
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/index.ts
Expand Up @@ -274,6 +274,16 @@ const getParentElement = (element: Node): Element | null => {
return parent
}

const getScrollMargins = (target: Element) => {
const computedStyle = window.getComputedStyle(target)
return {
top: parseFloat(computedStyle.scrollMarginTop) || 0,
right: parseFloat(computedStyle.scrollMarginRight) || 0,
bottom: parseFloat(computedStyle.scrollMarginBottom) || 0,
left: parseFloat(computedStyle.scrollMarginLeft) || 0,
}
}

/** @public */
export const compute = (target: Element, options: Options): ScrollAction[] => {
if (typeof document === 'undefined') {
Expand Down Expand Up @@ -342,20 +352,26 @@ export const compute = (target: Element, options: Options): ScrollAction[] => {
bottom: targetBottom,
left: targetLeft,
} = target.getBoundingClientRect()
const {
top: marginTop,
right: marginRight,
bottom: marginBottom,
left: marginLeft,
} = getScrollMargins(target)

// These values mutate as we loop through and generate scroll coordinates
let targetBlock: number =
block === 'start' || block === 'nearest'
? targetTop
? targetTop - marginTop
: block === 'end'
? targetBottom
: targetTop + targetHeight / 2 // block === 'center
? targetBottom + marginBottom
: targetTop + targetHeight / 2 - marginTop + marginBottom // block === 'center
let targetInline: number =
inline === 'center'
? targetLeft + targetWidth / 2
? targetLeft + targetWidth / 2 - marginLeft + marginRight
: inline === 'end'
? targetRight
: targetLeft // inline === 'start || inline === 'nearest
? targetRight + marginRight
: targetLeft - marginLeft // inline === 'start || inline === 'nearest

// Collect new scroll positions
const computations: ScrollAction[] = []
Expand Down

0 comments on commit b932154

Please sign in to comment.