-
Notifications
You must be signed in to change notification settings - Fork 157
/
core_components.ex
719 lines (658 loc) · 23.6 KB
/
core_components.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
defmodule LiveBeatsWeb.CoreComponents do
use Phoenix.Component
use LiveBeatsWeb, :verified_routes
alias Phoenix.LiveView.JS
alias LiveBeats.Accounts
alias LiveBeats.MediaLibrary
def home_path(nil = _current_user), do: "/"
def home_path(%Accounts.User{} = current_user), do: profile_path(current_user)
def profile_upload_path(%Accounts.User{} = user) do
~p"/#{user.username}/songs/new"
end
def profile_path(username) when is_binary(username) do
unverified_path(LiveBeatsWeb.Endpoint, LiveBeatsWeb.Router, ~p"/#{username}")
end
def profile_path(%Accounts.User{} = current_user) do
profile_path(current_user.username)
end
def profile_path(%MediaLibrary.Profile{} = profile) do
profile_path(profile.username)
end
slot :inner_block
def connection_status(assigns) do
~H"""
<div
id="connection-status"
class="hidden rounded-md bg-red-50 p-4 fixed top-1 right-1 w-96 fade-in-scale z-50"
phx-disconnected={JS.show()}
js-show={show("#connection-status")}
js-hide={hide("#connection-status")}
>
<div class="flex">
<div class="flex-shrink-0">
<svg
class="animate-spin -ml-1 mr-3 h-5 w-5 text-red-800"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
aria-hidden="true"
>
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4">
</circle>
<path
class="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
>
</path>
</svg>
</div>
<div class="ml-3">
<p class="text-sm font-medium text-red-800" role="alert">
<%= render_slot(@inner_block) %>
</p>
</div>
</div>
</div>
"""
end
attr :flash, :map
attr :kind, :atom
def flash(%{kind: :error} = assigns) do
~H"""
<div
:if={msg = Phoenix.Flash.get(@flash, @kind)}
id="flash"
class="rounded-md bg-red-50 p-4 fixed top-1 right-1 w-96 fade-in-scale z-50"
phx-click={
JS.push("lv:clear-flash")
|> JS.remove_class("fade-in-scale", to: "#flash")
|> hide("#flash")
}
phx-hook="Flash"
>
<div class="flex justify-between items-center space-x-3 text-red-700">
<.icon name={:exclamation_circle} class="w-5 w-5" />
<p class="flex-1 text-sm font-medium" role="alert">
<%= msg %>
</p>
<button
type="button"
class="inline-flex bg-red-50 rounded-md p-1.5 text-red-500 hover:bg-red-100 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-red-50 focus:ring-red-600"
>
<.icon name={:x} class="w-4 h-4" />
</button>
</div>
</div>
"""
end
def flash(%{kind: :info} = assigns) do
~H"""
<div
:if={msg = Phoenix.Flash.get(@flash, @kind)}
id="flash"
class="rounded-md bg-green-50 p-4 fixed top-1 right-1 w-96 fade-in-scale z-50"
phx-click={JS.push("lv:clear-flash") |> JS.remove_class("fade-in-scale") |> hide("#flash")}
phx-value-key="info"
>
<div class="flex justify-between items-center space-x-3 text-green-700">
<.icon name={:check_circle} class="w-5 h-5" />
<p class="flex-1 text-sm font-medium" role="alert">
<%= msg %>
</p>
<button
type="button"
class="inline-flex bg-green-50 rounded-md p-1.5 text-green-500 hover:bg-green-100 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-green-50 focus:ring-green-600"
>
<.icon name={:x} class="w-4 h-4" />
</button>
</div>
</div>
"""
end
def spinner(assigns) do
~H"""
<svg
class="inline-block animate-spin h-2.5 w-2.5 text-gray-400"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
aria-hidden="true"
>
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4">
</circle>
<path
class="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
>
</path>
</svg>
"""
end
attr :name, :atom, required: true
attr :outlined, :boolean, default: false
attr :rest, :global, default: %{class: "w-4 h-4 inline-block"}
def icon(assigns) do
assigns = assign_new(assigns, :"aria-hidden", fn -> !Map.has_key?(assigns, :"aria-label") end)
~H"""
<%= if @outlined do %>
<%= apply(Heroicons.Outline, @name, [Map.to_list(@rest)]) %>
<% else %>
<%= apply(Heroicons.Solid, @name, [Map.to_list(@rest)]) %>
<% end %>
"""
end
@doc """
Returns a button triggered dropdown with aria keyboard and focus supporrt.
Accepts the follow slots:
* `:id` - The id to uniquely identify this dropdown
* `:img` - The optional img to show beside the button title
* `:title` - The button title
* `:subtitle` - The button subtitle
## Examples
<.dropdown id={@id}>
<:img src={@current_user.avatar_url}/>
<:title><%= @current_user.name %></:title>
<:subtitle>@<%= @current_user.username %></:subtitle>
<:link navigate={profile_path(@current_user)}>View Profile</:link>
<:link navigate={~p"/profile/settings"}Settings</:link>
</.dropdown>
"""
attr :id, :string, required: true
slot :img do
attr :src, :string
end
slot :title
slot :subtitle
slot :link do
attr :navigate, :string
attr :href, :string
attr :method, :any
end
def dropdown(assigns) do
~H"""
<!-- User account dropdown -->
<div class="px-3 mt-6 relative inline-block text-left">
<div>
<button
id={@id}
type="button"
class="group w-full bg-gray-100 rounded-md px-3.5 py-2 text-sm text-left font-medium text-gray-700 hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-purple-500"
phx-click={show_dropdown("##{@id}-dropdown")}
phx-hook="Menu"
data-active-class="bg-gray-100"
aria-haspopup="true"
>
<span class="flex w-full justify-between items-center">
<span class="flex min-w-0 items-center justify-between space-x-3">
<%= for img <- @img do %>
<img
class="w-10 h-10 bg-gray-300 rounded-full flex-shrink-0"
alt=""
{assigns_to_attributes(img)}
/>
<% end %>
<span class="flex-1 flex flex-col min-w-0">
<span class="text-gray-900 text-sm font-medium truncate">
<%= render_slot(@title) %>
</span>
<span class="text-gray-500 text-sm truncate"><%= render_slot(@subtitle) %></span>
</span>
</span>
<svg
class="flex-shrink-0 h-5 w-5 text-gray-400 group-hover:text-gray-500"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M10 3a1 1 0 01.707.293l3 3a1 1 0 01-1.414 1.414L10 5.414 7.707 7.707a1 1 0 01-1.414-1.414l3-3A1 1 0 0110 3zm-3.707 9.293a1 1 0 011.414 0L10 14.586l2.293-2.293a1 1 0 011.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z"
clip-rule="evenodd"
>
</path>
</svg>
</span>
</button>
</div>
<div
id={"#{@id}-dropdown"}
phx-click-away={hide_dropdown("##{@id}-dropdown")}
class="hidden z-10 mx-3 origin-top absolute right-0 left-0 mt-1 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 divide-y divide-gray-200"
role="menu"
aria-labelledby={@id}
>
<div class="py-1" role="none">
<%= for link <- @link do %>
<.link
tabindex="-1"
role="menuitem"
class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-purple-500"
{link}
>
<%= render_slot(link) %>
</.link>
<% end %>
</div>
</div>
</div>
"""
end
def show_mobile_sidebar(js \\ %JS{}) do
js
|> JS.show(to: "#mobile-sidebar-container", transition: "fade-in")
|> JS.show(
to: "#mobile-sidebar",
display: "flex",
time: 300,
transition:
{"transition ease-in-out duration-300 transform", "-translate-x-full", "translate-x-0"}
)
|> JS.hide(to: "#show-mobile-sidebar", transition: "fade-out")
|> JS.dispatch("js:call", to: "#hide-mobile-sidebar", detail: %{call: "focus", args: []})
end
def hide_mobile_sidebar(js \\ %JS{}) do
js
|> JS.hide(to: "#mobile-sidebar-container", transition: "fade-out")
|> JS.hide(
to: "#mobile-sidebar",
time: 300,
transition:
{"transition ease-in-out duration-300 transform", "translate-x-0", "-translate-x-full"}
)
|> JS.show(to: "#show-mobile-sidebar", transition: "fade-in")
|> JS.dispatch("js:call", to: "#show-mobile-sidebar", detail: %{call: "focus", args: []})
end
def show(js \\ %JS{}, selector) do
JS.show(js,
to: selector,
time: 300,
display: "inline-block",
transition:
{"ease-out duration-300", "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95",
"opacity-100 translate-y-0 sm:scale-100"}
)
end
def fade_in, do: fade_in(nil)
def fade_in(js \\ %JS{}, id) do
JS.show(js,
to: id && "##{id}",
time: 1000,
display: "inline-block",
transition: {"ease-out duration-1000", "opacity-0", "opacity-100"}
)
end
def hide(js \\ %JS{}, selector) do
JS.hide(js,
to: selector,
time: 300,
transition:
{"transition ease-in duration-300", "transform opacity-100 scale-100",
"transform opacity-0 scale-95"}
)
end
def show_dropdown(to) do
JS.show(
to: to,
transition:
{"transition ease-out duration-120", "transform opacity-0 scale-95",
"transform opacity-100 scale-100"}
)
|> JS.set_attribute({"aria-expanded", "true"}, to: to)
end
def hide_dropdown(to) do
JS.hide(
to: to,
transition:
{"transition ease-in duration-120", "transform opacity-100 scale-100",
"transform opacity-0 scale-95"}
)
|> JS.remove_attribute("aria-expanded", to: to)
end
def show_modal(js \\ %JS{}, id) when is_binary(id) do
js
|> JS.remove_attribute("disabled", to: "##{id}-confirm")
|> JS.show(
to: "##{id}",
display: "inline-block",
transition: {"ease-out duration-300", "opacity-0", "opacity-100"}
)
|> JS.show(
to: "##{id}-container",
display: "inline-block",
transition:
{"ease-out duration-300", "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95",
"opacity-100 translate-y-0 sm:scale-100"}
)
|> js_call("##{id}-confirm", "focus", [])
end
def hide_modal(js \\ %JS{}, id) do
js
|> JS.set_attribute({"disabled", ""}, to: "##{id}-confirm")
|> JS.remove_class("fade-in", to: "##{id}")
|> JS.hide(
to: "##{id}",
transition: {"ease-in duration-200", "opacity-100", "opacity-0"}
)
|> JS.hide(
to: "##{id}-container",
transition:
{"ease-in duration-200", "opacity-100 translate-y-0 sm:scale-100",
"opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"}
)
|> JS.dispatch("click", to: "##{id} [data-modal-return]")
end
attr :id, :string, required: true
attr :show, :boolean, default: false
attr :patch, :string, default: nil
attr :navigate, :string, default: nil
attr :on_cancel, JS, default: %JS{}
attr :on_confirm, JS, default: %JS{}
attr :rest, :global
slot :title
slot :confirm do
attr :type, :string
attr :form, :string
end
slot :cancel
def modal(assigns) do
~H"""
<div
id={@id}
class={"fixed z-10 inset-0 overflow-y-auto #{if @show, do: "fade-in", else: "hidden"}"}
{@rest}
>
<.focus_wrap id={"#{@id}-focus-wrap"}>
<div
class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0"
aria-labelledby={"#{@id}-title"}
aria-describedby={"#{@id}-description"}
role="dialog"
aria-modal="true"
tabindex="0"
>
<div class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" aria-hidden="true">
</div>
<span class="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">
​
</span>
<div
id={"#{@id}-container"}
class={
"#{if @show, do: "fade-in-scale", else: "hidden"} sticky inline-block align-bottom bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform sm:my-8 sm:align-middle sm:max-w-xl sm:w-full sm:p-6"
}
phx-window-keydown={hide_modal(@on_cancel, @id)}
phx-key="escape"
phx-click-away={hide_modal(@on_cancel, @id)}
>
<%= if @patch do %>
<.link patch={@patch} data-modal-return class="hidden"></.link>
<% end %>
<%= if @navigate do %>
<.link navigate={@navigate} data-modal-return class="hidden"></.link>
<% end %>
<div class="sm:flex sm:items-start">
<div class="mx-auto flex-shrink-0 flex items-center justify-center h-8 w-8 rounded-full bg-purple-100 sm:mx-0">
<!-- Heroicon name: outline/plus -->
<.icon name={:information_circle} outlined class="h-6 w-6 text-purple-600" />
</div>
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left w-full mr-12">
<h3 class="text-lg leading-6 font-medium text-gray-900" id={"#{@id}-title"}>
<%= render_slot(@title) %>
</h3>
<div class="mt-2">
<p id={"#{@id}-content"} class="text-sm text-gray-500">
<%= render_slot(@inner_block) %>
</p>
</div>
</div>
</div>
<div class="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
<%= for confirm <- @confirm do %>
<button
id={"#{@id}-confirm"}
class="phx-submit-loading:opacity-50 disabled:opacity-50 w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:ml-3 sm:w-auto sm:text-sm"
phx-click={@on_confirm}
{assigns_to_attributes(confirm)}
>
<%= render_slot(confirm) %>
</button>
<% end %>
<%= for cancel <- @cancel do %>
<button
class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:w-auto sm:text-sm"
phx-click={hide_modal(@on_cancel, @id)}
{assigns_to_attributes(cancel)}
>
<%= render_slot(cancel) %>
</button>
<% end %>
</div>
</div>
</div>
</.focus_wrap>
</div>
"""
end
attr :id, :string, required: true
attr :min, :integer, default: 0
attr :max, :integer, default: 100
attr :value, :integer
def progress_bar(assigns) do
assigns = assign_new(assigns, :value, fn -> assigns[:min] || 0 end)
~H"""
<div
id={"#{@id}-container"}
class="bg-gray-200 flex-auto dark:bg-black rounded-full overflow-hidden"
phx-update="ignore"
>
<div
id={@id}
class="bg-lime-500 dark:bg-lime-400 h-1.5 w-0"
data-min={@min}
data-max={@max}
data-val={@value}
>
</div>
</div>
"""
end
slot :actions
def title_bar(assigns) do
~H"""
<!-- Page title & actions -->
<div class="border-b border-gray-200 px-4 py-4 sm:flex sm:items-center sm:justify-between sm:px-6 lg:px-8 sm:h-16">
<div class="flex-1 min-w-0">
<h1 class="text-lg font-medium leading-6 text-gray-900 sm:truncate focus:outline-none">
<%= render_slot(@inner_block) %>
</h1>
</div>
<%= if Enum.count(@actions) > 0 do %>
<div class="mt-4 flex sm:mt-0 sm:ml-4 space-x-4">
<%= render_slot(@actions) %>
</div>
<% end %>
</div>
"""
end
attr :patch, :string
attr :primary, :boolean, default: false
attr :rest, :global, include: ["disabled"]
slot :inner_block
def button(%{patch: _} = assigns) do
~H"""
<%= if @primary do %>
<.link patch={@patch} class="order-0 inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-purple-600 hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 sm:order-1 sm:ml-3" {@rest}>
<%= render_slot(@inner_block) %>
</.link>
<% else %>
<.link patch={@patch} class="order-1 inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 sm:order-0 sm:ml-0 lg:ml-3" {assigns_to_attributes(assigns, [:primary, :patch])}>
<%= render_slot(@inner_block) %>
</.link>
<% end %>
"""
end
def button(%{} = assigns) do
~H"""
<%= if @primary do %>
<button
type="button"
class="order-0 inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-purple-600 hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 sm:order-1 sm:ml-3"
{@rest}
>
<%= render_slot(@inner_block) %>
</button>
<% else %>
<button
type="button"
class="order-1 inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 sm:order-0 sm:ml-0 lg:ml-3"
{@rest}
>
<%= render_slot(@inner_block) %>
</button>
<% end %>
"""
end
attr :id, :any, default: nil
attr :row_id, :any, default: false
attr :row_click, :any, default: nil
attr :row_remove, :any, default: nil
attr :rows, :list, required: true
attr :streamable, :boolean, default: false
attr :sortable_drop, :string, default: nil
slot :col, required: true do
attr :label, :string
attr :class, :any
attr :class!, :any
end
def table(assigns) do
~H"""
<div class="mt-8 sm:block">
<div class="align-middle inline-block min-w-full border-b border-gray-200">
<table id={@id} class="min-w-full">
<thead>
<tr class="border-t border-gray-200">
<%= for col <- @col do %>
<th class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
<span class="lg:pl-2"><%= col.label %></span>
</th>
<% end %>
</tr>
</thead>
<tbody
id={"#{@id}-body"}
class="bg-white divide-y divide-gray-100"
phx-update={@streamable && "stream"}
phx-hook={@sortable_drop && "Sortable"}
data-drop={@sortable_drop}
>
<tr
:for={row <- @rows}
id={@row_id && @row_id.(row)}
phx-remove={@row_remove && @row_remove.(row)}
class="hover:bg-gray-50"
>
<td
:for={col <- @col}
phx-click={@row_click && @row_click.(row)}
class={[
"phxp-click-loading:cursor-not-allowed phxp-click-loading:pointer-events-none",
col[:class!] ||
"px-6 py-3 whitespace-nowrap text-sm font-medium text-gray-900 #{col[:class]}"
]}
>
<div class="flex items-center space-x-3 lg:pl-2">
<%= render_slot(col, row) %>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
"""
end
@doc """
Calls a wired up event listener to call a function with arguments.
window.addEventListener("js:call", e => e.target[e.detail.call](...e.detail.args))
"""
def js_call(js \\ %JS{}, to, call, args) do
JS.dispatch(js, "js:call", to: to, detail: %{call: call, args: args})
end
def push_js_cmd(socket, %JS{ops: ops}) do
Phoenix.LiveView.push_event(socket, "js:exec", %{cmd: Phoenix.json_library().encode!(ops)})
end
def focus(js \\ %JS{}, parent, to) do
JS.dispatch(js, "js:focus", to: to, detail: %{parent: parent})
end
def focus_closest(js \\ %JS{}, to) do
js
|> JS.dispatch("js:focus-closest", to: to)
|> hide(to)
end
@doc """
Generates tag for inlined form input errors.
"""
def error_tag(form, field) do
error(%{
errors: form.errors,
field: field,
input_name: Phoenix.HTML.Form.input_name(form, field)
})
end
def error(%{errors: errors, field: field} = assigns) do
assigns =
assigns
|> assign(:error_values, Keyword.get_values(errors, field))
|> assign_new(:class, fn -> "" end)
~H"""
<%= for error <- @error_values do %>
<span
phx-feedback-for={@input_name}
class={
"invalid-feedback inline-block pl-2 pr-2 text-sm text-white bg-red-600 rounded-md #{@class}"
}
>
<%= translate_error(error) %>
</span>
<% end %>
<%= if Enum.empty?(@error_values) do %>
<span class={"invalid-feedback inline-block h-0 #{@class}"}></span>
<% end %>
"""
end
@doc """
Translates an error message using gettext.
"""
def translate_error({msg, opts}) do
# When using gettext, we typically pass the strings we want
# to translate as a static argument:
#
# # Translate "is invalid" in the "errors" domain
# dgettext("errors", "is invalid")
#
# # Translate the number of files with plural rules
# dngettext("errors", "1 file", "%{count} files", count)
#
# Because the error messages we show in our forms and APIs
# are defined inside Ecto, we need to translate them dynamically.
# This requires us to call the Gettext module passing our gettext
# backend as first argument.
#
# Note we use the "errors" domain, which means translations
# should be written to the errors.po file. The :count option is
# set by Ecto and indicates we should also apply plural rules.
if count = opts[:count] do
Gettext.dngettext(LiveBeatsWeb.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(LiveBeatsWeb.Gettext, "errors", msg, opts)
end
end
def translate_changeset_errors(changeset) do
changeset.errors
|> Enum.map(fn {key, value} -> "#{key} #{translate_error(value)}" end)
|> Enum.join("\n")
end
end