@@ -9,6 +9,9 @@ export type Options<T> = StandardBehaviorOptions | CustomBehaviorOptions<T>
9
9
10
10
/** @public */
11
11
export interface StandardBehaviorOptions extends BaseOptions {
12
+ /**
13
+ * @defaultValue 'auto
14
+ */
12
15
behavior ?: ScrollBehavior
13
16
}
14
17
@@ -22,40 +25,17 @@ export type CustomScrollBehaviorCallback<T = unknown> = (
22
25
actions : ScrollAction [ ]
23
26
) => T
24
27
25
- function isStandardScrollBehavior (
28
+ let isStandardScrollBehavior = (
26
29
options : any
27
- ) : options is StandardBehaviorOptions {
28
- return options === Object ( options ) && Object . keys ( options ) . length !== 0
29
- }
30
+ ) : options is StandardBehaviorOptions =>
31
+ options === Object ( options ) && Object . keys ( options ) . length !== 0
30
32
31
- function isCustomScrollBehavior < T > (
33
+ let isCustomScrollBehavior = < T > (
32
34
options : any
33
- ) : options is CustomBehaviorOptions < T > {
34
- if ( typeof options === 'object' ) {
35
- return typeof options . behavior === 'function'
36
- }
37
- return false
38
- }
39
-
40
- function defaultBehavior (
41
- actions : ScrollAction [ ] ,
42
- behavior : ScrollBehavior = 'auto'
43
- ) {
44
- const canSmoothScroll = 'scrollBehavior' in document . body . style
45
-
46
- actions . forEach ( ( { el, top, left } ) => {
47
- // browser implements the new Element.prototype.scroll API that supports `behavior`
48
- // and guard window.scroll with supportsScrollBehavior
49
- if ( el . scroll && canSmoothScroll ) {
50
- el . scroll ( { top, left, behavior } )
51
- } else {
52
- el . scrollTop = top
53
- el . scrollLeft = left
54
- }
55
- } )
56
- }
35
+ ) : options is CustomBehaviorOptions < T > =>
36
+ typeof options === 'object' ? typeof options . behavior === 'function' : false
57
37
58
- function getOptions ( options : any ) : StandardBehaviorOptions {
38
+ let getOptions = ( options : any ) : StandardBehaviorOptions => {
59
39
// Handle alignToTop for legacy reasons, to be compatible with the spec
60
40
if ( options === false ) {
61
41
return { block : 'end' , inline : 'nearest' }
@@ -74,22 +54,22 @@ function getOptions(options: any): StandardBehaviorOptions {
74
54
// that imports in userland code (like if they use native smooth scrolling on some browsers, and the ponyfill for everything else)
75
55
// the named export allows this `import {auto as autoScrollIntoView, ponyfill as smoothScrollIntoView} from ...`
76
56
/** @public */
77
- function scrollIntoView < T > (
57
+ export default function scrollIntoView < T > (
78
58
target : Element ,
79
59
options : CustomBehaviorOptions < T >
80
60
) : T
81
61
/** @public */
82
- function scrollIntoView (
62
+ export default function scrollIntoView (
83
63
target : Element ,
84
64
options ?: StandardBehaviorOptions | boolean
85
65
) : void
86
66
/** @public */
87
- function scrollIntoView < T > (
67
+ export default function scrollIntoView < T > (
88
68
target : Element ,
89
69
options ?: StandardBehaviorOptions | CustomBehaviorOptions < T > | boolean
90
- ) {
70
+ ) : T | void {
91
71
// Browsers treats targets that aren't in the dom as a no-op and so should we
92
- const isTargetAttached =
72
+ let isTargetAttached =
93
73
target . isConnected ||
94
74
target . ownerDocument ! . documentElement ! . contains ( target )
95
75
@@ -103,11 +83,18 @@ function scrollIntoView<T>(
103
83
}
104
84
105
85
// @TODO see if it's possible to avoid this assignment
106
- const computeOptions = getOptions ( options )
107
- return defaultBehavior (
108
- compute ( target , computeOptions ) ,
109
- computeOptions . behavior
110
- )
111
- }
86
+ let computeOptions = getOptions ( options )
87
+ let actions = compute ( target , computeOptions )
88
+ let canSmoothScroll = 'scrollBehavior' in document . body . style
112
89
113
- export default scrollIntoView
90
+ actions . forEach ( ( { el, top, left } ) => {
91
+ // browser implements the new Element.prototype.scroll API that supports `behavior`
92
+ // and guard window.scroll with supportsScrollBehavior
93
+ if ( el . scroll && canSmoothScroll ) {
94
+ el . scroll ( { top, left, behavior : computeOptions . behavior } )
95
+ } else {
96
+ el . scrollTop = top
97
+ el . scrollLeft = left
98
+ }
99
+ } )
100
+ }
0 commit comments