Skip to content

Commit 6e3da8f

Browse files
committed
fix: use unique ids for background gradient
1 parent 06b5246 commit 6e3da8f

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

apps/playground/src/App.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function App() {
2929
const qrCodeOptions: ReactQRCodeProps = {
3030
ref,
3131
value: 'https://github.com/LGLabGreg/react-qr-code.git',
32-
size: 500,
32+
size: 256,
3333
marginSize: 3,
3434
background: '#f9f9f9',
3535
gradient: {
@@ -55,8 +55,8 @@ function App() {
5555
},
5656
imageSettings: {
5757
src: 'https://jd279l8p5w.ufs.sh/f/9xWilHEwGmJB6M4WMqmNVvfeTgSFGJm94uCp2xbtk87Zs1a3',
58-
width: 60,
59-
height: 60,
58+
width: 30,
59+
height: 30,
6060
excavate: true,
6161
x: 100,
6262
y: 100,
@@ -118,6 +118,29 @@ function App() {
118118
<button onClick={() => download({ format: 'jpeg' })}>Download JPEG</button>
119119
</div>
120120
<ReactQRCode {...qrCodeOptions} />
121+
<ReactQRCode
122+
background={{
123+
type: 'linear',
124+
rotation: 45,
125+
stops: [
126+
{ offset: '0%', color: '#ff7e5f' },
127+
{ offset: '100%', color: '#feb47b' },
128+
],
129+
}}
130+
size={256}
131+
value='https://reactqrcode.com'
132+
/>
133+
<ReactQRCode
134+
background={{
135+
type: 'radial',
136+
stops: [
137+
{ offset: '0%', color: '#f00' },
138+
{ offset: '100%', color: '#0f0' },
139+
],
140+
}}
141+
size={256}
142+
value='https://reactqrcode.com'
143+
/>
121144
</div>
122145
</div>
123146
)

packages/react-qr-code/src/components/background.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useId } from 'react'
2+
13
import { BG_GRADIENT_ID } from '../constants'
24
import type { BackgroundSettings } from '../types/lib'
35
import { calculateGradientVectors } from '../utils/svg'
@@ -12,6 +14,8 @@ const testProps = {
1214
}
1315

1416
export const Background = ({ background, numCells }: BackgroundProps) => {
17+
const uuid = useId()
18+
const backgroundId = `${BG_GRADIENT_ID}-${uuid}`
1519
if (!background) {
1620
return null
1721
}
@@ -28,14 +32,14 @@ export const Background = ({ background, numCells }: BackgroundProps) => {
2832
<>
2933
<defs>
3034
{background.type === 'linear' ? (
31-
<linearGradient id={BG_GRADIENT_ID} gradientUnits='userSpaceOnUse' {...vectors}>
35+
<linearGradient id={backgroundId} gradientUnits='userSpaceOnUse' {...vectors}>
3236
{background.stops?.map((stop, index) => (
3337
<stop key={index} offset={stop.offset} stopColor={stop.color} />
3438
))}
3539
</linearGradient>
3640
) : (
3741
<radialGradient
38-
id={BG_GRADIENT_ID}
42+
id={backgroundId}
3943
gradientUnits='userSpaceOnUse'
4044
cx='50%'
4145
cy='50%'
@@ -48,7 +52,7 @@ export const Background = ({ background, numCells }: BackgroundProps) => {
4852
)}
4953
</defs>
5054
<path
51-
fill={`url(#${BG_GRADIENT_ID})`}
55+
fill={`url(#${backgroundId})`}
5256
d={`M0,0 h${numCells}v${numCells}H0z`}
5357
{...testProps}
5458
/>

packages/react-qr-code/src/react-qr-code.test.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,22 @@ describe('ReactQRCode', () => {
150150
const { container } = render(<ReactQRCode value='test' background={gradient} />)
151151

152152
const backgroundPath = screen.getByTestId('background')
153-
const stops = container.querySelectorAll(`${selector}#${BG_GRADIENT_ID} stop`)
153+
const gradientElement = container.querySelector(
154+
`${selector}[id^="${BG_GRADIENT_ID}-"]`,
155+
)
156+
157+
const stops = gradientElement?.querySelectorAll('stop')
154158

155-
expect(backgroundPath).toHaveAttribute('fill', `url(#${BG_GRADIENT_ID})`)
159+
expect(backgroundPath).toHaveAttribute('fill', `url(#${gradientElement?.id})`)
156160
expect(container.querySelector(`${selector}`)).toBeInTheDocument()
157-
expect(stops).toHaveLength(2)
158-
expect(stops[0]).toHaveAttribute('stop-color', gradient.stops[0].color)
159-
expect(stops[0]).toHaveAttribute('offset', gradient.stops[0].offset)
160-
expect(stops[1]).toHaveAttribute('stop-color', gradient.stops[1].color)
161-
expect(stops[1]).toHaveAttribute('offset', gradient.stops[1].offset)
161+
expect(stops?.length).toBe(2)
162+
163+
if (stops) {
164+
expect(stops[0]).toHaveAttribute('stop-color', gradient.stops[0].color)
165+
expect(stops[0]).toHaveAttribute('offset', gradient.stops[0].offset)
166+
expect(stops[1]).toHaveAttribute('stop-color', gradient.stops[1].color)
167+
expect(stops[1]).toHaveAttribute('offset', gradient.stops[1].offset)
168+
}
162169
})
163170
})
164171

0 commit comments

Comments
 (0)