Module: NitroRails::ComponentsHelper

Defined in:
app/helpers/nitro_rails/components_helper.rb

Instance Method Summary collapse

Instance Method Details

#clockObject



30
31
32
33
34
# File 'app/helpers/nitro_rails/components_helper.rb', line 30

def clock 
  (:div, class: "clock", data: { controller: "clock" }) do
    (:span, Time.now.strftime("%-I:%M %p"), class: "clock-time", data: { "clock-target": "time" }) 
  end
end

#inline_svg(path, **options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/nitro_rails/components_helper.rb', line 3

def inline_svg(path, **options)
  nitro_options(options) do |options|
    options.concat(:class, "inline-svg")
    options.concat(:class, "svg-#{path.split("/").last}")
    options.concat(:class, options.delete(:size))
    options.concat(:style, "transform: rotate(#{options.delete(:rotate)}deg);") if options[:rotate]
  end

  # Rails.cache.clear
  svg = Rails.cache.fetch("inline_svg/#{path}", expires_in: 12.hours) do
    app_path = Rails.root.join("app/assets/images/svgs/#{path}.svg")
    gem_path = NitroRails::Engine.root.join("app/assets/images/nitro_rails/#{path}.svg")

    if File.exist?(app_path)
      File.read(app_path).html_safe
    elsif File.exist?(gem_path)
      File.read(gem_path).html_safe
    else
      "(SVG file not found: #{path})"
    end
  end
  
  (:div, **options) do 
    render inline: svg
  end
end

#search_bar(**options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/nitro_rails/components_helper.rb', line 36

def search_bar(**options)
  options[:class] = "search-bar #{options[:class]}"
  options[:data] ||= {}
  options[:data][:controller] = "search"

  (:div, **options) do 
    (:i, "", class: "fas fa-search") +
    form_with(url: '/', method: :get, data: { turbo: false }) do 
      text_field_tag(:query, params[:query], class: "search-bar-input", placeholder: "Search", data: { action: "keyup->search#search" })
    end
  end
end