-
Notifications
You must be signed in to change notification settings - Fork 4
/
helper.rb
50 lines (47 loc) · 2.12 KB
/
helper.rb
1
2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require "action_view"
module RailsHeroicon
module Helper
include ActionView::Helpers::TagHelper
mattr_accessor :icon_cache, default: {}
# To add a heroicon, call <tt><%= heroicon "icon_name" %></tt> on your erb template.
# Head over to https://heroicons.com to view all the icons.
#
# == Options
# The helper method accepts mutiple arguments such as:
#
# === Variant
# There are 4 types of variants: 'outline', 'solid', 'mini', and 'micro', the default being the 'outline'.
# To specify the solid variant, call <tt><%= heroicon "icon_name", variant: "solid" %></tt>
#
# === HTML attributes
# Any <tt>html</tt> attribute is supported, for eg:
#
# <tt><%= heroicon "icon_name", class: "text-gray-500", data: { controller: "icon" } %></tt>
#
# === Handling the icon size
# Normally, if you're just using vanilla heroicons with tailwindcss, you'd set <tt>w-5 h-5</tt> as class attributes
# on the svg. With this helper, you just need to set the <tt>size</tt> attribute on the icon.
#
# <tt><%= heroicon "icon_name", size: 20 %></tt>
#
# This will set the <tt>height</tt> and <tt>width</tt> attribute on the svg.
#
# If the variant is set as <tt>outline</tt> or <tt>solid</tt>, <tt>size</tt> defaults to 24, if the variant is set
# as <tt>mini</tt>, <tt>size</tt> defaults to 20, and if the variant is set as <tt>micro<tt>, <tt>size</tt>
# defaults to 16.
# However, this can be over-written with the <tt>size</tt> attribute.
#
# == Accessibility
# The helper method automatically sets <tt>aria-hidden=true</tt> if <tt>aria-label</tt> is not set, and
# if <tt>aria-label</tt> is set, then <tt>role=img</tt> is set automatically.
def heroicon(symbol, title: nil, **options)
cache_key = [symbol, title, options]
return icon_cache[cache_key] if icon_cache[cache_key]
icon = RailsHeroicon.new(symbol, **options)
title_tag = content_tag(:title, title) if title
tag = content_tag(:svg, title_tag.to_s.html_safe + icon.svg_path.html_safe, icon.options)
icon_cache[cache_key] = tag
tag
end
end
end