Skip to content
This repository was archived by the owner on May 11, 2026. It is now read-only.

Commit 5b7af72

Browse files
committed
🍈
1 parent 95247f9 commit 5b7af72

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

app/components/docs/visual_code_example.rb

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ def self.reset_collected_code
1313
@@collected_code = []
1414
end
1515

16-
def initialize(title: nil, description: nil, src: nil, context: nil)
16+
def initialize(ruby_code: nil, title: nil, description: nil, src: nil, context: nil, type: :component, content: nil, content_attributes: nil)
17+
@ruby_code = ruby_code
1718
@title = title
1819
@description = description
1920
@src = src
2021
@context = context
22+
@type = type
23+
@content = content
24+
@content_attributes = content_attributes
2125
end
2226

2327
def view_template(&)
28+
# @display_code = @ruby_code || CGI.unescapeHTML(capture(&))
2429
@display_code = CGI.unescapeHTML(capture(&))
2530
@@collected_code << @display_code
2631

@@ -55,7 +60,7 @@ def render_header
5560
def render_tab_triggers
5661
TabsList do
5762
render_tab_trigger("preview", "Preview", method(:eye_icon))
58-
render_tab_trigger("code", "Code", method(:code_icon))
63+
render_tab_trigger("code", "Code", method(:code_icon)) if @type == :component
5964
end
6065
end
6166

@@ -72,15 +77,25 @@ def render_tab_contents(&)
7277
end
7378

7479
def render_preview_tab(&block)
75-
return iframe_preview if @src
80+
block_class_name = @content.to_s
81+
82+
return iframe_preview(block_class_name) if @type == :block
7683

7784
raw_preview
7885
end
7986

80-
def iframe_preview
81-
div(class: "relative aspect-[4/2.5] w-full overflow-hidden rounded-md border", data: {controller: "iframe-theme"}) do
87+
def iframe_preview(block_name)
88+
div(class: "relative aspect-[4/2.5] w-full overflow-hidden rounded-md border") do
8289
div(class: "absolute inset-0 hidden w-[1600px] bg-background md:block") do
83-
iframe(src: @src, class: "size-full", data: {iframe_theme_target: "iframe"})
90+
if @content
91+
iframe(src: render_block_path(id: block_name, attributes: @content_attributes), class: "size-full")
92+
else
93+
iframe(srcdoc: safe("<div>You cannot render a ruby block for a block preview</div>"), class: "size-full")
94+
# TODO
95+
# decoded_code = CGI.unescapeHTML(@display_code)
96+
# html_content = render_block_to_html(decoded_code)
97+
# iframe(srcdoc: safe(html_content), class: "size-full")
98+
end
8499
end
85100
end
86101
end
@@ -100,6 +115,14 @@ def render_code_tab
100115
end
101116
end
102117

118+
def render_block_to_html(code)
119+
# Extract the component from "render ComponentName.new" pattern
120+
# and evaluate it to generate standalone HTML
121+
component_code = code.strip.sub(/^render\s+/, '')
122+
component = eval(component_code)
123+
component.call
124+
end
125+
103126
def eye_icon
104127
svg(
105128
xmlns: "http://www.w3.org/2000/svg",

0 commit comments

Comments
 (0)