module Coco class Alert < Coco::Component include Concerns::AcceptsOptions include Concerns::AcceptsTheme THEMES = %W[positive warning negative info positive-vivid warning-vivid negative-vivid info-vivid] accepts_option :dismissable, from: [true, false], default: false accepts_option :dismiss_after_action, from: [true, false], default: true 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 unless tag_attr?(:id) set_tag_attr(:id, "alert-#{rand(1000)}") end with_title { @title } unless title? end def initialize(title: nil, **kwargs) @title = title 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 def stamp type = unvividify(get_option_value(:theme))&.to_sym style = if get_option_value(:banner) :compact else vivid? ? :inverse : :subtle end coco_stamp(type, style:) end end end