@@ -2,6 +2,8 @@ defmodule PetalComponents.SlideOver do
2
2
use Phoenix.Component
3
3
alias Phoenix.LiveView.JS
4
4
5
+ attr :id , :string , default: "slide-over"
6
+
5
7
attr ( :origin , :string ,
6
8
default: "right" ,
7
9
values: [ "left" , "right" , "top" , "bottom" ] ,
@@ -41,12 +43,12 @@ defmodule PetalComponents.SlideOver do
41
43
~H"""
42
44
< div
43
45
{ @ rest }
44
- phx-mounted = { ! @ hide && show_slide_over ( @ origin ) }
45
- phx-remove = { hide_slide_over ( @ origin , @ close_slide_over_target ) }
46
+ phx-mounted = { ! @ hide && show_slide_over ( @ origin , @ id ) }
47
+ phx-remove = { hide_slide_over ( @ origin , @ id , @ close_slide_over_target ) }
46
48
class = "hidden pc-slide-over "
47
- id = " slide-over "
49
+ id = { @ id }
48
50
>
49
- < div id = " slide-over- overlay " class = "pc-slideover__overlay " aria-hidden = "true " > </ div >
51
+ < div id = { " #{ @ id } - overlay"} class = "hidden pc-slideover__overlay " aria-hidden = "true " > </ div >
50
52
51
53
< div
52
54
class = { [ "pc-slideover__wrapper" , get_margin_classes ( @ origin ) , @ class ] }
@@ -55,22 +57,26 @@ defmodule PetalComponents.SlideOver do
55
57
aria-modal = "true "
56
58
>
57
59
< div
58
- id = " slide-over- content "
60
+ id = { " #{ @ id } - content"}
59
61
class = { get_classes ( @ max_width , @ origin , @ class ) }
60
- phx-click-away = { @ close_on_click_away && hide_slide_over ( @ origin , @ close_slide_over_target ) }
61
- phx-window-keydown = { @ close_on_escape && hide_slide_over ( @ origin , @ close_slide_over_target ) }
62
+ phx-click-away = {
63
+ @ close_on_click_away && hide_slide_over ( @ origin , @ id , @ close_slide_over_target )
64
+ }
65
+ phx-window-keydown = {
66
+ @ close_on_escape && hide_slide_over ( @ origin , @ id , @ close_slide_over_target )
67
+ }
62
68
phx-key = "escape "
63
69
>
64
70
<!-- Header -->
65
71
< div class = "pc-slideover__header " >
66
72
< div class = "pc-slideover__header__container " >
67
- < div class = "pc-slideover__header__text " >
73
+ < div :if = { @ title } class = "pc-slideover__header__text " >
68
74
{ @ title }
69
75
</ div >
70
76
71
77
< button
72
78
type = "button "
73
- phx-click = { hide_slide_over ( @ origin , @ close_slide_over_target ) }
79
+ phx-click = { hide_slide_over ( @ origin , @ id , @ close_slide_over_target ) }
74
80
class = "pc-slideover__header__button "
75
81
>
76
82
< div class = "sr-only " > Close</ div >
@@ -90,53 +96,43 @@ defmodule PetalComponents.SlideOver do
90
96
"""
91
97
end
92
98
93
- def show_slide_over ( origin ) do
99
+ def show_slide_over ( origin , id \\ "slide-over" ) do
94
100
{ start_class , end_class } = get_transition_classes ( origin )
95
101
96
102
% JS { }
97
- |> JS . show ( to: "#slide-over " )
103
+ |> JS . show ( to: "##{ id } " )
98
104
|> JS . show (
99
- to: "#slide-over-overlay" ,
105
+ to: "##{ id } -overlay" ,
106
+ time: 300 ,
100
107
transition: { "transition-all transform ease-out duration-300" , "opacity-0" , "opacity-100" }
101
108
)
102
109
|> JS . show (
103
- to: "#slide-over-content" ,
104
- transition: {
105
- "transition-all transform ease-out duration-300" ,
106
- start_class ,
107
- end_class
108
- }
110
+ to: "##{ id } -content" ,
111
+ time: 300 ,
112
+ transition: { "transition-all transform ease-out duration-300" , start_class , end_class }
109
113
)
110
114
|> JS . add_class ( "overflow-hidden" , to: "body" )
111
- |> JS . focus_first ( to: "#slide-over -content" )
115
+ |> JS . focus_first ( to: "##{ id } -content" )
112
116
end
113
117
114
118
# The live view that calls <.slide_over> will need to handle the "close_slide_over" event. eg:
115
119
# def handle_event("close_slide_over", _, socket) do
116
120
# {:noreply, push_patch(socket, to: Routes.moderate_users_path(socket, :index))}
117
121
# end
118
- def hide_slide_over ( origin , close_slide_over_target \\ nil ) do
122
+ def hide_slide_over ( origin , id \\ "slide-over" , close_slide_over_target \\ nil ) do
119
123
{ end_class , start_class } = get_transition_classes ( origin )
120
124
121
125
js =
122
126
JS . remove_class ( "overflow-hidden" , to: "body" )
123
127
|> JS . hide (
124
- transition: {
125
- "ease-in duration-200" ,
126
- "opacity-100" ,
127
- "opacity-0"
128
- } ,
129
- to: "#slide-over-overlay"
128
+ transition: { "ease-in duration-200" , "opacity-100" , "opacity-0" } ,
129
+ to: "##{ id } -overlay"
130
130
)
131
131
|> JS . hide (
132
- transition: {
133
- "ease-in duration-200" ,
134
- start_class ,
135
- end_class
136
- } ,
137
- to: "#slide-over-content"
132
+ transition: { "ease-in duration-200" , start_class , end_class } ,
133
+ to: "##{ id } -content"
138
134
)
139
- |> JS . hide ( to: "#slide-over" )
135
+ |> JS . hide ( to: "##{ id } " , transition: { "duration-200" , "" , "" } )
140
136
141
137
if close_slide_over_target do
142
138
JS . push ( js , "close_slide_over" , target: close_slide_over_target )
@@ -155,7 +151,7 @@ defmodule PetalComponents.SlideOver do
155
151
end
156
152
157
153
defp get_classes ( max_width , origin , class ) do
158
- base_classes = "pc-slideover__box"
154
+ base_classes = "hidden pc-slideover__box"
159
155
160
156
slide_over_classes =
161
157
case origin do
0 commit comments