@@ -274,6 +274,16 @@ const getParentElement = (element: Node): Element | null => {
274
274
return parent
275
275
}
276
276
277
+ const getScrollMargins = ( target : Element ) => {
278
+ const computedStyle = window . getComputedStyle ( target )
279
+ return {
280
+ top : parseFloat ( computedStyle . scrollMarginTop ) || 0 ,
281
+ right : parseFloat ( computedStyle . scrollMarginRight ) || 0 ,
282
+ bottom : parseFloat ( computedStyle . scrollMarginBottom ) || 0 ,
283
+ left : parseFloat ( computedStyle . scrollMarginLeft ) || 0 ,
284
+ }
285
+ }
286
+
277
287
/** @public */
278
288
export const compute = ( target : Element , options : Options ) : ScrollAction [ ] => {
279
289
if ( typeof document === 'undefined' ) {
@@ -342,20 +352,26 @@ export const compute = (target: Element, options: Options): ScrollAction[] => {
342
352
bottom : targetBottom ,
343
353
left : targetLeft ,
344
354
} = target . getBoundingClientRect ( )
355
+ const {
356
+ top : marginTop ,
357
+ right : marginRight ,
358
+ bottom : marginBottom ,
359
+ left : marginLeft ,
360
+ } = getScrollMargins ( target )
345
361
346
362
// These values mutate as we loop through and generate scroll coordinates
347
363
let targetBlock : number =
348
364
block === 'start' || block === 'nearest'
349
- ? targetTop
365
+ ? targetTop - marginTop
350
366
: block === 'end'
351
- ? targetBottom
352
- : targetTop + targetHeight / 2 // block === 'center
367
+ ? targetBottom + marginBottom
368
+ : targetTop + targetHeight / 2 - marginTop + marginBottom // block === 'center
353
369
let targetInline : number =
354
370
inline === 'center'
355
- ? targetLeft + targetWidth / 2
371
+ ? targetLeft + targetWidth / 2 - marginLeft + marginRight
356
372
: inline === 'end'
357
- ? targetRight
358
- : targetLeft // inline === 'start || inline === 'nearest
373
+ ? targetRight + marginRight
374
+ : targetLeft - marginLeft // inline === 'start || inline === 'nearest
359
375
360
376
// Collect new scroll positions
361
377
const computations : ScrollAction [ ] = [ ]
0 commit comments