Skip to content

Commit f707d5d

Browse files
authored
Added base for a bloom_button component with a variant attribute (#2)
2 parents 824eace + 7e18b90 commit f707d5d

File tree

5 files changed

+173
-1
lines changed

5 files changed

+173
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
defmodule BloomSiteWeb.Components.Button do
2+
use Phoenix.Component
3+
4+
@doc """
5+
An extension of the *core_components button*.
6+
The `<.button>` has the ability to handle a variant attribute.
7+
This makes it possible to have a `contained` or `outlined` look.
8+
9+
⚠️ Uninstall the core button component!
10+
As you do not need two button components, the button component of the core phoenix_package can be deleted.
11+
"""
12+
13+
attr(:type, :string, default: nil)
14+
attr(:class, :string, default: nil)
15+
attr(:rest, :global, include: ~w(disabled form name value))
16+
attr(:variant, :string, default: "contained")
17+
18+
slot(:inner_block, required: true)
19+
20+
def button(assigns) do
21+
~H"""
22+
<button
23+
type={@type}
24+
class={[
25+
"rounded-lg px-3 py-2 box-sizing:border-box phx-submit-loading:opacity-75",
26+
"text-sm font-semibold leading-6 text-white active:text-white/80",
27+
get_variant(assigns.variant),
28+
@class
29+
]}
30+
{@rest}
31+
>
32+
<%= render_slot(@inner_block) %>
33+
</button>
34+
"""
35+
end
36+
37+
defp get_variant(variant) do
38+
case variant do
39+
"contained" ->
40+
"border-2 border-zinc-900 hover:border-zinc-700 bg-zinc-900 hover:bg-zinc-700"
41+
42+
"outlined" ->
43+
"border-2 border-zinc-900 bg-transparent text-zinc-900 hover:bg-zinc-700 hover:border-zinc-700 hover:text-white active:bg-zinc-700"
44+
45+
_ ->
46+
"bg-zinc-900 hover:bg-zinc-700"
47+
end
48+
end
49+
end

bloom_site/mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ defmodule BloomSite.MixProject do
5454
{:gettext, "~> 0.20"},
5555
{:jason, "~> 1.2"},
5656
{:plug_cowboy, "~> 2.5"},
57-
{:bloom, "~> 0.0.7"},
57+
{:bloom, path: "../"},
5858
# {:bloom, path: "..", override: true},
5959
{:phoenix_storybook, "~> 0.6.0"}
6060
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule BloomSite.Storybook.BloomComponents.Button do
2+
use PhoenixStorybook.Story, :component
3+
4+
def function, do: &BloomSiteWeb.Components.Button.button/1
5+
6+
def variations do
7+
[
8+
%Variation{
9+
id: :default,
10+
slots: [
11+
"Button"
12+
]
13+
},
14+
%Variation{
15+
id: :outlined,
16+
attributes: %{
17+
variant: "outlined"
18+
},
19+
slots: [
20+
"Outlined button"
21+
]
22+
},
23+
]
24+
end
25+
end

lib/bloom/components/button.ex

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
defmodule Bloom.Components.Button do
2+
use Phoenix.Component
3+
4+
@doc """
5+
An extension of the *core_components button*.
6+
The `<.button>` has the ability to handle a variant attribute.
7+
This makes it possible to have a `contained` or `outlined` look.
8+
9+
⚠️ Uninstall the core button component!
10+
As you do not need two button components, the button component of the core phoenix_package can be deleted.
11+
"""
12+
13+
attr(:type, :string, default: nil)
14+
attr(:class, :string, default: nil)
15+
attr(:rest, :global, include: ~w(disabled form name value))
16+
attr(:variant, :string, default: "contained")
17+
18+
slot(:inner_block, required: true)
19+
20+
def button(assigns) do
21+
~H"""
22+
<button
23+
type={@type}
24+
class={[
25+
"rounded-lg px-3 py-2 box-sizing:border-box phx-submit-loading:opacity-75",
26+
"text-sm font-semibold leading-6 text-white active:text-white/80",
27+
get_variant(assigns.variant),
28+
@class
29+
]}
30+
{@rest}
31+
>
32+
<%= render_slot(@inner_block) %>
33+
</button>
34+
"""
35+
end
36+
37+
defp get_variant(variant) do
38+
case variant do
39+
"contained" ->
40+
"border-2 border-zinc-900 hover:border-zinc-700 bg-zinc-900 hover:bg-zinc-700"
41+
42+
"outlined" ->
43+
"border-2 border-zinc-900 bg-transparent text-zinc-900 hover:bg-zinc-700 hover:border-zinc-700 hover:text-white active:bg-zinc-700"
44+
45+
_ ->
46+
"bg-zinc-900 hover:bg-zinc-700"
47+
end
48+
end
49+
end

priv/templates/button.ex

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
defmodule <%= @module_name %>Web.Components.Button do
2+
use Phoenix.Component
3+
4+
@doc """
5+
An extension of the *core_components button*.
6+
The `<.button>` has the ability to handle a variant attribute.
7+
This makes it possible to have a `contained` or `outlined` look.
8+
9+
⚠️ Uninstall the core button component!
10+
As you do not need two button components, the button component of the core phoenix_package can be deleted.
11+
"""
12+
13+
attr(:type, :string, default: nil)
14+
attr(:class, :string, default: nil)
15+
attr(:rest, :global, include: ~w(disabled form name value))
16+
attr(:variant, :string, default: "contained")
17+
18+
slot(:inner_block, required: true)
19+
20+
def button(assigns) do
21+
~H"""
22+
<button
23+
type={@type}
24+
class={[
25+
"rounded-lg px-3 py-2 box-sizing:border-box phx-submit-loading:opacity-75",
26+
"text-sm font-semibold leading-6 text-white active:text-white/80",
27+
get_variant(assigns.variant),
28+
@class
29+
]}
30+
{@rest}
31+
>
32+
<%%= render_slot(@inner_block) %>
33+
</button>
34+
"""
35+
end
36+
37+
defp get_variant(variant) do
38+
case variant do
39+
"contained" ->
40+
"border-2 border-zinc-900 hover:border-zinc-700 bg-zinc-900 hover:bg-zinc-700"
41+
42+
"outlined" ->
43+
"border-2 border-zinc-900 bg-transparent text-zinc-900 hover:bg-zinc-700 hover:border-zinc-700 hover:text-white active:bg-zinc-700"
44+
45+
_ ->
46+
"bg-zinc-900 hover:bg-zinc-700"
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)