Skip to content

Commit

Permalink
Merge pull request #7 from tokkozhin/dismiss-transparent-background-c…
Browse files Browse the repository at this point in the history
…olor-for-qr-code

feat: add renderBackground prop
  • Loading branch information
tokkozhin authored Dec 11, 2023
2 parents 86320b3 + eeaee5e commit b26dd87
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ maskPattern | undefined | number |[Descript
toSJISFunc | undefined | function |[Description](https://github.com/soldair/node-qrcode#tosjisfunc)
errorCorrectionLevel | 'M' | 'L' \| 'M' \| 'Q' \| 'H' |[Description](https://github.com/soldair/node-qrcode#errorCorrectionLevel)
renderCustomPieceItem | undefined | [RenderCustomPieceItem](#RenderCustomPieceItem) | Render custom piece of QR code. It must return svg component. If it defined, previous piece and eyes configurations won't be work
renderBackground | undefined | (pieceSize: number, bitMatrix: number[][]) => SvgProps['children'] | Ability to add any additional svg components behind qr code
children | undefined | (pieceSize: number, bitMatrix: number[][]) => SvgProps['children'] | Ability to add any additional svg components as children
...rest `<Svg/>` props |

Expand Down
36 changes: 34 additions & 2 deletions example/src/examples/DownloadQR.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useRef } from 'react';

import { StyleSheet, View, Pressable, Text, Alert } from 'react-native';
import QRCodeStyled from 'react-native-qrcode-styled';
import QRCodeStyled, { SVGGradient, SVGQRCodeStyledProps } from 'react-native-qrcode-styled';

import * as FileSystem from 'expo-file-system';
import * as MediaLibrary from 'expo-media-library';
import { Defs, Rect } from 'react-native-svg';
// also need to add MEDIA_LIBRARY permission for android
// https://docs.expo.dev/versions/latest/sdk/media-library/#configuration-in-appjsonappconfigjs

Expand Down Expand Up @@ -38,9 +39,40 @@ export default function DownloadQR() {
}
};

const renderBackground: SVGQRCodeStyledProps['renderBackground'] = (pieceSize, matrix) => {
const size = matrix.length * pieceSize + 50;

return (
<>
<Defs>
<SVGGradient
id="bgGradient"
origin={[0, 0]}
size={size}
type={'linear'}
options={{
colors: ['#01fff2', '#b634e6'],
start: [-0.3, -0.3],
end: [0.7, 0.7],
}}
/>
</Defs>

<Rect x={-25} y={-25} width={size} height={size} fill={'url(#bgGradient)'} />
</>
);
};

return (
<View style={styles.root}>
<QRCodeStyled ref={QRRef} data={'Download QR'} padding={25} pieceSize={8} />
<QRCodeStyled
ref={QRRef}
data={'Download QR'}
padding={25}
pieceSize={8}
color={'white'}
renderBackground={renderBackground}
/>

<Pressable
onPress={handlePressDownload}
Expand Down
6 changes: 6 additions & 0 deletions src/components/SVGQRCodeStyled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface SVGQRCodeStyledProps extends QRCodeOptions, PieceOptions, SvgPr
backgroundImage?: SVGImageProps;
logo?: LogoOptions;
children?: (pieceSize: number, bitMatrix: BitMatrix) => SvgProps['children'];
renderBackground?: (pieceSize: number, bitMatrix: BitMatrix) => SvgProps['children'];
}

function SVGQRCodeStyled(
Expand Down Expand Up @@ -68,6 +69,7 @@ function SVGQRCodeStyled(
toSJISFunc,
errorCorrectionLevel = 'M',
children,
renderBackground,
...props
}: SVGQRCodeStyledProps,
ref?: ForwardedRef<Svg> | null
Expand Down Expand Up @@ -158,6 +160,8 @@ function SVGQRCodeStyled(
</ClipPath>
</Defs>

{renderBackground?.(pieceSize, bitMatrix)}

<SVGImage
x="0"
y="0"
Expand Down Expand Up @@ -209,6 +213,8 @@ function SVGQRCodeStyled(
</Defs>
)}

{renderBackground?.(pieceSize, bitMatrix)}

<G fill={gradient ? 'url(#gradient)' : color}>{renderPieces()}</G>

{renderLogo()}
Expand Down

0 comments on commit b26dd87

Please sign in to comment.