Skip to content

Commit 128874e

Browse files
committed
[Documentation] Polish Toast docs page + devcontainer compose
- Replace onclick handlers with Stimulus toast-demo controller (Phlex blocks unsafe inline event attrs). - Move server-push and JS API examples to plain Codeblocks; reserve VisualCodeExample for snippets that work under instance_eval. - Wire docs/.devcontainer/compose.yaml to monorepo layout (mounts ruby_ui root, working_dir=docs, ports 3001:3000).
1 parent d88a551 commit 128874e

4 files changed

Lines changed: 84 additions & 51 deletions

File tree

docs/.devcontainer/compose.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ services:
77
dockerfile: .devcontainer/Dockerfile
88

99
volumes:
10-
- ../../web:/workspaces/web:cached
10+
- ../..:/workspaces/ruby_ui:cached
11+
working_dir: /workspaces/ruby_ui/docs
12+
ports:
13+
- "3001:3000"
1114

1215
# Overrides default command so things don't shut down after the process ends.
1316
command: sleep infinity

docs/app/javascript/controllers/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { application } from "./application"
77
import IframeThemeController from "./iframe_theme_controller"
88
application.register("iframe-theme", IframeThemeController)
99

10+
import ToastDemoController from "./toast_demo_controller"
11+
application.register("toast-demo", ToastDemoController)
12+
1013
import RubyUi__AccordionController from "./ruby_ui/accordion_controller"
1114
application.register("ruby-ui--accordion", RubyUi__AccordionController)
1215

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
fire(e) {
5+
const variant = e.params.variant || "default"
6+
const t = window.RubyUI?.toast
7+
if (!t) return
8+
const titles = { success: "Saved", error: "Boom", info: "Heads up", warning: "Storage almost full", default: "Hello" }
9+
const descs = { success: "Project updated.", error: "Server returned 500.", info: "New version available.", warning: "Almost out of space.", default: "Just so you know." }
10+
t[variant]?.(titles[variant], { description: descs[variant] })
11+
}
12+
13+
promise() {
14+
const p = new Promise((r) => setTimeout(() => r({ id: 42 }), 1500))
15+
window.RubyUI?.toast.promise(p, {
16+
loading: "Saving...",
17+
success: (v) => `Saved (id ${v.id})`,
18+
error: "Failed",
19+
})
20+
}
21+
22+
dismissAll() {
23+
window.RubyUI?.toast.dismiss()
24+
}
25+
}

docs/app/views/docs/toast.rb

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class 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
107109
end

0 commit comments

Comments
 (0)