Skip to content

Commit d87d36b

Browse files
authored
add button_classes to CoreComponents (#6089)
* add new link_button core component Buttons and Links must not be nested inside each other, otherwise there are accessibility issues. A previous PR did not fix the problem, but just inverted the problem. Adds a link that is styled like a button. Fixes #5770. Relates to: #5820 * button_classes/0
1 parent 6f8e881 commit d87d36b

File tree

6 files changed

+40
-38
lines changed

6 files changed

+40
-38
lines changed

installer/templates/phx_web/components/core_components.ex

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,25 @@ defmodule <%= @web_namespace %>.CoreComponents do
163163

164164
def button(assigns) do
165165
~H"""
166-
<button
167-
type={@type}
168-
class={[
169-
"phx-submit-loading:opacity-75 rounded-lg bg-zinc-900 hover:bg-zinc-700 py-2 px-3",
170-
"text-sm font-semibold leading-6 text-white active:text-white/80",
171-
@class
172-
]}
173-
{@rest}
174-
>
166+
<button type={@type} class={[button_classes(), @class]} {@rest}>
175167
{render_slot(@inner_block)}
176168
</button>
177169
"""
178170
end
179171

172+
@doc """
173+
Returns the default button classes.
174+
175+
## Examples
176+
177+
<.link class={button_classes()} href={~p"/items/new"}>
178+
New Item
179+
</.link>
180+
"""
181+
def button_classes do
182+
"inline-block phx-submit-loading:opacity-75 rounded-lg bg-zinc-900 hover:bg-zinc-700 py-2 px-3 text-sm font-semibold leading-6 text-white active:text-white/80"
183+
end
184+
180185
@doc """
181186
Renders an input with label and error messages.
182187

priv/templates/phx.gen.html/index.html.heex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<.header>
22
Listing <%= schema.human_plural %>
33
<:actions>
4-
<.button phx-click={JS.dispatch("click", to: {:inner, "a"})}>
5-
<.link href={~p"<%= schema.route_prefix %>/new"}>
6-
New <%= schema.human_singular %>
7-
</.link>
8-
</.button>
4+
<.link class={button_classes()} href={~p"<%= schema.route_prefix %>/new"}>
5+
New <%= schema.human_singular %>
6+
</.link>
97
</:actions>
108
</.header>
119

priv/templates/phx.gen.html/show.html.heex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
<%= schema.human_singular %> {@<%= schema.singular %>.id}
33
<:subtitle>This is a <%= schema.singular %> record from your database.</:subtitle>
44
<:actions>
5-
<.button phx-click={JS.dispatch("click", to: {:inner, "a"})}>
6-
<.link href={~p"<%= schema.route_prefix %>/#{@<%= schema.singular %>}/edit"}>
7-
Edit <%= schema.singular %>
8-
</.link>
9-
</.button>
5+
<.link class={button_classes()} href={~p"<%= schema.route_prefix %>/#{@<%= schema.singular %>}/edit"}>
6+
Edit <%= schema.singular %>
7+
</.link>
108
</:actions>
119
</.header>
1210

priv/templates/phx.gen.live/core_components.ex

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,25 @@ defmodule <%= @web_namespace %>.CoreComponents do
163163

164164
def button(assigns) do
165165
~H"""
166-
<button
167-
type={@type}
168-
class={[
169-
"phx-submit-loading:opacity-75 rounded-lg bg-zinc-900 hover:bg-zinc-700 py-2 px-3",
170-
"text-sm font-semibold leading-6 text-white active:text-white/80",
171-
@class
172-
]}
173-
{@rest}
174-
>
166+
<button type={@type} class={[button_classes(), @class]} {@rest}>
175167
{render_slot(@inner_block)}
176168
</button>
177169
"""
178170
end
179171

172+
@doc """
173+
Returns the default button classes.
174+
175+
## Examples
176+
177+
<.link class={button_classes()} href={~p"/items/new"}>
178+
New Item
179+
</.link>
180+
"""
181+
def button_classes do
182+
"inline-block phx-submit-loading:opacity-75 rounded-lg bg-zinc-900 hover:bg-zinc-700 py-2 px-3 text-sm font-semibold leading-6 text-white active:text-white/80"
183+
end
184+
180185
@doc """
181186
Renders an input with label and error messages.
182187

priv/templates/phx.gen.live/index.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
99
<.header>
1010
Listing <%= schema.human_plural %>
1111
<:actions>
12-
<.button phx-click={JS.dispatch("click", to: {:inner, "a"})}>
13-
<.link navigate={~p"<%= schema.route_prefix %>/new"}>
14-
New <%= schema.human_singular %>
15-
</.link>
16-
</.button>
12+
<.link class={button_classes()} navigate={~p"<%= schema.route_prefix %>/new"}>
13+
New <%= schema.human_singular %>
14+
</.link>
1715
</:actions>
1816
</.header>
1917

priv/templates/phx.gen.live/show.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
1010
<%= schema.human_singular %> {@<%= schema.singular %>.<%= schema.opts[:primary_key] || :id %>}
1111
<:subtitle>This is a <%= schema.singular %> record from your database.</:subtitle>
1212
<:actions>
13-
<.button phx-click={JS.dispatch("click", to: {:inner, "a"})}>
14-
<.link navigate={~p"<%= schema.route_prefix %>/#{@<%= schema.singular %>}/edit?return_to=show"}>
15-
Edit <%= schema.singular %>
16-
</.link>
17-
</.button>
13+
<.link class={button_classes()} navigate={~p"<%= schema.route_prefix %>/#{@<%= schema.singular %>}/edit?return_to=show"}>
14+
Edit <%= schema.singular %>
15+
</.link>
1816
</:actions>
1917
</.header>
2018

0 commit comments

Comments
 (0)