Skip to content

Commit f61018a

Browse files
authored
Support vite2 (#3490)
* refactor: supports vite2 * refactor: remove CommonJS dependencies * chore: update pkg * refactor: update * chore: update pkg
1 parent 75931a1 commit f61018a

File tree

18 files changed

+373
-50
lines changed

18 files changed

+373
-50
lines changed

components/_util/component-classes.ts

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/**
2+
* source by `component-classes`
3+
* https://github.com/component/classes.git
4+
*/
5+
6+
import { indexOf } from 'lodash-es';
7+
8+
/**
9+
* Whitespace regexp.
10+
*/
11+
const re = /\s+/;
12+
13+
export class ClassList {
14+
el: Element;
15+
list: DOMTokenList;
16+
17+
constructor(el: Element) {
18+
if (!el || !el.nodeType) {
19+
throw new Error('A DOM element reference is required');
20+
}
21+
this.el = el;
22+
this.list = el.classList;
23+
}
24+
25+
array() {
26+
const className = this.el.getAttribute('class') || '';
27+
const str = className.replace(/^\s+|\s+$/g, '');
28+
const arr = str.split(re);
29+
if ('' === arr[0]) arr.shift();
30+
return arr;
31+
}
32+
33+
/**
34+
* Add class `name` if not already present.
35+
*
36+
* @param {String} name
37+
* @return {ClassList}
38+
* @api public
39+
*/
40+
add(name: string): ClassList {
41+
// classList
42+
if (this.list) {
43+
this.list.add(name);
44+
return this;
45+
}
46+
47+
// fallback
48+
const arr = this.array();
49+
const i = indexOf(arr, name);
50+
if (!~i) arr.push(name);
51+
this.el.className = arr.join(' ');
52+
return this;
53+
}
54+
/**
55+
* Remove class `name` when present, or
56+
* pass a regular expression to remove
57+
* any which match.
58+
*
59+
* @param {String|RegExp} name
60+
* @return {ClassList}
61+
* @api public
62+
*/
63+
remove(name: string | RegExp): ClassList {
64+
if ('[object RegExp]' === toString.call(name)) {
65+
return this._removeMatching(name as RegExp);
66+
}
67+
68+
// classList
69+
if (this.list) {
70+
this.list.remove(name as string);
71+
return this;
72+
}
73+
74+
// fallback
75+
const arr = this.array();
76+
const i = indexOf(arr, name);
77+
if (~i) arr.splice(i, 1);
78+
this.el.className = arr.join(' ');
79+
return this;
80+
}
81+
/**
82+
* Remove all classes matching `re`.
83+
*
84+
* @param {RegExp} re
85+
* @return {ClassList}
86+
* @api private
87+
*/
88+
_removeMatching(re: RegExp): ClassList {
89+
const arr = this.array();
90+
for (let i = 0; i < arr.length; i++) {
91+
if (re.test(arr[i])) {
92+
this.remove(arr[i]);
93+
}
94+
}
95+
return this;
96+
}
97+
98+
/**
99+
* Toggle class `name`, can force state via `force`.
100+
*
101+
* For browsers that support classList, but do not support `force` yet,
102+
* the mistake will be detected and corrected.
103+
*
104+
* @param {String} name
105+
* @param {Boolean} force
106+
* @return {ClassList}
107+
* @api public
108+
*/
109+
toggle(name: string, force: boolean): ClassList {
110+
// classList
111+
if (this.list) {
112+
if ('undefined' !== typeof force) {
113+
if (force !== this.list.toggle(name, force)) {
114+
this.list.toggle(name); // toggle again to correct
115+
}
116+
} else {
117+
this.list.toggle(name);
118+
}
119+
return this;
120+
}
121+
122+
// fallback
123+
if ('undefined' !== typeof force) {
124+
if (!force) {
125+
this.remove(name);
126+
} else {
127+
this.add(name);
128+
}
129+
} else {
130+
if (this.has(name)) {
131+
this.remove(name);
132+
} else {
133+
this.add(name);
134+
}
135+
}
136+
137+
return this;
138+
}
139+
/**
140+
* Check if class `name` is present.
141+
*
142+
* @param {String} name
143+
* @api public
144+
*/
145+
has(name: string) {
146+
return this.list ? this.list.contains(name) : !!~indexOf(this.array(), name);
147+
}
148+
/**
149+
* Check if class `name` is present.
150+
*
151+
* @param {String} name
152+
* @api public
153+
*/
154+
contains(name: string) {
155+
return this.has(name);
156+
}
157+
}
158+
159+
/**
160+
* Wrap `el` in a `ClassList`.
161+
*
162+
* @param {Element} el
163+
* @return {ClassList}
164+
* @api public
165+
*/
166+
export default function(el: Element): ClassList {
167+
return new ClassList(el);
168+
}

components/_util/css-animation/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://github.com/yiminghe/css-animation 1.5.0
22

33
import Event from './Event';
4-
import classes from 'component-classes';
4+
import classes from '../component-classes';
55
import { requestAnimationTimeout, cancelAnimationTimeout } from '../requestAnimationTimeout';
66

77
const isCssAnimationSupported = Event.endEvents.length !== 0;

components/_util/dom-closest.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* source by `dom-closest`
3+
* https://github.com/necolas/dom-closest.git
4+
*/
5+
6+
import matches from './dom-matches';
7+
8+
/**
9+
* @param element {Element}
10+
* @param selector {String}
11+
* @param context {Element=}
12+
* @return {Element}
13+
*/
14+
export default function(element, selector, context) {
15+
context = context || document;
16+
// guard against orphans
17+
element = { parentNode: element };
18+
19+
while ((element = element.parentNode) && element !== context) {
20+
if (matches(element, selector)) {
21+
return element;
22+
}
23+
}
24+
}

components/_util/dom-matches.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* source by `dom-matches`
3+
* https://github.com/necolas/dom-matches.git
4+
*/
5+
6+
/**
7+
* Determine if a DOM element matches a CSS selector
8+
*
9+
* @param {Element} elem
10+
* @param {String} selector
11+
* @return {Boolean}
12+
* @api public
13+
*/
14+
15+
export default function matches(elem, selector) {
16+
// Vendor-specific implementations of `Element.prototype.matches()`.
17+
const proto = window.Element.prototype;
18+
const nativeMatches =
19+
proto.matches ||
20+
proto.mozMatchesSelector ||
21+
proto.msMatchesSelector ||
22+
proto.oMatchesSelector ||
23+
proto.webkitMatchesSelector;
24+
25+
if (!elem || elem.nodeType !== 1) {
26+
return false;
27+
}
28+
29+
const parentElem = elem.parentNode;
30+
31+
// use native 'matches'
32+
if (nativeMatches) {
33+
return nativeMatches.call(elem, selector);
34+
}
35+
36+
// native support for `matches` is missing and a fallback is required
37+
const nodes = parentElem.querySelectorAll(selector);
38+
const len = nodes.length;
39+
40+
for (let i = 0; i < len; i++) {
41+
if (nodes[i] === elem) {
42+
return true;
43+
}
44+
}
45+
46+
return false;
47+
}

components/_util/json2mq.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* source by `json2mq`
3+
* https://github.com/akiran/json2mq.git
4+
*/
5+
6+
const camel2hyphen = function(str) {
7+
return str
8+
.replace(/[A-Z]/g, function(match) {
9+
return '-' + match.toLowerCase();
10+
})
11+
.toLowerCase();
12+
};
13+
14+
const isDimension = function(feature) {
15+
const re = /[height|width]$/;
16+
return re.test(feature);
17+
};
18+
19+
const obj2mq = function(obj) {
20+
let mq = '';
21+
const features = Object.keys(obj);
22+
features.forEach(function(feature, index) {
23+
let value = obj[feature];
24+
feature = camel2hyphen(feature);
25+
// Add px to dimension features
26+
if (isDimension(feature) && typeof value === 'number') {
27+
value = value + 'px';
28+
}
29+
if (value === true) {
30+
mq += feature;
31+
} else if (value === false) {
32+
mq += 'not ' + feature;
33+
} else {
34+
mq += '(' + feature + ': ' + value + ')';
35+
}
36+
if (index < features.length - 1) {
37+
mq += ' and ';
38+
}
39+
});
40+
return mq;
41+
};
42+
43+
export default function(query) {
44+
let mq = '';
45+
if (typeof query === 'string') {
46+
return query;
47+
}
48+
// Handling array of media queries
49+
if (query instanceof Array) {
50+
query.forEach(function(q, index) {
51+
mq += obj2mq(q);
52+
if (index < query.length - 1) {
53+
mq += ', ';
54+
}
55+
});
56+
return mq;
57+
}
58+
// Handling single media query
59+
return obj2mq(query);
60+
}

components/_util/openAnimation.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import cssAnimation from './css-animation';
2-
import raf from 'raf';
32
import { nextTick } from 'vue';
43

54
function animate(node, show, done) {
@@ -9,7 +8,7 @@ function animate(node, show, done) {
98
return cssAnimation(node, 'ant-motion-collapse-legacy', {
109
start() {
1110
if (appearRequestAnimationFrameId) {
12-
raf.cancel(appearRequestAnimationFrameId);
11+
cancelAnimationFrame(appearRequestAnimationFrameId);
1312
}
1413
if (!show) {
1514
node.style.height = `${node.offsetHeight}px`;
@@ -19,7 +18,7 @@ function animate(node, show, done) {
1918
// not get offsetHeight when appear
2019
// set it into raf get correct offsetHeight
2120
if (height === 0) {
22-
appearRequestAnimationFrameId = raf(() => {
21+
appearRequestAnimationFrameId = requestAnimationFrame(() => {
2322
height = node.offsetHeight;
2423
node.style.height = '0px';
2524
node.style.opacity = '0';
@@ -32,19 +31,19 @@ function animate(node, show, done) {
3231
},
3332
active() {
3433
if (requestAnimationFrameId) {
35-
raf.cancel(requestAnimationFrameId);
34+
cancelAnimationFrame(requestAnimationFrameId);
3635
}
37-
requestAnimationFrameId = raf(() => {
36+
requestAnimationFrameId = requestAnimationFrame(() => {
3837
node.style.height = `${show ? height : 0}px`;
3938
node.style.opacity = show ? '1' : '0';
4039
});
4140
},
4241
end() {
4342
if (appearRequestAnimationFrameId) {
44-
raf.cancel(appearRequestAnimationFrameId);
43+
cancelAnimationFrame(appearRequestAnimationFrameId);
4544
}
4645
if (requestAnimationFrameId) {
47-
raf.cancel(requestAnimationFrameId);
46+
cancelAnimationFrame(requestAnimationFrameId);
4847
}
4948
node.style.height = '';
5049
node.style.opacity = '';

components/_util/raf.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import raf from 'raf';
2-
31
interface RafMap {
42
[id: number]: number;
53
}
@@ -19,19 +17,19 @@ export default function wrapperRaf(callback: () => void, delayFrames = 1): numbe
1917
callback();
2018
delete ids[myId];
2119
} else {
22-
ids[myId] = raf(internalCallback);
20+
ids[myId] = requestAnimationFrame(internalCallback);
2321
}
2422
}
2523

26-
ids[myId] = raf(internalCallback);
24+
ids[myId] = requestAnimationFrame(internalCallback);
2725

2826
return myId;
2927
}
3028

3129
wrapperRaf.cancel = function cancel(pid?: number) {
3230
if (pid === undefined) return;
3331

34-
raf.cancel(ids[pid]);
32+
cancelAnimationFrame(ids[pid]);
3533
delete ids[pid];
3634
};
3735

0 commit comments

Comments
 (0)