QR 코드 (QR Code)
SlQrCode 컴포넌트는 QR 코드를 생성해 Canvas API로 그려주는 요소예요. QR 코드는 스마트폰으로 빠르게 스캔할 수 있는 작은 정보를 사용자에게 제공할 때 유용합니다. 대부분의 스마트폰에는 QR 코드 스캐너가 내장되어 있어서, 카메라를 QR 코드에 갖다 대기만 하면 자동으로 해독해서 웹사이트 방문, 전화 걸기, 메시지 읽기 등을 할 수 있게 만들어 줍니다.
출처: 문서
본문
직접 값(value)을 넣어서 QR 코드를 만들어 보는 예시예요. 아래 코드를 따라 하면서 어떤 식으로 동작하는지 확인해 보세요.
<div class="qr-overview">
<sl-qr-code value="https://shoelace.style/" label="Scan this code to visit Shoelace on the web!"></sl-qr-code>
<br />
<sl-input maxlength="255" clearable label="Value"></sl-input>
</div>
<script>
const container = document.querySelector('.qr-overview');
const qrCode = container.querySelector('sl-qr-code');
const input = container.querySelector('sl-input');
customElements.whenDefined('sl-qr-code').then(() => {
input.value = qrCode.value;
input.addEventListener('sl-input', () => (qrCode.value = input.value));
});
</script>
<style>
.qr-overview {
max-width: 256px;
}
.qr-overview sl-input {
margin-top: 1rem;
}
</style>
React에서는 이렇게 사용할 수 있어요.
import { useState } from 'react';
import SlQrCode from '@shoelace-style/shoelace/dist/react/qr-code';
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const css = `
.qr-overview {
max-width: 256px;
}
.qr-overview sl-input {
margin-top: 1rem;
}
`;
const App = () => {
const [value, setValue] = useState('https://shoelace.style/');
return (
<>
<div className="qr-overview">
<SlQrCode value={value} label="Scan this code to visit Shoelace on the web!" />
<br />
<SlInput maxlength="255" clearable onInput={event => setValue(event.target.value)} />
</div>
<style>{css}</style>
</>
);
};
예시 (Examples)
색상 (Colors)
fill과 background 속성을 이용하면 QR 코드의 색을 바꿀 수 있어요. QR 코드 스캐너와의 호환성을 위해 항상 대비(contrast)가 좋은 색을 고르는 게 좋습니다.
<sl-qr-code value="https://shoelace.style/" fill="deeppink" background="white"></sl-qr-code>
import SlQrCode from '@shoelace-style/shoelace/dist/react/qr-code';
const App = () => <SlQrCode value="https://shoelace.style/" fill="deeppink" background="white" />;
크기 (Size)
size 속성으로 QR 코드의 크기를 바꿀 수 있어요.
<sl-qr-code value="https://shoelace.style/" size="64"></sl-qr-code>
import SlQrCode from '@shoelace-style/shoelace/dist/react/qr-code';
const App = () => <SlQrCode value="https://shoelace.style/" size="64" />;
모서리 (Radius)
radius 속성을 이용하면 둥근 모서리 효과를 만들 수 있어요.
<sl-qr-code value="https://shoelace.style/" radius="0.5"></sl-qr-code>
import SlQrCode from '@shoelace-style/shoelace/dist/react/qr-code';
const App = () => <SlQrCode value="https://shoelace.style/" radius="0.5" />;
오류 정정 (Error Correction)
QR 코드는 error-correction 속성으로 설정할 수 있는 다양한 오류 정정 레벨로 렌더링할 수 있어요. 아래 예시는 같은 값을 서로 다른 오류 정정 레벨로 4개의 코드를 생성한 모습입니다.
<div class="qr-error-correction">
<sl-qr-code value="https://shoelace.style/" error-correction="L"></sl-qr-code>
<sl-qr-code value="https://shoelace.style/" error-correction="M"></sl-qr-code>
<sl-qr-code value="https://shoelace.style/" error-correction="Q"></sl-qr-code>
<sl-qr-code value="https://shoelace.style/" error-correction="H"></sl-qr-code>
</div>
<style>
.qr-error-correction {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
</style>
import SlQrCode from '@shoelace-style/shoelace/dist/react/qr-code';
const css = `
.qr-error-correction {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
`;
const App = () => {
return (
<>
<div className="qr-error-correction">
<SlQrCode value="https://shoelace.style/" error-correction="L" />
<SlQrCode value="https://shoelace.style/" error-correction="M" />
<SlQrCode value="https://shoelace.style/" error-correction="Q" />
<SlQrCode value="https://shoelace.style/" error-correction="H" />
</div>
<style>{css}</style>
</>
);
};
불러오기 (Importing)
autoloader나 전통적인 loader를 사용 중이라면 이 부분은 무시해도 돼요. 그게 아니라면 아래 스니펫 중 원하는 방식으로 이 컴포넌트만 골라서 불러올 수 있어요.
Script 태그로 CDN에서 이 컴포넌트를 불러오려면:
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/qr-code/qr-code.js"></script>
JavaScript import 구문으로 CDN에서 불러오려면:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/qr-code/qr-code.js';
번들러(bundler)를 사용해 불러오려면:
import '@shoelace-style/shoelace/dist/components/qr-code/qr-code.js';
React 컴포넌트로 불러오려면:
import SlQrCode from '@shoelace-style/shoelace/dist/react/qr-code';
속성 (Properties)
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
value |
The QR code’s value. | string |
'' |
|
label |
The label for assistive devices to announce. If unspecified, the value will be used instead. | string |
'' |
|
size |
The size of the QR code, in pixels. | number |
128 |
|
fill |
The fill color. This can be any valid CSS color, but not a CSS custom property. | string |
'black' |
|
background |
The background color. This can be any valid CSS color or transparent. It cannot be a CSS custom property. |
string |
'white' |
|
radius |
The edge radius of each module. Must be between 0 and 0.5. | number |
0 |
|
errorCorrection error-correction |
The level of error correction to use. Learn more | 'L' | 'M' | 'Q' | 'H' |
'H' |
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
속성(attribute)과 프로퍼티(property)에 대해 더 알아보기.
파트 (Parts)
| Name | Description |
|---|---|
base |
The component’s base wrapper. |
CSS 파트 커스터마이징에 대해 더 알아보기.