forked from godaddy/svgs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
435 lines (392 loc) · 9.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import PropTypes from 'prop-types';
import React from 'react';
import rip from 'rip-out';
/**
* PropType specification where a value can be represented as number and string.
*
* @type {PropTypes}
* @private
*/
const numb = PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]);
/**
* Helper function to copy and paste over properties to a different object if
* they exists.
*
* @param {Object} from Object to copy from.
* @param {Object} to Object to paste to.
* @param {String} props Name of the property
* @private
*/
function copypaste(from, to, ...props) {
props.forEach((prop) => {
if (prop in from) to[prop] = from[prop];
});
}
/**
* The `react-native-svg` has some crazy api's that do not match with the
* properties that can be applied to SVG elements. This prepare function removes
* those properties and adds the properties back in their correct location.
*
* @param {Object} props Properties given to us.
* @returns {Object} Cleaned object.
* @private
*/
function prepare(props) {
const clean = rip(props,
'translate', 'scale', 'rotate', 'skewX', 'skewY', 'originX', 'originY',
'fontFamily', 'fontSize', 'fontWeight', 'fontStyle',
'style'
);
const transform = [];
//
// Correctly apply the transformation properties.
// To apply originX and originY we need to translate the element on those values and
// translate them back once the element is scaled, rotated and skewed.
//
if ('originX' in props || 'originY' in props) transform.push(`translate(${props.originX || 0}, ${props.originY || 0})`);
if ('translate' in props) transform.push(`translate(${props.translate})`);
if ('scale' in props) transform.push(`scale(${props.scale})`);
if ('rotate' in props) transform.push(`rotate(${props.rotate})`);
if ('skewX' in props) transform.push(`skewX(${props.skewX})`);
if ('skewY' in props) transform.push(`skewY(${props.skewY})`);
if ('originX' in props || 'originY' in props) transform.push(`translate(${-props.originX || 0}, ${-props.originY || 0})`);
if (transform.length) clean.transform = transform.join(' ');
//
// Correctly set the initial style value.
//
const style = ('style' in props) ? props.style : {};
//
// This is the nasty part where we depend on React internals to work as
// intended. If we add an empty object as style, it shouldn't render a `style`
// attribute. So we can safely conditionally add things to our `style` object
// and re-introduce it to our `clean` object
//
copypaste(props, style, 'fontFamily', 'fontSize', 'fontWeight', 'fontStyle');
clean.style = style;
//
// React-Native svg provides as a default of `xMidYMid` if aspectRatio is not
// specified with align information. So we need to support this behavior and
// correctly default to `xMidYMid [mode]`.
//
const preserve = clean.preserveAspectRatio;
if (preserve && preserve !== 'none' && !~preserve.indexOf(' ')) {
clean.preserveAspectRatio = 'xMidYMid ' + preserve;
}
return clean;
}
/**
* Return a circle SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Circle SVG.
* @public
*/
function Circle(props) {
return <circle { ...prepare(props) } />;
}
/**
* Return a clipPath SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} ClipPath SVG.
* @public
*/
function ClipPath(props) {
return <clipPath { ...prepare(props) } />;
}
/**
* Return a defs SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Defs SVG.
* @public
*/
function Defs(props) {
return <defs { ...prepare(props) } />;
}
/**
* Return a ellipse SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Ellipse SVG.
* @public
*/
function Ellipse(props) {
return <ellipse { ...prepare(props) } />;
}
/**
* Return a g SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} G SVG.
* @public
*/
function G(props) {
const { x, y, ...rest } = props;
if ((x || y) && !rest.translate) {
rest.translate = `${x || 0}, ${y || 0}`;
}
return <g { ...prepare(rest) } />;
}
/**
* PropType validation for the <G />.
*
* @type {Object}
* @private
*/
G.propTypes = {
x: numb,
y: numb
};
/**
* Return a image SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Image SVG.
* @public
*/
function Image(props) {
return <image { ...prepare(props) } />;
}
/**
* Return a line SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Line SVG.
* @public
*/
function Line(props) {
return <line { ...prepare(props) } />;
}
/**
* Return a linearGradient SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} LinearGradient SVG.
* @public
*/
function LinearGradient(props) {
return <linearGradient { ...prepare(props) } />;
}
/**
* Return a path SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Path SVG.
* @public
*/
function Path(props) {
return <path { ...prepare(props) } />;
}
/**
* Return a polygon SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Polygon SVG.
* @public
*/
function Polygon(props) {
return <polygon { ...prepare(props) } />;
}
/**
* Return a polyline SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Polyline SVG.
* @public
*/
function Polyline(props) {
return <polyline { ...prepare(props) } />;
}
/**
* Return a radialGradient SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} RadialGradient SVG.
* @public
*/
function RadialGradient(props) {
return <radialGradient { ...prepare(props) } />;
}
/**
* Return a rect SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Rect SVG.
* @public
*/
function Rect(props) {
return <rect { ...prepare(props) } />;
}
/**
* Return a stop SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Stop SVG.
* @public
*/
function Stop(props) {
return <stop { ...prepare(props) } />;
}
/**
* Return a SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} SVG.
* @public
*/
function Svg(props) {
const { title, ...rest } = props;
if (title) {
return (
<svg role='img' aria-label='[title]' { ...prepare(rest) }>
<title>{ title }</title>
{ props.children }
</svg>
);
}
return <svg { ...prepare(rest) } />;
}
/**
* PropType validation for the <Svg />.
*
* @type {Object}
* @private
*/
Svg.propTypes = {
title: PropTypes.string,
children: PropTypes.any
};
/**
* Return a symbol SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Symbol SVG.
* @public
*/
function Symbol(props) {
return <symbol { ...prepare(props) } />;
}
/**
* Return a text SVG element.
*
* @returns {React.Component} Text SVG.
* @public
* @param {Object} props The properties that are spread on the SVG element.
* @param {String} props.x x position
* @param {String} props.y y position
* @param {String} props.dx delta x
* @param {String} props.dy delta y
* @param {String} props.rotate rotation
*/
function Text(props) {
const { x, y, dx, dy, rotate, ...rest } = props;
return <text { ...prepare(rest) } { ...{ x, y, dx, dy, rotate } } />;
}
/**
* PropType validation for the <Text />.
*
* @type {Object}
* @private
*/
Text.propTypes = {
x: numb,
y: numb,
dx: numb,
dy: numb,
rotate: numb
};
/**
* Return a tspan SVG element.
*
* @returns {React.Component} TSpan SVG.
* @public
* @param {Object} props The properties that are spread on the SVG element.
* @param {String} props.x x position
* @param {String} props.y y position
* @param {String} props.dx delta x
* @param {String} props.dy delta y
* @param {String} props.rotate rotation
*/
function TSpan(props) {
const { x, y, dx, dy, rotate, ...rest } = props;
return <tspan { ...prepare(rest) } { ...{ x, y, dx, dy, rotate } } />;
}
/**
* PropType validation for the <TSpan />.
*
* @type {Object}
* @private
*/
TSpan.propTypes = Text.propTypes;
/**
* Return a textpath SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} TextPath SVG.
* @public
*/
function TextPath(props) {
return <textPath { ...prepare(props) } />;
}
/**
* Return a use SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Use SVG.
* @public
*/
function Use(props) {
return <use { ...prepare(props) } />;
}
/**
* Return a mask SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Use SVG.
* @public
*/
function Mask(props) {
return <mask { ...prepare(props) } />;
}
/**
* Return a pattern SVG element.
*
* @param {Object} props The properties that are spread on the SVG element.
* @returns {React.Component} Use SVG.
* @public
*/
function Pattern(props) {
return <pattern { ...prepare(props) } />;
}
//
// Expose everything in the same way as `react-native-svg` is doing.
//
export {
Circle,
ClipPath,
Defs,
Ellipse,
G,
Image,
Line,
LinearGradient,
Mask,
Path,
Pattern,
Polygon,
Polyline,
RadialGradient,
Rect,
Stop,
Svg,
Symbol,
TSpan,
Text,
TextPath,
Use
};
export default Svg;