-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tabs] Reduce the bundle size (#12256)
- Loading branch information
1 parent
d2c16fc
commit e53ef8f
Showing
7 changed files
with
113 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
function easeInOutSin(time) { | ||
return (1 + Math.sin(Math.PI * time - Math.PI / 2)) / 2; | ||
} | ||
|
||
function animate(prop, element, to, options = {}, cb = () => {}) { | ||
const { | ||
ease = easeInOutSin, | ||
duration = 300, // standard | ||
} = options; | ||
|
||
let start = null; | ||
const from = element[prop]; | ||
let cancelled = false; | ||
|
||
const cancel = () => { | ||
cancelled = true; | ||
}; | ||
|
||
const step = timestamp => { | ||
if (cancelled) { | ||
cb(new Error('Animation cancelled')); | ||
return; | ||
} | ||
|
||
if (start === null) { | ||
start = timestamp; | ||
} | ||
const time = Math.min(1, (timestamp - start) / duration); | ||
|
||
element[prop] = ease(time) * (to - from) + from; | ||
|
||
if (time >= 1) { | ||
requestAnimationFrame(() => { | ||
cb(null); | ||
}); | ||
return; | ||
} | ||
|
||
requestAnimationFrame(step); | ||
}; | ||
|
||
if (from === to) { | ||
cb(new Error('Element already at target position')); | ||
return cancel; | ||
} | ||
|
||
requestAnimationFrame(step); | ||
return cancel; | ||
} | ||
|
||
export default animate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { assert } from 'chai'; | ||
import animate from './animate'; | ||
|
||
describe('animate', () => { | ||
let container; | ||
|
||
before(() => { | ||
container = document.createElement('div'); | ||
container.style.cssText = [ | ||
'height: 100px', | ||
'width: 100px', | ||
'overflow: scroll', | ||
'border: 1px solid #000', | ||
].join(';'); | ||
const box = document.createElement('div'); | ||
box.style.cssText = ['height: 100px', 'width: 1000px'].join(';'); | ||
container.appendChild(box); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
after(() => { | ||
document.body.removeChild(container); | ||
}); | ||
|
||
it('should work', done => { | ||
container.scrollLeft = 200; | ||
animate('scrollLeft', container, 300, {}, () => { | ||
assert.strictEqual(container.scrollLeft, 300); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should work when asking for the current value', done => { | ||
container.scrollLeft = 200; | ||
animate('scrollLeft', container, 200, {}, () => { | ||
assert.strictEqual(container.scrollLeft, 200); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should be able to cancel the animatation', done => { | ||
container.scrollLeft = 200; | ||
const cancel = animate('scrollLeft', container, 300, {}, () => { | ||
assert.strictEqual(container.scrollLeft, 200); | ||
done(); | ||
}); | ||
cancel(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters