defmodule Raxol.Examples.Showcase do
@moduledoc """
Interactive component showcase demonstrating all Raxol components.
Run with: mix raxol.examples showcase
"""
use Raxol.Core.Runtime.Application
import Raxol.LiveView, only: [assign: 2, assign: 3, update_in: 3, put_in: 3]
alias Raxol.UI.Components.{
Box,
Text,
Button,
TextInput,
Select,
Table,
ProgressBar,
Tabs
}
@components [
%{id: "text", name: "Text & Typography", icon: "[TEXT]"},
%{id: "buttons", name: "Buttons", icon: "[BTN]"},
%{id: "inputs", name: "Form Inputs", icon: "[FORM]"},
%{id: "layout", name: "Layout", icon: "[LAYOUT]"},
%{id: "data", name: "Data Display", icon: "[DATA]"},
%{id: "feedback", name: "Feedback", icon: "[CHAT]"},
%{id: "navigation", name: "Navigation", icon: "[NAV]"},
%{id: "advanced", name: "Advanced", icon: "[ADV]"}
]
@impl true
def mount(_params, socket) do
{:ok,
socket
|> assign(selected_component: "text")
|> assign(theme: "dark")
|> assign(demo_state: initial_demo_state())}
end
defp initial_demo_state do
%{
# Text demo
text_style: "normal",
# Button demo
button_variant: "primary",
button_clicks: 0,
# Input demo
text_value: "",
select_value: nil,
checkbox_value: false,
radio_value: "option1",
# Table demo
table_data: generate_sample_data(),
selected_row: nil,
# Progress demo
progress_value: 65,
# Tabs demo
active_tab: "overview"
}
end
@impl true
def render(assigns) do
~H"""
<%= get_component_title(@selected_component) %>
<%= render_component_demo(@selected_component, @demo_state, assigns) %>
Example Code
<%= get_component_code(@selected_component) %>
"""
end
# Component Demos
defp render_component_demo("text", state, assigns) do
~H"""
Normal textBold textItalic textUnderlined textStrikethrough textColored textText with backgroundSmall textLarge textHeading 1Heading 2Heading 3const example = "inline code";
function hello(name) {
console.log(`Hello, ${name}!`);
}
"""
end
defp render_component_demo("buttons", state, assigns) do
~H"""
"""
end
defp render_component_demo("inputs", state, assigns) do
~H"""
Accept terms and conditions
Dark Mode
"""
end
defp render_component_demo("layout", _state, assigns) do
~H"""
Box Component
Single Border
Double Border
Rounded Border
Grid Layout12 (span 2)34 (full width)Stack LayoutItem 1Item 2Item 3Spacer ComponentLeftRight
"""
end
defp render_component_demo("data", state, assigns) do
~H"""
Table Component
List ComponentFirst itemSecond item with iconSelected itemFourth itemProgress BarStats Display
"""
end
defp render_component_demo("feedback", _state, assigns) do
~H"""
This is an informational alert message.
Success! Your changes have been saved.
Warning: This action cannot be undone.
Error: Failed to connect to server.
Loading StatesSkeleton Loading
"""
end
defp render_component_demo("navigation", state, assigns) do
~H"""
Overview content goes here...Details content goes here...Settings content goes here...Menu Component
"""
end
defp render_component_demo("advanced", _state, assigns) do
~H"""
Chart ComponentTree ViewVirtual List (10,000 items)
~H"<%= item %>"
end}
/>
Markdown Renderer
# Markdown Support
Raxol supports **bold**, *italic*, and `inline code`.
- List item 1
- List item 2
> Blockquotes are also supported
"""
end
# Event Handlers
@impl true
def handle_event("select_component", %{"id" => id}, socket) do
{:noreply, assign(socket, selected_component: id)}
end
@impl true
def handle_event("toggle_theme", _, socket) do
new_theme =
case socket.assigns.theme do
"dark" -> "light"
_ -> "dark"
end
{:noreply, assign(socket, theme: new_theme)}
end
@impl true
def handle_event("increment_clicks", _, socket) do
{:noreply, update_in(socket.assigns.demo_state.button_clicks, &(&1 + 1))}
end
@impl true
def handle_event("update_text", %{"value" => value}, socket) do
{:noreply, put_in(socket.assigns.demo_state.text_value, value)}
end
@impl true
def handle_event("show_toast", _, socket) do
Raxol.Toast.show("This is a toast message!", type: :success)
{:noreply, socket}
end
@impl true
def handle_event("show_modal", _, socket) do
Raxol.Modal.show(
title: "Example Modal",
content: "This is a modal dialog window.",
buttons: [
%{label: "Cancel", action: :close},
%{label: "OK", action: :close, variant: :primary}
]
)
{:noreply, socket}
end
# Helper Functions
defp get_component_title(id) do
component = Enum.find(@components, &(&1.id == id))
"#{component.icon} #{component.name}"
end
defp get_component_code("text") do
"""
Styled text with Raxol
Main Heading
def hello, do: "world"
"""
end
defp get_component_code("buttons") do
"""
"""
end
defp get_component_code(_), do: "# Example code for this component..."
defp generate_sample_data do
Enum.map(1..5, fn i ->
%{
id: i,
name: "Item #{i}",
status: Enum.random(["Active", "Pending", "Inactive"]),
progress: :rand.uniform(100)
}
end)
end
end
# Component: Sidebar
defmodule Raxol.Examples.Showcase.ComponentSidebar do
use Raxol.Component
prop(:components, {:list, :map}, required: true)
prop(:selected, :string, required: true)
prop(:onSelect, :string, required: true)
@impl true
def render(assigns) do
~H"""
Components
<%= for component <- @components do %>
<%= component.icon %> <%= component.name %>
<% end %>
"""
end
end
# Component: Theme Toggle
defmodule Raxol.Examples.Showcase.ThemeToggle do
use Raxol.Component
prop(:theme, :string, required: true)
prop(:onChange, :string, required: true)
@impl true
def render(assigns) do
~H"""
"""
end
end