Sha256: 7a49f9a7f240689397e78084b391f98542620d8b4ef06945253d976ed9193d1a

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Primer
  # Conditionally renders a `Primer::BaseComponent` around the given content. If the given condition
  # is true, a `Primer::BaseComponent` will render around the content. If the condition is false, only
  # the content is rendered.
  #
  # @example True conditional
  #   <%# condition is true, so content will be wrapped in a <span> tag
  #   <%= render Primer::ConditionalWrapper.new(condition: true, tag: :span, class: "foobar")) do %>
  #     <%# also rendered %>
  #     <p class="bazboo">Some text</p>
  #   <% end %>
  #
  # @example False conditional
  #   <%# condition is false so no <span> tag will render around the content (i.e. the <p> tag)
  #   <%= render(Primer::ConditionalWrapper.new(condition: false, tag: :span, class: "foobar")) do %>
  #     <%# this content will be rendered %>
  #     <p class="bazboo">Some text</p>
  #   <% end %>
  #
  # @param condition [Boolean] Whether or not to wrap the content in a `Primer::BaseComponent`.
  # @param base_component_arguments [Hash] The arguments to pass to `Primer::BaseComponent`.
  class ConditionalWrapper < Primer::Component
    warn_on_deprecated_slot_setter

    def initialize(condition:, **base_component_arguments)
      @condition = condition
      @base_component_arguments = base_component_arguments
    end

    def call
      return content unless @condition

      BaseComponent.new(**@base_component_arguments).render_in(self) { content }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
primer_view_components-0.0.115 app/components/primer/conditional_wrapper.rb