module Coco module App module Elements class Alert < Coco::Component include Concerns::WithIcon include Concerns::AcceptsTheme THEMES = %W[positive warning negative info positive-vivid warning-vivid negative-vivid info-vivid] THEME_ICONS = { positive: :check_circle, positive_vivid: :check_circle, warning: :alert_triangle, warning_vivid: :alert_triangle, negative: :alert_circle, negative_vivid: :alert_circle, info: :info, info_vivid: :info }.freeze accepts_option :dismissable, from: [true, false], default: false accepts_option :banner, from: [true, false], default: false accepts_option :theme, from: THEMES, default: "info" accepts_option :single_line, from: [true, false] renders_one :title renders_one :action, ->(**kwargs, &block) do theme = vivid? ? "neutral-dark" : get_option_value(:theme) Coco::App::Elements::Button.new(**kwargs, theme: theme, size: :sm, icon: nil) end renders_one :secondary_action, ->(**kwargs, &block) do theme = vivid? ? "neutral-light" : "text-#{get_option_value(:theme)}" Coco::App::Elements::Button.new(**kwargs, theme: theme, size: :sm, icon: nil) end renders_one :link, ->(**kwargs, &block) do theme = vivid? ? nil : unvividify(get_option_value(:theme)) Coco::App::Elements::Link.new(**kwargs, theme: theme, size: :sm, icon: nil) end before_render do theme = get_option_value(:theme) unless tag_attr?(:id) set_tag_attr(:id, "alert-#{rand(1000)}") end icon(name: THEME_ICONS[theme.tr("-", "_").to_sym]) end def dismissable? get_option_value(:dismissable) end def single_line? get_option_value(:single_line) end def vivid? theme = get_option_value(:theme) theme.to_s.match?(/vivid$/) end def unvividify(theme) theme.to_s.match?(/vivid$/) ? theme.to_s.underscore.gsub("_vivid", "").to_sym : theme end end end end end