diff --git a/.changeset/gentle-lobsters-knock.md b/.changeset/gentle-lobsters-knock.md new file mode 100644 index 0000000..389d6a1 --- /dev/null +++ b/.changeset/gentle-lobsters-knock.md @@ -0,0 +1,5 @@ +--- +"barqode": patch +--- + +Initial release diff --git a/docs/package.json b/docs/package.json index d31cddf..69e64b8 100644 --- a/docs/package.json +++ b/docs/package.json @@ -14,20 +14,21 @@ "check:watch": "pnpm build:content && svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch" }, "devDependencies": { - "@svecodocs/kit": "^0.0.5", + "@svecodocs/kit": "^0.1.1", "@sveltejs/adapter-cloudflare": "^4.8.0", - "@sveltejs/kit": "^2.0.0", - "@sveltejs/vite-plugin-svelte": "^4.0.0", + "@sveltejs/kit": "^2.9.0", + "@sveltejs/vite-plugin-svelte": "^4.0.2", "@tailwindcss/vite": "4.0.0-beta.4", + "barqode": "workspace:^", "mdsx": "^0.0.6", "phosphor-svelte": "^3.0.0", - "svelte": "^5.2.11", - "svelte-check": "^4.0.0", + "svelte": "^5.4.0", + "svelte-check": "^4.1.0", "svelte-preprocess": "^6.0.3", "tailwindcss": "4.0.0-beta.4", - "typescript": "^5.0.0", + "typescript": "^5.7.2", "velite": "^0.2.1", - "vite": "^5.0.11" + "vite": "^5.4.11" }, "private": true } diff --git a/docs/src/content/components/barqode-dropzone.md b/docs/src/content/components/barqode-dropzone.md new file mode 100644 index 0000000..63f2a02 --- /dev/null +++ b/docs/src/content/components/barqode-dropzone.md @@ -0,0 +1,108 @@ +--- +title: BarqodeDropzone +description: Click to upload images, drag & drop or use your camera to scan. +section: Components +--- + + + +This component functions as a file input with the `capture` attribute set to `environment` which allows users to take a picture with their camera. You can also drag-and-drop image files from your desktop or images embedded into other web pages anywhere in the area the component occupies. The images are directly scanned and positive results are indicated by the `onDetect` callback. + +## Demo + + + +## Usage + +```svelte + + +
+ +

Click to upload or drop an image here

