Class: Playbook::PbIcon::Icon

Inherits:
KitBase
  • Object
show all
Defined in:
app/pb_kits/playbook/pb_icon/icon.rb

Constant Summary collapse

ICON_PATH_DEV_CACHE_TTL_SECONDS =
2
ICON_PATH_PROD_CACHE_TTL_SECONDS =
60

Constants included from Playbook::PositionPropsCss

Playbook::PositionPropsCss::POSITION_VALUES

Instance Attribute Summary

Attributes included from Playbook::Props

#values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from KitBase

#combined_html_options, #global_inline_props, #object, #pb_content_tag, #react_component

Methods included from MaxHeight

included, #max_height_options, #max_height_props, #max_height_values

Methods included from MinHeight

included, #min_height_options, #min_height_props, #min_height_values

Methods included from Height

#height_options, #height_props, #height_values, included

Methods included from VerticalAlign

included, #vertical_align_options, #vertical_align_props, #vertical_align_values

Methods included from Bottom

#bottom_options, #bottom_values, included

Methods included from Playbook::PositionPropsCss

#positioning_css

Methods included from Right

included, #right_options, #right_values

Methods included from Top

included, #top_options, #top_values

Methods included from Left

included, #left_options, #left_values

Methods included from Truncate

included, #truncate_options, #truncate_props, #truncate_values

Methods included from Overflow

included, #overflow_options, #overflow_props, #overflow_values

Methods included from TextAlign

included, #text_align_options, #text_align_props, #text_align_values

Methods included from BorderRadius

#border_radius_options, #border_radius_props, #border_radius_values, included

Methods included from Hover

#hover_attributes, #hover_background_values, #hover_color_values, #hover_options, #hover_props, #hover_scale_values, #hover_shadow_values, #hover_underline_values, #hover_values, #hover_visible_values, included

Methods included from Playbook::Position

included, #position_options, #position_values

Methods included from Order

included, #order_options, #order_props, #order_values

Methods included from FlexShrink

#flex_shrink_options, #flex_shrink_props, #flex_shrink_values, included

Methods included from FlexGrow

#flex_grow_options, #flex_grow_props, #flex_grow_values, included

Methods included from Flex

#flex_options, #flex_props, #flex_values, included

Methods included from AlignSelf

#align_self_options, #align_self_props, #align_self_values, included

Methods included from AlignContent

#align_content_options, #align_content_props, #align_content_values, included

Methods included from AlignItems

#align_items_options, #align_items_props, #align_items_values, included

Methods included from JustifySelf

included, #justify_self_options, #justify_self_props, #justify_self_values

Methods included from JustifyContent

included, #justify_content_options, #justify_content_props, #justify_content_values

Methods included from FlexWrap

#flex_wrap_options, #flex_wrap_props, #flex_wrap_values, included

Methods included from FlexDirection

#flex_direction_options, #flex_direction_props, #flex_direction_values, included

Methods included from Cursor

#cursor_options, #cursor_props, #cursor_values, included

Methods included from Display

#display_options, #display_props, #display_size_values, #display_values, included

Methods included from LineHeight

included, #line_height_options, #line_height_props, #line_height_values

Methods included from Shadow

included, #shadow_options, #shadow_props, #shadow_values

Methods included from NumberSpacing

included, #number_spacing_options, #number_spacing_values

Methods included from ZIndex

included, #screen_size_values, #z_index_options, #z_index_props, #z_index_values

Methods included from Spacing

#break_method_values, #column_gap_options, #column_gap_props, #filter_classname, #gap_options, #gap_props, #gap_values, included, #max_width_options, #max_width_props, #max_width_values, #min_width_options, #min_width_props, #min_width_values, #row_gap_options, #row_gap_props, #screen_size_values, #spacing_options, #spacing_props, #spacing_values, #width_options, #width_props, #width_values

Methods included from Classnames

#generate_classname, #generate_classname_without_spacing, included

Methods included from Playbook::Props

#initialize, #prop

Methods included from Playbook::PbKitHelper

#deprecated_kit_warning, #pb_rails

Methods included from Playbook::PbFormsHelper

#pb_form_with

Class Method Details

.icon_alias_mapObject

Cache aliases.json across the process, but invalidate when the file changes (dev-safe)



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/pb_kits/playbook/pb_icon/icon.rb', line 154

def icon_alias_map
  return @icon_alias_map if alias_cache_fresh?

  cache_mutex.synchronize do
    return @icon_alias_map if alias_cache_fresh?

    @icon_alias_map =
      if Rails.application.config.respond_to?(:icon_alias_path)
        base_path = Rails.application.config.icon_alias_path
        full_path = Rails.root.join(base_path)
        @icon_alias_map_mtime = safe_mtime(full_path)

        json = File.read(full_path)
        parsed = JSON.parse(json)
        parsed.fetch("aliases", {}).freeze
      end
  end

  @icon_alias_map
