Skip to content

Commit 7044713

Browse files
tonybdesignstipsan
authored andcommitted
Feat: Two-arguments signature (#148)
* changes in arguments in main func config and animate and centerIfNeeded goes to arguments options object * changes in arguments in main func config and animate and centerIfNeeded goes to arguments options object * let replaced by const for offset option * type check added for type guard * old way of using arguments is handled to work with 2 and 5 args * Fix tests
1 parent b6cc1c9 commit 7044713

File tree

1 file changed

+50
-17
lines changed

1 file changed

+50
-17
lines changed

src/index.ts

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,50 @@ export interface OffsetConfig {
1212
offsetRight?: number
1313
}
1414

15+
export interface Options {
16+
animateOptions?: AnimateOptions
17+
boundary?: Element
18+
centerIfNeeded?: boolean
19+
offsetConfig?: OffsetConfig
20+
}
21+
22+
function isBoolean(options: boolean | Options): options is boolean {
23+
return typeof options === 'boolean'
24+
}
25+
1526
export default function scrollIntoViewIfNeeded(
1627
elem: Element,
17-
centerIfNeeded?: boolean,
18-
options?: AnimateOptions,
28+
options: boolean | Options,
29+
animateOptions?: AnimateOptions,
1930
finalElement?: Element,
20-
config: OffsetConfig = {}
31+
offsetOptions: OffsetConfig = {}
2132
) {
2233
if (!elem || !(elem instanceof HTMLElement))
2334
throw new Error('Element is required in scrollIntoViewIfNeeded')
2435

36+
let config: Options = {}
37+
38+
if (isBoolean(options)) {
39+
config.centerIfNeeded = options
40+
} else {
41+
config = options
42+
}
43+
44+
if (animateOptions) {
45+
config.animateOptions = animateOptions
46+
}
47+
48+
if (finalElement) {
49+
config.boundary = finalElement
50+
}
51+
52+
if (offsetOptions) {
53+
config.offsetConfig = offsetOptions
54+
}
55+
2556
function withinBounds(value, min, max, extent) {
2657
if (
27-
false === centerIfNeeded ||
58+
config.centerIfNeeded === false ||
2859
(max <= value + extent && value <= min + extent)
2960
) {
3061
return Math.min(max, Math.max(min, value))
@@ -33,10 +64,12 @@ export default function scrollIntoViewIfNeeded(
3364
}
3465
}
3566

36-
const offsetTop = config.offsetTop || 0
37-
const offsetLeft = config.offsetLeft || 0
38-
const offsetBottom = config.offsetBottom || 0
39-
const offsetRight = config.offsetRight || 0
67+
const { offsetConfig = {} } = config
68+
69+
const offsetTop = offsetConfig.offsetTop || 0
70+
const offsetLeft = offsetConfig.offsetLeft || 0
71+
const offsetBottom = offsetConfig.offsetBottom || 0
72+
const offsetRight = offsetConfig.offsetRight || 0
4073

4174
function makeArea(left, top, width, height) {
4275
return {
@@ -55,7 +88,7 @@ export default function scrollIntoViewIfNeeded(
5588
)
5689
},
5790
relativeFromTo: function(lhs, rhs) {
58-
var newLeft = left + offsetLeft,
91+
let newLeft = left + offsetLeft,
5992
newTop = top + offsetTop
6093
lhs = lhs.offsetParent
6194
rhs = rhs.offsetParent
@@ -75,7 +108,7 @@ export default function scrollIntoViewIfNeeded(
75108
}
76109
}
77110

78-
var parent,
111+
let parent,
79112
area = makeArea(
80113
elem.offsetLeft,
81114
elem.offsetTop,
@@ -84,34 +117,34 @@ export default function scrollIntoViewIfNeeded(
84117
)
85118
while (
86119
(parent = elem.parentNode) instanceof HTMLElement &&
87-
elem !== finalElement
120+
elem !== config.boundary
88121
) {
89-
var clientLeft = parent.offsetLeft + parent.clientLeft
90-
var clientTop = parent.offsetTop + parent.clientTop
122+
const clientLeft = parent.offsetLeft + parent.clientLeft
123+
const clientTop = parent.offsetTop + parent.clientTop
91124

92125
// Make area relative to parent's client area.
93126
area = area.relativeFromTo(elem, parent).translate(-clientLeft, -clientTop)
94127

95-
var scrollLeft = withinBounds(
128+
const scrollLeft = withinBounds(
96129
parent.scrollLeft,
97130
area.right - parent.clientWidth,
98131
area.left,
99132
parent.clientWidth
100133
)
101-
var scrollTop = withinBounds(
134+
const scrollTop = withinBounds(
102135
parent.scrollTop,
103136
area.bottom - parent.clientHeight,
104137
area.top,
105138
parent.clientHeight
106139
)
107-
if (options) {
140+
if (config.animateOptions) {
108141
animate(
109142
parent,
110143
{
111144
scrollLeft: scrollLeft,
112145
scrollTop: scrollTop,
113146
},
114-
options
147+
config.animateOptions
115148
)
116149
} else {
117150
parent.scrollLeft = scrollLeft

0 commit comments

Comments
 (0)