惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
J
Java Code Geeks
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
月光博客
月光博客
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
L
LangChain Blog
腾讯CDC
Y
Y Combinator Blog
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
MyScale Blog
MyScale Blog
博客园 - Franky
IT之家
IT之家
博客园_首页

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
A Simpler ButtonComponent: Just Render a Div
Tony Rowan · 2026-05-31 · via DEV Community
Cover image for A Simpler ButtonComponent: Just Render a Div

Tony Rowan

A couple of weeks ago I wrote about trying to build a universal ButtonComponent — one with no custom CSS classes. The result worked but was complicated. The night I wrote it up, a simpler approach just came to me.

The trick: a ButtonComponent that just renders a div with the correct styles applied. No magic methods, no parameter fudging, just a div that looks like a button. The caller is responsible for what it does — it already knows whether it needs an anchor, a form submit, or something else entirely. The component just makes it look right.

Usage

Since the component just renders a regular div, you can use it with any Rails helper that accepts a block or plain old HTML.

<%= link_to calendar_path do %>
  <%= render ButtonComponent.new(icon: :calendar, label: t("navigation.calendar")) %>
<% end %>

<%= content_tag(:button, type: :submit) do %>
  <%= render ButtonComponent.new(icon: :check, label: t("actions.save")) %>
<% end %>

<%= form.button(type: :submit) do %>
  <%= render ButtonComponent.new(icon: :check, label: t("actions.submit")) %>
<% end %>

<button>
  <%= render ButtonComponent.new(icon: :check) %>
</button>

Enter fullscreen mode Exit fullscreen mode

Anything that wraps HTML works. The component doesn't care.

The Component

The component renders the icon and label with the correct colours. It's dead simple.

class ButtonComponent < ViewComponent::Component
  BASE_CLASSES = "cursor-pointer block font-bold ..."

  COLOURS = {
    grape: "bg-grape-600 hover:bg-grape-700 text-white",
    cherry: "bg-cherry-600 hover:bg-cherry-700 text-white"
  }.freeze

  def initialize(icon: nil, label: nil, colour: :grape)
    ...
  end

  def classes
    class_list(BASE_CLASSES, COLOURS.fetch(colour))
  end
end

Enter fullscreen mode Exit fullscreen mode

<%= content_tag(:div, class: classes) do %>
  <% if icon %>
    <%= render IconComponent.new(icon) %>
  <% end %>

  <% if label %>
    <%= content_tag(:span, label) %>
  <% end %>
<% end %>

Enter fullscreen mode Exit fullscreen mode

That's it! Well...plus a few more parameters if you want to control the CSS classes for the icon and the label from the caller.

The component manages how it looks. How it behaves is up to the caller, just as it should be.

Drawbacks

This trades flexibility for consistency and enforcement. The universal button component could enforce aria labels or data attributes much more strictly. This version doesn't afford that, but in return anything can be made to look like a button — not just the element types the component pre-defines. It brings back the flexibility of the CSS approach, with a bit more structural consistency and less duplication when buttons have internal structure like icons with labels.

Where I'm At

I still think just using custom CSS classes for buttons (i.e. button-grape) is the way to go for most projects, especially if your buttons are structurally simple. But this is a workable alternative if your buttons have a little going on internally and you want that to stay consistent. Responsibilities are where they ought to be and it scales to every new usage automatically — no need to keep faffing with magic method definitions. A much more attractive proposition.


Originally posted on tonyrowan.tech