Skip to content

Commit 873c04b

Browse files
committed
slide_over ids are configurable
1 parent 753da01 commit 873c04b

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

lib/petal_components/slide_over.ex

+24-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ defmodule PetalComponents.SlideOver do
22
use Phoenix.Component
33
alias Phoenix.LiveView.JS
44

5+
attr :id, :string, default: "slide-over"
6+
57
attr(:origin, :string,
68
default: "right",
79
values: ["left", "right", "top", "bottom"],
@@ -41,12 +43,12 @@ defmodule PetalComponents.SlideOver do
4143
~H"""
4244
<div
4345
{@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)}
4648
class="hidden pc-slide-over"
47-
id="slide-over"
49+
id={@id}
4850
>
49-
<div id="slide-over-overlay" class="hidden pc-slideover__overlay" aria-hidden="true"></div>
51+
<div id={"#{@id}-overlay"} class="hidden pc-slideover__overlay" aria-hidden="true"></div>
5052
5153
<div
5254
class={["pc-slideover__wrapper", get_margin_classes(@origin), @class]}
@@ -55,22 +57,26 @@ defmodule PetalComponents.SlideOver do
5557
aria-modal="true"
5658
>
5759
<div
58-
id="slide-over-content"
60+
id={"#{@id}-content"}
5961
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+
}
6268
phx-key="escape"
6369
>
6470
<!-- Header -->
6571
<div class="pc-slideover__header">
6672
<div class="pc-slideover__header__container">
67-
<div class="pc-slideover__header__text">
73+
<div :if={@title} class="pc-slideover__header__text">
6874
{@title}
6975
</div>
7076
7177
<button
7278
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)}
7480
class="pc-slideover__header__button"
7581
>
7682
<div class="sr-only">Close</div>
@@ -90,43 +96,43 @@ defmodule PetalComponents.SlideOver do
9096
"""
9197
end
9298

93-
def show_slide_over(origin) do
99+
def show_slide_over(origin, id \\ "slide-over") do
94100
{start_class, end_class} = get_transition_classes(origin)
95101

96102
%JS{}
97-
|> JS.show(to: "#slide-over")
103+
|> JS.show(to: "##{id}")
98104
|> JS.show(
99-
to: "#slide-over-overlay",
105+
to: "##{id}-overlay",
100106
time: 300,
101107
transition: {"transition-all transform ease-out duration-300", "opacity-0", "opacity-100"}
102108
)
103109
|> JS.show(
104-
to: "#slide-over-content",
110+
to: "##{id}-content",
105111
time: 300,
106112
transition: {"transition-all transform ease-out duration-300", start_class, end_class}
107113
)
108114
|> JS.add_class("overflow-hidden", to: "body")
109-
|> JS.focus_first(to: "#slide-over-content")
115+
|> JS.focus_first(to: "##{id}-content")
110116
end
111117

112118
# The live view that calls <.slide_over> will need to handle the "close_slide_over" event. eg:
113119
# def handle_event("close_slide_over", _, socket) do
114120
# {:noreply, push_patch(socket, to: Routes.moderate_users_path(socket, :index))}
115121
# end
116-
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
117123
{end_class, start_class} = get_transition_classes(origin)
118124

119125
js =
120126
JS.remove_class("overflow-hidden", to: "body")
121127
|> JS.hide(
122128
transition: {"ease-in duration-200", "opacity-100", "opacity-0"},
123-
to: "#slide-over-overlay"
129+
to: "##{id}-overlay"
124130
)
125131
|> JS.hide(
126132
transition: {"ease-in duration-200", start_class, end_class},
127-
to: "#slide-over-content"
133+
to: "##{id}-content"
128134
)
129-
|> JS.hide(to: "#slide-over", transition: {"duration-200", "", ""})
135+
|> JS.hide(to: "##{id}", transition: {"duration-200", "", ""})
130136

131137
if close_slide_over_target do
132138
JS.push(js, "close_slide_over", target: close_slide_over_target)

0 commit comments

Comments
 (0)