Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add logo to svg #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/useQRCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,43 @@ function useSVGComponent() {
const SVGComponent = <T extends HTMLDivElement>({
text,
options,
logo,
}: IQRCode) => {
const inputRef = React.useRef<T>(null);

React.useEffect(() => {
QRCode.toString(text, options, function (error: Error, svg: string) {
if (error) {
throw error;
}
let _svg = '';
let x = 0;
let y = 0;
if (logo) {
const logoWidth = logo?.options?.width || 30;
if (logo?.options?.x && logo?.options?.y) {
x = logo?.options?.x;
y = logo?.options?.y;
} else if (!logo?.options?.x || !logo?.options?.y) {
// let margin = options?.margin;
// margin = !margin ? (margin === 0 ? 0 : 32) : margin * 8;
// const width = options?.width || 116 + margin;
// const center = (width - logoWidth) / 2;
}
const image = `<image xlink:href=${logo?.src} width="${logoWidth}" height=${logoWidth} x=${x} y=${y} preserveAspectRatio="none" />`;
const position = svg.indexOf('</svg>');
_svg = [
svg.slice(0, position),
image,
svg.slice(position),
].join('');
} else {
_svg = svg;
}
if (inputRef.current instanceof HTMLDivElement) {
inputRef.current.innerHTML = svg;
inputRef.current.innerHTML = _svg;
}
});
}, [text, options]);
}, [text, options, logo]);

return <div ref={inputRef} />;
};
Expand Down
1 change: 1 addition & 0 deletions supports/create-next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"next": "12.3.1",
"next-qrcode": "file:../..",
"react": "18.2.0",
"react-dom": "18.2.0"
},
Expand Down
14 changes: 9 additions & 5 deletions supports/create-next-app/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ const Home: NextPage = () => {
<SVG
text={'https://github.com/bunlong/next-qrcode'}
options={{
type: 'image/jpeg',
quality: 0.3,
level: 'M',
margin: 3,
scale: 4,
margin: 5,
width: 200,
color: {
dark: '#010599FF',
light: '#FFBF60FF',
},
}}
logo={{
src: 'https://cdn-icons-png.flaticon.com/512/124/124010.png',
options: {
width: 5,
x: 0,
y: 0,
}
}}
/>
</>
)
Expand Down