Skip to content

Commit d59b117

Browse files
fix: add margin even when scrollSpeed=100 (#160)
1 parent d835f34 commit d59b117

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Scrolls the element into view. If already in view, no scroll occurs.
152152
- **options (optional):** Additional options for scrolling.
153153
- `scrollSpeed (number):` Scroll speed (when scrolling occurs). 0 to 100. 100 is instant. Default is `100`.
154154
- `scrollDelay (number):` Time to wait after scrolling (when scrolling occurs). Default is `200`.
155-
- `inViewportMargin (number):` Margin (in px) to add around the element when ensuring it is in the viewport. (Does not take effect if `scrollSpeed=100`, or if CDP scroll fails.) Default is `0`.
155+
- `inViewportMargin (number):` Margin (in px) to add around the element when ensuring it is in the viewport. Default is `0`.
156156

157157
#### `getLocation(): Vector`
158158

src/spoof.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export interface ScrollOptions {
4040
readonly scrollDelay?: number
4141
/**
4242
* Margin (in px) to add around the element when ensuring it is in the viewport.
43-
* (Does not take effect if `scrollSpeed=100`, or if CDP scroll fails.)
44-
* @default 30
43+
* (Does not take effect if CDP scroll fails.)
44+
* @default 0
4545
*/
4646
readonly inViewportMargin?: number
4747
}
@@ -633,12 +633,11 @@ export const createCursor = (
633633
}
634634

635635
// Add margin around the element
636-
const margin = optionsResolved.inViewportMargin
637636
const marginedBox = {
638-
top: elemBox.top - margin,
639-
left: elemBox.left - margin,
640-
bottom: elemBox.bottom + margin,
641-
right: elemBox.right + margin
637+
top: elemBox.top - optionsResolved.inViewportMargin,
638+
left: elemBox.left - optionsResolved.inViewportMargin,
639+
bottom: elemBox.bottom + optionsResolved.inViewportMargin,
640+
right: elemBox.right + optionsResolved.inViewportMargin
642641
}
643642

644643
// Get position relative to the whole document
@@ -734,7 +733,7 @@ export const createCursor = (
734733
}
735734
}
736735

737-
if (scrollSpeed === 100) {
736+
if (scrollSpeed === 100 && optionsResolved.inViewportMargin <= 0) {
738737
try {
739738
const { objectId } = elem.remoteObject()
740739
if (objectId === undefined) throw new Error()

0 commit comments

Comments
 (0)