This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Neon-20/qrcode
qrcode
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import QRCodeStyling from 'qr-code-styling'; | ||
|
||
interface GenerateQRProps { | ||
url: string | URL; | ||
size?: number; | ||
background?: string; | ||
color?: string; | ||
} | ||
|
||
const GenerateQR: React.FC<GenerateQRProps> = ({ url, size = 512, background = 'white', color = 'black' }) => { | ||
const qrCodeRef = useRef<HTMLDivElement | null>(null); | ||
|
||
useEffect(() => { | ||
if (qrCodeRef.current) { | ||
const qrCode = new QRCodeStyling({ | ||
width: size, | ||
height: size, | ||
data: url.toString(), | ||
image: '', | ||
margin: 20, | ||
qrOptions: { | ||
typeNumber: 0, | ||
mode: 'Byte', | ||
errorCorrectionLevel: 'Q', | ||
}, | ||
dotsOptions: { | ||
color: color, | ||
type: 'square', | ||
}, | ||
backgroundOptions: { | ||
color: background, | ||
}, | ||
}); | ||
|
||
qrCode.append(qrCodeRef.current); | ||
qrCode.update(); | ||
} | ||
}, [url, size, background, color]); | ||
|
||
return ( | ||
<div ref={qrCodeRef}></div> | ||
); | ||
}; | ||
|
||
export default GenerateQR; |