Skip to content

Commit 65c7660

Browse files
committed
refactor to support tree shake
1 parent 47613e5 commit 65c7660

File tree

3 files changed

+68
-74
lines changed

3 files changed

+68
-74
lines changed

src/calculate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const handleScroll: handleScrollCallback = (
3131
parent.scrollTop = scrollTop
3232
}
3333

34-
export function calculate(target: Element, options: CalculateOptions) {
34+
export const calculate = (target: Element, options: CalculateOptions) => {
3535
if (!target || !(target instanceof HTMLElement)) {
3636
throw new Error('Element is required in scrollIntoViewIfNeeded')
3737
}

src/legacy.ts

Lines changed: 64 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const float32ArraySupported = typeof Float32Array === 'function'
2020
const A = (aA1, aA2) => {
2121
return 1.0 - 3.0 * aA2 + 3.0 * aA1
2222
}
23-
function B(aA1, aA2) {
23+
const B = (aA1, aA2) => {
2424
return 3.0 * aA2 - 6.0 * aA1
2525
}
26-
function C(aA1) {
26+
const C = aA1 => {
2727
return 3.0 * aA1
2828
}
2929

@@ -38,7 +38,7 @@ const getSlope = (aT, aA1, aA2) => {
3838
}
3939

4040
const binarySubdivide = (aX, aA, aB, mX1, mX2) => {
41-
var currentX,
41+
let currentX,
4242
currentT,
4343
i = 0
4444
do {
@@ -74,19 +74,19 @@ const bezier = (mX1, mY1, mX2, mY2) => {
7474
}
7575

7676
// Precompute samples table
77-
var sampleValues = float32ArraySupported
77+
const sampleValues = float32ArraySupported
7878
? new Float32Array(kSplineTableSize)
7979
: new Array(kSplineTableSize)
8080
if (mX1 !== mY1 || mX2 !== mY2) {
81-
for (var i = 0; i < kSplineTableSize; ++i) {
81+
for (let i = 0; i < kSplineTableSize; ++i) {
8282
sampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2)
8383
}
8484
}
8585

8686
const getTForX = aX => {
87-
var intervalStart = 0.0
88-
var currentSample = 1
89-
var lastSample = kSplineTableSize - 1
87+
let intervalStart = 0.0
88+
let currentSample = 1
89+
let lastSample = kSplineTableSize - 1
9090

9191
for (
9292
;
@@ -134,21 +134,49 @@ const bezier = (mX1, mY1, mX2, mY2) => {
134134
}
135135
}
136136

137-
// Predefined set of animations. Similar to CSS easing functions
138-
const animations = {
139-
ease: bezier(0.25, 0.1, 0.25, 1),
140-
easeIn: bezier(0.42, 0, 1, 1),
141-
easeOut: bezier(0, 0, 0.58, 1),
142-
easeInOut: bezier(0.42, 0, 0.58, 1),
143-
linear: bezier(0, 0, 1, 1),
137+
const noop = () => {}
138+
139+
const getScheduler = scheduler => {
140+
if (!scheduler) {
141+
var canRaf = typeof window !== 'undefined' && window.requestAnimationFrame
142+
return canRaf ? rafScheduler() : timeoutScheduler()
143+
}
144+
if (typeof scheduler.next !== 'function')
145+
throw new Error('Scheduler is supposed to have next(cb) function')
146+
if (typeof scheduler.cancel !== 'function')
147+
throw new Error('Scheduler is supposed to have cancel(handle) function')
148+
149+
return scheduler
150+
}
151+
152+
const rafScheduler = () => {
153+
return {
154+
next: window.requestAnimationFrame.bind(window),
155+
cancel: window.cancelAnimationFrame.bind(window),
156+
}
157+
}
158+
159+
const timeoutScheduler = () => {
160+
return {
161+
next: cb => setTimeout(cb, 1000 / 60),
162+
cancel: id => clearTimeout(id),
163+
}
144164
}
145165

146166
export const animate = (source, target, options) => {
147-
var start = Object.create(null)
148-
var diff = Object.create(null)
167+
const start = Object.create(null)
168+
const diff = Object.create(null)
169+
// Predefined set of animations. Similar to CSS easing functions
170+
const animations = {
171+
ease: bezier(0.25, 0.1, 0.25, 1),
172+
easeIn: bezier(0.42, 0, 1, 1),
173+
easeOut: bezier(0, 0, 0.58, 1),
174+
easeInOut: bezier(0.42, 0, 0.58, 1),
175+
linear: bezier(0, 0, 1, 1),
176+
}
149177
options = options || {}
150178
// We let clients specify their own easing function
151-
var easing =
179+
let easing =
152180
typeof options.easing === 'function'
153181
? options.easing
154182
: animations[options.easing]
@@ -161,34 +189,29 @@ export const animate = (source, target, options) => {
161189
easing = animations.ease
162190
}
163191

164-
var step = typeof options.step === 'function' ? options.step : noop
165-
var done = typeof options.done === 'function' ? options.done : noop
192+
const step = typeof options.step === 'function' ? options.step : noop
193+
const done = typeof options.done === 'function' ? options.done : noop
166194

167-
var scheduler = getScheduler(options.scheduler)
195+
const scheduler = getScheduler(options.scheduler)
168196

169-
var keys = Object.keys(target)
197+
const keys = Object.keys(target)
170198
keys.forEach(key => {
171199
start[key] = source[key]
172200
diff[key] = target[key] - source[key]
173201
})
174202

175-
var durationInMs = options.duration || 400
176-
var durationInFrames = Math.max(1, durationInMs * 0.06) // 0.06 because 60 frames pers 1,000 ms
177-
var previousAnimationId
178-
var frame = 0
203+
const durationInMs = options.duration || 400
204+
const durationInFrames = Math.max(1, durationInMs * 0.06) // 0.06 because 60 frames pers 1,000 ms
205+
let previousAnimationId
206+
let frame = 0
179207

180-
previousAnimationId = scheduler.next(loop)
181-
182-
return {
183-
cancel: cancel,
184-
}
185-
186-
function cancel() {
187-
scheduler.cancel(previousAnimationId)
188-
previousAnimationId = 0
208+
const setValues = t => {
209+
keys.forEach(function(key) {
210+
source[key] = diff[key] * t + start[key]
211+
})
189212
}
190213

191-
function loop() {
214+
const loop = () => {
192215
var t = easing(frame / durationInFrames)
193216
frame += 1
194217
setValues(t)
@@ -203,42 +226,12 @@ export const animate = (source, target, options) => {
203226
}
204227
}
205228

206-
function setValues(t) {
207-
keys.forEach(function(key) {
208-
source[key] = diff[key] * t + start[key]
209-
})
210-
}
211-
}
212-
213-
function noop() {}
214-
215-
function getScheduler(scheduler) {
216-
if (!scheduler) {
217-
var canRaf = typeof window !== 'undefined' && window.requestAnimationFrame
218-
return canRaf ? rafScheduler() : timeoutScheduler()
219-
}
220-
if (typeof scheduler.next !== 'function')
221-
throw new Error('Scheduler is supposed to have next(cb) function')
222-
if (typeof scheduler.cancel !== 'function')
223-
throw new Error('Scheduler is supposed to have cancel(handle) function')
224-
225-
return scheduler
226-
}
229+
previousAnimationId = scheduler.next(loop)
227230

228-
function rafScheduler() {
229-
return {
230-
next: window.requestAnimationFrame.bind(window),
231-
cancel: window.cancelAnimationFrame.bind(window),
231+
const cancel = () => {
232+
scheduler.cancel(previousAnimationId)
233+
previousAnimationId = 0
232234
}
233-
}
234235

235-
function timeoutScheduler() {
236-
return {
237-
next: function(cb) {
238-
return setTimeout(cb, 1000 / 60)
239-
},
240-
cancel: function(id) {
241-
return clearTimeout(id)
242-
},
243-
}
236+
return { cancel }
244237
}

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
"declaration": true,
77
"rootDir": "src",
88
"outDir": "./dist",
9-
"removeComments": false,
9+
"removeComments": true,
1010
"noImplicitReturns": true,
1111
"noFallthroughCasesInSwitch": true,
1212
"strictNullChecks": true,
1313
"noUnusedLocals": true,
14-
"noUnusedParameters": true
14+
"noUnusedParameters": true,
15+
"alwaysStrict": true
1516
},
1617
"exclude": ["node_modules", "dist", "example", "**/*-test.ts"]
1718
}

0 commit comments

Comments
 (0)