Skip to content

Commit d67f4ff

Browse files
[Tabs] Reduce the bundle size
1 parent 869692e commit d67f4ff

File tree

7 files changed

+96
-46
lines changed

7 files changed

+96
-46
lines changed

.size-limit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = [
2727
name: 'The size of all the modules of material-ui.',
2828
webpack: true,
2929
path: 'packages/material-ui/build/index.js',
30-
limit: '94.9 KB',
30+
limit: '94.7 KB',
3131
},
3232
{
3333
name: 'The main bundle of the docs',

packages/material-ui/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"react-jss": "^8.1.0",
6161
"react-transition-group": "^2.2.1",
6262
"recompose": "^0.27.0",
63-
"scroll": "^2.0.3",
6463
"warning": "^4.0.1"
6564
},
6665
"sideEffects": false,

packages/material-ui/src/Tabs/Tabs.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import classNames from 'classnames';
77
import EventListener from 'react-event-listener';
88
import debounce from 'debounce'; // < 1kb payload overhead when lodash/debounce is > 3kb.
99
import { getNormalizedScrollLeft, detectScrollType } from 'normalize-scroll-left';
10-
// TODO: should we fork it?
11-
// https://github.com/michaelrhodes/scroll/issues/10
12-
import scroll from 'scroll';
10+
import animate from '../internal/animate';
1311
import ScrollbarSize from './ScrollbarSize';
1412
import withStyles from '../styles/withStyles';
1513
import TabIndicator from './TabIndicator';
@@ -199,7 +197,7 @@ class Tabs extends React.Component {
199197
const nextScrollLeft = this.tabsRef.scrollLeft + delta * multiplier;
200198
// Fix for Edge
201199
const invert = theme.direction === 'rtl' && detectScrollType() === 'reverse' ? -1 : 1;
202-
scroll.left(this.tabsRef, invert * nextScrollLeft);
200+
this.scroll(invert * nextScrollLeft);
203201
};
204202

