Skip to content

Commit 9033baf

Browse files
committed
feat: support custom DropDown button inner content
feat: support navbar wrapper, button, menu item, and dropdown item class customization fix button size property name fix: remove extra li
1 parent ca7a177 commit 9033baf

3 files changed

Lines changed: 45 additions & 19 deletions

File tree

lib/surface_bootstrap/dropdown.ex

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ defmodule SurfaceBootstrap.DropDown do
2626
@doc "ID of dropdown, required to work"
2727
prop id, :string, required: true
2828

29-
@doc "Label of dropdown link/button"
30-
prop label, :string, required: true
29+
@doc "Label of dropdown link/button. Use button_content to provide inner content."
30+
prop label, :string
3131

3232
@doc """
3333
If prop `split` is set to `true`, wrapper type is automatically set to `btn_group`.
@@ -51,6 +51,15 @@ defmodule SurfaceBootstrap.DropDown do
5151

5252
prop button_size, :string, values: ~w(small normal large), default: "normal"
5353

54+
@doc "Custom class for the dropdown button"
55+
prop button_class, :css_class, default: []
56+
57+
@doc "Custom class for the wrapper"
58+
prop wrapper_class, :css_class, default: []
59+
60+
@doc "Custom class for the dropdown menu"
61+
prop menu_class, :css_class, default: []
62+
5463
@doc """
5564
Show dropdown with a separate arrow to click on (split view)? Defaults false.
5665
If set to true will automatically set `@wrapper = "btn_group"` and `@button = true`.
@@ -63,6 +72,8 @@ defmodule SurfaceBootstrap.DropDown do
6372
@doc "Show dropdown as active"
6473
prop active, :boolean
6574

75+
slot button_content
76+
6677
slot dropdown_items
6778

6879
slot default
@@ -86,11 +97,11 @@ defmodule SurfaceBootstrap.DropDown do
8697
id={@id}
8798
:hook="DropDown"
8899
class={
89-
dropdown: @wrapper == "dropdown" && !@direction,
100+
[dropdown: @wrapper == "dropdown" && !@direction,
90101
dropup: @direction == "up",
91102
dropend: @direction == "right",
92103
dropstart: @direction == "left",
93-
"btn-group": @wrapper == "btn_group" || @split == true
104+
"btn-group": @wrapper == "btn_group" || @split == true] ++ @wrapper_class
94105
}
95106
:attrs={
96107
"data-bsnclass": "show"
@@ -107,12 +118,12 @@ defmodule SurfaceBootstrap.DropDown do
107118
id={@id}
108119
:hook="DropDown"
109120
class={
110-
"nav-item",
121+
["nav-item",
111122
dropdown: !@direction,
112123
dropup: @direction == "up",
113124
dropend: @direction == "right",
114125
dropstart: @direction == "left",
115-
"btn-group": @wrapper == "btn_group" || @split == true
126+
"btn-group": @wrapper == "btn_group" || @split == true] ++ @wrapper_class
116127
}
117128
:attrs={
118129
"data-bsnclass": "show"
@@ -151,17 +162,19 @@ defmodule SurfaceBootstrap.DropDown do
151162
<a
152163
id={@id <> "dropdown"}
153164
class={
154-
"dropdown-toggle": !@split,
165+
["dropdown-toggle": !@split,
155166
"nav-link": @wrapper == "nav_item",
156167
active: @active,
157168
btn: @button,
158169
"btn-#{@color}": @button && @color,
159170
"btn-lg": @button && @button_size == "large",
160-
"btn-sm": @button && @button_size == "small"
171+
"btn-sm": @button && @button_size == "small"] ++ @button_class
161172
}
162173
href={!@split && "#"}
163174
>
164-
{@label}
175+
<#slot name="button_content">
176+
{@label}
177+
</#slot>
165178
</a>
166179
"""
167180
end
@@ -193,8 +206,8 @@ defmodule SurfaceBootstrap.DropDown do
193206
~F"""
194207
<div
195208
class={
196-
"dropdown-menu",
197-
"dropdown-menu-dark": @dark
209+
["dropdown-menu",
210+
"dropdown-menu-dark": @dark] ++ @menu_class
198211
}
199212
:attrs={
200213
"data-bsnstyle": true,
@@ -210,18 +223,16 @@ defmodule SurfaceBootstrap.DropDown do
210223
~F"""
211224
<ul
212225
class={
213-
"dropdown-menu",
214-
"dropdown-menu-dark": @dark
226+
["dropdown-menu",
227+
"dropdown-menu-dark": @dark] ++ @menu_class
215228
}
216229
:attrs={
217230
"data-bsnstyle": true,
218231
"aria-labelledby": @id
219232
}
220233
>
221234
{#for {_item, index} <- Enum.with_index(@dropdown_items)}
222-
<li>
223-
<#slot name="dropdown_items" index={index} />
224-
</li>
235+
<#slot name="dropdown_items" index={index} />
225236
{/for}
226237
</ul>
227238
"""

lib/surface_bootstrap/dropdown/item.ex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ defmodule SurfaceBootstrap.DropDown.Item do
1212
- `<LivePatch class="dropdown-item"">`
1313
- `<hr class="dropdown-divider">`
1414
"""
15-
1615
use Surface.Component, slot: "dropdown_items"
16+
17+
@doc "Classes to propagate to the li."
18+
prop class, :css_class, default: []
19+
20+
slot default
21+
22+
def render(assigns) do
23+
~F"""
24+
<li class={@class}>
25+
<#slot />
26+
</li>
27+
"""
28+
end
1729
end

priv/catalogue/navbar/example01.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ defmodule SurfaceBootstrap.Catalogue.NavBar.Example01 do
5151
</NavBar.ItemGroup>
5252
<NavBar.ItemGroup position="right">
5353
<NavBar.ListItem>
54-
<DropDown id="foo" wrapper="nav_item" label="John Doe">
55-
<DropDownItem>
54+
<DropDown id="foo" wrapper="nav_item" wrapper_class="d-inline" button_class="btn-outline-dark" button_size="small">
55+
<:button_content>
56+
<span class="text-primary">John Doe</span>
57+
</:button_content>
58+
<DropDownItem class="w-100">
5659
<Link to="#" class="dropdown-item">Edit profile</Link>
5760
</DropDownItem>
5861
<DropDownItem>

0 commit comments

Comments
 (0)