MCPcopy Create free account
hub / github.com/angular/components / supportsScrollBehavior

Function supportsScrollBehavior

src/cdk/platform/features/scrolling.ts:35–65  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33
34/** Check whether the browser supports scroll behaviors. */
35export function supportsScrollBehavior(): boolean {
36 if (scrollBehaviorSupported == null) {
37 // If we're not in the browser, it can't be supported. Also check for `Element`, because
38 // some projects stub out the global `document` during SSR which can throw us off.
39 if (typeof document !== 'object' || !document || typeof Element !== 'function' || !Element) {
40 scrollBehaviorSupported = false;
41 return scrollBehaviorSupported;
42 }
43
44 // If the element can have a `scrollBehavior` style, we can be sure that it's supported.
45 if (document.documentElement?.style && 'scrollBehavior' in document.documentElement!.style) {
46 scrollBehaviorSupported = true;
47 } else {
48 // At this point we have 3 possibilities: `scrollTo` isn't supported at all, it's
49 // supported but it doesn't handle scroll behavior, or it has been polyfilled.
50 const scrollToFunction: Function | undefined = Element.prototype.scrollTo;
51
52 if (scrollToFunction) {
53 // We can detect if the function has been polyfilled by calling `toString` on it. Native
54 // functions are obfuscated using `[native code]`, whereas if it was overwritten we'd get
55 // the actual function source. Via https://davidwalsh.name/detect-native-function. Consider
56 // polyfilled functions as supporting scroll behavior.
57 scrollBehaviorSupported = !/\{\s*\[native code\]\s*\}/.test(scrollToFunction.toString());
58 } else {
59 scrollBehaviorSupported = false;
60 }
61 }
62 }
63
64 return scrollBehaviorSupported;
65}
66
67/**
68 * Checks the type of RTL scroll axis used by this browser. As of time of writing, Chrome is NORMAL,

Callers 3

_applyScrollToOptionsMethod · 0.90

Calls 1

toStringMethod · 0.80

Tested by

no test coverage detected