defmodule Raxol.Examples.Showcase do
@moduledoc """
Interactive component showcase demonstrating all Raxol components.
Run with: mix raxol.examples showcase
"""
use Raxol.Application
alias Raxol.UI.Components.{
Box,
Text,
Button,
TextInput,
Select,
Table,
ProgressBar,
Tabs
}
@components [
%{id: "text", name: "Text & Typography", icon: "📝"},
%{id: "buttons", name: "Buttons", icon: "🔘"},
%{id: "inputs", name: "Form Inputs", icon: "📋"},
%{id: "layout", name: "Layout", icon: "📐"},
%{id: "data", name: "Data Display", icon: "📊"},
%{id: "feedback", name: "Feedback", icon: "💬"},
%{id: "navigation", name: "Navigation", icon: "🧭"},
%{id: "advanced", name: "Advanced", icon: "⚡"}
]
@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 text
Bold text
Italic text
Underlined text
Strikethrough text
Colored text
Text with background
Small text
Large text
Heading 1
Heading 2
Heading 3
const 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 Layout
1
2 (span 2)
3
4 (full width)
Stack Layout
Item 1
Item 2
Item 3
Spacer Component
Left
Right
"""
end
defp render_component_demo("data", state, assigns) do
~H"""
Table Component
List Component
First item
Second item with icon
Selected item
Fourth item
Progress Bar
Stats 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 States
Skeleton 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 Component
Tree View
Virtual 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 = if socket.assigns.theme == "dark", do: "light", else: "dark"
{: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