-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcomponents.ex
More file actions
1341 lines (1174 loc) · 40.2 KB
/
components.ex
File metadata and controls
1341 lines (1174 loc) · 40.2 KB
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule LiveDebugger.App.Web.Components do
@moduledoc """
Core components used in the LiveDebugger application.
These are the general building blocks.
"""
use Phoenix.Component
alias LiveDebugger.App.Utils.Format
alias LiveDebugger.App.Debugger.Web.Components.Pages
alias Phoenix.LiveView.JS
alias LiveDebugger.App.Web.Hooks.Flash.ExceptionFlashData
alias LiveDebugger.App.Web.Hooks.Flash.LinkFlashData
@report_issue_url "https://github.com/software-mansion/live-debugger/issues/new/choose"
@doc """
Alert message component. Use it to display error messages or warnings.
"""
attr(:class, :any, default: nil, doc: "Additional classes to add to the alert.")
attr(:with_icon, :boolean, default: false, doc: "Whether to show an icon.")
attr(:heading, :string, default: nil, doc: "Heading for the alert.")
attr(:rest, :global)
slot(:inner_block, required: true)
def alert(assigns) do
~H"""
<div
class={[
"bg-error-bg border border-error-border text-error-text text-sm p-2 flex flex-col gap-1 rounded-lg"
| List.wrap(@class)
]}
{@rest}
>
<div class="flex items-center gap-2">
<.icon :if={@with_icon} name="icon-x-circle" class="text-error-icon" />
<p class="font-medium"><%= @heading %></p>
</div>
<%= render_slot(@inner_block) %>
</div>
"""
end
@doc """
Checkbox element usable in forms.
## Examples
<.form for={@form}>
<.checkbox field={@form[:my_field]} label="My Field" />
</.form>
"""
attr(:field, Phoenix.HTML.FormField, required: true)
attr(:label, :string, default: nil)
attr(:wrapper_class, :any, default: nil, doc: "Additional classes for the wrapper div.")
attr(:input_class, :any, default: nil, doc: "Additional classes for the input element.")
attr(:label_class, :any, default: nil, doc: "Additional classes for the label element.")
attr(:rest, :global, include: ~w(type))
def checkbox(assigns) do
~H"""
<div class={["flex items-center gap-2" | List.wrap(@wrapper_class)]}>
<input
id={@field.id}
name={@field.name}
type="checkbox"
checked={@field.value}
class={[
"w-4 h-4 text-ui-accent border border-default-border"
| List.wrap(@input_class)
]}
{@rest}
/>
<label :if={@label} for={@field.id} class={["" | List.wrap(@label_class)]}>
<%= @label %>
</label>
</div>
"""
end
@doc """
Select dropdown element usable in forms.
## Examples
<.form for={@form}>
<.select field={@form[:my_field]} label="My Field" options={[{"Option 1", "1"}, {"Option 2", "2"}]} />
</.form>
"""
attr(:field, Phoenix.HTML.FormField, required: true)
attr(:label, :string, default: nil)
attr(:options, :list,
required: true,
doc: "List of {label, value} tuples for the select options"
)
attr(:wrapper_class, :any, default: nil, doc: "Additional classes for the wrapper div.")
attr(:select_class, :any, default: nil, doc: "Additional classes for the select element.")
attr(:label_class, :any, default: nil, doc: "Additional classes for the label element.")
attr(:rest, :global)
def select(assigns) do
~H"""
<div class={["flex flex-col gap-2" | List.wrap(@wrapper_class)]}>
<label :if={@label} for={@field.id} class={["font-medium text-sm" | List.wrap(@label_class)]}>
<%= @label %>
</label>
<select
id={@field.id}
name={@field.name}
class={[
"w-full rounded bg-surface-0-bg border border-default-border text-xs"
| List.wrap(@select_class)
]}
{@rest}
>
<%= Phoenix.HTML.Form.options_for_select(@options, @field.value) %>
</select>
</div>
"""
end
@doc """
Text input element usable in forms.
## Examples
<.form for={@form}>
<.text_input field={@form[:my_field]} label="My Field" placeholder="Enter text..." />
</.form>
"""
attr(:field, Phoenix.HTML.FormField, required: true)
attr(:label, :string, default: nil)
attr(:wrapper_class, :any, default: nil, doc: "Additional classes for the wrapper div.")
attr(:input_class, :any, default: nil, doc: "Additional classes for the input element.")
attr(:label_class, :any, default: nil, doc: "Additional classes for the label element.")
attr(:rest, :global, include: ~w(type placeholder))
def text_input(assigns) do
~H"""
<div class={["flex flex-col gap-2" | List.wrap(@wrapper_class)]}>
<label :if={@label} for={@field.id} class={["font-medium text-sm" | List.wrap(@label_class)]}>
<%= @label %>
</label>
<input
type={@rest[:type] || "text"}
id={@field.id}
name={@field.name}
value={@field.value}
class={[
"w-full rounded bg-surface-0-bg border border-default-border text-xs placeholder:text-ui-muted px-3 py-2"
| List.wrap(@input_class)
]}
{@rest}
/>
</div>
"""
end
@doc """
Textarea element usable in forms.
## Examples
<.form for={@form}>
<.textarea field={@form[:my_field]} label="My Field" placeholder="Enter text..." />
</.form>
"""
attr(:field, Phoenix.HTML.FormField, required: true)
attr(:label, :string, default: nil)
attr(:wrapper_class, :any, default: nil, doc: "Additional classes for the wrapper div.")
attr(:textarea_class, :any, default: nil, doc: "Additional classes for the textarea element.")
attr(:label_class, :any, default: nil, doc: "Additional classes for the label element.")
attr(:rest, :global, include: ~w(rows placeholder))
def textarea(assigns) do
~H"""
<div class={["flex flex-col gap-2" | List.wrap(@wrapper_class)]}>
<label :if={@label} for={@field.id} class={["font-medium text-sm" | List.wrap(@label_class)]}>
<%= @label %>
</label>
<textarea
id={@field.id}
name={@field.name}
class={[
"w-full rounded bg-surface-0-bg border border-default-border text-xs placeholder:text-ui-muted resize-y"
| List.wrap(@textarea_class)
]}
{@rest}
><%= @field.value %></textarea>
</div>
"""
end
attr(:field, Phoenix.HTML.FormField, required: true)
attr(:label, :string, default: nil)
attr(:wrapper_class, :any, default: nil, doc: "Additional classes for the wrapper div.")
attr(:label_class, :any, default: nil, doc: "Additional classes for the label element.")
attr(:rest, :global)
def codearea(assigns) do
~H"""
<div
id={"#{@field.id}-codearea-wrapper"}
phx-hook="CodeMirrorTextarea"
data-value={@field.value}
class={["flex flex-col gap-2" | List.wrap(@wrapper_class)]}
>
<label :if={@label} for={@field.id} class={["font-medium text-sm" | List.wrap(@label_class)]}>
<%= @label %>
</label>
<textarea id={@field.id} name={@field.name} class="hidden" phx-debounce="250" {@rest}><%= @field.value %></textarea>
<div id={"#{@field.id}-codemirror"} phx-update="ignore"></div>
</div>
"""
end
@doc """
Button component with customizable variant and size.
"""
attr(:variant, :string, default: "primary", values: ["primary", "secondary"])
attr(:size, :string, default: "md", values: ["md", "sm"])
attr(:class, :any, default: nil, doc: "Additional classes to add to the button.")
attr(:rest, :global, include: ~w(disabled))
slot(:inner_block, required: true)
def button(assigns) do
~H"""
<button
class={
[
"w-max h-max rounded text-xs font-semibold disabled:opacity-50 disabled:pointer-events-none",
button_color_classes(@variant),
button_size_classes(@size)
] ++
List.wrap(@class)
}
{@rest}
>
<%= render_slot(@inner_block) %>
</button>
"""
end
@doc """
Link styled as a button. Use for external links that should match button appearance.
"""
attr(:href, :string, required: true)
attr(:variant, :string, default: "secondary", values: ["primary", "secondary"])
attr(:size, :string, default: "sm", values: ["md", "sm"])
attr(:target, :string, default: "_blank")
attr(:id, :string, default: nil)
attr(:class, :any, default: nil, doc: "Additional classes to add.")
attr(:rest, :global, include: ~w(rel))
slot(:inner_block, required: true)
def button_link(assigns) do
~H"""
<.link
href={@href}
target={@target}
id={@id}
class={[
"inline-flex items-center gap-1.5 shrink-0 w-max h-max rounded text-xs font-semibold",
button_size_classes(@size),
button_color_classes(@variant),
@class
]}
{@rest}
>
<%= render_slot(@inner_block) %>
</.link>
"""
end
@doc """
Collapsible element that can be toggled open and closed.
It uses the `details` and `summary` HTML elements.
`hide-on-open` and `show-on-open` css classes are used to hide or show elements based on the open state of the collapsible.
## Examples
<.collapsible id="collapsible" open={true}>
<:label>
<div>
<div class="hide-on-open">Info when closed</div>
<div class="show-on-open">Info when open</div>
</div>
</:label>
<div>Content</div>
</.collapsible>
"""
attr(:id, :string, required: true)
attr(:class, :any, default: nil, doc: "CSS class for parent container")
attr(:label_class, :any, default: nil, doc: "CSS class for the label")
attr(:chevron_class, :any, default: nil, doc: "CSS class for the chevron icon")
attr(:open, :boolean, default: false, doc: "Whether the collapsible is open by default")
attr(:save_state_in_browser, :boolean,
default: false,
doc: "Whether to save the open/closed state in the browser's local storage"
)
attr(:icon, :string,
default: "icon-chevron-right",
doc: "Icon for chevron. It will be rotated 90 degrees when the collapsible is open"
)
attr(:rest, :global)
slot(:label, required: true)
slot(:inner_block, required: true)
def collapsible(assigns) do
assigns =
assigns
|> assign(:open, to_string(assigns.open))
|> assign(:save_state_in_browser, to_string(assigns.save_state_in_browser))
~H"""
<details
id={@id}
phx-hook="Collapsible"
data-open={@open}
data-save-state-in-browser={@save_state_in_browser}
class={[
"block"
| List.wrap(@class)
]}
>
<summary
id={@id <> "-summary"}
class={["flex items-center cursor-pointer" | List.wrap(@label_class)]}
{@rest}
>
<.icon name={@icon} class={["rotate-icon shrink-0" | List.wrap(@chevron_class)]} />
<%= render_slot(@label) %>
</summary>
<%= render_slot(@inner_block) %>
</details>
"""
end
@doc """
Static collapsible element. It doesn't perform any client-side actions.
## Examples
<.static_collapsible id="collapsible" open={true}>
<:label :let={open}>
<%= if(open, do: "Open", else: "Closed") %>
</:label>
<div>Content</div>
</.static_collapsible>
"""
attr(:open, :boolean, required: true, doc: "State of the collapsible")
attr(:class, :any, default: nil, doc: "CSS class for parent container")
attr(:label_class, :any, default: nil, doc: "CSS class for the label")
attr(:chevron_class, :any, default: nil, doc: "CSS class for the chevron icon")
attr(:rest, :global)
slot(:label, required: true)
slot(:inner_block, required: true)
def static_collapsible(assigns) do
~H"""
<div class={["block" | List.wrap(@class)]}>
<div class={["flex items-center cursor-pointer" | List.wrap(@label_class)]} {@rest}>
<.icon
name="icon-chevron-right"
class={["shrink-0", if(@open, do: "rotate-90") | List.wrap(@chevron_class)]}
/>
<%= render_slot(@label, @open) %>
</div>
<%= if(@open, do: render_slot(@inner_block)) %>
</div>
"""
end
@doc """
Renders flash notices.
## Examples
<.flash flash={@flash} />
<.flash phx-mounted={show("#flash")}>Welcome Back!</.flash>
"""
attr(:id, :string, doc: "the optional id of flash container")
attr(:flash, :map, default: %{}, doc: "the map of flash messages to display")
attr(:kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup")
attr(:rest, :global, doc: "the arbitrary HTML attributes to add to the flash container")
def flash(assigns) do
message = Phoenix.Flash.get(assigns.flash, assigns.kind)
assigns =
assigns
|> assign_new(:id, fn -> "flash-#{assigns.kind}" end)
|> assign(:message, message)
~H"""
<div
:if={@message}
id={@id}
phx-hook="AutoClearFlash"
role="alert"
class={[
"max-sm:animate-fade-in-mobile sm:animate-fade-in fixed left-2 bottom-2 w-80 sm:w-96 z-50 rounded-sm p-4 flex justify-between items-center gap-3",
@kind == :error && "bg-error-bg text-error-text border-error-text border",
@kind == :info &&
"bg-info-bg text-info-text border-info-border border"
]}
{@rest}
>
<div class="flex gap-3 items-start min-w-0">
<div>
<.icon :if={@kind == :error} name="icon-x-circle" class="text-error-icon w-3 h-3" />
<.icon :if={@kind == :info} name="icon-info" class="text-info-icon w-3 h-3" />
</div>
<p class="min-w-0">
<div class="flex flex-col flex-1 min-w-0">
<%= cond do %>
<% match?(%ExceptionFlashData{}, @message) -> %>
<.exception_flash_message message={@message} />
<% match?(%LinkFlashData{}, @message) -> %>
<.link_flash_message message={@message} />
<% is_binary(@message) -> %>
<%= @message %>
<% end %>
</div>
</p>
</div>
<button
phx-click={
"lv:clear-flash"
|> JS.push(value: %{key: @kind})
|> JS.hide(
to: "##{@id}",
time: 200,
transition: "max-sm:animate-fade-out-mobile sm:animate-fade-out"
)
}
aria-label="close"
>
<.icon name="icon-cross w-4 h-4" />
</button>
</div>
"""
end
@doc """
Shows the flash group with standard titles and content.
## Examples
<.flash_group flash={@flash} />
"""
attr(:flash, :map, required: true, doc: "the map of flash messages")
attr(:id, :string, default: "flash-group", doc: "the optional id of flash container")
def flash_group(assigns) do
~H"""
<div id={@id}>
<.flash kind={:info} flash={@flash} />
<.flash kind={:error} flash={@flash} />
</div>
"""
end
attr(:id, :string, required: true)
attr(:title, :string, required: true)
attr(:class, :any, default: nil)
attr(:title_class, :any, default: nil)
attr(:inner_class, :any, default: nil)
slot(:right_panel)
slot(:title_sub_panel)
slot(:inner_block)
def section(assigns) do
~H"""
<div
id={@id}
class={[
"w-full min-w-[20rem] flex flex-col shadow-custom rounded-sm bg-surface-0-bg border border-default-border"
| List.wrap(@class)
]}
>
<div class="px-4 flex items-center h-12 p-2 border-b border-default-border">
<div class="flex justify-between items-center w-full gap-2">
<div class={[
"font-medium text-sm min-w-26 flex items-center gap-2"
| List.wrap(@title_class)
]}>
<p><%= @title %></p>
<%= render_slot(@title_sub_panel) %>
</div>
<div class="w-max">
<%= render_slot(@right_panel) %>
</div>
</div>
</div>
<div class={[
"flex flex-1 overflow-auto rounded-sm bg-surface-0-bg" | List.wrap(@inner_class)
]}>
<%= render_slot(@inner_block) %>
</div>
</div>
"""
end
attr(:id, :string, required: true)
attr(:title, :string, required: true)
attr(:save_state_in_browser, :boolean,
default: false,
doc: "Whether to save the open/closed state in the browser's local storage"
)
attr(:class, :any, default: nil)
attr(:title_class, :any, default: nil)
attr(:inner_class, :any, default: nil)
slot(:right_panel)
slot(:title_sub_panel)
slot(:inner_block)
def collapsible_section(assigns) do
~H"""
<.collapsible
id={@id}
phx-hook="CollapsedSectionPulse"
class={[
"w-full min-w-[20rem] flex flex-col shadow-custom rounded-sm bg-surface-0-bg border border-default-border"
| List.wrap(@class)
]}
label_class="pr-4 flex items-center h-12 p-2 border-b border-default-border"
save_state_in_browser={@save_state_in_browser}
>
<:label>
<div class="ml-1 flex justify-between items-center w-full gap-2">
<div class={[
"font-medium text-sm min-w-26 flex items-center gap-2"
| List.wrap(@title_class)
]}>
<p><%= @title %></p>
<%= render_slot(@title_sub_panel) %>
</div>
<div class="w-max">
<%= render_slot(@right_panel) %>
</div>
</div>
</:label>
<div class={[
"flex flex-1 overflow-auto rounded-sm bg-surface-0-bg" | List.wrap(@inner_class)
]}>
<%= render_slot(@inner_block) %>
</div>
</.collapsible>
"""
end
@doc """
Info tooltip for sections
"""
attr(:id, :string, required: true)
attr(:content, :string, required: true)
attr(:position, :string, default: "top-center")
def section_info_tooltip(assigns) do
~H"""
<.tooltip
id={@id <> "-tooltip"}
content={@content}
position={@position}
>
<span class="flex items-center justify-center h-full">
<.icon name="icon-info" class="w-4 h-4 bg-button-secondary-content" />
</span>
</.tooltip>
"""
end
@doc """
Typography component to render headings.
"""
attr(:class, :any, default: nil, doc: "Additional classes to add to the heading.")
attr(:rest, :global)
slot(:inner_block, required: true)
def h1(assigns) do
~H"""
<h1 class={["text-xl font-semibold" | List.wrap(@class)]} {@rest}>
<%= render_slot(@inner_block) %>
</h1>
"""
end
@doc """
Renders an icon.
Not all icons are available. If you want to use an icon check if it exists in the `assets/icons` folder.
`name` must start with `icon-`
## Examples
<.icon name="icon-play" />
"""
attr(:name, :string, required: true, doc: "The name of the icon. Must start with `icon-`.")
attr(:class, :any, default: nil, doc: "Additional classes to add to the icon.")
attr(:rest, :global)
def icon(%{name: "icon-" <> _} = assigns) do
~H"""
<span class={[@name | List.wrap(@class)]} {@rest}></span>
"""
end
@doc """
Button with only an icon in it.
"""
attr(:icon, :string, required: true, doc: "Icon to be displayed as a button.")
attr(:variant, :string,
default: "primary",
values: ["primary", "secondary"],
doc: "Variant of the button."
)
attr(:class, :any, default: nil, doc: "Additional classes to add to the button.")
attr(:rest, :global, include: ~w(id disabled))
def icon_button(assigns) do
assigns =
assign(assigns, :aria_label, assigns[:"aria-label"] || Format.kebab_to_text(assigns.icon))
~H"""
<.button
aria-label={@aria_label}
class={["w-7! h-7! px-[0.2rem] py-[0.2rem]" | List.wrap(@class)]}
variant={@variant}
{@rest}
>
<.icon name={@icon} class="h-4 w-4" />
</.button>
"""
end
@doc """
Renders a list of elements using the `item` slot.
## Examples
<.list elements={["Item 1", "Item 2", "Item 3"]}>
<:item :let={item}>
<div class="p-2 bg-gray-100 rounded">
<%= item %>
</div>
</:item>
</.list>
"""
attr(:elements, :list,
required: true,
doc: "Elements that will be displayed in the list's `item` slot."
)
attr(:class, :any, default: nil, doc: "Additional classes for the list container.")
attr(:item_class, :any, default: nil, doc: "Additional classes for each item.")
slot(:item, required: true)
def list(assigns) do
~H"""
<ul class={[
"w-full flex flex-col overflow-auto p-2" | List.wrap(@class)
]}>
<li :for={elem <- @elements} class={@item_class}>
<%= render_slot(@item, elem) %>
</li>
</ul>
"""
end
@doc """
Renders a sidebar slide over element.
Clicking outside or the cross icon results in the `close-sidebar` event being triggered.
"""
attr(:id, :string, required: true)
attr(:sidebar_hidden?, :boolean, default: true, doc: "The default state of the sidebar")
attr(:event_target, :any, default: nil, doc: "The target of the closing sidebar event")
attr(:page, :atom, required: true, values: [:node_inspector, :global_traces])
attr(:trigger_sidebar, :boolean, default: false)
slot(:inner_block)
def sidebar_slide_over(assigns) do
~H"""
<span
:if={@trigger_sidebar}
class="hidden [--open-sidebar:1] md_ct:[--open-sidebar:0]"
id="sidebar-auto-opener"
phx-hook="OpenComponentsTree"
data-cmd={Pages.get_open_sidebar_js(@page)}
>
</span>
<div class="w-max flex bg-sidebar-bg shadow-custom h-full">
<div
id={@id}
phx-hook="CloseSidebarOnResize"
data-cmd={Pages.get_close_sidebar_js(@page)}
class={[
(@sidebar_hidden? && "hidden") || "flex",
"fixed inset-0 bg-black/25 justify-end items-start md_ct:flex md_ct:static md_ct:inset-auto md_ct:bg-transparent z-20",
"[--narrow-view:1]",
"md_ct:[--narrow-view:0]"
]}
>
<div
class="w-full h-full md_ct:hidden"
phx-click="close-sidebar"
{@event_target && %{:"phx-target" => @event_target} || %{}}
>
</div>
<div
class="shrink-0 h-full w-80 bg-sidebar-bg flex flex-col gap-1 justify-between border-x border-default-border md_ct:border-l"
id="components-tree-sidebar-container"
>
<.icon_button
:if={!@sidebar_hidden?}
icon="icon-cross"
class="absolute top-4 right-4 md_ct:hidden"
variant="secondary"
phx-click="close-sidebar"
{@event_target && %{:"phx-target" => @event_target} || %{}}
/>
<%= render_slot(@inner_block) %>
</div>
</div>
</div>
"""
end
@doc """
Renders a fullscreen using Fullscreen hook.
It can be opened and via browser "open" event (by default) with JS.dispatch or via server event (check example in fullscreen button).
You can use `fullscreen_button` to open this fullscreen.
You can close the fullscreen using X button or by pressing ESC key.
"""
attr(:id, :string, required: true)
attr(:title, :string, default: "", doc: "Title of the fullscreen.")
attr(:send_close_event, :boolean,
default: false,
doc:
"Whether to send a `fullscreen-closed` event to the server when the fullscreen is closed."
)
attr(:class, :any,
default: nil,
doc: "Additional classes to be added to the fullscreen element."
)
slot(:inner_block, required: true)
slot(:search_bar_slot)
slot(:header, doc: "Optional custom header slot to replace the default one")
def fullscreen(assigns) do
~H"""
<dialog
id={@id}
phx-hook="Fullscreen"
data-send-close-event={@send_close_event}
class={[
"relative h-max w-full xl:w-max xl:min-w-[50rem] bg-surface-0-bg overflow-auto hidden flex-col rounded-md backdrop:bg-black backdrop:opacity-50"
| List.wrap(@class)
]}
>
<div phx-click-away={JS.dispatch("close", to: "##{@id}")}>
<%= if @header != [] do %>
<%= render_slot(@header) %>
<% else %>
<div class="w-full h-12 py-auto px-3 flex justify-between items-center border-b border-default-border pt-1">
<div class="flex justify-between items-center w-full font-semibold text-primary-text text-base">
<%= @title %>
<div class="mr-2 font-normal"><%= render_slot(@search_bar_slot) %></div>
</div>
<.icon_button
id={"#{@id}-close"}
phx-click={JS.dispatch("close", to: "##{@id}")}
icon="icon-cross"
variant="secondary"
/>
</div>
<% end %>
<div class="overflow-auto flex flex-col gap-2 text-primary-text">
<%= render_slot(@inner_block) %>
</div>
</div>
</dialog>
"""
end
@doc """
Renders a button which will show a fullscreen when clicked.
You can override `phx-click` value, but remember to push correct event at the end of `handle_event` function.
## Examples
<.fullscreen_button
id="my-fullscreen"
phx-click="open-fullscreen"
icon="icon-expand"
/>
@impl true
def handle_event("open-fullscreen", _, socket) do
trace_id = String.to_integer(string_id)
socket
|> push_event("my-fullscreen-open", %{})
|> noreply()
end
"""
attr(:id, :string, required: true, doc: "Same as `id` of the fullscreen.")
attr(:icon, :string,
default: "icon-expand",
doc: "Icon to be displayed as a button"
)
attr(:rest, :global, include: ~w(class))
def fullscreen_button(assigns) do
~H"""
<.tooltip id={@id <> "-tooltip"} content="Fullscreen" position="top-center">
<.icon_button
id={"#{@id}-button"}
phx-click={@rest[:"phx-click"] || JS.dispatch("open", to: "##{@id}")}
icon={@icon}
data-fullscreen-id={@id}
variant="secondary"
{@rest}
/>
</.tooltip>
"""
end
@doc """
Circle spinner component used to indicate loading state.
"""
attr(:class, :any, default: nil, doc: "CSS class")
attr(:size, :string,
default: "md",
values: ["xs", "sm", "md", "lg", "xl"],
doc: "Size of the spinner"
)
attr(:show, :boolean, default: true, doc: "show or hide spinner")
attr(:rest, :global)
def spinner(assigns) do
size_class =
case assigns.size do
"xs" -> "h-4 w-4"
"sm" -> "h-6 w-6"
"md" -> "h-8 w-8"
"lg" -> "h-10 w-10"
"xl" -> "h-12 w-12"
end
assigns = assign(assigns, :size_class, size_class)
~H"""
<svg
{@rest}
class={
["animate-spin", @size_class, if(!@show, do: "hidden")] ++
List.wrap(@class)
}
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle class="opacity-10" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" />
<path
class="none"
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"
/>
</svg>
"""
end
@doc """
Renders a badge with text and icon.
Used to add small labels in UI (e.g. `Embedded`).
"""
attr(:text, :string, required: true)
attr(:icon, :string, required: true)
def badge(assigns) do
~H"""
<div class="py-1 px-1.5 w-max flex gap-0.5 bg-surface-0-bg border border-default-border text-3xs font-semibold rounded-xl items-center">
<.icon class="w-3 h-3 text-accent-icon" name={@icon} />
<p class="text-accent-text"><%= @text %></p>
</div>
"""
end
@doc """
Renders a status dot with a tooltip.
"""
attr(:id, :string, default: "status-dot")
attr(:status, :atom, required: true)
attr(:pulse?, :boolean, default: false)
attr(:tooltip, :string, required: true)
def status_dot(assigns) do
assigns =
case assigns.status do
:success -> assign(assigns, :bg_class, "bg-status-dot-success-bg")
:warning -> assign(assigns, :bg_class, "bg-status-dot-warning-bg")
:error -> assign(assigns, :bg_class, "bg-status-dot-error-bg")
end
~H"""
<.tooltip id={@id <> "-tooltip"} content={@tooltip}>
<span class="relative flex size-2">
<span
:if={@pulse?}
class={"absolute inline-flex h-full w-full animate-ping rounded-full #{@bg_class} opacity-75"}
>
</span>
<span class={"relative inline-flex size-2 rounded-full #{@bg_class}"}></span>
</span>
</.tooltip>
"""
end
@doc """
Renders a tooltip using Tooltip hook.
"""
attr(:id, :string, required: true, doc: "ID of the tooltip.")
attr(:content, :string, default: nil)
attr(:position, :string,
default: "top",
values: ["top", "bottom", "left", "right", "top-center"]
)
attr(:rest, :global)
attr(:fullscreen?, :boolean,
default: false,
doc: "Whether the tooltip is in fullscreen mode"
)
slot(:inner_block, required: true)
def tooltip(assigns) do
assigns =
assign(assigns, :enabled?, assigns.content not in [nil, ""] and not assigns.fullscreen?)
~H"""
<div
id={@id}
phx-hook={if @enabled?, do: "Tooltip"}
data-tooltip={if @enabled?, do: @content}
data-position={if @enabled?, do: @position}
aria-describedby={if @enabled?, do: "tooltip"}
{@rest}
>
<%= render_slot(@inner_block) %>
</div>
"""
end
@doc """
Link to report an issue on GitHub.
"""
attr(:class, :any, default: nil)
attr(:text, :string, default: "See any issues?")
def report_issue(assigns) do
assigns = assign(assigns, :report_issue_url, @report_issue_url)