Skip to content

Commit 2166404

Browse files
committed
fix: improve typings, reduce bundlesize
1 parent e481bee commit 2166404

File tree

1 file changed

+29
-42
lines changed

1 file changed

+29
-42
lines changed

src/index.ts

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export type Options<T> = StandardBehaviorOptions | CustomBehaviorOptions<T>
99

1010
/** @public */
1111
export interface StandardBehaviorOptions extends BaseOptions {
12+
/**
13+
* @defaultValue 'auto
14+
*/
1215
behavior?: ScrollBehavior
1316
}
1417

@@ -22,40 +25,17 @@ export type CustomScrollBehaviorCallback<T = unknown> = (
2225
actions: ScrollAction[]
2326
) => T
2427

25-
function isStandardScrollBehavior(
28+
let isStandardScrollBehavior = (
2629
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
3032

31-
function isCustomScrollBehavior<T>(
33+
let isCustomScrollBehavior = <T>(
3234
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
5737

58-
function getOptions(options: any): StandardBehaviorOptions {
38+
let getOptions = (options: any): StandardBehaviorOptions => {
5939
// Handle alignToTop for legacy reasons, to be compatible with the spec
6040
if (options === false) {
6141
return { block: 'end', inline: 'nearest' }
@@ -74,22 +54,22 @@ function getOptions(options: any): StandardBehaviorOptions {
7454
// that imports in userland code (like if they use native smooth scrolling on some browsers, and the ponyfill for everything else)
7555
// the named export allows this `import {auto as autoScrollIntoView, ponyfill as smoothScrollIntoView} from ...`
7656
/** @public */
77-
function scrollIntoView<T>(
57+
export default function scrollIntoView<T>(
7858
target: Element,
7959
options: CustomBehaviorOptions<T>
8060
): T
8161
/** @public */
82-
function scrollIntoView(
62+
export default function scrollIntoView(
8363
target: Element,
8464
options?: StandardBehaviorOptions | boolean
8565
): void
8666
/** @public */
87-
function scrollIntoView<T>(
67+
export default function scrollIntoView<T>(
8868
target: Element,
8969
options?: StandardBehaviorOptions | CustomBehaviorOptions<T> | boolean
90-
) {
70+
): T | void {
9171
// Browsers treats targets that aren't in the dom as a no-op and so should we
92-
const isTargetAttached =
72+
let isTargetAttached =
9373
target.isConnected ||
9474
target.ownerDocument!.documentElement!.contains(target)
9575

@@ -103,11 +83,18 @@ function scrollIntoView<T>(
10383
}
10484

10585
// @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
11289

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

Comments
 (0)