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] accepts_option :cloak, from: [true, false], default: true, private: true accepts_option :condensed, from: [true, false], default: false renders_one :title renders_one :message renders_one :action, ->(*args, **kwargs, &block) do theme = vivid? ? "neutral-dark" : get_option_value(:theme) @action_data = {args: args, kwargs: kwargs.merge(theme: theme), block: block} end renders_one :secondary_action, ->(*args, **kwargs, &block) do theme = vivid? ? "neutral-light" : "text-#{get_option_value(:theme)}" @secondary_action_data = {args: args, kwargs: kwargs.merge(theme: theme), block: block} end renders_one :link, ->(*args, **kwargs, &block) do theme = vivid? ? nil : unvividify(get_option_value(:theme)) @link_data = {args: args, kwargs: kwargs.merge(theme: theme), block: block} 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 cloak? get_option_value(:cloak) 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 def button_size get_option_value(:condensed) ? :xs : :sm end end end end end