9
9
import { ScrollStrategy } from './scroll-strategy' ;
10
10
import { ViewportRuler } from '@angular/cdk/scrolling' ;
11
11
import { coerceCssPixelValue } from '@angular/cdk/coercion' ;
12
+ import { supportsScrollBehavior } from '@angular/cdk/platform' ;
12
13
13
- /**
14
- * Extended `CSSStyleDeclaration` that includes `scrollBehavior` which isn't part of the
15
- * built-in TS typings. Once it is, this declaration can be removed safely.
16
- * @docs -private
17
- */
18
- type ScrollBehaviorCSSStyleDeclaration = CSSStyleDeclaration & { scrollBehavior : string } ;
14
+ const scrollBehaviorSupported = supportsScrollBehavior ( ) ;
19
15
20
16
/**
21
17
* Strategy that will prevent the user from scrolling while the overlay is visible.
22
18
*/
23
19
export class BlockScrollStrategy implements ScrollStrategy {
24
20
private _previousHTMLStyles = { top : '' , left : '' } ;
25
- private _previousScrollPosition : { top : number , left : number } ;
21
+ private _previousScrollPosition : { top : number , left : number } ;
26
22
private _isEnabled = false ;
27
23
private _document : Document ;
28
24
@@ -31,7 +27,7 @@ export class BlockScrollStrategy implements ScrollStrategy {
31
27
}
32
28
33
29
/** Attaches this scroll strategy to an overlay. */
34
- attach ( ) { }
30
+ attach ( ) { }
35
31
36
32
/** Blocks page-level scroll while the attached overlay is open. */
37
33
enable ( ) {
@@ -58,8 +54,8 @@ export class BlockScrollStrategy implements ScrollStrategy {
58
54
if ( this . _isEnabled ) {
59
55
const html = this . _document . documentElement ! ;
60
56
const body = this . _document . body ! ;
61
- const htmlStyle = html . style as ScrollBehaviorCSSStyleDeclaration ;
62
- const bodyStyle = body . style as ScrollBehaviorCSSStyleDeclaration ;
57
+ const htmlStyle = html . style ;
58
+ const bodyStyle = body . style ;
63
59
const previousHtmlScrollBehavior = htmlStyle . scrollBehavior || '' ;
64
60
const previousBodyScrollBehavior = bodyStyle . scrollBehavior || '' ;
65
61
@@ -71,12 +67,19 @@ export class BlockScrollStrategy implements ScrollStrategy {
71
67
72
68
// Disable user-defined smooth scrolling temporarily while we restore the scroll position.
73
69
// See https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior
74
- htmlStyle . scrollBehavior = bodyStyle . scrollBehavior = 'auto' ;
70
+ // Note that we don't mutate the property if the browser doesn't support `scroll-behavior`,
71
+ // because it can throw off feature detections in `supportsScrollBehavior` which
72
+ // checks for `'scrollBehavior' in documentElement.style`.
73
+ if ( scrollBehaviorSupported ) {
74
+ htmlStyle . scrollBehavior = bodyStyle . scrollBehavior = 'auto' ;
75
+ }
75
76
76
77
window . scroll ( this . _previousScrollPosition . left , this . _previousScrollPosition . top ) ;
77
78
78
- htmlStyle . scrollBehavior = previousHtmlScrollBehavior ;
79
- bodyStyle . scrollBehavior = previousBodyScrollBehavior ;
79
+ if ( scrollBehaviorSupported ) {
80
+ htmlStyle . scrollBehavior = previousHtmlScrollBehavior ;
81
+ bodyStyle . scrollBehavior = previousBodyScrollBehavior ;
82
+ }
80
83
}
81
84
}
82
85
0 commit comments