defmodule BitstylesPhoenix.Alpine3.Dropdown do use BitstylesPhoenix.Component alias BitstylesPhoenix.Component.Dropdown, as: RawDropdown @moduledoc """ Components for rendering a dropdown menu powered by Alpine 3. """ @doc """ Renders a dropdown component with a button, a menu and options with Alpine3. Supports all attributes and slots from `BitstylesPhoenix.Component.Dropdown.ui_dropdown`. ## Attributes - `x_name` - The name of the boolean x-data property for alpine to store the menu state. Defaults to `dropdownOpen`. ## Prevent page-flickering The dropdown sets the `x-cloak` property on the menu to avoid flickering on initial page load. To make this work, you need to add the following snippet to your stylesheets: [x-cloak] { display: none !important; } See https://alpinejs.dev/directives/cloak for more information on `x-cloak`. """ story( "Dropdown with alpine", ''' iex> assigns = %{} ...> render ~H""" ...> <.ui_js_dropdown> ...> <:button label="Select me"/> ...> <:menu> ...> <.ui_dropdown_option href="#" class="u-h6"> ...> Option 1 ...> ...> <.ui_dropdown_option href="#" class="u-h6"> ...> Option 2 ...> ...> ...> ...> """ """
""" ''', extra_html: """ """, height: "200px" ) story( "Custom x-data name and icon file", ''' iex> assigns = %{} ...> render ~H""" ...> <.ui_js_dropdown x_name="myOwnDropDown"> ...> <:button label="Select me" icon_file="assets/icons.svg"/> ...> <:menu> ...> <.ui_dropdown_option href="#" class="u-h6"> ...> Option 1 ...> ...> <.ui_dropdown_option href="#" class="u-h6"> ...> Option 2 ...> ...> ...> ...> """ """ """ ''', extra_html: """ """, height: "200px" ) def ui_js_dropdown(assigns) do {_button, button_extra} = assigns_from_single_slot(assigns, :button) {_menu, menu_extra} = assigns_from_single_slot(assigns, :menu) extra = assigns_to_attributes(assigns, [:menu, :button, :x_name]) assigns = assigns |> assign(extra: extra, button_extra: button_extra, menu_extra: menu_extra) |> assign_new(:x_name, fn -> "dropdownOpen" end) ~H"""