The Pragmatic Studio

Button Clicks

Notes & Exercises

Code Snapshot

A snapshot of the code at the end of the video is in the video-code/03-button-clicks directory of the code bundle.

Two Ways to Assign State ✌️

In the video, we passed the assign/2 function a keyword list like so:

assign(socket, brightness: 10)

Alternatively, you can call the assign/3 function and pass the key (typically an atom) and value as separate arguments, like this:

assign(socket, :brightness, 10)

Either way works. Passing a keyword list, however, makes it easier to assign multiple key/value pairs if necessary. For example, if we also needed to store the initial color temperature of the light then we'd pass a keyword list like so:

assign(socket, brightness: 10, temp: 3000)

Anyway, that's just a lil' pro-tip. 😉

Exercise: Constrain Min/Max Brightness

You may have noticed that you can continue to turn the light up even after it's reached full brightness. Same with turning the light down even when it's already off.

If you're keen to keep the values in bounds, that's easy to do using Elixir's min and max functions.

Exercise: Light Me Up!

Here's a fun one to practice everything you learned in the video...

Add a button that when clicked sets the brightness to a random number between 0 and 100. The app already has a fire.svg image that you can use for the button if you like.

Try doing it from memory before peeking at the answer for guidance. 👀

  1. Add a button that emits an event when clicked.

  2. Handle the event by assigning a random number as the brightness.

  3. Throw a disco party! 🕺


The solutions for the exercises in this module are in the exercise-solutions/03-button-clicks directory of the code bundle.

📚 Dive Deeper

  • Phoenix 1.7 apps are pre-configured to use Tailwind CSS by default. But rather than using Tailwind's utility classes directly in the LiveView templates, we used Tailwind's @apply directive in the assets/css/app.css file. That way there's a lot less CSS to look around in templates, and we can better focus on learning LiveView.

    Check out our Using Tailwind CSS in Phoenix 1.7 tutorial for more details on how we set it all up.

How's Your Elixir Game?

If you're new to the Elixir programming language, learning LiveView might feel like someone tied your shoe laces together and asked you play some hoops. 🏀 🤕

Imagine knowing Elixir so well you can drive to the basket and dunk! ⛹🏻 That's what you'll be able to do after taking our Elixir & OTP course. So before getting too far into LiveView, we highly recommend joining us in the Elixir course to gain clarity and confidence with the language.


We start with the Elixir fundamentals and leave no stone unturned as we build an app step-by-step from scratch. And the second half is all about OTP, so it's like getting two courses in one. 🙌

We think you're gonna love it!

All course material, including videos and source code, is copyrighted and licensed for individual use only. You may make copies for your own personal use (e.g. on your laptop, on your iPad, on your backup drive). However, you may not transfer ownership or share the material with other people. We make no guarantees that the source code is fit for any purpose. Course material may not be used to create training material, courses, books, and the like. Please support us by encouraging others to purchase their own copies. Thank you!

Copyright © 2005–2024, The Pragmatic Studio. All Rights Reserved.

Phoenix LiveView Pro

2nd Edition for LiveView 0.20

0% complete