Skip to content

Commit add6549

Browse files
committed
Demo components page for React Components (#2850)
1 parent 14821de commit add6549

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

assets/js/react/components/Bar.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export type BarProps = {
2+
before: React.ReactNode;
3+
after: React.ReactNode;
4+
children: React.ReactNode;
5+
};
6+
7+
export const Bar = ({ before, after, children }: BarProps) => (
8+
<>
9+
{before}
10+
<p>Bar:</p>
11+
{children}
12+
{after}
13+
</>
14+
);

assets/js/react/components/Baz.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type BazProps = {
2+
baz: number;
3+
children: React.ReactNode;
4+
};
5+
6+
export const Baz = ({ baz, children }: BazProps) => (
7+
<>
8+
<p>Baz: {baz}</p>
9+
{children}
10+
</>
11+
);

assets/js/react/components/Foo.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type FooProps = {
2+
foo: number;
3+
children: React.ReactNode;
4+
};
5+
6+
export const Foo = ({ foo, children }: FooProps) => (
7+
<>
8+
<p>Foo: {foo}</p>
9+
{children}
10+
</>
11+
);

assets/js/react/components/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export { Foo, type FooProps } from './Foo';
2+
export { Bar, type BarProps } from './Bar';
3+
export { Baz, type BazProps } from './Baz';
4+
15
export { Fallback, type FallbackProps } from './Fallback';
26
export { Boundary, type BoundaryProps } from './Boundary';
37
export { Slot, type SlotProps } from './Slot';
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
defmodule LightningWeb.Dev.ReactLive do
2+
# Internal Development Page for viewing and working on React components.
3+
# Access this page at /dev/react
4+
@moduledoc false
5+
use LightningWeb, {:live_view, layout: {LightningWeb.Layouts, :blank}}
6+
7+
import React
8+
9+
@impl true
10+
def mount(_params, _session, socket) do
11+
socket = assign(socket, :baz, 0)
12+
{:ok, socket}
13+
end
14+
15+
@impl true
16+
def handle_event("inc", _params, socket) do
17+
{:noreply, update(socket, :baz, &(&1 + 1))}
18+
end
19+
20+
attr :foo, :integer
21+
slot :inner_block
22+
jsx("components/Foo.tsx")
23+
24+
slot :before
25+
slot :inner_block, required: true
26+
slot :after, required: true
27+
jsx("components/Bar.tsx")
28+
29+
attr :baz, :integer, default: 0
30+
jsx("components/Baz.tsx")
31+
32+
@impl true
33+
def render(assigns) do
34+
assigns = assign(assigns, foo: 42)
35+
36+
~H"""
37+
<.Foo foo={@foo}>
38+
<.Bar>
39+
<:before>
40+
<p style="color: blue">Bar before slot</p>
41+
</:before>
42+
<.Baz baz={@baz} />
43+
<:after>
44+
<p style="color: red">Bar after slot</p>
45+
</:after>
46+
</.Bar>
47+
</.Foo>
48+
<button
49+
phx-click="inc"
50+
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"
51+
>
52+
Increment
53+
</button>
54+
"""
55+
end
56+
end

lib/lightning_web/router.ex

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ defmodule LightningWeb.Router do
263263
forward "/mailbox", Plug.Swoosh.MailboxPreview
264264

265265
live "/components", LightningWeb.Dev.ComponentsLive, :index
266+
live "/react", LightningWeb.Dev.ReactLive, :index
266267
end
267268
end
268269

0 commit comments

Comments
 (0)