Sha256: 839c9d48a45c3cf16e4db23b30037a548479f6ebce6282a64d7662b0c4281e88

Contents?: true

Size: 1.44 KB

Versions: 122

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Ariadne
  # Creates a ariadne-rounded label that resembles a medicine pill.
  class PillComponent < Ariadne::Component
    DEFAULT_TAG = :span
    TAG_OPTIONS = [DEFAULT_TAG].freeze

    DEFAULT_CLASSES = "ariadne-flex-shrink-0 ariadne-inline-block ariadne-px-2 ariadne-py-1 ariadne-text-xs ariadne-font-medium ariadne-rounded-full ariadne-whitespace-nowrap"

    # @example Default
    #
    #   <%= render(Ariadne::PillComponent.new(color: [49, 186, 115, 1.0])) { "Admin" } %>
    #
    # @param tag [Symbol, String] The rendered tag name.
    # @param color [String] The rgba color of the pill.
    # @param classes [String] <%= link_to_classes_docs %>
    # @param attributes [Hash] <%= link_to_attributes_docs %>
    def initialize(tag: DEFAULT_TAG, color:, classes: "", attributes: {})
      @tag = check_incoming_tag(DEFAULT_TAG, tag)

      @red = color[0]
      @green = color[1]
      @blue = color[2]
      @alpha = color[3]

      @attributes = attributes
      @attributes["style"] = "background-color: rgba(#{@red}, #{@green}, #{@blue}, #{@alpha});"
      @text_color = contrast_of(@red, @green, @blue)

      @classes = merge_class_names(
        DEFAULT_CLASSES,
        classes,
        @text_color,
      )
    end

    private def contrast_of(red, green, blue)
      luminance = (0.299 * red + 0.587 * green + 0.114 * blue) / 255

      luminance > 0.5 ? "ariadne-text-black" : "ariadne-text-white"
    end
  end
end

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.45-x86_64-darwin app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.45-arm64-darwin app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.45-x86_64-linux app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.45-aarch64-linux app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.44-aarch64-linux app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.44-arm64-darwin app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.44-x86_64-linux app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.44-x86_64-darwin app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.44-x64-mingw-ucrt app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.44 app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.39-x86_64-darwin app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.39-x86_64-linux app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.39-x64-mingw32 app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.39-aarch64-linux app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.39-x64-mingw-ucrt app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.39-arm64-darwin app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.39 app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.38-x64-mingw32 app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.38-aarch64-linux app/components/ariadne/pill_component.rb
ariadne_view_components-0.0.38-x86_64-darwin app/components/ariadne/pill_component.rb