Skip to content

Commit fcc5c24

Browse files
committed
Improve URL page further
1 parent b920cc2 commit fcc5c24

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
lines changed

apps/components_guide_web/assets/css/app.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ h3 {
8181
p {
8282
padding: var(--paragraph-spacing);
8383
}
84+
p:empty {
85+
display: none;
86+
}
8487
li {
8588
padding: var(--listitem-spacing);
8689
}

apps/components_guide_web/lib/components_guide_web/templates/web_standards/url.html.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,9 @@ const current = new URL(window.location.href);
2222
const current = new URL(request.url);
2323
```
2424

25-
## Swift’s `URL`
26-
27-
```swift
28-
let url = URL(string: "https://www.example.org/")
29-
30-
```
31-
3225
## Elixir’s `URI`
3326

3427
```elixir
3528
url = URI.parse("https://www.example.org/")
3629

3730
```
38-
39-
## Go’s `net/url`
40-
41-
```go
42-
u, err := url.Parse("https://www.example.org/")
43-
```

apps/components_guide_web/lib/components_guide_web/templates/web_standards/url_live.ex

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
1010
URI.parse(raw_url)
1111
end
1212

13+
def read(%State{} = state, :query) do
14+
%URI{query: query} = to_url(state)
15+
case query do
16+
"" -> ""
17+
value -> "?" <> value
18+
end
19+
end
20+
1321
def to_url_string(%State{raw_url: raw_url}) do
1422
URI.parse(raw_url) |> URI.to_string()
1523
end
@@ -51,10 +59,15 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
5159

5260
def add_new_query(%State{} = state) do
5361
url = to_url(state)
54-
url = put_in(url.query, url.query <> "&a=b")
62+
# url = put_in(url.query, url.query <> "&a=b")
63+
# url = update_in(url.query, fn query_string -> add_query_params_to(query_string, "a=b") end)
64+
url = update_in(url.query, &add_query_params_to(&1, "a=b"))
5565
put_in(state.raw_url, url |> URI.to_string())
5666
end
5767

68+
defp add_query_params_to("", new_pair), do: new_pair
69+
defp add_query_params_to(query_string, new_pair), do: query_string <> "&" <> new_pair
70+
5871
def change_query_vars(%State{} = state, query_vars) do
5972
url = to_url(state)
6073
url = put_in(url.query, URI.encode_query(query_vars))
@@ -71,7 +84,7 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
7184
<form phx-change=change>
7285
7386
<pre class="p-4 my-2" style="color: #d6deeb;">
74-
<code><span class="text-green-400"><%= State.to_url(@state).scheme %></span>://<span class="text-yellow-400"><%= State.to_url(@state).host %></span><span class="text-orange-400"><%= State.to_url(@state).path %></span><span class="text-indigo-400">?<%= State.to_url(@state).query %></span></code>
87+
<code><span class="text-green-400"><%= State.to_url(@state).scheme %></span>://<span class="text-yellow-400"><%= State.to_url(@state).host %></span><span class="text-orange-400"><%= State.to_url(@state).path %></span><span class="text-indigo-400"><%= @state |> State.read(:query) %></span></code>
7588
</pre>
7689
7790
<div class="flex flex-col space-y-4">
@@ -111,8 +124,10 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
111124
112125
</form>
113126
127+
<h2>Live code snippets</h2>
128+
114129
<section aria-labelledby=javascript-url-heading>
115-
<h2 id=javascript-url-heading>JavaScript’s <code>URL</code></h2>
130+
<h3 id=javascript-url-heading>JavaScript’s <code>URL</code></h3>
116131
117132
<pre class="language-js" phx-hook=PreCode><code>const url = new URL(
118133
'<%= @state |> State.to_url() |> URI.to_string() %>'
@@ -131,20 +146,53 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
131146
</section>
132147
133148
<section aria-labelledby=swift-url-heading>
134-
<h2 id=swift-url-heading>Swift’s <code>URL</code></h2>
149+
<h3 id=swift-url-heading>Swift’s <code>URL</code></h3>
135150
136151
<pre class="language-swift" phx-hook=PreCode><code>let url = URL(
137152
string: "<%= @state |> State.to_url() |> URI.to_string() %>"
138153
)!
139-
url.scheme // Optional("<%= State.to_url(@state).scheme %>")
140-
url.host // Optional("<%= State.to_url(@state).host %>")
154+
url.scheme // <%= State.to_url(@state).scheme |> format(:swift_optional) %>
155+
url.host // <%= State.to_url(@state).host |> format(:swift_optional) %>
141156
url.path // "<%= State.to_url(@state).path %>"
142-
url.query // Optional("<%= State.to_url(@state).query %>")
157+
url.query // <%= State.to_url(@state).query |> format(:swift_optional) %>
158+
</code></pre>
159+
</section>
160+
161+
<section aria-labelledby=golang-url-heading>
162+
<h3 id=golang-url-heading>Golang’s <code>net/url</code></h3>
163+
164+
<pre class="language-go" phx-hook=PreCode><code>import (
165+
"log"
166+
"net/url"
167+
)
168+
169+
exampleURL, err := url.Parse(
170+
"<%= @state |> State.to_url() |> URI.to_string() %>"
171+
)
172+
if err != nil {
173+
log.Fatal(err)
174+
}
175+
exampleURL.Scheme // "<%= State.to_url(@state).scheme %>"
176+
exampleURL.Host // "<%= State.to_url(@state).host %>"
177+
exampleURL.Path // "<%= State.to_url(@state).path %>"
178+
179+
exampleURL.RawQuery // "<%= State.to_url(@state).query %>"
180+
query, err := url.ParseQuery(exampleURL.RawQuery)
181+
if err != nil {
182+
log.Fatal(err)
183+
}
184+
<%= for {key, value} <- State.get_query_vars(@state) do
185+
"query.Get(\"#{key}\") // \"#{value}\""
186+
end |> Enum.join("\n") %>
143187
</code></pre>
144188
</section>
145189
"""
146190
end
147191

192+
defp format(nil, :swift_optional), do: "nil"
193+
defp format("", :swift_optional), do: "nil"
194+
defp format(value, :swift_optional), do: "Optional(\"#{value}\")"
195+
148196
def handle_event(
149197
"change",
150198
changes = %{

0 commit comments

Comments
 (0)