Menu

Badge

Default badge
Success badge
Critical badge
Pill badge
render Badge.new { "Default badge" }
render Badge.new(color: :success) { "Success badge" }
render Badge.new(color: :critical) { "Critical badge" }
render Badge.new(kind: :pill) { "Pill badge" }

Installation

Add the component to your project

CLI

Run the following command in your terminal

bundle exec essence add badge
Manually

Copy and paste the following code into your project

components/badge.rb
# frozen_string_literal: true

class Components::Badge < Components::Essence
  attr_reader :color, :kind, :size

  def initialize(color: :primary, kind: :default, size: :md, **attributes)
    @color = color
    @kind = kind
    @size = size
    super(**attributes)
  end

  def view_template(&)
    div(**attributes, &)
  end

  private

  def component_classes
    {
      _: {
        _: "inline-flex items-center justify-center w-fit border border-transparent font-medium transition duration-150 hover:opacity-90",
        color: {
          none: "",
          primary: "text-gray-900 border-gray-200",
          success: "text-white bg-emerald-500",
          critical: "text-white bg-rose-500",
          warning: "text-white bg-amber-500",
          info: "text-white bg-blue-500",
          dark: "text-white bg-gray-900",
          white: "text-gray-900 bg-white"
        },
        kind: {
          none: "",
          default: "rounded-sm",
          pill: "rounded-full"
        },
        size: {
          none: "",
          sm: "text-[0.65rem] px-2 py-0.5 gap-1 min-w-8",
          md: "text-xs px-2.5 py-1 gap-2 min-w-12",
          lg: "text-sm px-4 py-1 gap-2 min-w-16"
        }
      }
    }.freeze
  end
end

Reference

This is how you can use this component

Attribute

Description

kind
Badge kind
Available values —
primarysuccesscriticalwarninginfodarkwhite
size
Badge size
Available values —
smmdlg