11# frozen_string_literal: true
22
33class Views ::Docs ::Toast < Views ::Base
4+ include Phlex ::Rails ::Helpers ::ButtonTo
5+
46 def view_template
57 component = "Toast"
68
@@ -20,61 +22,76 @@ def view_template
2022 end
2123
2224 Heading ( level : 2 ) { "Variants" }
23-
24- render Docs ::VisualCodeExample . new ( title : "Click any to spawn" , context : self ) do
25- div ( class : "flex flex-wrap gap-2" , data_controller : "toast-demo" ) do
26- variants_buttons
27- end
28- nil
25+ p ( class : "text-muted-foreground text-sm" ) { "Click any to push a toast from the server (Turbo Stream)." }
26+ div ( class : "flex flex-wrap gap-2 mt-2" ) do
27+ button_to "Default" , docs_toast_demo_default_path , data : { turbo_stream : true } , class : button_class
28+ button_to "Success" , docs_toast_demo_success_path , data : { turbo_stream : true } , class : button_class
29+ button_to "Error" , docs_toast_demo_error_path , data : { turbo_stream : true } , class : button_class
30+ button_to "Warning" , docs_toast_demo_warning_path , data : { turbo_stream : true } , class : button_class
31+ button_to "Info" , docs_toast_demo_info_path , data : { turbo_stream : true } , class : button_class
32+ button_to "With action" , docs_toast_demo_with_action_path , data : { turbo_stream : true } , class : button_class
2933 end
3034
3135 Heading ( level : 2 ) { "Server-pushed (Turbo Stream)" }
32-
33- render Docs ::VisualCodeExample . new ( title : "Append from controller" , context : self ) do
34- button_to (
35- "Push from server" ,
36- docs_toast_demo_with_action_path ,
37- class : "inline-flex items-center justify-center rounded-md border border-input bg-background px-3 py-2 text-sm font-medium hover:bg-accent" ,
38- data : { turbo_stream : true }
39- )
40- nil
36+ div ( class : "rounded-md border bg-muted/30 p-4" ) do
37+ Codeblock ( <<~RUBY , syntax : :ruby )
38+ # In your controller, respond with a Turbo Stream that appends
39+ # a Toast::Item to the global region (id="ruby-ui-toaster"):
40+ render turbo_stream: turbo_stream.append("ruby-ui-toaster") {
41+ render RubyUI::ToastItem.new(variant: :success) do
42+ render RubyUI::ToastIcon.new(variant: :success)
43+ render RubyUI::ToastTitle.new { "Saved" }
44+ render RubyUI::ToastDescription.new { "Project updated." }
45+ render RubyUI::ToastClose.new
46+ end
47+ }
48+ RUBY
4149 end
4250
4351 Heading ( level : 2 ) { "JS API" }
52+ div ( class : "flex flex-wrap gap-2 mt-2" , data : { controller : "toast-demo" } ) do
53+ %w[ success error info warning ] . each do |variant |
54+ button (
55+ type : "button" ,
56+ class : button_class ,
57+ data : { action : "click->toast-demo#fire" , toast_demo_variant_param : variant }
58+ ) { "toast.#{ variant } " }
59+ end
60+ button ( type : "button" , class : button_class , data : { action : "click->toast-demo#promise" } ) { "toast.promise" }
61+ button ( type : "button" , class : button_class , data : { action : "click->toast-demo#dismissAll" } ) { "dismiss all" }
62+ end
4463
45- render Docs :: VisualCodeExample . new ( title : "window.RubyUI.toast" , context : self ) do
46- <<~JS
64+ div ( class : "rounded-md border bg-muted/30 p-4 mt-4" ) do
65+ Codeblock ( <<~JS , syntax : :javascript )
4766 RubyUI.toast.success("Saved", { description: "Project updated." })
48- RubyUI.toast.error("Boom", { description: "Server returned 500." } )
67+ RubyUI.toast.error("Boom")
4968 RubyUI.toast.info("Heads up")
5069 RubyUI.toast.warning("Storage almost full")
5170 RubyUI.toast.loading("Working...")
52- RubyUI.toast.dismiss(id) // or no arg to dismiss all
53-
54- RubyUI.toast.promise(
55- fetch("/things"),
56- { loading: "Saving...", success: "Saved", error: "Failed" }
57- )
71+ RubyUI.toast.dismiss(id) // no-arg dismisses all
72+ RubyUI.toast.promise(p, { loading, success, error })
5873 JS
5974 end
6075
6176 Heading ( level : 2 ) { "Position" }
62-
63- render Docs ::VisualCodeExample . new ( title : "All six positions" , context : self ) do
77+ render Docs ::VisualCodeExample . new ( title : "Configurable on the Region" , context : self ) do
6478 <<~RUBY
65- render RubyUI::ToastRegion.new(position: :top_right, expand: true, max: 5)
79+ render RubyUI::ToastRegion.new(
80+ position: :top_right,
81+ expand: true,
82+ max: 5,
83+ duration: 6000
84+ )
6685 RUBY
6786 end
6887
6988 Heading ( level : 2 ) { "Rails flash bridge" }
70-
7189 render Docs ::VisualCodeExample . new ( title : "Renders flash on initial load" , context : self ) do
7290 <<~RUBY
73- # In your controller
74- flash[:notice] = "Saved"
75-
76- # In your layout, pass flash to the region
77- render RubyUI::ToastRegion.new(flash: flash.to_h)
91+ # In your controller:
92+ # flash[:notice] = "Saved"
93+ # In your layout:
94+ render RubyUI::ToastRegion.new(flash: helpers.flash.to_h)
7895 RUBY
7996 end
8097
@@ -86,22 +103,7 @@ def view_template
86103
87104 private
88105
89- def variants_buttons
90- [
91- { label : "Default" , path : docs_toast_demo_default_path } ,
92- { label : "Success" , path : docs_toast_demo_success_path } ,
93- { label : "Error" , path : docs_toast_demo_error_path } ,
94- { label : "Warning" , path : docs_toast_demo_warning_path } ,
95- { label : "Info" , path : docs_toast_demo_info_path } ,
96- { label : "With action" , path : docs_toast_demo_with_action_path }
97- ] . each do |btn |
98- button_to (
99- btn [ :label ] ,
100- btn [ :path ] ,
101- class : "inline-flex items-center justify-center rounded-md border border-input bg-background px-3 py-2 text-sm font-medium hover:bg-accent" ,
102- data : { turbo_stream : true } ,
103- form : { class : "inline" }
104- )
105- end
106+ def button_class
107+ "inline-flex items-center justify-center rounded-md border border-input bg-background px-3 py-2 text-sm font-medium hover:bg-accent"
106108 end
107109end
0 commit comments