Skip to content

Commit 99c9892

Browse files
committed
WIP
1 parent 1492f1c commit 99c9892

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

.github/workflows/ci-cd.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ name: "CI/CD: Continuous integration and continuous deployment"
99
tags:
1010
- "v*"
1111
pull_request:
12-
merge_group:
1312

1413
jobs:
1514
build-check-test-push:

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ yarn-error.log
88
/templates
99
graphql.config.json
1010
output/*
11+
<<<<<<< Updated upstream
1112
.env
13+
<<<<<<< Updated upstream
1214
.env.local
15+
=======
16+
=======
17+
.env*
18+
!.env.template
19+
>>>>>>> Stashed changes
20+
>>>>>>> Stashed changes
1321
secrets
1422
/test/results/*

src/components/inlineSVG.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1-
import React, { useRef, useEffect } from 'react';
1+
import React, { useRef, useEffect, useMemo } from 'react';
22
import PropTypes from 'prop-types';
33

44
const InlineSVG = ({ src, ...otherProps }) => {
5-
return (
6-
<div
7-
// eslint-disable-next-line react/no-danger
8-
dangerouslySetInnerHTML={{ __html: src }}
9-
{...otherProps}
10-
/>
11-
);
5+
const svgId = Math.random()
6+
.toString(36)
7+
.slice(2, 12);
8+
const processedSVG = useMemo(() => {
9+
const prefix = `svg-${svgId}`;
10+
11+
return src.replace(
12+
/<style>([\s\S]*?)<\/style>|class="([^"]+)"|id="([^"]+)"/g,
13+
(match, styles, classAttr, idAttr) => {
14+
if (styles) {
15+
return `<style>${styles
16+
// Class selectors: only match when followed by { or , (selector context)
17+
.replace(/(^|[^\w-])\.([a-zA-Z_][\w-]*)(?=\s*[{,])/gm, `$1.${prefix}-$2`)
18+
// ID selectors: only match when followed by { or , (selector context)
19+
.replace(/(^|[^\w-])#([a-zA-Z_][\w-]*)(?=\s*[{,])/gm, `$1#${prefix}-$2`)}</style>`;
20+
}
21+
if (classAttr) {
22+
return `class="${classAttr
23+
.split(/\s+/)
24+
.map(c => `${prefix}-${c}`)
25+
.join(' ')}"`;
26+
}
27+
if (idAttr) {
28+
return `id="${prefix}-${idAttr}"`;
29+
}
30+
return match;
31+
},
32+
);
33+
}, [svgId, src]);
34+
35+
return <div dangerouslySetInnerHTML={{ __html: processedSVG }} {...otherProps} />;
1236
};
1337

1438
InlineSVG.propTypes = {

0 commit comments

Comments
 (0)