Components

QR code

Text as a QR code: one SVG path in a quiet zone, a logo over its centre, saved as a PNG or an SVG.

Previews are drawn in the theme you made.

Edit it
scheme
size
scheme
direction

Basic #

import { QrCode, QrCodeSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

// A value, encoded on the page: no network, no canvas. The code is drawn on
// a light square whatever the page's scheme, which every reader can scan.
export default example({
  title: 'Basic',
  slots: QrCodeSlots,
  recipe: Recipes.QrCode,
  view: ({ style, h }) =>
    QrCode.view({ id: 'site-qr', value: 'https://foldkit-plus.pages.dev', style }, h),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

import { QrCode, QrCodeSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const LOGO = `data:image/svg+xml,${encodeURIComponent(
  '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><rect width="32" height="32" rx="8" fill="#4f46e5"/><path d="M10 9h12v4h-8v3h7v4h-7v6h-4z" fill="#fff"/></svg>',
)}`

// An overlay raises the error correction to H, so the code still reads under it.
export default example({
  title: 'A logo over the centre',
  slots: QrCodeSlots,
  recipe: Recipes.QrCode,
  view: ({ style, h }) =>
    QrCode.view(
      {
        id: 'logo-qr',
        value: 'https://foldkit-plus.pages.dev/components',
        label: 'QR code for the Foldkit Plus components',
        overlay: h.img([h.Src(LOGO), h.Alt('')]),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Error correction and margin #

import { QrCode, QrCodeSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'

// `level` trades size for resilience: L recovers 7% of the code, H 30%, at
// the cost of more, smaller modules. `quietZone` is the margin a scanner
// needs, four modules by default.
export default example({
  title: 'Error correction and margin',
  slots: QrCodeSlots,
  recipe: Recipes.QrCode,
  pins: { size: 'sm' },
  view: ({ style, h }) =>
    row(h, [
      QrCode.view({ id: 'low-qr', value: 'https://example.com/a/1', level: 'L', style }, h),
      QrCode.view({ id: 'high-qr', value: 'https://example.com/a/1', level: 'H', style }, h),
      QrCode.view({ id: 'tight-qr', value: 'https://example.com/a/1', quietZone: 1, style }, h),
    ]),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Saved as a PNG or an SVG #

import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Button, QrCode, QrCodeSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row, stack } from '../../snippets/layout.js'

const Save = Recipes.Button.style({ variant: 'outline', size: 'sm' })

const Model = Schema.Struct({ saved: Schema.Array(QrCode.Format) })
// `Downloaded` and `DownloadFailed` are what `QrCode.Download` answers.
const Message = defineMessageUnion({
  Pressed: { format: QrCode.Format },
  Downloaded: { format: QrCode.Format },
  DownloadFailed: { message: Schema.String },
})

// The buttons send a Message; `update` answers it with `QrCode.Download`,
// which saves the drawn code in the colours the page shows it in.
export default example({
  title: 'Saved as a PNG or an SVG',
  slots: QrCodeSlots,
  recipe: Recipes.QrCode,
  state: {
    Model,
    Message,
    init: () => ({ model: { saved: [] } }),
    update: (model, message) =>
      Message.match(message, {
        Pressed: ({ format }) => ({
          model,
          commands: [
            QrCode.Download({ id: 'wifi-qr', format, filename: 'guest-wifi', pixels: 512 }),
          ],
        }),
        Downloaded: ({ format }) => ({ model: { saved: [...model.saved, format] } }),
        DownloadFailed: () => ({ model }),
      }),
  },
  view: ({ style, onMessage, h }) =>
    stack(h, [
      QrCode.view({ id: 'wifi-qr', value: 'WIFI:T:WPA;S:Guests;P:welcome-in;;', style }, h),
      row(h, [
        Button.view(
          {
            label: 'Download PNG',
            onClick: onMessage(Message.Pressed({ format: 'png' })),
            style: Save,
          },
          h,
        ),
        Button.view(
          {
            label: 'Download SVG',
            onClick: onMessage(Message.Pressed({ format: 'svg' })),
            style: Save,
          },
          h,
        ),
      ]),
    ]),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Usage

Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

The recipe ↗This code ↗