Module: NeonSakura::IconHelper

Included in:
StyleGuideController
Defined in:
lib/neon_sakura/icon_helper.rb

Instance Method Summary collapse

Instance Method Details

#available_iconsArray<String>

Get a list of all available icons

Returns:

  • (Array<String>)

    Array of icon names



23
24
25
26
27
28
29
30
# File 'lib/neon_sakura/icon_helper.rb', line 23

def available_icons
  # Use gem's icon directory when running within the gem, otherwise use app's directory
  root_path = defined?(NeonSakura::Engine) ? NeonSakura::Engine.root : Rails.root
  icon_files = Dir.glob(File.join(root_path, "app", "views", "shared", "icons", "_*.html.erb"))
  icon_files.map do |file|
    File.basename(file, ".html.erb").gsub(/^_/, "")
  end.sort
end

#icon_exists?(name) ⇒ Boolean

Check if an icon exists

Parameters:

  • name (String)

    The icon name to check

Returns:

  • (Boolean)

    Whether the icon exists



35
36
37
38
39
# File 'lib/neon_sakura/icon_helper.rb', line 35

def icon_exists?(name)
  # Use gem's icon directory when running within the gem, otherwise use app's directory
  root_path = defined?(NeonSakura::Engine) ? NeonSakura::Engine.root : Rails.root
  File.exist?(File.join(root_path, "app", "views", "shared", "icons", "_#{name}.html.erb"))
end

#render_icon(name, options = {}) ⇒ String

Renders an icon partial with the given options All other options are passed through to the icon partial as local_assigns

Parameters:

  • name (String)

    The icon name (without underscore prefix)

  • options (Hash) (defaults to: {})

    Options for the icon

Options Hash (options):

  • :css_class (String)

    CSS class for the icon

  • :aria_hidden (Boolean)

    Whether the icon is aria-hidden

  • :aria_label (String)

    Label for accessibility

Returns:

  • (String)

    Rendered icon partial



13
14
15
16
17
18
19
# File 'lib/neon_sakura/icon_helper.rb', line 13

def render_icon(name, options = {})
  # Set default CSS class if not provided
  options[:css_class] ||= "w-4 h-4"

  # Pass all options to the partial as local_assigns
  render "shared/icons/#{name}", options
end

#render_icon_list(icon_names, options = {}) ⇒ String

Renders a list of icons with consistent styling

Parameters:

  • icon_names (Array<String>)

    Array of icon names

  • options (Hash) (defaults to: {})

    Options for rendering

Returns:

  • (String)

    HTML string with rendered icons



45
46
47
48
49
50
51
52
# File 'lib/neon_sakura/icon_helper.rb', line 45

def render_icon_list(icon_names, options = {})
  css_class = options[:css_class] || "flex items-center gap-2"
  icons_html = icon_names.map do |icon_name|
    render_icon(icon_name, options.merge(css_class: options[:icon_css_class]))
  end.join("\n")

  "<div class=\"#{css_class}\">#{icons_html}</div>".html_safe
end

#render_login_logo(options = {}) ⇒ String?

Renders the configured login logo from NeonSakura configuration Falls back to app icon if no login logo is configured

Parameters:

  • options (Hash) (defaults to: {})

    Options to override configuration

Options Hash (options):

  • :icon (String)

    Icon name to use (overrides config)

  • :css_class (String)

    CSS class to use (overrides config)

  • :gradient_id (String)

    Gradient ID for icons that support it

Returns:

  • (String, nil)

    Rendered icon partial or nil if no icon configured



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/neon_sakura/icon_helper.rb', line 61

def (options = {})
  config = NeonSakura.config. || {}

  # Determine which icon to use
  icon_name = options[:icon] || config[:icon] || NeonSakura.config.app_icon

  # Return nil if no icon is configured
  return nil if icon_name.blank?

  # Build render options
  render_options = {
    css_class: options[:css_class] || config[:css_class] || "w-12 h-12 sm:w-16 sm:h-16",
    gradient_id: options[:gradient_id] || config[:gradient_id]
  }.compact

  # Render the icon using render_theme_icon (which supports gradient_id)
  render_theme_icon(icon_name, render_options)
end