From 6f965d6381dadbefe5d3d7e64f7c681be3972173 Mon Sep 17 00:00:00 2001 From: Matthew Erhard Date: Fri, 6 Dec 2024 18:12:27 -0500 Subject: [PATCH] Override classes on slots using slot attributes (#19) --- .../components/station_ui/html/accordion.ex | 75 ++++++++++++------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/sources/lib/components/station_ui/html/accordion.ex b/sources/lib/components/station_ui/html/accordion.ex index 1bfb83b..2cf9ece 100644 --- a/sources/lib/components/station_ui/html/accordion.ex +++ b/sources/lib/components/station_ui/html/accordion.ex @@ -4,7 +4,13 @@ defmodule StationUI.HTML.Accordion do import StationUI.HTML.Icon, only: [icon: 1] alias Phoenix.LiveView.JS - @moduledoc """ + @header_default_classes "text-base sm:text-lg md:text-xl" + defp header_default_classes, do: @header_default_classes + + @content_default_classes "text-base md:text-lg" + defp content_default_classes, do: @content_default_classes + + @doc """ The accordion component renders a list of items with child content that can be expanded or collapsed. ## Example @@ -18,70 +24,74 @@ defmodule StationUI.HTML.Accordion do - Suggested size classes - - The Default size for accordions is "md" but the size can be change by passing in these additional classes - using `header_size_class="..."` and `content_size_class="..."` as follows - - header_size_class: + Suggested size classes for `header` (default: md): sm: "p-1 text-base sm:text-lg gap-x-0.5" md: "p-1 text-base sm:text-lg md:text-xl md:py-1 md:pr-1 md:pl-1.5 md:gap-x-1" lg: "p-1 text-base sm:text-lg md:text-xl lg:text-2xl md:py-1 md:pr-1 md:pl-1.5 lg:pl-2 md:gap-x-1 lg:gap-x-1.5" xl: "p-1 text-base sm:text-lg md:text-xl lg:text-2xl xl:text-3xl md:pt-1 md:pb-0 md:pr-1 md:pl-1.5 lg:pl-4 sm:gap-x-3 md:gap-x-4 lg:gap-x-5" - content_size_class: + Suggested size classes for `content` (default: md): sm: "text-base" md: "grid transition-grid-rows text-base md:text-lg" lg: "md:text-lg lg:text-xl" xl: "md:text-lg lg:text-xl xl:text-2xl" """ + + attr :id, :string + attr :rest, :global + slot :header, required: true do attr :button_id, :string + attr :class, :string, doc: @header_default_classes end - slot :content, required: true - attr :header_size_class, :string, default: "text-base sm:text-lg md:text-xl" - attr :content_size_class, :string, default: "text-base md:text-lg" - attr :rest, :global + slot :content, required: true do + attr :class, :string, doc: @content_default_classes + end def accordion(assigns) do assigns = assigns - |> assign(:header, List.wrap(assigns.header)) - |> assign(:content, List.wrap(assigns.content)) - |> assign(:random_id, :rand.uniform(9999)) - |> assign(:items, Enum.with_index(Enum.zip(List.wrap(assigns.header), List.wrap(assigns.content)))) + |> assign_new(:id, fn -> :rand.uniform(9999) end) + |> assign(:items, assigns.header |> Enum.zip(assigns.content) |> Enum.with_index()) ~H""" -
+
<% # Accordion Trigger %>