205203
scrollSelectedIntoView = () => {
@@ -213,14 +211,18 @@ class Tabs extends React.Component {
213211
if (tabMeta.left < tabsMeta.left) {
214212
// left side of button is out of view
215213
const nextScrollLeft = tabsMeta.scrollLeft + (tabMeta.left - tabsMeta.left);
216-
scroll.left(this.tabsRef, nextScrollLeft);
214+
this.scroll(nextScrollLeft);
217215
} else if (tabMeta.right > tabsMeta.right) {
218216
// right side of button is out of view
219217
const nextScrollLeft = tabsMeta.scrollLeft + (tabMeta.right - tabsMeta.right);
220-
scroll.left(this.tabsRef, nextScrollLeft);
218+
this.scroll(nextScrollLeft);
221219
}
222220
};
223221

222+
scroll = value => {
223+
animate('scrollLeft', this.tabsRef, value);
224+
};
225+
224226
updateScrollButtonState = () => {
225227
const { scrollable, scrollButtons, theme } = this.props;
226228

packages/material-ui/src/Tabs/Tabs.test.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import { assert } from 'chai';
33
import { spy, stub, useFakeTimers } from 'sinon';
4-
import scroll from 'scroll';
54
import { ShallowWrapper } from 'enzyme';
65
import consoleErrorMock from 'test/utils/consoleErrorMock';
76
import { createShallow, createMount, getClasses, unwrap } from '../test-utils';
@@ -574,19 +573,19 @@ describe('<Tabs />', () => {
574573
let metaStub;
575574

576575
beforeEach(() => {
577-
scrollStub = stub(scroll, 'left');
578576
const wrapper = shallow(
579577
<Tabs width="md" onChange={noop} value={0} scrollable>
580578
<Tab />
581579
<Tab />
582580
</Tabs>,
583581
);
584582
instance = wrapper.instance();
583+
scrollStub = stub(instance, 'scroll');
585584
metaStub = stub(instance, 'getTabsMeta');
586585
});
587586

588587
afterEach(() => {
589-
scroll.left.restore();
588+
instance.scroll.restore();
590589
});
591590

592591
it('should scroll left tab into view', () => {
@@ -596,7 +595,7 @@ describe('<Tabs />', () => {
596595
});
597596

598597
instance.scrollSelectedIntoView();
599-
assert.strictEqual(scrollStub.args[0][1], 0, 'should scroll to 0 position');
598+
assert.strictEqual(scrollStub.args[0][0], 0, 'should scroll to 0 position');
600599
});
601600

602601
it('should scroll right tab into view', () => {
@@ -606,7 +605,7 @@ describe('<Tabs />', () => {
606605
});
607606

608607
instance.scrollSelectedIntoView();
609-
assert.strictEqual(scrollStub.args[0][1], 10, 'should scroll to 10 position');
608+
assert.strictEqual(scrollStub.args[0][0], 10, 'should scroll to 10 position');
610609
});
611610

612611
it('should support value=false', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function easeInOutSin(time) {
2+
return (1 + Math.sin(Math.PI * time - Math.PI / 2)) / 2;
3+
}
4+
5+
function animate(prop, element, to, options = {}, cb = () => {}) {
6+
const {
7+
ease = easeInOutSin,
8+
duration = 300, // standard
9+
} = options;
10+
11+
let start = null;
12+
const from = element[prop];
13+
let cancelled = false;
14+
15+
const cancel = () => {
16+
cancelled = true;
17+
};
18+
19+
const step = timestamp => {
20+
if (cancelled) {
21+
cb(new Error('Animation cancelled'));
22+
return;
23+
}
24+
25+
if (start === null) {
26+
start = timestamp;
27+
}
28+
const time = Math.min(1, (timestamp - start) / duration);
29+
30+
element[prop] = ease(time) * (to - from) + from;
31+
32+
if (time >= 1) {
33+
requestAnimationFrame(() => {
34+
cb(null);
35+
});
36+
return;
37+
}
38+
39+
requestAnimationFrame(step);
40+
};
41+
42+
if (from === to) {
43+
cb(new Error('Element already at target position'));
44+
return cancel;
45+
}
46+
47+
requestAnimationFrame(step);
48+
return cancel;
49+
}
50+
51+
export default animate;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { assert } from 'chai';
2+
import animate from './animate';
3+
4+
describe('animate', () => {
5+
let container;
6+
7+
before(() => {
8+
container = document.createElement('div');
9+
container.style.cssText = [
10+
'height: 100px',
11+
'width: 100px',
12+
'overflow: scroll',
13+
'border: 1px solid #000',
14+
].join(';');
15+
const box = document.createElement('div');
16+
box.style.cssText = ['height: 100px', 'width: 1000px'].join(';');
17+
container.appendChild(box);
18+
document.body.appendChild(container);
19+
});
20+
21+
after(() => {
22+
document.body.removeChild(container);
23+
});
24+
25+
it('should work', done => {
26+
container.scrollLeft = 200;
27+
animate('scrollLeft', container, 300, {}, () => {
28+
assert.strictEqual(container.scrollLeft, 300);
29+
done();
30+
});
31+
});
32+
});

yarn.lock

-33
Original file line numberDiff line numberDiff line change
@@ -3771,10 +3771,6 @@ dom-serializer@0, dom-serializer@~0.1.0:
37713771
domelementtype "~1.1.1"
37723772
entities "~1.1.1"
37733773

3774-
dom-walk@^0.1.0:
3775-
version "0.1.1"
3776-
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.1.tgz#672226dc74c8f799ad35307df936aba11acd6018"
3777-
37783774
domain-browser@^1.1.1:
37793775
version "1.2.0"
37803776
resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-1.2.0.tgz#3d31f50191a6749dd1375a7f522e823d42e54eda"
@@ -5096,13 +5092,6 @@ global-dirs@^0.1.0:
50965092
dependencies:
50975093
ini "^1.3.4"
50985094

5099-
global@~4.3.0:
5100-
version "4.3.2"
5101-
resolved "https://registry.yarnpkg.com/global/-/global-4.3.2.tgz#e76989268a6c74c38908b1305b10fc0e394e9d0f"
5102-
dependencies:
5103-
min-document "^2.19.0"
5104-
process "~0.5.1"
5105-
51065095
globals@^11.1.0, globals@^11.7.0:
51075096
version "11.7.0"
51085097
resolved "https://registry.yarnpkg.com/globals/-/globals-11.7.0.tgz#a583faa43055b1aca771914bf68258e2fc125673"
@@ -6964,12 +6953,6 @@ mimic-response@^1.0.0:
69646953
version "1.0.1"
69656954
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b"
69666955

6967-
min-document@^2.19.0:
6968-
version "2.19.0"
6969-
resolved "https://registry.yarnpkg.com/min-document/-/min-document-2.19.0.tgz#7bd282e3f5842ed295bb748cdd9f1ffa2c824685"
6970-
dependencies:
6971-
dom-walk "^0.1.0"
6972-
69736956
minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1:
69746957
version "1.0.1"
69756958
resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz#2e194de044626d4a10e7f7fbc00ce73e83e4d5c7"
@@ -8100,10 +8083,6 @@ process@^0.11.10:
81008083
version "0.11.10"
81018084
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
81028085

8103-
process@~0.5.1:
8104-
version "0.5.2"
8105-
resolved "https://registry.yarnpkg.com/process/-/process-0.5.2.tgz#1638d8a8e34c2f440a91db95ab9aeb677fc185cf"
8106-
81078086
progress@^2.0.0:
81088087
version "2.0.0"
81098088
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.0.tgz#8a1be366bf8fc23db2bd23f10c6fe920b4389d1f"
@@ -8275,12 +8254,6 @@ raf@^3.4.0:
82758254
dependencies:
82768255
performance-now "^2.1.0"
82778256

8278-
rafl@~1.2.1:
8279-
version "1.2.2"
8280-
resolved "https://registry.yarnpkg.com/rafl/-/rafl-1.2.2.tgz#fe930f758211020d47e38815f5196a8be4150740"
8281-
dependencies:
8282-
global "~4.3.0"
8283-
82848257
railroad-diagrams@^1.0.0:
82858258
version "1.0.0"
82868259
resolved "https://registry.yarnpkg.com/railroad-diagrams/-/railroad-diagrams-1.0.0.tgz#eb7e6267548ddedfb899c1b90e57374559cddb7e"
@@ -9185,12 +9158,6 @@ schema-utils@^0.4.2, schema-utils@^0.4.3, schema-utils@^0.4.4, schema-utils@^0.4
91859158
ajv "^6.1.0"
91869159
ajv-keywords "^3.1.0"
91879160

9188-
scroll@^2.0.3:
9189-
version "2.0.3"
9190-
resolved "https://registry.yarnpkg.com/scroll/-/scroll-2.0.3.tgz#0951b785544205fd17753bc3d294738ba16fc2ab"
9191-
dependencies:
9192-
rafl "~1.2.1"
9193-
91949161
section-iterator@^2.0.0:
91959162
version "2.0.0"
91969163
resolved "https://registry.yarnpkg.com/section-iterator/-/section-iterator-2.0.0.tgz#bf444d7afeeb94ad43c39ad2fb26151627ccba2a"

0 commit comments

Comments
 (0)