shoelace/docs/components/qr-code.md

162 wiersze
4.2 KiB
Markdown
Czysty Zwykły widok Historia

2021-04-22 19:42:23 +00:00
# QR Code
[component-header:sl-qr-code]
Generates a [QR code](https://www.qrcode.com/) and renders it using the [Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API).
QR codes are useful for providing small pieces of information to users who can quickly scan them with a smartphone. Most smartphones have built-in QR code scanners, so simply pointing the camera at a QR code will decode it and allow the user to visit a website, dial a phone number, read a message, etc.
```html preview
2021-07-10 00:38:55 +00:00
<div class="qr-overview">
<sl-qr-code value="https://shoelace.style/" label="Scan this code to visit Shoelace on the web!"></sl-qr-code>
2022-03-02 15:10:41 +00:00
<br />
2021-07-10 00:38:55 +00:00
<sl-input maxlength="255" clearable></sl-input>
</div>
<script>
const container = document.querySelector('.qr-overview');
const qrCode = container.querySelector('sl-qr-code');
const input = container.querySelector('sl-input');
input.value = qrCode.value;
2022-03-02 15:10:41 +00:00
input.addEventListener('sl-input', () => (qrCode.value = input.value));
2021-07-10 00:38:55 +00:00
</script>
<style>
.qr-overview {
max-width: 256px;
}
.qr-overview sl-input {
margin-top: 1rem;
}
</style>
2021-04-22 19:42:23 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { useState } from 'react';
import { SlQrCode, SlInput } from '@shoelace-style/shoelace/dist/react';
const css = `
.qr-overview {
max-width: 256px;
}
.qr-overview sl-input {
margin-top: 1rem;
}
`;
const App = () => {
const [value, setValue] = useState('https://shoelace.style/');
return (
<>
2021-11-05 14:06:06 +00:00
<div className="qr-overview">
2021-11-04 22:12:47 +00:00
<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>
</>
);
};
```
2021-04-22 19:42:23 +00:00
## Examples
### Colors
2021-07-08 21:41:10 +00:00
Use the `fill` and `background` attributes to modify the QR code's colors. You should always ensure good contrast for optimal compatibility with QR code scanners.
2021-04-22 19:42:23 +00:00
```html preview
<sl-qr-code value="https://shoelace.style/" fill="deeppink" background="white"></sl-qr-code>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlQrCode } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => <SlQrCode value="https://shoelace.style/" fill="deeppink" background="white" />;
2021-11-04 22:12:47 +00:00
```
2021-04-22 19:42:23 +00:00
### Size
2021-07-08 21:41:10 +00:00
Use the `size` attribute to change the size of the QR code.
2021-04-22 19:42:23 +00:00
```html preview
<sl-qr-code value="https://shoelace.style/" size="64"></sl-qr-code>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlQrCode } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => <SlQrCode value="https://shoelace.style/" size="64" />;
2021-11-04 22:12:47 +00:00
```
2021-04-22 19:42:23 +00:00
### Radius
2021-07-08 21:41:10 +00:00
Create a rounded effect with the `radius` attribute.
2021-04-22 19:42:23 +00:00
```html preview
<sl-qr-code value="https://shoelace.style/" radius="0.5"></sl-qr-code>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlQrCode } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => <SlQrCode value="https://shoelace.style/" radius="0.5" />;
2021-11-04 22:12:47 +00:00
```
2021-04-22 19:42:23 +00:00
### Error Correction
QR codes can be rendered with various levels of [error correction](https://www.qrcode.com/en/about/error_correction.html) that can be set using the `error-correction` attribute. This example generates four codes with the same value using different error correction levels.
```html preview
<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>
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlQrCode } from '@shoelace-style/shoelace/dist/react';
const css = `
.qr-error-correction {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
`;
const App = () => {
return (
<>
2021-11-05 14:06:06 +00:00
<div className="qr-error-correction">
2021-11-04 22:12:47 +00:00
<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>
</>
);
};
```
2021-04-22 19:42:23 +00:00
[component-metadata:sl-qr-code]