624 B · text History dc2edab
defmodule DiogramosWeb.QR do
  @moduledoc """
  Renders QR codes to inline SVG, for embedding in a HEEx template via
  `raw/1`.
  """

  @default_width 280

  @doc """
  Renders `payload` as an SVG QR code, `width` pixels square (default
  #{@default_width}).

  Raises if the payload exceeds what the encoder can hold — the limit is
  over 2KB, and a pairing URI is a couple of hundred bytes.
  """
  @spec svg(String.t(), keyword()) :: String.t()
  def svg(payload, opts \\ []) when is_binary(payload) do
    payload
    |> EQRCode.encode()
    |> EQRCode.svg(width: Keyword.get(opts, :width, @default_width))
  end
end