Skip to content

First release

Compare
Choose a tag to compare
@stephannv stephannv released this 26 Sep 13:31
· 9 commits to main since this release
class Button < Phlex::HTML
  include Phlex::Variants

  style do
    base "btn"

    variants do
      color do
        default "btn-default"
        primary "btn-primary"
        danger "btn-danger"
      end

      size do
        xs "btn-xs"
        md "btn-md"
        lg "btn-lg"
      end

      outline do
        yes "btn-outline"
      end
    end

    defaults color: :default, size: :md
  end

  attr_reader :color, :size, :outline

  def initialize(color: nil, size: nil, outline: nil)
    @color = color
    @size = size
    @outline = outline
  end

  def view_template(&)
    a(class: build_style(color:, size:, outline:), &)
  end
end

Button.new.call { "Hello" }
# => "<a class="btn btn-default btn-md">Hello<a>"

Button.new(color: :primary, size: :lg, outline: true).call { "Hello" }
# => "<a class="btn btn-primary btn-lg btn-outline">Hello<a>"

Button.build_style(color: :danger, size: :xs)
# => "btn btn-danger btn-xs"