Skip to content

Commit

Permalink
Demo components page for React Components (#2850)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeorin committed Mar 5, 2025
1 parent 14821de commit add6549
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
14 changes: 14 additions & 0 deletions assets/js/react/components/Bar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type BarProps = {
before: React.ReactNode;
after: React.ReactNode;
children: React.ReactNode;
};

export const Bar = ({ before, after, children }: BarProps) => (
<>
{before}
<p>Bar:</p>
{children}
{after}
</>
);
11 changes: 11 additions & 0 deletions assets/js/react/components/Baz.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type BazProps = {
baz: number;
children: React.ReactNode;
};

export const Baz = ({ baz, children }: BazProps) => (
<>
<p>Baz: {baz}</p>
{children}
</>
);
11 changes: 11 additions & 0 deletions assets/js/react/components/Foo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type FooProps = {
foo: number;
children: React.ReactNode;
};

export const Foo = ({ foo, children }: FooProps) => (
<>
<p>Foo: {foo}</p>
{children}
</>
);
4 changes: 4 additions & 0 deletions assets/js/react/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export { Foo, type FooProps } from './Foo';
export { Bar, type BarProps } from './Bar';
export { Baz, type BazProps } from './Baz';

export { Fallback, type FallbackProps } from './Fallback';
export { Boundary, type BoundaryProps } from './Boundary';
export { Slot, type SlotProps } from './Slot';
56 changes: 56 additions & 0 deletions lib/lightning_web/live/dev/react_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule LightningWeb.Dev.ReactLive do
# Internal Development Page for viewing and working on React components.
# Access this page at /dev/react
@moduledoc false
use LightningWeb, {:live_view, layout: {LightningWeb.Layouts, :blank}}

import React

@impl true
def mount(_params, _session, socket) do
socket = assign(socket, :baz, 0)
{:ok, socket}
end

@impl true
def handle_event("inc", _params, socket) do
{:noreply, update(socket, :baz, &(&1 + 1))}
end

attr :foo, :integer
slot :inner_block
jsx("components/Foo.tsx")

slot :before
slot :inner_block, required: true
slot :after, required: true
jsx("components/Bar.tsx")

attr :baz, :integer, default: 0
jsx("components/Baz.tsx")

@impl true
def render(assigns) do
assigns = assign(assigns, foo: 42)

~H"""
<.Foo foo={@foo}>
<.Bar>
<:before>
<p style="color: blue">Bar before slot</p>
</:before>
<.Baz baz={@baz} />
<:after>
<p style="color: red">Bar after slot</p>
</:after>
</.Bar>
</.Foo>
<button
phx-click="inc"
class="inline-flex justify-center items-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 phx-submit-loading:opacity-75 bg-primary-600 hover:bg-primary-700 text-white focus:ring-primary-500 disabled:bg-primary-300 rounded-md"
>
Increment
</button>
"""
end
end
1 change: 1 addition & 0 deletions lib/lightning_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ defmodule LightningWeb.Router do
forward "/mailbox", Plug.Swoosh.MailboxPreview

live "/components", LightningWeb.Dev.ComponentsLive, :index
live "/react", LightningWeb.Dev.ReactLive, :index
end
end

Expand Down

0 comments on commit add6549

Please sign in to comment.