Class: Ariadne::ImageComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/ariadne/image_component.rb

Overview

Use ‘Image` to render images.

Constant Summary

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError, FetchOrFallbackHelper::TRUE_OR_FALSE

Instance Method Summary collapse

Methods included from LoggerHelper

#logger, #silence_deprecations?, #silence_warnings?

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#check_incoming_attribute, #check_incoming_tag, #check_incoming_value, #fetch_or_raise, #fetch_or_raise_boolean

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(tag: :img, src:, alt:, lazy: false, classes: "", attributes: {}) ⇒ ImageComponent

Returns a new instance of ImageComponent.

Examples:

Default


<%= render(Ariadne::ImageComponent.new(src: "https://github.com/github.png", alt: "GitHub")) %>

Helper


<%= ariadne_image(src: "https://github.com/github.png", alt: "GitHub") %>

Lazy loading


<%= render(Ariadne::ImageComponent.new(src: "https://github.com/github.png", alt: "GitHub", lazy: true)) %>

Custom size


<%= render(Ariadne::ImageComponent.new(src: "https://github.com/github.png", alt: "GitHub", attributes: { height: 100, width: 100 })) %>

Parameters:

  • tag (Symbol, String) (defaults to: :img)

    The rendered tag name

  • classes (String) (defaults to: "")

    <%= link_to_classes_docs %>

  • src (String)

    The source url of the image.

  • alt (String)

    Specifies an alternate text for the image.

  • lazy (Boolean) (defaults to: false)

    Whether or not to lazily load the image.

  • attributes (Hash) (defaults to: {})

    <%= link_to_attributes_docs %>



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/components/ariadne/image_component.rb', line 31

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

Instance Method Details

#callObject



47
48
49
# File 'app/components/ariadne/image_component.rb', line 47

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