Skip to content

Commit bf676ba

Browse files
committed
Update snippet layout with editing flag
1 parent 19f722a commit bf676ba

File tree

7 files changed

+90
-63
lines changed

7 files changed

+90
-63
lines changed

app/javascript/css/application.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@
3636
@import './components/callout.css';
3737
@import './components/color-scheme.scss';
3838
@import './components/table.css';
39+
@import './components/snippet.css';

app/javascript/css/components/code.scss

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@
6666
& .code-title {
6767
font-weight: 600;
6868
display: block;
69-
text-align: center;
69+
text-align: right;
7070
line-height: 1;
71+
flex-grow: 2;
7172

7273
& input {
7374
font-size: var(--code-header-font-size);
7475
line-height: var(--code-header-line-height);
7576
text-align: right;
77+
width: 100%;
7678
}
7779

7880
& a {
@@ -208,44 +210,3 @@
208210
grid-area: 1 / 1 / 2 / 2;
209211
}
210212
}
211-
212-
/*
213-
.autogrow-wrapper taken from
214-
https://css-tricks.com/the-cleanest-trick-for-autogrowing-textareas/
215-
*/
216-
.autogrow-wrapper {
217-
/* easy way to plop the elements on top of each other and have them both sized based on the tallest one's height */
218-
display: grid;
219-
}
220-
221-
.autogrow-wrapper::after {
222-
/* Note the weird space! Needed to preventy jumpy behavior */
223-
content: attr(data-replicated-value) ' ';
224-
225-
/* Hidden from view, clicks, and screen readers */
226-
visibility: hidden;
227-
228-
line-height: var(--code-line-height);
229-
font-family: var(--monospace);
230-
font-size: var(--step--1);
231-
}
232-
233-
.autogrow-wrapper > textarea {
234-
min-height: auto;
235-
236-
/* You could leave this, but after a user resizes, then it ruins the auto sizing */
237-
resize: none;
238-
239-
/* Firefox shows scrollbar on growth, you can hide like this. */
240-
overflow: hidden;
241-
}
242-
243-
.autogrow-wrapper > textarea,
244-
.autogrow-wrapper::after {
245-
/* Place on top of each other */
246-
grid-area: 1 / 1 / 2 / 2;
247-
248-
white-space: pre-wrap;
249-
word-wrap: break-word;
250-
max-width: var(--grid-max-width);
251-
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.snippet {
2+
filter: drop-shadow(0 0 0.75rem var(--joy-color-50));
3+
}
4+
5+
.snippet-background {
6+
background-color: var(--joy-color-200);
7+
padding: var(--space-l);
8+
width: fit-content;
9+
border-radius: 0.5rem;
10+
}
11+
12+
/*
13+
.autogrow-wrapper taken from
14+
https://css-tricks.com/the-cleanest-trick-for-autogrowing-textareas/
15+
*/
16+
.autogrow-wrapper {
17+
/* easy way to plop the elements on top of each other and have them both sized based on the tallest one's height */
18+
display: grid;
19+
}
20+
21+
.autogrow-wrapper::after {
22+
/* Note the weird space! Needed to preventy jumpy behavior */
23+
content: attr(data-replicated-value) ' ';
24+
25+
/* Hidden from view, clicks, and screen readers */
26+
visibility: hidden;
27+
28+
line-height: var(--code-line-height);
29+
font-family: var(--monospace);
30+
font-size: var(--step--1);
31+
}
32+
33+
.autogrow-wrapper > textarea {
34+
min-height: auto;
35+
36+
/* You could leave this, but after a user resizes, then it ruins the auto sizing */
37+
resize: none;
38+
39+
/* Firefox shows scrollbar on growth, you can hide like this. */
40+
overflow: hidden;
41+
}
42+
43+
.autogrow-wrapper > textarea,
44+
.autogrow-wrapper::after {
45+
/* Place on top of each other */
46+
grid-area: 1 / 1 / 2 / 2;
47+
48+
white-space: pre-wrap;
49+
word-wrap: break-word;
50+
max-width: var(--grid-max-width);
51+
}

app/views/components/code_block/snippet.rb

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,35 @@ class CodeBlock::Snippet < ApplicationComponent
55

66
attr_reader :snippet
77

8-
def initialize(snippet, **)
8+
def initialize(snippet, editing: false, **)
99
@snippet = snippet
10+
@editing = editing
1011
end
1112

1213
def view_template
13-
render CodeBlock::Container.new(language: language, class: "snippet") do
14-
render CodeBlock::Header.new do
15-
label(class: "sr-only", for: "snippet[filename]") { "Filename" }
16-
input(type: "text", name: "snippet[filename]", value: filename)
17-
end
14+
div(class: "snippet-background") do
15+
render CodeBlock::Container.new(language: language, class: "snippet") do
16+
render CodeBlock::Header.new do
17+
if editing?
18+
label(class: "sr-only", for: "snippet[filename]") { "Filename" }
19+
input(type: "text", name: "snippet[filename]", value: filename)
20+
else
21+
filename
22+
end
23+
end
1824

19-
render CodeBlock::Body.new(data: {controller: "snippet-editor"}) do
20-
div(class: "grid-stack") do
21-
render CodeBlock::Code.new(source, language: language, data: {snippet_editor_target: "source"})
22-
label(class: "sr-only", for: "snippet[source]") { "Source" }
23-
div(class: "code-editor autogrow-wrapper") do
24-
textarea(
25-
name: "snippet[source]",
26-
data: {snippet_editor_target: "textarea"}
27-
) { source }
25+
render CodeBlock::Body.new(data: controller_data) do
26+
div(class: "grid-stack") do
27+
render CodeBlock::Code.new(source, language: language, data: {snippet_editor_target: "source"})
28+
if editing?
29+
label(class: "sr-only", for: "snippet[source]") { "Source" }
30+
div(class: "code-editor autogrow-wrapper") do
31+
textarea(
32+
name: "snippet[source]",
33+
data: {snippet_editor_target: "textarea"}
34+
) { source }
35+
end
36+
end
2837
end
2938
end
3039
end
@@ -44,5 +53,11 @@ def source
4453
snippet.source || ""
4554
end
4655

56+
def editing? = !!@editing
57+
4758
delegate :language, :filename, to: :snippet
59+
60+
def controller_data
61+
editing? ? {controller: "snippet-editor"} : {}
62+
end
4863
end

app/views/snippets/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</div>
2727

2828
<%= turbo_frame_tag dom_id(snippet, :code_block), class: "snippet-frame grid-cols-12" do %>
29-
<%= render snippet %>
29+
<%= render CodeBlock::Snippet.new(snippet, editing: true) %>
3030
<% end %>
3131

3232
<fieldset>

app/views/snippets/index.html.erb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
</div>
66

77
<% @snippets.each do |snippet| %>
8-
<%= render snippet %>
9-
<p>
10-
<%= link_to "Show this snippet", snippet %>
11-
</p>
8+
<%= link_to snippet, class: "block" do %>
9+
<%= render CodeBlock::Snippet.new(snippet) %>
10+
<% end %>
1211
<% end %>
1312
</div>

app/views/snippets/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<%= render Pages::Header.new(title: "Snippet #{@snippet.filename}".strip) %>
22
<div class="section-content container py-gap">
3-
<%= render @snippet %>
3+
<%= render CodeBlock::Snippet.new(@snippet) %>
44

55
<div>
66
<%= link_to "Edit this snippet", edit_snippet_path(@snippet) %> |

0 commit comments

Comments
 (0)