Sha256: a975f2889c3b7e086ea65d20f9c44ee254dd202801bd8ac378199fe32595a24e

Contents?: true

Size: 1.38 KB

Versions: 172

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Ariadne
  # All Ariadne ViewComponents accept a standard set of options: classes, which match Tailwind CSS classes, and attributes, which match HTML attributes..
  #
  # Under the hood, any-non class attributes is passed to Rails' [`tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag)
  # (for self-closing tags) or [`content_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag).
  class BaseComponent < Ariadne::Component
    SELF_CLOSING_TAGS = [:area, :base, :br, :col, :embed, :hr, :img, :input, :link, :meta, :param, :source, :track, :wbr].freeze

    def initialize(tag:, classes: "", attributes: {})
      @tag = tag

      @attributes = validate_attributes(tag: tag, attributes: attributes) || {}
      @attributes[:"data-ariadne-view-component"] = true

      classes = @attributes.fetch(:classes, "") if classes.blank?
      @classes = validate_class_names(classes) || {}
      @content_tag_args = {}
    end

    def call
      options = @attributes.merge(@classes)
      if SELF_CLOSING_TAGS.include?(@tag)
        tag(@tag, options)
      else
        content_tag(@tag, content, options)
      end
    end

    private def validate_class_names(classes)
      return if classes.blank?

      { class: Ariadne::ViewComponents.tailwind_merger.merge(classes) }
    end
  end
end

Version data entries

172 entries across 172 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.32-aarch64-linux app/components/ariadne/base_component.rb
ariadne_view_components-0.0.31 app/components/ariadne/base_component.rb
ariadne_view_components-0.0.31-x86_64-linux app/components/ariadne/base_component.rb
ariadne_view_components-0.0.31-x86_64-darwin app/components/ariadne/base_component.rb
ariadne_view_components-0.0.31-x64-mingw32 app/components/ariadne/base_component.rb
ariadne_view_components-0.0.31-x64-mingw-ucrt app/components/ariadne/base_component.rb
ariadne_view_components-0.0.31-arm64-darwin app/components/ariadne/base_component.rb
ariadne_view_components-0.0.31-aarch64-linux app/components/ariadne/base_component.rb
ariadne_view_components-0.0.30 app/components/ariadne/base_component.rb
ariadne_view_components-0.0.30-x86_64-linux app/components/ariadne/base_component.rb
ariadne_view_components-0.0.30-x86_64-darwin app/components/ariadne/base_component.rb
ariadne_view_components-0.0.30-x64-mingw32 app/components/ariadne/base_component.rb
ariadne_view_components-0.0.30-x64-mingw-ucrt app/components/ariadne/base_component.rb
ariadne_view_components-0.0.30-arm64-darwin app/components/ariadne/base_component.rb
ariadne_view_components-0.0.30-aarch64-linux app/components/ariadne/base_component.rb
ariadne_view_components-0.0.29 app/components/ariadne/base_component.rb
ariadne_view_components-0.0.29-x86_64-linux app/components/ariadne/base_component.rb
ariadne_view_components-0.0.29-x86_64-darwin app/components/ariadne/base_component.rb
ariadne_view_components-0.0.29-x64-mingw32 app/components/ariadne/base_component.rb
ariadne_view_components-0.0.29-x64-mingw-ucrt app/components/ariadne/base_component.rb