Sha256: dad9e725a77fa01314b11b60a4279554b0aaeccc6fd2aa5c9451eca6c862c649

Contents?: true

Size: 1.92 KB

Versions: 25

Compression:

Stored size: 1.92 KB

Contents

module Coco
  class SystemBanner < Coco::Component
    include Concerns::AcceptsOptions
    include Concerns::WrapsComponent

    accepts_option :dismissable, from: [true, false], default: false

    wraps_component :alert do |args|
      theme = vivid_theme_name(args.fetch(:theme, nil)) || :info_vivid
      Coco::Alert.new(
        **args,
        theme: theme,
        banner: true,
        cloak: false,
        single_line: true,
        dismissable: dismissable?,
        dismiss_after_action: false,
        condensed: true
      )
    end

    %i[action secondary_action link].each do |slot_name|
      renders_one slot_name, ->(*args, **kwargs, &block) do
        alert.send(:"with_#{slot_name}", *args, **kwargs, &block)
      end
    end

    renders_one :title, ->(text = nil, **kwargs, &block) do
      alert.public_send(:with_title, **kwargs) { text.presence || block.call }
    end

    renders_one :message, ->(text = nil, **kwargs, &block) do
      alert.public_send(:with_message, **kwargs) { text.presence || block.call }
    end

    before_render do
      if dismissable? && id.blank?
        raise ArgumentError, "Dismissable banners must be given an ID"
      end
    end

    attr_reader :dismiss_for, :id

    def initialize(id: nil, dismiss_for: nil, **)
      @id = id
      @dismiss_for = dismiss_for
      set_tag_attr(:id, id)
    end

    def dismissable?
      get_option_value(:dismissable) == true
    end

    def render?
      helpers.cookies[dismiss_cookie_name] != "true"
    end

    def dismiss_cookie_name
      :"cb_system_banner_#{tag_attr(:id)&.underscore}_dismissed"
    end

    def alpine_data
      dismissable? ? {
        cookie_name: dismiss_cookie_name,
        cookie_expiry: dismiss_for&.in_days&.to_i,
        cookie_value: "true"
      } : {}
    end

    private

    def vivid_theme_name(theme)
      if theme.present?
        :"#{theme.to_s.underscore.gsub("_vivid", "")}_vivid"
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
coveragebook_components-0.19.8 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.19.7 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.19.6 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.19.5 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.19.4 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.19.3 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.19.2 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.19.1 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.19.0 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.18.8 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.18.7 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.18.0 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.17.7 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.17.6 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.17.5 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.17.4 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.17.3 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.17.2 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.17.1 app/components/coco/messaging/system_banner/system_banner.rb
coveragebook_components-0.17.0 app/components/coco/messaging/system_banner/system_banner.rb