+
+
+ +Last detected: {result} + + +``` + +## Props + +### `formats` + +Type: [`BarcodeFormat[]`](https://github.com/Sec-ant/barcode-detector?tab=readme-ov-file#barcode-detector) + +Default: `["qr_code"]` + +Configure the barcode formats to detect. By default, only QR codes are detected. + +If you want to detect multiple formats, pass an array of formats: + +```svelte + +``` + +Under the hood, the standard [BarcodeDetector API](https://developer.mozilla.org/en-US/docs/Web/API/BarcodeDetector) is used. Support varies across devices, operating systems and browsers. All components will prefer to use the native implementation if available and otherwise falls back to a polyfill implementation. + +Note that even if the native implementation is available, the component still might use the polyfill. For example, if the native implementation only supports the format `'qr_code'` but the you select the formats `['qr_code', 'aztec']`. + +### `onDetect` + +Type: `(detectedCodes: DetectedBarcode[]) => void` + +Callback function that is called when a barcode is detected. + +It receives an array of [detected barcodes](https://developer.mozilla.org/en-US/docs/Web/API/BarcodeDetector/detect#return_value), one callback per image. + +If not barcode is detected, the array will be empty. + +### `onDragover` + +Type: `(isDraggingOver: boolean) => void` + +Callback function that is called when a file is dragged over the drop zone. + +### `onError` + +Type: `(error: Error) => void` + +Callback function that is called when an error occurs. + +TODO: insert link to errors. + +### Other props + +The `BarqodeDropzone` component accepts all attributes that a standard `input` element accepts. + +By default, the following attributes are set: + +- `type="file"`. This is required to make the input a file input. You should not change this. +- `name="image"`. This is the name of the file input. +- `accept="image/*"`. This restricts the file types that can be uploaded to images. +- `capture="environment"`. This tells the browser to open the camera when the input is clicked on mobile devices. You can choose between `user` and `environment`, which opens the front and back camera respectively. You can also disable this functionality by setting it to `null`. +- `multiple`. This allows the user to upload multiple files at once. You can disable this by settings this to `false`. + +## Browser Support + +This component depends on the [File Reader API](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) which is widely supported in modern browsers. diff --git a/docs/src/content/components/barqode-stream.md b/docs/src/content/components/barqode-stream.md new file mode 100644 index 0000000..1d11c44 --- /dev/null +++ b/docs/src/content/components/barqode-stream.md @@ -0,0 +1,203 @@ +--- +title: BarqodeStream +description: Continuously scans frames from a camera stream. +section: Components +--- + + + +The `BarqodeStream` component continuously scans frames from a camera stream and detects barcodes in real-time. + +## Demo + + + +## Usage + +```svelte + + +
+ + {#if loading} +
Loading...
+ {/if} +
+
+ +Last detected: {result} +``` + +## Props + +### `constraints` + +Type: [`MediaTrackConstraints`](https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints) + +Default: `{ video: { facingMode: "environment" } }` + +Configure the the various camera options, for example whether to use front or rear camera. + +The object must be of type `MediaTrackConstraints`. + +The object is passed as-is to `getUserMedia`, which is the API call for requesting a camera stream: + +```js +navigator.mediaDevices.getUserMedia({ + audio: false, + video: the_constraint_object_you_provide, +}); +``` + +When `constraints` is updated, a new camera stream is requested which triggers the `onCameraOn` callback again. You can catch errors with the `onError` callback. An error can occur when you try to use the front camera on a device that doesn't have one for example. + +### `formats` + +Type: [`BarcodeFormat[]`](https://github.com/Sec-ant/barcode-detector?tab=readme-ov-file#barcode-detector) + +Default: `["qr_code"]` + +Configure the barcode formats to detect. By default, only QR codes are detected. + +If you want to detect multiple formats, pass an array of formats: + +```svelte + +``` + + + +Don't select more barcode formats than needed. + +Scanning becomes more expensive the more formats you select. + + + +Under the hood, the standard [BarcodeDetector API](https://developer.mozilla.org/en-US/docs/Web/API/BarcodeDetector) is used. Support varies across devices, operating systems and browsers. All components will prefer to use the native implementation if available and otherwise falls back to a polyfill implementation. + +Note that even if the native implementation is available, the component still might use the polyfill. For example, if the native implementation only supports the format `'qr_code'` but the you select the formats `['qr_code', 'aztec']`. + +### `paused` + +Type: `boolean` (bindable) + +Default: `false` + +Whether the camera stream is or should be paused. Bindable, which means that you can pause/unpause the stream from outside the component. + +Pausing the stream by setting `paused` to `true` is useful if you want to show some microinteraction after successful scans. When the you set it to `false`, the camera stream will be restarted and the `onCameraOn` callback function will be triggered again. + +### `torch` + +Type: `boolean` + +Default: `false` + +Turn the camera flashlight on or off. + +This is not consistently supported by all devices and browsers. Support can even vary on the same device with the same browser. For example the rear camera often has a flashlight but the front camera does not. + +We can only tell if flashlight control is supported once the camera is loaded and the `onCameraOn` callback has been called. At the moment, enabling the torch may silently fail on unsupported devices, but in the `onCameraOn` callback payload you can access the `MediaTrackCapabilities` object, from which you can determine if the torch is supported. + +The camera stream must be reloaded when turning the torch on or off. That means the `onCameraOn` event will be emitted again. + +### `track` + +Type: `(detectedCodes: DetectedBarcode[], ctx: CanvasRenderingContext2D) => void` + +Callback function that can be used to visually highlight detected barcodes. + +A transparent canvas is overlaid on top of the camera stream. The `track` function is used to draw on this canvas. + +It receives an array of [detected barcodes](https://developer.mozilla.org/en-US/docs/Web/API/BarcodeDetector/detect#return_value) and a [CanvasRenderingContext2D](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D) as the second argument. + +Note that when `track` is set the scanning frequency has to be increased. So if you want to go easy on your target device you might not want to enable tracking. + + + +The `track` function is called for every frame. It is important to keep the function as performant as possible. + +This can lead to performance issues on low-end devices and memory leaks if not handled correctly. + + + +### `onCameraOn` + +Type: `(capabilities: MediaTrackCapabilities) => void` + +Callback function that is called when the camera stream is successfully loaded. + +It might take a while before the component is ready and the scanning process starts. The user has to be asked for camera access permission first and the camera stream has to be loaded. + +If you want to show a loading indicator, you can wait for this callback to be called. It is called as soon as the camera start streaming. + +The callback receives the a promise which resolves with the cameras [`MediaTrackCapabilities`](https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackCapabilities) when everything is ready. + +### `onError` + +Type: `(error: Error) => void` + +Callback function that is called when an error occurs. + +TODO: insert link to errors. + +### `onCameraOff` + +Type: `() => void` + +Callback function that is called when the camera stream is stopped. + +This can happen when the camera constraints are modified, for example when switching between front and rear camera or when turning the torch on or off. + +### `onDetect` + +Type: `(detectedCodes: DetectedBarcode[]) => void` + +Callback function that is called when a barcode is detected. + +It receives an array of [detected barcodes](https://developer.mozilla.org/en-US/docs/Web/API/BarcodeDetector/detect#return_value). + + + +If you scan the same barcode multiple times in a row, `onDetect` is still only called once. When you hold a barcode in the camera, frames are actually decoded multiple times a second but you don't want to be flooded with callbacks that often. That's why the last decoded QR code is always cached and only new results are propagated. However, changing the value of `paused` resets this internal cache. + + + +## Browser Support + +This component depends on the [Media Capture and Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Media_Capture_and_Streams_API) which is widely supported in modern browsers. diff --git a/docs/src/content/components/button.md b/docs/src/content/components/button.md deleted file mode 100644 index 6aaa66f..0000000 --- a/docs/src/content/components/button.md +++ /dev/null @@ -1,50 +0,0 @@ ---- -title: Button -description: A button component to use in examples and documentation. -section: Components ---- - - - -## Usage - -```svelte title="document.md" - - - - - - - - -``` - -## Example - -### Default Size - - - - - - - - - - - -### Small Size - - - - - - - - - - diff --git a/docs/src/content/components/callout.md b/docs/src/content/components/callout.md deleted file mode 100644 index a7457ff..0000000 --- a/docs/src/content/components/callout.md +++ /dev/null @@ -1,88 +0,0 @@ ---- -title: Callout -description: A callout component to highlight important information. -section: Components ---- - - - -Callouts (also known as _admonitions_) are used to highlight a block of text. There are five types of callouts available: `'note'`, `'warning'`, `'danger'`, `'tip'`, and `'success'`. - -You can override the default icon for the callout by passing a component via the `icon` prop. - -## Usage - -```svelte title="document.md" - - - - - This is a note, used to highlight important information or provide additional context. You can use - markdown in here as well! Just ensure you include a space between the component and the content in - your Markdown file. - - -``` - -## Examples - -### Warning - - - -This is an example of a warning callout. - - - -### Note - - - -This is an example of a note callout. - - - -### Danger - - - -This is an example of a danger callout. - - - -### Tip - - - -This is an example of a tip callout. - - - -### Success - - - -This is an example of a success callout. - - - -### Custom Icon - - - -This is an example of a note callout with a custom icon. - - - -### Custom Title - - - -This is an example of a warning callout with a custom title. - - diff --git a/docs/src/content/configuration/navigation.md b/docs/src/content/configuration/navigation.md deleted file mode 100644 index 506ec69..0000000 --- a/docs/src/content/configuration/navigation.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -title: Navigation -description: Learn how to customize the navigation in your Svecodocs project. -section: Configuration ---- - -Navigation is a key component of every site, documenting the structure of your site and providing a clear path for users to navigate through your content. - -Svecodocs comes with a navigation structure that is designed to be flexible and customizable. Each page in your site should have a corresponding navigation item, and the navigation items should be nested according to their hierarchy. - -## Navigation Structure - -### Anchors - -Anchors are links that are displayed at the top of the sidebar and typically used to either highlight important information or provide quick access to linked content. - -### Sections - -Sections are used to group related navigation items together. They are typically used to organize content into different categories, such as "Components", "Configuration", and "Utilities". diff --git a/docs/src/content/configuration/theme.md b/docs/src/content/configuration/theme.md deleted file mode 100644 index 49b248b..0000000 --- a/docs/src/content/configuration/theme.md +++ /dev/null @@ -1,52 +0,0 @@ ---- -title: Theme -description: Learn how to customize the theme in your Svecodocs project. -section: Configuration ---- - -The theme determines the branded color scheme for your site. A theme for each of the TailwindCSS colors is provided by the `@svecodocs/kit` package. Each theme has been designed to present well in both light and dark mode. - -## Using a theme - -To use a theme, import the theme file into your `src/app.css` file _before_ importing the `@svecodocs/kit/globals.css` file. - -```css -/* @import "@svecodocs/kit/theme-orange.css"; */ -@import "@svecodocs/kit/theme-emerald.css"; -@import "@svecodocs/kit/globals.css"; -``` - -It's not recommended to customize the theme to maintain consistency across the UI components that are provided by Svecodocs and align with the provided themes. - -## Available themes - -| Theme name | Import path | -| ---------- | ---------------------------------- | -| orange | `@svecodocs/kit/theme-orange.css` | -| green | `@svecodocs/kit/theme-green.css` | -| blue | `@svecodocs/kit/theme-blue.css` | -| purple | `@svecodocs/kit/theme-purple.css` | -| pink | `@svecodocs/kit/theme-pink.css` | -| lime | `@svecodocs/kit/theme-lime.css` | -| yellow | `@svecodocs/kit/theme-yellow.css` | -| cyan | `@svecodocs/kit/theme-cyan.css` | -| teal | `@svecodocs/kit/theme-teal.css` | -| violet | `@svecodocs/kit/theme-violet.css` | -| amber | `@svecodocs/kit/theme-amber.css` | -| red | `@svecodocs/kit/theme-red.css` | -| sky | `@svecodocs/kit/theme-sky.css` | -| emerald | `@svecodocs/kit/theme-emerald.css` | -| fuchsia | `@svecodocs/kit/theme-fuchsia.css` | -| rose | `@svecodocs/kit/theme-rose.css` | - -## Tailwind Variables - -Svecodocs uses TailwindCSS to style the UI components and provides a set of Tailwind variables that can be used to style your examples/custom components. - -### Gray - -We override the TailwindCSS `gray` color scale to provide our own grays. - -### Brand - -You can use the `brand` color to use the brand color of your project. diff --git a/docs/src/content/demos/full-demo.md b/docs/src/content/demos/full-demo.md new file mode 100644 index 0000000..838c3c1 --- /dev/null +++ b/docs/src/content/demos/full-demo.md @@ -0,0 +1,253 @@ +--- +title: Full demo +description: Kitchen sink demo of the BarqodeStream component. +section: Demos +--- + + + +Modern mobile phones often have a variety of different cameras installed (e.g. front, rear, +wide-angle, infrared, desk-view). The one picked by default is sometimes not the best +choice. For more fine-grained control, you can select a camera by device constraints or by +the device ID. + +Detected codes are visually highlighted in real-time. In this demo you can use the track function dropdown to change the flavor. + +By default only QR-codes are detected but a variety of other barcode formats are also supported. +You can select one or multiple but the more you select the more expensive scanning becomes. + +## Demo + + + +## Usage + +```svelte + + + + + + + + + +{#each Object.keys(barcodeFormats) as option} + {@const barcodeOption = option as BarcodeFormat} +
+ + +
+{/each} + +{#if error} + {error} +{/if} + +
+ +
+ +Last result: {result} +``` diff --git a/docs/src/content/getting-started.md b/docs/src/content/getting-started.md index 564de98..a0957f1 100644 --- a/docs/src/content/getting-started.md +++ b/docs/src/content/getting-started.md @@ -1,82 +1,43 @@ --- title: Getting Started -description: A quick guide to get started using Svecodocs +description: A quick guide to get started using barqode section: Overview --- - - -The following guide will walk you through the process of getting a Svecodocs project up and running. +The following guide will walk you through installing and setting up the barqode components in your Svelte project. -## Clone the starter template +## Installation -Clone the Svecodocs starter template: +Install the package using your preferred package manager: ```bash -pnpx degit svecosystem/svecodocs/starter -``` - -## Navigation - -The starter template comes with a basic navigation structure to get your started. To customize the navigation, adjust the `src/lib/navigation.ts` file. - -```ts -import { createNavigation } from "@svecodocs/kit"; - -export const navigation = createNavigation({ - // Customize the navigation here -}); -``` - -## Site config - -The site config is used to configure site-wide settings, such as the title, description, keywords, ogImage, and other metadata. - -The config is located in the `src/lib/site-config.ts` file. - -```ts -import { defineSiteConfig } from "@svecodocs/kit"; - -export const siteConfig = defineSiteConfig({ - title: "Svecodocs", - description: "A SvelteKit docs starter template", - keywords: ["sveltekit", "docs", "starter", "template"], - ogImage: { - url: "https://docs.sveco.dev/og.png", - height: 630, - width: 1200, - }, -}); +npm install barqode ``` -### Per-Route Site Config - -You can override any part of the site config on a per-route basis using the `useSiteConfig` hook. +## Basic Usage - -This feature is still being worked on. - +The simplest way to use the library is with the `BarqodeStream` component for live scanning: -## Theme +```svelte + -```css {1-2} -/* @import "@svecodocs/kit/themes/orange.css"; */ -@import "@svecodocs/kit/themes/emerald.css"; -@import "@svecodocs/kit/globals.css"; +
+ +
+ + ``` -## Logo - -To customize the logo displayed in the sidebar header, head to the `src/routes/(docs)/+layout.svelte` file and adjust the contents of the `logo` snippet. If the logo has a light and dark version, ensure to handle those similarly to the default Svecosystem logo. - -```svelte title="src/routes/(docs)/+layout.svelte" -{#snippet logo()} -