Skip to content

Latest commit

 

History

History
56 lines (37 loc) · 1.41 KB

qr_code.md

File metadata and controls

56 lines (37 loc) · 1.41 KB

QrCode

Prerequisites: run component setup (view_primitives:install) once per app.

Container for displaying a QR code. Accepts a pre-rendered image URL or raw SVG/HTML generated by a Ruby gem.

Installation

rails g view_primitives:add qr_code

Creates app/components/ui/qr_code_component.rb.

Refresh after a gem upgrade:

rails g view_primitives:update --only qr_code

Usage

With a pre-rendered image URL

<%= ui :qr_code, src: "https://api.qrserver.com/v1/create-qr-code/?data=https://example.com",
                 alt: "Link to example.com" %>

With the rqrcode gem

Add gem "rqrcode" to your Gemfile, then generate SVG inline:

<%= ui :qr_code, size: 200 do %>
  <%= RQRCode::QRCode.new("https://example.com").as_svg(viewbox: true).html_safe %>
<% end %>

Custom size

size: applies only when using src: — it sets the width and height on the <img>.

<%= ui :qr_code, src: qr_url, alt: "QR code", size: 150 %>

API

Option Type Default Description
src String nil Pre-rendered image URL; renders an <img> when provided
alt String "QR code" Accessible label for the <img>
size Integer 200 Pixel dimensions of the <img> (ignored when using block content)
**html_attrs Hash — Forwarded to the outer <div>