Skip to content

Commit

Permalink
DRY Tailwind Class duplication into btn/1 function #166
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jul 30, 2023
1 parent 3a8ae45 commit f2767bf
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/counter_web/live/counter_component.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
defmodule CounterComponent do
use Phoenix.LiveComponent

# Avoid duplicating the Tailwind Classes and show hot to inline a function call:
defp btn(color) do
"text-6xl pb-2 w-20 rounded-lg bg-#{color}-500 hover:bg-#{color}-600"
end

def render(assigns) do
~H"""
<div class="text-center">
<h1 class="text-4xl font-bold text-center"> Counter: <%= @val %> </h1>
<button phx-click="dec" class="text-6xl pb-2 w-20 bg-red-500 hover:bg-red-600 rounded-lg">
<button phx-click="dec" class={btn("red")}>
-
</button>
<button phx-click="inc" class="text-6xl pb-2 w-20 bg-green-500 hover:bg-green-600 rounded-lg">
<button phx-click="inc" class={btn("green")}>
+
</button>
</div>
Expand Down

0 comments on commit f2767bf

Please sign in to comment.