Sha256: ce9218e578c8b9d99f2a2d9431afcdb9441a9b993a1ad15329df13276f801c4e

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Ariadne
  # Use `Image` to render images.
  #
  # @accessibility
  #   Always provide a meaningful `alt`.
  class ImageComponent < Ariadne::Component
    # @example Default
    #
    #   <%= render(Ariadne::ImageComponent.new(src: "https://github.com/github.png", alt: "GitHub")) %>
    #
    # @example Helper
    #
    #   <%= ariadne_image(src: "https://github.com/github.png", alt: "GitHub") %>
    #
    # @example Lazy loading
    #
    #   <%= render(Ariadne::ImageComponent.new(src: "https://github.com/github.png", alt: "GitHub", lazy: true)) %>
    #
    # @example Custom size
    #
    #   <%= render(Ariadne::ImageComponent.new(src: "https://github.com/github.png", alt: "GitHub", attributes: { height: 100, width: 100 })) %>
    #
    # @param tag [Symbol, String] The rendered tag name
    # @param classes [String] <%= link_to_classes_docs %>
    # @param src [String] The source url of the image.
    # @param alt [String] Specifies an alternate text for the image.
    # @param lazy [Boolean] Whether or not to lazily load the image.
    # @param attributes [Hash] <%= link_to_attributes_docs %>
    def initialize(tag: :img, src:, alt:, lazy: false, classes: "", attributes: {})
      @attributes = attributes

      @src = src
      @tag = check_incoming_tag(:img, tag)
      @classes = classes

      @attributes[:alt] = alt
      @attributes[:src] = image_path(@src)

      return unless lazy

      @attributes[:loading] = :lazy
      @attributes[:decoding] = :async
    end

    def call
      render(Ariadne::BaseComponent.new(tag: @tag, classes: @classes, attributes: @attributes))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.3 app/components/ariadne/image_component.rb
ariadne_view_components-0.0.2 app/components/ariadne/image_component.rb
ariadne_view_components-0.0.1 app/components/ariadne/image_component.rb