Class: Shadcn::Toaster::Toast::Component

Inherits:
ApplicationViewComponent show all
Defined in:
app/components/shadcn/toaster/toast/component.rb

Overview

One toast.

A real component rather than a shape the JavaScript builds, because the most useful way to raise one in a Rails app is from the server: a flash rendered with the page, or a turbo_stream.append onto the list. The controller then only has to start its clock and take it away again.

Constant Summary collapse

VARIANTS =
{
  default: nil,
  success: "circle-check",
  info: "info",
  warning: "triangle-alert",
  error: "octagon-x",
  loading: "loader-circle"
}.freeze

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Instance Method Summary collapse

Methods inherited from ApplicationViewComponent

#css_classes, default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #tag_name

Constructor Details

#initialize(title: nil, description: nil, variant: :default, duration: nil, dismissible: true, **attributes) ⇒ Component

Returns a new instance of Component.



65
66
67
68
69
70
71
72
73
# File 'app/components/shadcn/toaster/toast/component.rb', line 65

def initialize(title: nil, description: nil, variant: :default, duration: nil,
               dismissible: true, **attributes)
  @variant = VARIANTS.key?(variant&.to_sym) ? variant.to_sym : :default
  @title = title
  @description = description
  @duration = duration
  @dismissible = dismissible
  super(**attributes)
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



63
64
65
# File 'app/components/shadcn/toaster/toast/component.rb', line 63

def description
  @description
end

#dismissibleObject (readonly)

Returns the value of attribute dismissible.



63
64
65
# File 'app/components/shadcn/toaster/toast/component.rb', line 63

def dismissible
  @dismissible
end

#durationObject (readonly)

Returns the value of attribute duration.



63
64
65
# File 'app/components/shadcn/toaster/toast/component.rb', line 63

def duration
  @duration
end

#titleObject (readonly)

Returns the value of attribute title.



63
64
65
# File 'app/components/shadcn/toaster/toast/component.rb', line 63

def title
  @title
end

#variantObject (readonly)

Returns the value of attribute variant.



63
64
65
# File 'app/components/shadcn/toaster/toast/component.rb', line 63

def variant
  @variant
end

Instance Method Details

#callObject



91
92
93
# File 'app/components/shadcn/toaster/toast/component.rb', line 91

def call
  render_element(body: safe_join([ icon, body, dismiss ].compact))
end

#element_attributes(**defaults) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/components/shadcn/toaster/toast/component.rb', line 77

def element_attributes(**defaults)
  # No `role` here, and sonner sets none either (index.tsx renders a bare
  # `<li>`): a role on an `<li>` replaces its implicit `listitem`, and
  # an `<ol>` whose children are not list items is a list axe fails and
  # a screen reader miscounts. The announcement comes from the region's
  # `aria-live`, which is where sonner puts it too (index.tsx:790).
  super(**{
    "data-variant" => variant,
    "data-state" => "open",
    "data-duration" => duration,
    "data-shadcn--toaster-target" => "toast"
  }.compact.merge(defaults))
end

#style_variantsObject



75
# File 'app/components/shadcn/toaster/toast/component.rb', line 75

def style_variants = { variant: }