Skip to content

Commit f9984f7

Browse files
committed
working on proper esm support
1 parent e3162de commit f9984f7

File tree

3 files changed

+98
-102
lines changed

3 files changed

+98
-102
lines changed

src/calculate.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export interface ScrollCoordinates {
1212
export type handleScrollCallback = (
1313
parent: HTMLElement,
1414
coordinates: ScrollCoordinates,
15-
config: Options
15+
config: CalculateOptions
1616
) => void
1717

18-
export interface Options {
18+
export interface CalculateOptions {
1919
// A handler that handles scrolling the view to the new coordinates
2020
handleScroll?: handleScrollCallback
2121
boundary?: Element
@@ -31,9 +31,10 @@ const handleScroll: handleScrollCallback = (
3131
parent.scrollTop = scrollTop
3232
}
3333

34-
export default function calculate(target: Element, options: Options) {
35-
if (!target || !(target instanceof HTMLElement))
34+
export function calculate(target: Element, options: CalculateOptions) {
35+
if (!target || !(target instanceof HTMLElement)) {
3636
throw new Error('Element is required in scrollIntoViewIfNeeded')
37+
}
3738

3839
const config = { handleScroll, ...options }
3940
const defaultOffset = { top: 0, right: 0, bottom: 0, left: 0 }

src/index.ts

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,2 @@
1-
import animate from 'amator'
2-
import calculate, { Options as CalculateOptions } from './calculate'
3-
4-
// Legacy
5-
export interface AnimateOptions {
6-
duration?: number
7-
easing?: 'ease' | 'easeIn' | 'easeOut' | 'easeInOut' | 'linear'
8-
}
9-
10-
// Legacy
11-
export interface OffsetConfig {
12-
offsetTop?: number
13-
offsetLeft?: number
14-
offsetBottom?: number
15-
offsetRight?: number
16-
}
17-
18-
export interface Offset {
19-
top?: number
20-
right?: number
21-
bottom?: number
22-
left?: number
23-
}
24-
25-
const handleScroll = (parent, { scrollLeft, scrollTop }, config) => {
26-
if (config.duration) {
27-
animate(
28-
parent,
29-
{
30-
scrollLeft: scrollLeft,
31-
scrollTop: scrollTop,
32-
},
33-
{ duration: config.duration, easing: config.easing }
34-
)
35-
} else {
36-
parent.scrollLeft = scrollLeft
37-
parent.scrollTop = scrollTop
38-
}
39-
}
40-
41-
export interface Options extends CalculateOptions {
42-
// Setting a duration will enable animations
43-
duration?: number
44-
// Easing only take effect if a duration is set
45-
easing?: 'ease' | 'easeIn' | 'easeOut' | 'easeInOut' | 'linear'
46-
}
47-
48-
function isBoolean(options: boolean | Options): options is boolean {
49-
return typeof options === 'boolean'
50-
}
51-
52-
export default function scrollIntoViewIfNeeded(
53-
target: Element,
54-
options: boolean | Options,
55-
animateOptions?: AnimateOptions,
56-
finalElement?: Element,
57-
offsetOptions: OffsetConfig = {}
58-
) {
59-
if (!target || !(target instanceof HTMLElement))
60-
throw new Error('Element is required in scrollIntoViewIfNeeded')
61-
62-
let config: Options = { centerIfNeeded: false, handleScroll }
63-
64-
if (isBoolean(options)) {
65-
config.centerIfNeeded = options
66-
} else {
67-
config = { ...config, ...options }
68-
}
69-
70-
const defaultOffset = { top: 0, right: 0, bottom: 0, left: 0 }
71-
config.offset = config.offset
72-
? { ...defaultOffset, ...config.offset }
73-
: defaultOffset
74-
75-
if (animateOptions) {
76-
config.duration = animateOptions.duration
77-
config.easing = animateOptions.easing
78-
}
79-
80-
if (finalElement) {
81-
config.boundary = finalElement
82-
}
83-
84-
if (offsetOptions.offsetTop) {
85-
config.offset.top = offsetOptions.offsetTop
86-
}
87-
if (offsetOptions.offsetRight) {
88-
config.offset.right = offsetOptions.offsetRight
89-
}
90-
if (offsetOptions.offsetBottom) {
91-
config.offset.bottom = offsetOptions.offsetBottom
92-
}
93-
if (offsetOptions.offsetLeft) {
94-
config.offset.left = offsetOptions.offsetLeft
95-
}
96-
97-
return calculate(target, config)
98-
}
1+
export { calculate } from './calculate'
2+
export { default, Options } from './transition'

src/transition.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import animate from 'amator' /*esm pure*/
2+
import { calculate, CalculateOptions } from './calculate' /*esm pure*/
3+
4+
// Legacy
5+
export interface AnimateOptions {
6+
duration?: number
7+
easing?: 'ease' | 'easeIn' | 'easeOut' | 'easeInOut' | 'linear'
8+
}
9+
10+
// Legacy
11+
export interface OffsetConfig {
12+
offsetTop?: number
13+
offsetLeft?: number
14+
offsetBottom?: number
15+
offsetRight?: number
16+
}
17+
18+
const handleScroll = (parent, { scrollLeft, scrollTop }, config) => {
19+
if (config.duration) {
20+
animate(
21+
parent,
22+
{
23+
scrollLeft: scrollLeft,
24+
scrollTop: scrollTop,
25+
},
26+
{ duration: config.duration, easing: config.easing }
27+
)
28+
} else {
29+
parent.scrollLeft = scrollLeft
30+
parent.scrollTop = scrollTop
31+
}
32+
}
33+
34+
export interface Options extends CalculateOptions {
35+
// Setting a duration will enable animations
36+
duration?: number
37+
// Easing only take effect if a duration is set
38+
easing?: 'ease' | 'easeIn' | 'easeOut' | 'easeInOut' | 'linear'
39+
}
40+
41+
function isBoolean(options: boolean | Options): options is boolean {
42+
return typeof options === 'boolean'
43+
}
44+
45+
export default function scrollIntoViewIfNeeded(
46+
target: Element,
47+
options: boolean | Options,
48+
animateOptions?: AnimateOptions,
49+
finalElement?: Element,
50+
offsetOptions: OffsetConfig = {}
51+
) {
52+
if (!target || !(target instanceof HTMLElement))
53+
throw new Error('Element is required in scrollIntoViewIfNeeded')
54+
55+
let config: Options = { centerIfNeeded: false, handleScroll }
56+
57+
if (isBoolean(options)) {
58+
config.centerIfNeeded = options
59+
} else {
60+
config = { ...config, ...options }
61+
}
62+
63+
const defaultOffset = { top: 0, right: 0, bottom: 0, left: 0 }
64+
config.offset = config.offset
65+
? { ...defaultOffset, ...config.offset }
66+
: defaultOffset
67+
68+
if (animateOptions) {
69+
config.duration = animateOptions.duration
70+
config.easing = animateOptions.easing
71+
}
72+
73+
if (finalElement) {
74+
config.boundary = finalElement
75+
}
76+
77+
if (offsetOptions.offsetTop) {
78+
config.offset.top = offsetOptions.offsetTop
79+
}
80+
if (offsetOptions.offsetRight) {
81+
config.offset.right = offsetOptions.offsetRight
82+
}
83+
if (offsetOptions.offsetBottom) {
84+
config.offset.bottom = offsetOptions.offsetBottom
85+
}
86+
if (offsetOptions.offsetLeft) {
87+
config.offset.left = offsetOptions.offsetLeft
88+
}
89+
90+
return calculate(target, config)
91+
}

0 commit comments

Comments
 (0)