Skip to content

Commit 72110c8

Browse files
authored
fix: issue #720 where there is a conflict with typescript >= 4.0 typings for Window.visualViewport and same type defined by this library (#725)
This fixes #720. At moment, microbundle does not support typescript >= 4.0, where typings Window has visualViewport exists. So, we can't upgrade typescript to 4.0, and just add a local hack to compute function to prevent typings conflict in global Window.visualViewport interface - between this definition https://github.com/stipsan/compute-scroll-into-view/blob/v1.0.14/src/index.ts#L20 and typescript >= 4.0 definition
1 parent 7cebb40 commit 72110c8

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/index.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ interface visualViewport {
1212
width: number
1313
}
1414

15-
// @TODO report to typescript
16-
declare var visualViewport: visualViewport
17-
18-
declare global {
19-
interface Window {
20-
visualViewport?: {
21-
height: number
22-
width: number
23-
}
24-
}
25-
}
26-
2715
type ScrollLogicalPosition = 'start' | 'center' | 'end' | 'nearest'
2816
// This new option is tracked in this PR, which is the most likely candidate at the time: https://github.com/w3c/csswg-drafts/pull/1805
2917
type ScrollMode = 'always' | 'if-needed'
@@ -242,6 +230,11 @@ function alignNearest(
242230
}
243231

244232
export default (target: Element, options: Options): CustomScrollAction[] => {
233+
//TODO: remove this hack when microbundle will support typescript >= 4.0
234+
const windowWithViewport = (window as unknown) as Window & {
235+
visualViewport: visualViewport
236+
}
237+
245238
const {
246239
scrollMode,
247240
block,
@@ -295,11 +288,11 @@ export default (target: Element, options: Options): CustomScrollAction[] => {
295288
// and viewport dimensions on window.innerWidth/Height
296289
// https://www.quirksmode.org/mobile/viewports2.html
297290
// https://bokand.github.io/viewport/index.html
298-
const viewportWidth = window.visualViewport
299-
? visualViewport.width
291+
const viewportWidth = windowWithViewport.visualViewport
292+
? windowWithViewport.visualViewport.width
300293
: innerWidth
301-
const viewportHeight = window.visualViewport
302-
? visualViewport.height
294+
const viewportHeight = windowWithViewport.visualViewport
295+
? windowWithViewport.visualViewport.height
303296
: innerHeight
304297

305298
// Newer browsers supports scroll[X|Y], page[X|Y]Offset is

0 commit comments

Comments
 (0)