# https://www.figma.com/file/S3q1SkVngbwHuwpxHKCsgtJj/Moon---Components?node-id=23452%3A7
defmodule Moon.Components.Accordion do
@moduledoc false
use Moon.StatelessComponent
alias Phoenix.LiveView.JS
alias Moon.Autolayouts.LeftToRight
alias Moon.Autolayouts.PullAside
alias Moon.Icons.ControlsChevronUp
prop(id, :string)
prop(class, :css_class)
prop(open_by_default, :boolean, default: false)
prop(is_content_inside, :boolean, default: true)
prop(is_content_overflow_hidden, :boolean, default: true)
prop(with_button, :boolean, default: true)
prop(disabled, :boolean, default: false)
# we do add 2-letter sizes for consistence with Ract API, long sizes for backward compability & nice
prop(size, :string,
values!: ["small", "medium", "large", "xlarge", "sm", "md", "lg", "xl"],
default: "md"
)
prop(pull_a_side_class, :css_class)
prop(bg_color, :css_class, default: "bg-goku")
slot(title, required: true)
slot(header_content)
slot(content, required: true)
def render(assigns) do
~F"""
<:left>
<#slot {@title} />
<:right>
<#slot {@header_content} />
"-arrow"}
class={
"transition-transform transition-200",
"rotate-90": !@open_by_default,
"rotate-180": @open_by_default
}
>
{#if @is_content_inside}
"-content"}
class={
"w-full h-full",
get_margin(@size),
"overflow-hidden": @is_content_overflow_hidden,
hidden: !@open_by_default
}
>
<#slot {@content} />
{/if}
{#if !@is_content_inside}
"-content"}
class={
"w-full h-full",
get_margin(@size),
"overflow-hidden": @is_content_overflow_hidden,
hidden: !@open_by_default
}
>
<#slot {@content} />
{/if}
"""
end
def toggle_content(id, disabled) do
if !disabled do
JS.toggle(to: "#" <> id <> "-content")
|> JS.dispatch(
"moon:update-accordion-aria",
detail: %{
accordion_id: id
},
to: "#" <> id
)
|> JS.remove_class(
"rotate-90",
to: "#" <> id <> "-arrow.rotate-90"
)
|> JS.add_class(
"rotate-90",
to: "#" <> id <> "-arrow:not(.rotate-90)"
)
|> JS.remove_class(
"rotate-180",
to: "#" <> id <> "-arrow.rotate-180"
)
|> JS.add_class(
"rotate-180",
to: "#" <> id <> "-arrow:not(.rotate-180)"
)
end
end
defp get_padding(size) do
case size do
"xlarge" -> "p-4"
"xl" -> "p-4"
"large" -> "p-3"
"lg" -> "p-3"
"medium" -> "py-2 pr-2 pl-3"
"md" -> "py-2 pr-2 pl-3"
_ -> "py-1 pr-1 pl-3"
end
end
def get_margin(size) do
case size do
"xlarge" -> "mt-4"
"xl" -> "mt-4"
"large" -> "mt-3"
"lg" -> "mt-3"
_ -> "mt-2"
end
end
defp font_class(size) do
case size do
"xlarge" -> "text-moon-16"
"xl" -> "text-moon-16"
_ -> "text-moon-14"
end
end
end