|
| 1 | +defmodule PrimaWeb.CodeExample do |
| 2 | + @moduledoc """ |
| 3 | + A component for displaying live demos alongside their source code. |
| 4 | +
|
| 5 | + Renders the component example in a tabbed interface with Preview and Code views. |
| 6 | + The component can either render provided content via inner_block or dynamically |
| 7 | + render HEEx templates from files. |
| 8 | + """ |
| 9 | + use Phoenix.Component |
| 10 | + alias Phoenix.LiveView.JS |
| 11 | + |
| 12 | + attr :file, :string, required: true, doc: "Path to file in priv/code_examples/" |
| 13 | + |
| 14 | + @doc """ |
| 15 | + Displays a live demo alongside syntax-highlighted source code. |
| 16 | +
|
| 17 | + Renders the component example with a tabbed interface for switching between |
| 18 | + Preview and Code views. The source code is loaded from a file in priv/code_examples/ |
| 19 | + and automatically rendered in the preview. |
| 20 | +
|
| 21 | + ## Example |
| 22 | +
|
| 23 | + <.code_example file="dropdown/basic_dropdown.heex" /> |
| 24 | + """ |
| 25 | + def code_example(assigns) do |
| 26 | + source = get_code_source(assigns) |
| 27 | + assigns = assign(assigns, :highlighted_code, highlight_code(source)) |
| 28 | + assigns = assign(assigns, :rendered_content, render_heex_content(source, assigns)) |
| 29 | + id = assigns[:id] || "code-example-#{:erlang.unique_integer([:positive])}" |
| 30 | + assigns = assign(assigns, :id, id) |
| 31 | + |
| 32 | + ~H""" |
| 33 | + <div class="relative border border-gray-200 rounded-lg bg-white" id={@id}> |
| 34 | + <div class="flex items-center justify-between border-b border-gray-200 rounded-t-lg px-4 py-2 bg-gray-50"> |
| 35 | + <div class="flex gap-2"> |
| 36 | + <button |
| 37 | + type="button" |
| 38 | + phx-click={ |
| 39 | + JS.add_class("hidden", to: "##{@id}-code") |
| 40 | + |> JS.remove_class("hidden", to: "##{@id}-preview") |
| 41 | + |> JS.add_class("bg-white border-gray-300 text-gray-900", to: "##{@id}-preview-tab") |
| 42 | + |> JS.remove_class("bg-white border-gray-300 text-gray-900", to: "##{@id}-code-tab") |
| 43 | + |> JS.add_class("bg-gray-50 border-transparent text-gray-600", to: "##{@id}-code-tab") |
| 44 | + |> JS.remove_class("bg-gray-50 border-transparent text-gray-600", |
| 45 | + to: "##{@id}-preview-tab" |
| 46 | + ) |
| 47 | + } |
| 48 | + id={"#{@id}-preview-tab"} |
| 49 | + class="px-3 py-1.5 text-sm font-medium rounded border bg-white border-gray-300 text-gray-900 transition-colors" |
| 50 | + > |
| 51 | + Preview |
| 52 | + </button> |
| 53 | + <button |
| 54 | + type="button" |
| 55 | + phx-click={ |
| 56 | + JS.add_class("hidden", to: "##{@id}-preview") |
| 57 | + |> JS.remove_class("hidden", to: "##{@id}-code") |
| 58 | + |> JS.add_class("bg-white border-gray-300 text-gray-900", to: "##{@id}-code-tab") |
| 59 | + |> JS.remove_class("bg-white border-gray-300 text-gray-900", to: "##{@id}-preview-tab") |
| 60 | + |> JS.add_class("bg-gray-50 border-transparent text-gray-600", |
| 61 | + to: "##{@id}-preview-tab" |
| 62 | + ) |
| 63 | + |> JS.remove_class("bg-gray-50 border-transparent text-gray-600", |
| 64 | + to: "##{@id}-code-tab" |
| 65 | + ) |
| 66 | + } |
| 67 | + id={"#{@id}-code-tab"} |
| 68 | + class="px-3 py-1.5 text-sm font-medium rounded border bg-gray-50 border-transparent text-gray-600 transition-colors" |
| 69 | + > |
| 70 | + Code |
| 71 | + </button> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | +
|
| 75 | + <div id={"#{@id}-preview"} class="p-6"> |
| 76 | + {Phoenix.HTML.raw(@rendered_content)} |
| 77 | + </div> |
| 78 | +
|
| 79 | + <div id={"#{@id}-code"} class="hidden"> |
| 80 | + <!-- <div class="p-4 bg-gray-900 rounded-b-lg overflow-x-auto text-sm"> --> |
| 81 | + {Phoenix.HTML.raw(@highlighted_code)} |
| 82 | + <!-- </div> --> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + """ |
| 86 | + end |
| 87 | + |
| 88 | + defp get_code_source(%{file: file}) when is_binary(file) do |
| 89 | + file_path = Path.join(["priv", "code_examples", file]) |
| 90 | + |
| 91 | + case File.read(file_path) do |
| 92 | + {:ok, content} -> content |
| 93 | + {:error, _} -> "Error: Could not read file '#{file}'" |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + defp highlight_code(source) do |
| 98 | + source |
| 99 | + |> String.trim() |
| 100 | + |> Autumn.highlight!( |
| 101 | + language: "heex", |
| 102 | + formatter: |
| 103 | + {:html_inline, theme: "molokai", pre_class: "p-4 rounded-b-lg overflow-x-auto text-sm"} |
| 104 | + ) |
| 105 | + end |
| 106 | + |
| 107 | + defp render_heex_content(template_string, assigns) do |
| 108 | + try do |
| 109 | + # Compile and evaluate the HEEx template with component imports |
| 110 | + {result, _} = |
| 111 | + Code.eval_string( |
| 112 | + """ |
| 113 | + import Phoenix.Component |
| 114 | + import Prima.Modal |
| 115 | + import Prima.Dropdown |
| 116 | + import Prima.Combobox |
| 117 | + import PrimaWeb.CoreComponents |
| 118 | +
|
| 119 | + ~H\"\"\" |
| 120 | + #{template_string} |
| 121 | + \"\"\" |
| 122 | + """, |
| 123 | + assigns: assigns |
| 124 | + ) |
| 125 | + |
| 126 | + {:safe, result} |
| 127 | + rescue |
| 128 | + e -> |
| 129 | + "<div class='text-red-600 p-4'>Error rendering template: #{Exception.message(e)}</div>" |
| 130 | + end |
| 131 | + end |
| 132 | +end |
0 commit comments