Skip to content

Commit

Permalink
perf: optimize svg root prop handling, simplify element development
Browse files Browse the repository at this point in the history
This change allows modifying the element code without causing it to load
the native components more than once, making live-reload usable.
  • Loading branch information
msand committed Jan 18, 2020
1 parent 0fa4177 commit f0cd11d
Show file tree
Hide file tree
Showing 24 changed files with 164 additions and 145 deletions.
4 changes: 0 additions & 4 deletions __tests__/__snapshots__/css.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ exports[`supports CSS in style element 1`] = `
"backgroundColor": "transparent",
"borderWidth": 0,
},
undefined,
Object {
"opacity": 1,
},
Object {
"flex": 0,
"height": "100%",
Expand Down
65 changes: 44 additions & 21 deletions src/ReactNativeSVG.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import Shape from './elements/Shape';
import Rect, { RNSVGRect } from './elements/Rect';
import Circle, { RNSVGCircle } from './elements/Circle';
import Ellipse, { RNSVGEllipse } from './elements/Ellipse';
import Rect from './elements/Rect';
import Circle from './elements/Circle';
import Ellipse from './elements/Ellipse';
import Polygon from './elements/Polygon';
import Polyline from './elements/Polyline';
import Line, { RNSVGLine } from './elements/Line';
import Svg, { RNSVGSvg } from './elements/Svg';
import Path, { RNSVGPath } from './elements/Path';
import G, { RNSVGGroup } from './elements/G';
import Text, { RNSVGText } from './elements/Text';
import TSpan, { RNSVGTSpan } from './elements/TSpan';
import TextPath, { RNSVGTextPath } from './elements/TextPath';
import Use, { RNSVGUse } from './elements/Use';
import Image, { RNSVGImage } from './elements/Image';
import Symbol, { RNSVGSymbol } from './elements/Symbol';
import Defs, { RNSVGDefs } from './elements/Defs';
import LinearGradient, { RNSVGLinearGradient } from './elements/LinearGradient';
import RadialGradient, { RNSVGRadialGradient } from './elements/RadialGradient';
import Line from './elements/Line';
import Svg from './elements/Svg';
import Path from './elements/Path';
import G from './elements/G';
import Text from './elements/Text';
import TSpan from './elements/TSpan';
import TextPath from './elements/TextPath';
import Use from './elements/Use';
import Image from './elements/Image';
import Symbol from './elements/Symbol';
import Defs from './elements/Defs';
import LinearGradient from './elements/LinearGradient';
import RadialGradient from './elements/RadialGradient';
import Stop from './elements/Stop';
import ClipPath, { RNSVGClipPath } from './elements/ClipPath';
import Pattern, { RNSVGPattern } from './elements/Pattern';
import Mask, { RNSVGMask } from './elements/Mask';
import Marker, { RNSVGMarker } from './elements/Marker';
import ForeignObject, { RNSVGForeignObject } from './elements/ForeignObject';
import ClipPath from './elements/ClipPath';
import Pattern from './elements/Pattern';
import Mask from './elements/Mask';
import Marker from './elements/Marker';
import ForeignObject from './elements/ForeignObject';
import { parse, SvgAst, SvgFromUri, SvgFromXml, SvgUri, SvgXml } from './xml';
import {
SvgCss,
Expand All @@ -31,6 +31,29 @@ import {
SvgWithCssUri,
inlineStyles,
} from './css';
import {
RNSVGCircle,
RNSVGClipPath,
RNSVGDefs,
RNSVGEllipse,
RNSVGForeignObject,
RNSVGGroup,
RNSVGImage,
RNSVGLine,
RNSVGLinearGradient,
RNSVGMarker,
RNSVGMask,
RNSVGPath,
RNSVGPattern,
RNSVGRadialGradient,
RNSVGRect,
RNSVGSvg,
RNSVGSymbol,
RNSVGText,
RNSVGTextPath,
RNSVGTSpan,
RNSVGUse,
} from './elements/NativeComponents';

export {
Svg,
Expand Down
4 changes: 1 addition & 3 deletions src/elements/Circle.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import { extract } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types';
import Shape from './Shape';
import { RNSVGCircle } from './NativeComponents';

export default class Circle extends Shape<{
cx?: NumberProp;
Expand Down Expand Up @@ -31,5 +31,3 @@ export default class Circle extends Shape<{
);
}
}

export const RNSVGCircle = requireNativeComponent('RNSVGCircle');
4 changes: 1 addition & 3 deletions src/elements/ClipPath.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import { extract } from '../lib/extract/extractProps';
import Shape from './Shape';
import { RNSVGClipPath } from './NativeComponents';

export default class ClipPath extends Shape<{}> {
static displayName = 'ClipPath';
Expand All @@ -15,5 +15,3 @@ export default class ClipPath extends Shape<{}> {
);
}
}

export const RNSVGClipPath = requireNativeComponent('RNSVGClipPath');
4 changes: 1 addition & 3 deletions src/elements/Defs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { requireNativeComponent } from 'react-native';
import { RNSVGDefs } from './NativeComponents';

export default class Defs extends Component {
static displayName = 'Defs';
Expand All @@ -8,5 +8,3 @@ export default class Defs extends Component {
return <RNSVGDefs>{this.props.children}</RNSVGDefs>;
}
}

export const RNSVGDefs = requireNativeComponent('RNSVGDefs');
4 changes: 1 addition & 3 deletions src/elements/Ellipse.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import { extract } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types';
import Shape from './Shape';
import { RNSVGEllipse } from './NativeComponents';

export default class Ellipse extends Shape<{
cx?: NumberProp;
Expand Down Expand Up @@ -34,5 +34,3 @@ export default class Ellipse extends Shape<{
);
}
}

export const RNSVGEllipse = requireNativeComponent('RNSVGEllipse');
4 changes: 1 addition & 3 deletions src/elements/ForeignObject.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import { withoutXY } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types';
import G from './G';
import { RNSVGForeignObject } from './NativeComponents';

export default class ForeignObject extends G<{
x?: NumberProp;
Expand Down Expand Up @@ -36,5 +36,3 @@ export default class ForeignObject extends G<{
);
}
}

export const RNSVGForeignObject = requireNativeComponent('RNSVGForeignObject');
4 changes: 1 addition & 3 deletions src/elements/G.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps';
import { extractFont } from '../lib/extract/extractText';
import extractTransform from '../lib/extract/extractTransform';
import { TransformProps } from '../lib/extract/types';
import Shape from './Shape';
import { RNSVGGroup } from './NativeComponents';

export default class G<P> extends Shape<P> {
static displayName = 'G';
Expand Down Expand Up @@ -43,5 +43,3 @@ const hasProps = (obj: {}) => {
}
return false;
};

export const RNSVGGroup = requireNativeComponent('RNSVGGroup');
11 changes: 3 additions & 8 deletions src/elements/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import React from 'react';
import {
Image,
ImageSourcePropType,
requireNativeComponent,
} from 'react-native';
import { meetOrSliceTypes, alignEnum } from '../lib/extract/extractViewBox';
import { Image, ImageSourcePropType } from 'react-native';
import { alignEnum, meetOrSliceTypes } from '../lib/extract/extractViewBox';
import { withoutXY } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types';
import Shape from './Shape';
import { RNSVGImage } from './NativeComponents';

const spacesRegExp = /\s+/;

Expand Down Expand Up @@ -68,5 +65,3 @@ export default class SvgImage extends Shape<{
);
}
}

export const RNSVGImage = requireNativeComponent('RNSVGImage');
4 changes: 1 addition & 3 deletions src/elements/Line.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import { extract } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types';
import Shape from './Shape';
import { RNSVGLine } from './NativeComponents';

export default class Line extends Shape<{
x1?: NumberProp;
Expand Down Expand Up @@ -34,5 +34,3 @@ export default class Line extends Shape<{
);
}
}

export const RNSVGLine = requireNativeComponent('RNSVGLine');
6 changes: 1 addition & 5 deletions src/elements/LinearGradient.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { ReactElement } from 'react';
import { requireNativeComponent } from 'react-native';
import extractGradient from '../lib/extract/extractGradient';
import { NumberProp, TransformProps } from '../lib/extract/types';
import Shape from './Shape';
import { RNSVGLinearGradient } from './NativeComponents';

export default class LinearGradient extends Shape<{
id?: string;
Expand Down Expand Up @@ -39,7 +39,3 @@ export default class LinearGradient extends Shape<{
);
}
}

export const RNSVGLinearGradient = requireNativeComponent(
'RNSVGLinearGradient',
);
4 changes: 1 addition & 3 deletions src/elements/Marker.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import extractViewBox from '../lib/extract/extractViewBox';
import { NumberProp } from '../lib/extract/types';
import Shape from './Shape';
import { RNSVGMarker } from './NativeComponents';

export default class Marker extends Shape<{
id?: string;
Expand Down Expand Up @@ -57,5 +57,3 @@ export default class Marker extends Shape<{
);
}
}

export const RNSVGMarker = requireNativeComponent('RNSVGMarker');
4 changes: 1 addition & 3 deletions src/elements/Mask.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import extractTransform from '../lib/extract/extractTransform';
import { withoutXY } from '../lib/extract/extractProps';
import { NumberProp, TransformProps } from '../lib/extract/types';
import units from '../lib/units';
import Shape from './Shape';
import { RNSVGMask } from './NativeComponents';

export default class Mask extends Shape<{
x?: NumberProp;
Expand Down Expand Up @@ -57,5 +57,3 @@ export default class Mask extends Shape<{
);
}
}

export const RNSVGMask = requireNativeComponent('RNSVGMask');
23 changes: 23 additions & 0 deletions src/elements/NativeComponents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { requireNativeComponent as rnc } from 'react-native';

export const RNSVGSvg = rnc('RNSVGSvgView');
export const RNSVGCircle = rnc('RNSVGCircle');
export const RNSVGClipPath = rnc('RNSVGClipPath');
export const RNSVGDefs = rnc('RNSVGDefs');
export const RNSVGEllipse = rnc('RNSVGEllipse');
export const RNSVGForeignObject = rnc('RNSVGForeignObject');
export const RNSVGGroup = rnc('RNSVGGroup');
export const RNSVGImage = rnc('RNSVGImage');
export const RNSVGLine = rnc('RNSVGLine');
export const RNSVGLinearGradient = rnc('RNSVGLinearGradient');
export const RNSVGMarker = rnc('RNSVGMarker');
export const RNSVGMask = rnc('RNSVGMask');
export const RNSVGPath = rnc('RNSVGPath');
export const RNSVGPattern = rnc('RNSVGPattern');
export const RNSVGRadialGradient = rnc('RNSVGRadialGradient');
export const RNSVGRect = rnc('RNSVGRect');
export const RNSVGSymbol = rnc('RNSVGSymbol');
export const RNSVGText = rnc('RNSVGText');
export const RNSVGTextPath = rnc('RNSVGTextPath');
export const RNSVGTSpan = rnc('RNSVGTSpan');
export const RNSVGUse = rnc('RNSVGUse');
4 changes: 1 addition & 3 deletions src/elements/Path.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import { extract } from '../lib/extract/extractProps';
import Shape from './Shape';
import { RNSVGPath } from './NativeComponents';

export default class Path extends Shape<{
d?: string;
Expand All @@ -15,5 +15,3 @@ export default class Path extends Shape<{
);
}
}

export const RNSVGPath = requireNativeComponent('RNSVGPath');
4 changes: 1 addition & 3 deletions src/elements/Pattern.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import extractTransform from '../lib/extract/extractTransform';
import extractViewBox from '../lib/extract/extractViewBox';
import { NumberProp, TransformProps } from '../lib/extract/types';
import units from '../lib/units';
import Shape from './Shape';
import { RNSVGPattern } from './NativeComponents';

export default class Pattern extends Shape<{
id?: string;
Expand Down Expand Up @@ -66,5 +66,3 @@ export default class Pattern extends Shape<{
);
}
}

export const RNSVGPattern = requireNativeComponent('RNSVGPattern');
6 changes: 1 addition & 5 deletions src/elements/RadialGradient.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { ReactElement } from 'react';
import { requireNativeComponent } from 'react-native';
import extractGradient from '../lib/extract/extractGradient';
import { NumberProp, TransformProps } from '../lib/extract/types';
import Shape from './Shape';
import { RNSVGRadialGradient } from './NativeComponents';

export default class RadialGradient extends Shape<{
fx?: NumberProp;
Expand Down Expand Up @@ -43,7 +43,3 @@ export default class RadialGradient extends Shape<{
);
}
}

export const RNSVGRadialGradient = requireNativeComponent(
'RNSVGRadialGradient',
);
4 changes: 1 addition & 3 deletions src/elements/Rect.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import { withoutXY } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types';
import Shape from './Shape';
import { RNSVGRect } from './NativeComponents';

export default class Rect extends Shape<{
x?: NumberProp;
Expand Down Expand Up @@ -38,5 +38,3 @@ export default class Rect extends Shape<{
);
}
}

export const RNSVGRect = requireNativeComponent('RNSVGRect');
Loading

0 comments on commit f0cd11d

Please sign in to comment.