1import React from 'react'; 2import * as Svg from 'react-native-svg'; 3 4import Example from './Example'; 5 6const { Defs, G, Path, Use, Symbol, Circle, ClipPath, LinearGradient, RadialGradient, Stop, Rect } = 7 Svg; 8 9class UseExample extends React.Component { 10 static title = 'Reuse svg code'; 11 render() { 12 return ( 13 <Svg.Svg height="100" width="300"> 14 <Defs> 15 <G id="shape"> 16 <G> 17 <Circle cx="50" cy="50" r="50" /> 18 <Rect x="50" y="50" width="50" height="50" /> 19 <Circle cx="50" cy="50" r="5" fill="blue" /> 20 </G> 21 </G> 22 </Defs> 23 <Use href="#shape" x="20" y="0" /> 24 <Use href="#shape" x="170" y="0" /> 25 </Svg.Svg> 26 ); 27 } 28} 29 30class UseShapes extends React.Component { 31 static title = 'Using Shapes Outside of a Defs Element'; 32 render() { 33 return ( 34 <Svg.Svg height="110" width="200"> 35 <G id="shape"> 36 <Rect x="0" y="0" width="50" height="50" /> 37 </G> 38 <Use href="#shape" x="75" y="50" fill="#0f0" /> 39 <Use href="#shape" x="110" y="0" stroke="#0ff" fill="#8a3" rotation="45" origin="25, 25" /> 40 <Use href="#shape" x="150" y="50" stroke="#0f0" strokeWidth="1" fill="none" /> 41 </Svg.Svg> 42 ); 43 } 44} 45 46class DefsExample extends React.Component { 47 static title = 'Basic Defs usage'; 48 49 render() { 50 return ( 51 <Svg.Svg height="100" width="100"> 52 <Defs> 53 <G id="path" x="5" y="2" opacity="0.9"> 54 <Path 55 id="test" 56 d="M38.459,1.66A0.884,0.884,0,0,1,39,2.5a0.7,0.7,0,0,1-.3.575L23.235,16.092,27.58,26.1a1.4,1.4,0,0,1,.148.3,1.3,1.3,0,0,1,0,.377,1.266,1.266,0,0,1-2.078.991L15.526,20.6l-7.58,4.35a1.255,1.255,0,0,1-.485,0,1.267,1.267,0,0,1-1.277-1.258q0-.01,0-0.02a1.429,1.429,0,0,1,0-.446C7.243,20.253,8.6,16.369,8.6,16.29L3.433,13.545A0.743,0.743,0,0,1,2.9,12.822a0.822,0.822,0,0,1,.623-0.773l8.164-2.972,3.018-8.5A0.822,0.822,0,0,1,15.427,0a0.752,0.752,0,0,1,.752.555l2.563,6.936S37.65,1.727,37.792,1.685A1.15,1.15,0,0,1,38.459,1.66Z" 57 /> 58 </G> 59 <ClipPath id="clip"> 60 <Circle r="25%" cx="0%" cy="0%" /> 61 </ClipPath> 62 <LinearGradient id="linear" x1="0%" y1="0%" x2="100%" y2="0%"> 63 <Stop offset="0%" stopColor="yellow" /> 64 <Stop offset="50%" stopColor="red" /> 65 <Stop offset="100%" stopColor="blue" /> 66 </LinearGradient> 67 <RadialGradient id="radial" cx="50%" cy="50%" r="50%" fx="50%" fy="50%"> 68 <Stop offset="0%" stopColor="yellow" /> 69 <Stop offset="50%" stopColor="red" /> 70 <Stop offset="100%" stopColor="blue" /> 71 </RadialGradient> 72 </Defs> 73 <Use href="#path" x="0" fill="blue" opacity="0.6" /> 74 <Use href="#path" x="20" y="5" fill="url(#linear)" /> 75 <Use href="#path" clipPath="url(#clip)" fillOpacity="0.6" stroke="#000" strokeWidth="1" /> 76 <Use href="#path" x="-10" y="20" fill="url(#radial)" /> 77 </Svg.Svg> 78 ); 79 } 80} 81 82class SymbolExample extends React.Component { 83 static title = 'Symbol example, reuse elements with viewBox prop'; 84 render() { 85 return ( 86 <Svg.Svg height="150" width="110"> 87 <Symbol id="symbol" viewBox="0 0 150 110"> 88 <Circle cx="50" cy="50" r="40" strokeWidth="8" stroke="red" fill="red" /> 89 <Circle cx="90" cy="60" r="40" strokeWidth="8" stroke="green" fill="white" /> 90 </Symbol> 91 92 <Use href="#symbol" x="0" y="0" width="100" height="50" /> 93 <Use href="#symbol" x="10" y="50" width="75" height="38" /> 94 <Use href="#symbol" x="20" y="100" width="50" height="25" /> 95 </Svg.Svg> 96 ); 97 } 98} 99 100const icon = ( 101 <Svg.Svg height="20" width="20"> 102 <Defs> 103 <G id="path" scale="0.5"> 104 <Path d="M38.459,1.66A0.884,0.884,0,0,1,39,2.5a0.7,0.7,0,0,1-.3.575L23.235,16.092,27.58,26.1a1.4,1.4,0,0,1,.148.3,1.3,1.3,0,0,1,0,.377,1.266,1.266,0,0,1-2.078.991L15.526,20.6l-7.58,4.35a1.255,1.255,0,0,1-.485,0,1.267,1.267,0,0,1-1.277-1.258q0-.01,0-0.02a1.429,1.429,0,0,1,0-.446C7.243,20.253,8.6,16.369,8.6,16.29L3.433,13.545A0.743,0.743,0,0,1,2.9,12.822a0.822,0.822,0,0,1,.623-0.773l8.164-2.972,3.018-8.5A0.822,0.822,0,0,1,15.427,0a0.752,0.752,0,0,1,.752.555l2.563,6.936S37.65,1.727,37.792,1.685A1.15,1.15,0,0,1,38.459,1.66Z" /> 105 </G> 106 <ClipPath id="clip"> 107 <Circle r="50%" cx="0%" cy="0%" /> 108 </ClipPath> 109 </Defs> 110 <Use href="#path" fill="#3a8" /> 111 <Use 112 href="#path" 113 fill="red" 114 clipPath="url(#clip)" 115 fillOpacity="0.6" 116 stroke="#000" 117 strokeWidth="1" 118 /> 119 </Svg.Svg> 120); 121 122const Reusable: Example = { 123 icon, 124 samples: [UseExample, UseShapes, DefsExample, SymbolExample], 125}; 126 127export default Reusable; 128