Skip to content

Commit c56f97f

Browse files
committed
Put slide in it's own function
1 parent 66d4a9c commit c56f97f

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

lib/petal_components/carousel.ex

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ defmodule PetalComponents.Carousel do
1717
use Phoenix.Component
1818
import PetalComponents.Icon, only: [icon: 1]
1919
import Phoenix.LiveView.Utils, only: [random_id: 0]
20+
alias Phoenix.HTML.Link
21+
import PetalComponents.Link
2022

2123
@doc """
2224
The `carousel` component is used to create interactive carousels with customizable attributes
@@ -56,6 +58,9 @@ defmodule PetalComponents.Carousel do
5658
attr :title, :string, doc: "Title of the slide"
5759
attr :description, :string, doc: "Description of the slide"
5860
attr :class, :string, doc: "Custom CSS class for additional styling"
61+
attr :image, :string, doc: "URL of the image to display in the slide"
62+
attr :navigate, :string, doc: "Internal route to navigate to when the slide is clicked"
63+
attr :href, :string, doc: "External URL to navigate to when the slide is clicked"
5964
end
6065

6166
def carousel(assigns) do
@@ -125,18 +130,15 @@ defmodule PetalComponents.Carousel do
125130
end
126131
}
127132
>
128-
<div class="pc-carousel__slide-content">
129-
<div class="pc-carousel__content">
130-
<div class="pc-carousel__content-wrapper">
131-
<div class="pc-carousel__title">
132-
{slide[:title] || "Slide #{index + 1}"}
133-
</div>
134-
<p :if={!is_nil(slide[:description])} class="pc-carousel__description">
135-
{slide[:description]}
136-
</p>
137-
</div>
138-
</div>
139-
</div>
133+
<.slide_content
134+
slide={slide}
135+
index={index}
136+
navigate={slide[:navigate]}
137+
href={slide[:href]}
138+
image={slide[:image]}
139+
title={slide[:title] || "Slide #{index + 1}"}
140+
description={slide[:description]}
141+
/>
140142
</div>
141143
</div>
142144
@@ -183,4 +185,31 @@ defmodule PetalComponents.Carousel do
183185
defp text_position_class("center"), do: "[&_.description-wrapper]:text-center"
184186
defp text_position_class("end"), do: "[&_.description-wrapper]:text-end"
185187
defp text_position_class(_), do: ""
188+
189+
defp slide_content(assigns) do
190+
~H"""
191+
<div class="pc-carousel__slide-content">
192+
<div :if={!is_nil(@image)} class="pc-carousel__image-wrapper">
193+
<img src={@image} class="pc-carousel__image" />
194+
</div>
195+
<div class="pc-carousel__content">
196+
<div class="pc-carousel__content-wrapper">
197+
<div class="pc-carousel__title">
198+
{@title}
199+
</div>
200+
<p :if={!is_nil(@description)} class="pc-carousel__description">
201+
{@description}
202+
</p>
203+
</div>
204+
</div>
205+
</div>
206+
207+
<.link :if={@navigate} to={@navigate} class="pc-carousel__link absolute inset-0 z-20">
208+
<span class="sr-only">View slide details</span>
209+
</.link>
210+
<a :if={@href} href={@href} class="pc-carousel__link absolute inset-0 z-20">
211+
<span class="sr-only">View external link</span>
212+
</a>
213+
"""
214+
end
186215
end

0 commit comments

Comments
 (0)