Lesson 3 — Simple components: props and variants
These are leaf components — they render entirely from their props with no yielding or slots. They are the primitives everything else is built from.
Components::Button
Pico styles <button> beautifully by default. Variants map to Pico’s built-in classes: secondary, contrast (for danger), and outline (for ghost). The default primary style needs no class at all.
|
|
Notice how clean the component is without Tailwind utility strings. The structure — prop declarations, variant lookup, attribute rendering — is immediately clear.
Note:
type: "button"default — an untyped<button>inside a form submits it accidentally. Defaulting to"button"is the safe choice; passtype: "submit"explicitly when needed.
Adding Button to the demo
|
|
Run ruby demo.rb. You should see a row of styled buttons.
Components::Badge
Pico has no native badge component, but <mark> is styled as a highlighted inline element. We use it as the base and add colour variant classes defined in demo.css:
|
|
Add to the demo:
|
|
Components::Avatar
Pico has no native avatar. We use a <div> with a CSS class and rely on demo.css for the circle, sizing, and background:
|
|
Add to the demo:
|
|
Exercise — Components::Link
Create app/components/link.rb. Build a Components::Link component that renders an <a> tag accepting label:, href:, and variant: props.
Pico styles <a> tags by default. It also supports a secondary class for a muted style, and role="button" for a button-styled link.
Expected variants:
:default— standard Pico link (no class needed):secondary— muted link (class"secondary"):button— link that looks like a button (role="button")
Then add it to the demo:
|
|
Solution
|
|