Module: UI::AspectRatioBehavior

Included in:
AspectRatio, AspectRatioComponent
Defined in:
app/behaviors/ui/aspect_ratio_behavior.rb

Overview

UI::AspectRatioBehavior

Constant Summary collapse

DEFAULT_RATIO =

Default aspect ratio is 1:1 (square)

1.0

Instance Method Summary collapse

Instance Method Details

#aspect_ratio_classesObject

Merge base classes with custom classes



28
29
30
# File 'app/behaviors/ui/aspect_ratio_behavior.rb', line 28

def aspect_ratio_classes
  TailwindMerge::Merger.new.merge([@classes].compact.join(" "))
end

#aspect_ratio_content_attributesObject

Build attributes for the content wrapper



50
51
52
53
54
# File 'app/behaviors/ui/aspect_ratio_behavior.rb', line 50

def aspect_ratio_content_attributes
  {
    style: "position: absolute; top: 0; right: 0; bottom: 0; left: 0;"
  }
end

#aspect_ratio_html_attributesObject

Build complete HTML attributes hash for aspect ratio container



33
34
35
36
37
38
39
40
# File 'app/behaviors/ui/aspect_ratio_behavior.rb', line 33

def aspect_ratio_html_attributes
  base_attrs = @attributes || {}
  base_attrs.merge(
    class: aspect_ratio_classes,
    "data-slot": "aspect-ratio",
    style: "position: relative; width: 100%;"
  )
end

#aspect_ratio_paddingObject

Calculate padding-bottom percentage for aspect ratio Formula: (height / width) * 100



21
22
23
24
25
# File 'app/behaviors/ui/aspect_ratio_behavior.rb', line 21

def aspect_ratio_padding
  ratio_value = @ratio || DEFAULT_RATIO
  percentage = (1.0 / ratio_value) * 100
  "#{percentage}%"
end

#aspect_ratio_padding_attributesObject

Build attributes for the padding element



43
44
45
46
47
# File 'app/behaviors/ui/aspect_ratio_behavior.rb', line 43

def aspect_ratio_padding_attributes
  {
    style: "padding-bottom: #{aspect_ratio_padding};"
  }
end