Aspect Ratio
Displays content within a desired ratio.
Usage
16/9
AspectRatio(aspect_ratio: "16/9", class: "rounded-md overflow-hidden border shadow-sm") do img( alt: "Placeholder", loading: "lazy", src: helpers.image_path('pattern.jpg') ) end
Copied!
Copy failed!
4/3
AspectRatio(aspect_ratio: "4/3", class: "rounded-md overflow-hidden border shadow-sm") do img( alt: "Placeholder", loading: "lazy", src: helpers.image_path('pattern.jpg') ) end
Copied!
Copy failed!
1/1
AspectRatio(aspect_ratio: "1/1", class: "rounded-md overflow-hidden border shadow-sm") do img( alt: "Placeholder", loading: "lazy", src: helpers.image_path('pattern.jpg') ) end
Copied!
Copy failed!
21/9
AspectRatio(aspect_ratio: "21/9", class: "rounded-md overflow-hidden border shadow-sm") do img( alt: "Placeholder", loading: "lazy", src: helpers.image_path('pattern.jpg') ) end
Copied!
Copy failed!
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component AspectRatio
Copied!
Copy failed!
Manual installation
1
Add RubyUI::AspectRatio
to app/components/ruby_ui/aspect_ratio.rb
# frozen_string_literal: true module RubyUI class AspectRatio < Base def initialize(aspect_ratio: "16/9", **attrs) raise "aspect_ratio must be in the format of a string with a slash in the middle (eg. '16/9', '1/1')" unless aspect_ratio.is_a?(String) && aspect_ratio.include?("/") @aspect_ratio = aspect_ratio super(**attrs) end def view_template(&block) div( class: "relative w-full", style: "padding-bottom: #{padding_bottom}%;" ) do div(**attrs, &block) end end private def padding_bottom @aspect_ratio.split("/").map(&:to_i).reverse.reduce(&:fdiv) * 100 end def default_attrs { class: "bg-muted absolute inset-0 [&>img]:object-cover [&>img]:absolute [&>img]:h-full [&>img]:w-full [&>img]:inset-0 [&>img]:text-transparent" } end end end
Copied!
Copy failed!
Components
Component | Built using | Source |
---|---|---|
AspectRatio | Phlex |