Skip to content

Instantly share code, notes, and snippets.

@lubien
Created March 28, 2024 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lubien/eb38a2d7e1d3a4f552e66fb95006fa69 to your computer and use it in GitHub Desktop.
Save lubien/eb38a2d7e1d3a4f552e66fb95006fa69 to your computer and use it in GitHub Desktop.
Mix.install([
{:liveview_playground, "~> 0.1.1"}
])
defmodule PageLive do
use LiveviewPlaygroundWeb, :live_view
def mount(_params, _session, socket) do
socket = assign(socket, tab: "home")
{:ok, socket}
end
def render(assigns) do
~H"""
<div>
<%= case @tab do %>
<% "home" -> %>
<p>You're on my personal page!</p>
<% "about" -> %>
<p>Hi, I'm a LiveView developer!</p>
<% "contact" -> %>
<p>Mail me to bot [at] company [dot] com</p>
<% end %>
</div>
<input :if={@tab != "home"} type="button" value="Open Home" phx-click="show_home" >
<input :if={@tab != "about"} type="button" value="Open About" phx-click="show_about" >
<input :if={@tab != "contact"} type="button" value="Open Contact" phx-click="show_contact" >
"""
end
def handle_event("show_home", _params, socket) do
socket = assign(socket, tab: "home")
{:noreply, socket}
end
def handle_event("show_about", _params, socket) do
socket = assign(socket, tab: "about")
{:noreply, socket}
end
def handle_event("show_contact", _params, socket) do
socket = assign(socket, tab: "contact")
{:noreply, socket}
end
end
LiveviewPlayground.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment