Class: PhlexKit::ToastRegion
- Inherits:
-
BaseComponent
- Object
- Phlex::HTML
- BaseComponent
- PhlexKit::ToastRegion
- Defined in:
- app/components/phlex_kit/toast/toast_region.rb
Overview
Toast region ("toaster"), ported from ruby_ui's RubyUI::ToastRegion. Renders the fixed, position-anchored
- stack plus hidden skeletons (one
per variant) and action/cancel/close slot templates. The phlex-kit--toaster
controller clones those to spawn toasts client-side — via
window.PhlexKit.toast(...), a "phlex-kit:toast" window event, or the toast
Turbo Stream action. Pass flash: to render server-side flash messages as
toasts on page load. Per-toast behaviour (timer, swipe) lives on each
PhlexKit::ToastItem. Tailwind → vanilla .pk-toast* (toast.css).
Constant Summary collapse
- SKELETON_VARIANTS =
%i[default success error warning info loading].freeze
- POSITIONS =
{ top_left: "top-left", top_center: "top-center", top_right: "top-right", bottom_left: "bottom-left", bottom_center: "bottom-center", bottom_right: "bottom-right" }.freeze
Instance Method Summary collapse
-
#initialize(position: :bottom_right, expand: false, max: 3, duration: 4000, gap: 14, offset: 24, theme: :system, rich_colors: false, close_button: false, hotkey: %w[alt t],, dir: :ltr, flash: nil, id: "pk-toaster", **attrs) ⇒ ToastRegion
constructor
A new instance of ToastRegion.
- #view_template(&block) ⇒ Object
Methods inherited from BaseComponent
Constructor Details
#initialize(position: :bottom_right, expand: false, max: 3, duration: 4000, gap: 14, offset: 24, theme: :system, rich_colors: false, close_button: false, hotkey: %w[alt t],, dir: :ltr, flash: nil, id: "pk-toaster", **attrs) ⇒ ToastRegion
Returns a new instance of ToastRegion.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/components/phlex_kit/toast/toast_region.rb', line 22 def initialize( position: :bottom_right, expand: false, max: 3, duration: 4000, gap: 14, offset: 24, theme: :system, rich_colors: false, close_button: false, hotkey: %w[alt t], dir: :ltr, flash: nil, id: "pk-toaster", **attrs ) @position = POSITIONS.fetch(position.to_sym) @expand = @max = max @duration = duration @gap = gap @offset = offset # theme:/rich_colors: are Sonner API surface the kit deliberately does # not implement (tokens are :root-scoped; there is no status palette) — # fail loud rather than silently no-op (audit round 6). @theme = theme.to_sym unless @theme == :system raise ArgumentError, "ToastRegion theme: only :system is supported — toasts follow the page theme (:root[data-theme])" end if rich_colors raise ArgumentError, "ToastRegion rich_colors: is not supported — the kit has no status color palette (toasts follow --pk-* tokens)" end @close_button = @hotkey = hotkey @dir = dir.to_sym unless %i[ltr rtl auto].include?(@dir) raise ArgumentError, "ToastRegion dir: must be :ltr, :rtl or :auto" end @flash = flash # Base id for the <ol> stack; the wrapper takes "#{id}-region". Defaults # keep the historical "pk-toaster"/"pk-toaster-region" pair — pass a # distinct id when rendering more than one region on a page. @id = id @attrs = attrs end |
Instance Method Details
#view_template(&block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/components/phlex_kit/toast/toast_region.rb', line 68 def view_template(&block) div(**mix(region_attrs, @attrs)) do ol(id: @id, class: "pk-toast-list") do render_flash if @flash yield(self) if block end SKELETON_VARIANTS.each { |variant| skeleton(variant) } slot_template("actionTpl") { render ToastAction.new(label: "") } slot_template("cancelTpl") { render ToastCancel.new(label: "") } slot_template("closeTpl") { render ToastClose.new } end end |