Skip to content

Commit b3193e7

Browse files
committed
add props.children so you can inject stuff in your svg (eg: <defs> that can be used with --allow-override-fill)
1 parent d3f7f3a commit b3193e7

109 files changed

Lines changed: 139 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/adjust-svg.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ const prepareSvgProps = (svg: string): string => {
2727
};
2828

2929
const injectSvgJsProps = (svg: string): string => {
30-
return svg.replace(">", " {...props}>");
30+
// Inject {...props} and {props.children} just after the opening tag of <svg> or <Svg>
31+
// 1. Inject {...props} as before
32+
// 2. Immediately after, inject {props.children}
33+
// This ensures that children can be injected by React
34+
return svg
35+
.replace(/(\{\.\.\.props\}>)/, "$1{props.children}")
36+
.replace(/(>)/, " {...props}>{props.children}");
3137
};
3238

3339
const dashToCamelCaseProps = (svg: string): string => {

tests/components/with-native-for-typescript/SVGClean.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type { SvgProps } from "react-native-svg";
2828
const SVGClean = (props: SvgProps) => {
2929
return (
3030
<Svg viewBox="0 0 30 30" {...props}>
31+
{props.children}
3132
<Path d="m15 3c-6.627 0-12 5.373-12 12 0 5.623 3.872 10.328 9.092 11.63-.056-.162-.092-.35-.092-.583v-2.051c-.487 0-1.303 0-1.508 0-.821 0-1.551-.353-1.905-1.009-.393-.729-.461-1.844-1.435-2.526-.289-.227-.069-.486.264-.451.615.174 1.125.596 1.605 1.222.478.627.703.769 1.596.769.433 0 1.081-.025 1.691-.121.328-.833.895-1.6 1.588-1.962-3.996-.411-5.903-2.399-5.903-5.098 0-1.162.495-2.286 1.336-3.233-.276-.94-.623-2.857.106-3.587 1.798 0 2.885 1.166 3.146 1.481.896-.307 1.88-.481 2.914-.481 1.036 0 2.024.174 2.922.483.258-.313 1.346-1.483 3.148-1.483.732.731.381 2.656.102 3.594.836.945 1.328 2.066 1.328 3.226 0 2.697-1.904 4.684-5.894 5.097 1.098.573 1.899 2.183 1.899 3.396v2.734c0 .104-.023.179-.035.268 4.676-1.639 8.035-6.079 8.035-11.315 0-6.627-5.373-12-12-12z" />
3233
</Svg>
3334
);

tests/components/with-native-for-typescript/SVGEdgeCaseWidth.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type { SvgProps } from "react-native-svg";
2828
const SVGEdgeCaseWidth = (props: SvgProps) => {
2929
return (
3030
<Svg viewBox="0 0 512 512" {...props}>
31+
{props.children}
3132
<Path d="M336 192h40a40 40 0 0140 40v192a40 40 0 01-40 40H136a40 40 0 01-40-40V232a40 40 0 0140-40h40M336 128l-80-80-80 80M256 321V48" />
3233
</Svg>
3334
);

tests/components/with-native-for-typescript/SVGMatrixNegative.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type { SvgProps } from "react-native-svg";
2828
const SVGMatrixNegative = (props: SvgProps) => {
2929
return (
3030
<Svg viewBox="0 0 288 272" {...props}>
31+
{props.children}
3132
<LinearGradient
3233
id="a"
3334
x1="19.028507%"

tests/components/with-native-for-typescript/SVGPctUnit.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type { SvgProps } from "react-native-svg";
2828
const SVGPctUnit = (props: SvgProps) => {
2929
return (
3030
<Svg {...props}>
31+
{props.children}
3132
<Circle cx="2%" cy="-4.0%" />
3233
</Svg>
3334
);

tests/components/with-native-for-typescript/SVGSimple.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type { SvgProps } from "react-native-svg";
2828
const SVGSimple = (props: SvgProps) => {
2929
return (
3030
<Svg viewBox="0 0 512 512" {...props}>
31+
{props.children}
3132
<Path
3233
d="M336 192h40a40 40 0 0140 40v192a40 40 0 01-40 40H136a40 40 0 01-40-40V232a40 40 0 0140-40h40M336 128l-80-80-80 80M256 321V48"
3334
fill="none"

tests/components/with-native-for-typescript/SVGSketchExport.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import type { SvgProps } from "react-native-svg";
2828
const SVGSketchExport = (props: SvgProps) => {
2929
return (
3030
<Svg viewBox="0 0 97 171" {...props}>
31+
{props.children}
32+
3133
<G
3234
id="Page-1"
3335
stroke="none"

tests/components/with-native-for-typescript/SVGWithFill.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type { SvgProps } from "react-native-svg";
2828
const SVGWithFill = (props: SvgProps) => {
2929
return (
3030
<Svg viewBox="0 0 30 30" {...props}>
31+
{props.children}
3132
<G fill="none" fillRule="evenodd">
3233
<Path d="m0 0h30v30h-30z" fill="#dd4b39" />
3334
<Path

tests/components/with-native-for-typescript/SVGWithStroke.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import type { SvgProps } from "react-native-svg";
2828
const SVGWithStroke = (props: SvgProps) => {
2929
return (
3030
<Svg viewBox="0 0 512 512" {...props}>
31+
{props.children}
32+
3133
<Path
3234
d="M256,160c16-63.16,76.43-95.41,208-96a15.94,15.94,0,0,1,16,16V368a16,16,0,0,1-16,16c-128,0-177.45,25.81-208,64-30.37-38-80-64-208-64-9.88,0-16-8.05-16-17.93V80A15.94,15.94,0,0,1,48,64C179.57,64.59,240,96.84,256,160Z"
3335
fill="none"

tests/components/with-native-for-typescript/SVGWithStyleAndData.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type { SvgProps } from "react-native-svg";
2828
const SVGWithStyleAndData = (props: SvgProps) => {
2929
return (
3030
<Svg {...props}>
31+
{props.children}
3132
<Mask style={{ maskType: "alpha" }} />
3233
<Circle cx="90" cy="90" data-circle="true" fill="black" />
3334
</Svg>

0 commit comments

Comments
 (0)