end

.icon_path_indexObject

Cache an index of icon_name to file path for all SVGs in the configured directory, with invalidation based on directory mtime Avoids recursive Dir.glob for every icon render



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/pb_kits/playbook/pb_icon/icon.rb', line 177

def icon_path_index
  return @icon_path_index if index_cache_fresh?

  cache_mutex.synchronize do
    return @icon_path_index if index_cache_fresh?

    @icon_path_index =
      if Rails.application.config.respond_to?(:icon_path)
        base_path = Rails.application.config.icon_path
        root = Rails.root.join(base_path)

        # If path doesn't exist, keep behavior aligned (no path resolution)
        if Dir.exist?(root)
          @icon_path_index_cache_key = icon_path_cache_key(root)

          # One scan builds the map for O(1) lookups
          # Key is the filename (without .svg) to match existing usage
          index = {}
          Dir.glob(File.join(root.to_s, "**", "*.svg")).sort.each do |p|
            name = File.basename(p, ".svg")
            index[name] ||= p
          end
          index.freeze
        else
          @icon_path_index_cache_key = nil
          {}
        end
      else
        {}
      end

    @icon_path_index_checked_at = monotonic_now
  end

  @icon_path_index
end

Instance Method Details

#asset_pathObject

Instance-level memoization of resolved asset path



97
98
99
100
101
102
103
104
105
106
# File 'app/pb_kits/playbook/pb_icon/icon.rb', line 97

def asset_path
  return @asset_path if defined?(@asset_path)

  @asset_path =
    if Rails.application.config.respond_to?(:icon_path)
      resolved_icon = resolve_alias(icon)
      path = self.class.icon_path_index[resolved_icon]
      path if path && File.exist?(path)
    end
end

#classnameObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/pb_kits/playbook/pb_icon/icon.rb', line 51

def classname
  generate_classname(
    "pb_icon_kit",
    color_class,
    font_style_class,
    icon_class,
    border_class,
    fixed_width_class,
    flip_class,
    inverse_class,
    list_item_class,
    pull_class,
    pulse_class,
    rotation_class,
    size_class,
    spin_class,
    separator: " "
  )
end

#custom_icon_classnameObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/pb_kits/playbook/pb_icon/icon.rb', line 71

def custom_icon_classname
  generate_classname(
    "pb_icon_kit",
    border_class,
    color_class,
    fixed_width_class,
    flip_class,
    inverse_class,
    list_item_class,
    pull_class,
    pulse_class,
    rotation_class,
    size_class,
    spin_class,
    separator: " "
  )
end

#icon_alias_mapObject

Instance-level memoization of alias map lookup result



90
91
92
93
94
# File 'app/pb_kits/playbook/pb_icon/icon.rb', line 90

def icon_alias_map
  return @icon_alias_map if defined?(@icon_alias_map)

  @icon_alias_map = self.class.icon_alias_map
end

#is_svg?Boolean

Returns:



108
109
110
111
112
# File 'app/pb_kits/playbook/pb_icon/icon.rb', line 108

def is_svg?
  return @is_svg if defined?(@is_svg)

  @is_svg = (icon || custom_icon.to_s).include?(".svg") || asset_path.present?
end

#render_svgObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/pb_kits/playbook/pb_icon/icon.rb', line 114

def render_svg
  doc = Nokogiri::XML(URI.open(asset_path || icon || custom_icon)) # rubocop:disable Security/Open
  svg = doc.at_css("svg")
  return "" unless svg

  svg["class"] = %w[pb_custom_icon svg-inline--fa].concat([object.custom_icon_classname]).join(" ")
  svg["id"] = object.id
  svg["height"] = "auto"
  svg["width"] = "auto"
  svg["tabindex"] = object.tabindex
  fill_color = object.color || "currentColor"

  # Safely apply fill to all paths (avoids nil errors + handles multi-path icons)
  doc.css("path").each { |p| p["fill"] = fill_color }

  if object.data.present?
    object.data.each do |key, value|
      svg["data-#{key}"] = value
    end
  end

  if object.aria.present?
    object.aria.each do |key, value|
      k = key.to_s
      attr = k.start_with?("aria-") ? k : "aria-#{k.tr('_', '-')}"
      svg[attr] = value
    end
  end

  raw doc
rescue OpenURI::HTTPError, StandardError
  # Handle any exceptions and return an empty string
  ""
end

#valid_emoji?Boolean

Returns:



46
47
48
49
# File 'app/pb_kits/playbook/pb_icon/icon.rb', line 46

def valid_emoji?
  emoji_regex = /\p{Emoji}/
  emoji_regex.match?(icon)
end