defmodule AshHqWeb.Components.DocSidebar do @moduledoc """ Renders the sidebar data, and uses `Phoenix.LiveView.JS` to manage selection/collapsible state. """ use Phoenix.LiveComponent alias AshHqWeb.Components.Icon alias Phoenix.LiveView.JS import AshHqWeb.Tails attr :libraries, :any attr :remove_version, :any attr :class, :any attr :sidebar_data, :any, default: [] def render(assigns) do ~H""" """ end defp id(category, library, name, id, item_id, global_id) do if category == "Tutorials" && library == "Ash" && name == "Get Started" do if String.starts_with?(global_id, "mobile") do "mobile-get-started-guide" else "get-started-guide" end else "#{global_id}-#{id}-#{slug(category)}-#{slug(library)}-#{item_id}" end end defp has_active?(list) when is_list(list), do: Enum.any?(list, &has_active?/1) defp has_active?({_, list}), do: has_active?(list) defp has_active?(%{active?: true}), do: true defp has_active?(_), do: false def mark_active(js \\ %JS{}, id) do js |> JS.remove_class( "bg-base-light-200 dark:bg-base-dark-750 active-sidebar-nav", to: ".active-sidebar-nav" ) |> JS.add_class( "bg-base-light-200 dark:bg-base-dark-750 active-sidebar-nav", to: "##{id}" ) |> JS.add_class( "bg-base-light-200 dark:bg-base-dark-750 active-sidebar-nav", to: "##{add_or_remove_mobile(id)}" ) end def collapse(js \\ %JS{}, id) do js |> JS.remove_class( "rotate-[-90deg]", to: "##{id}-chevron.rotate-\\[-90deg\\]" ) |> JS.remove_class( "rotate-[-90deg]", to: "##{add_or_remove_mobile(id)}-chevron.rotate-\\[-90deg\\]" ) |> JS.add_class( "rotate-[-90deg]", to: "##{id}-chevron:not(.rotate-\\[-90deg\\])" ) |> JS.add_class( "rotate-[-90deg]", to: "##{add_or_remove_mobile(id)}-chevron:not(.rotate-\\[-90deg\\])" ) |> JS.toggle(to: "##{id}-contents") |> JS.toggle(to: "##{add_or_remove_mobile(id)}-contents") end defp add_or_remove_mobile("mobile-" <> rest), do: rest defp add_or_remove_mobile(rest), do: "mobile-#{rest}" defp slug(string) do string |> String.downcase() |> String.replace(" ", "_") |> String.replace(~r/[^a-z0-9-_]/, "-") end end