Skip to content

Commit 317a394

Browse files
committed
chore(examples): refactor
1 parent 4f08aaf commit 317a394

File tree

9 files changed

+148
-96
lines changed

9 files changed

+148
-96
lines changed

examples/jsx/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
},
1010
"devDependencies": {
1111
"babel-plugin-transform-react-jsx": "^6.24.1",
12-
"vite-redom-jsx": "^1.5.2",
12+
"vite-redom-jsx": "^2.0.2",
1313
"vite-userscript-plugin": "workspace:*"
1414
},
1515
"dependencies": {
16-
"redom": "link:../../../vite-redom-jsx/packages/redom/",
17-
"redom-jsx": "^1.2.1"
16+
"redom-jsx": "^3.29.1"
1817
}
1918
}

examples/jsx/src/App.tsx

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,16 @@
1-
import { Router, mount, router, unmount } from 'redom'
2-
import type { RedomComponent, RedomEl } from 'redom'
3-
import { Counter } from './Counter.js'
4-
5-
class H1 implements RedomComponent {
6-
public el: RedomEl
7-
8-
constructor() {
9-
;<span this="el">hello</span>
10-
}
11-
}
12-
13-
class H2 implements RedomComponent {
14-
public el: RedomEl
15-
16-
constructor() {
17-
;<span this="el">world</span>
18-
}
19-
}
20-
21-
class Heading {
22-
public el: Router
23-
24-
constructor() {
25-
this.el = router('p.heading', {
26-
h1: H1,
27-
h2: H2
28-
})
29-
30-
this.el.update('h1')
31-
}
32-
}
33-
34-
const list = Array.from({ length: 5 }, () => Math.random())
1+
import { mount, unmount } from 'redom-jsx'
2+
import type { RedomComponent, RedomEl } from 'redom-jsx'
3+
import { Counter } from './Counter.jsx'
354

365
export class App implements RedomComponent {
37-
public el: RedomEl
38-
public counter: RedomComponent
39-
public button: HTMLElement
40-
public router: Router
41-
public toggle = true
6+
el: RedomEl
427

43-
constructor() {
44-
this.render()
45-
}
8+
private counter: RedomComponent
9+
private button: HTMLElement
4610

47-
private render(): void {
48-
;<div this="el">
11+
constructor() {
12+
// prettier-ignore
13+
<div this="el">
4914
<Counter
5015
this="counter"
5116
initialValue={-10}
@@ -60,30 +25,7 @@ export class App implements RedomComponent {
6025
}
6126
}}
6227
>
63-
mount/unmount counter
64-
</button>
65-
<p>
66-
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Illum enim
67-
veniam id consequatur, dolor recusandae minima dolore ab eius sunt quo
68-
totam quasi nam? Cum voluptate et id porro odit!
69-
</p>
70-
{list.map((value) => (
71-
<p>{value}</p>
72-
))}
73-
<a
74-
href="https://google.com"
75-
target="_blank"
76-
>
77-
This is link
78-
</a>
79-
<Heading this="router" />
80-
<button
81-
onclick={() => {
82-
this.toggle = !this.toggle
83-
this.router.el.update!(this.toggle ? 'h1' : 'h2')
84-
}}
85-
>
86-
toggle
28+
Toggle
8729
</button>
8830
</div>
8931
}

examples/jsx/src/Counter.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { RedomComponent, RedomEl, RedomProps } from 'redom'
1+
import type { RedomComponent, RedomEl, RedomProps } from 'redom-jsx'
22

33
interface Props {
44
initialValue?: number
55
}
66

77
export class Counter implements RedomComponent {
8-
public el: RedomEl
8+
el: RedomEl
99

1010
private counter: number
1111
private initialCounter: number
@@ -37,6 +37,7 @@ export class Counter implements RedomComponent {
3737
}
3838

3939
private render(): void {
40-
;<h1 this="el"></h1>
40+
// prettier-ignore
41+
<h1 this="el"></h1>
4142
}
4243
}

examples/jsx/src/Heading.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { RedomElement, router } from 'redom-jsx'
2+
import type { RedomComponent, RedomEl, Router } from 'redom-jsx'
3+
4+
class H1 implements RedomComponent {
5+
el: RedomEl
6+
7+
constructor() {
8+
// prettier-ignore
9+
<h1 this="el">Lorem ipsum dolor sit amet. (h1)</h1>
10+
}
11+
}
12+
13+
class H2 implements RedomComponent {
14+
el: RedomEl
15+
16+
constructor() {
17+
// prettier-ignore
18+
<h2 this="el">Lorem ipsum dolor sit amet. (h2)</h2>
19+
}
20+
}
21+
22+
export class Heading {
23+
el: Router
24+
toggle = true
25+
26+
constructor() {
27+
this.el = router('span', {
28+
h1: H1,
29+
h2: H2
30+
})
31+
32+
this.el.update('h1')
33+
}
34+
}

examples/jsx/src/ListId.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { mount } from 'redom-jsx'
2+
import type { RedomComponent, RedomEl, RedomElement, RedomProps } from 'redom-jsx'
3+
4+
interface ItemsProps {
5+
count: number
6+
}
7+
8+
class Items implements RedomComponent {
9+
el: RedomEl
10+
11+
private count: number
12+
13+
constructor({ count }: RedomProps<ItemsProps>) {
14+
this.count = count
15+
this.render()
16+
}
17+
18+
update(): void {
19+
this.el.innerHTML = ''
20+
mount(this.el, <>{this.generateList()}</>)
21+
}
22+
23+
private generateList(): RedomElement[] {
24+
const list = Array.from({ length: this.count }, () =>
25+
Math.random().toString(16).slice(2)
26+
)
27+
28+
return list.map((value) => (
29+
<>
30+
<hr />
31+
<p>{value}</p>
32+
</>
33+
))
34+
}
35+
36+
private render(): void {
37+
<span this="el">{this.generateList()}</span>
38+
}
39+
}
40+
41+
export class ListId implements RedomComponent {
42+
el: RedomEl
43+
44+
private items: Items
45+
46+
constructor() {
47+
this.render()
48+
}
49+
50+
private render(): void {
51+
// prettier-ignore
52+
<div this="el">
53+
<h1 this="h1">List</h1>
54+
<Items this="items" count={5} />
55+
<button onclick={() => this.items.update()}>Update</button>
56+
</div>
57+
}
58+
}

examples/jsx/src/Toggle.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { RedomComponent, RedomEl } from "redom-jsx";
2+
import { Heading } from "./Heading.jsx";
3+
4+
export class Toggle implements RedomComponent {
5+
el: RedomEl;
6+
7+
private heading: Heading
8+
9+
constructor() {
10+
<div this="el">
11+
<Heading this="heading" />
12+
<button
13+
onclick={() => {
14+
this.heading.toggle = !this.heading.toggle
15+
this.heading.el.update!(this.heading.toggle ? 'h1' : 'h2')
16+
}}
17+
>
18+
Toggle
19+
</button>
20+
</div>
21+
}
22+
}

examples/jsx/src/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { mount } from 'redom'
1+
import { mount } from 'redom-jsx'
22
import { App } from './App.js'
3+
import { ListId } from './ListId.jsx'
4+
import { Toggle } from './Toggle.jsx'
35

46
mount(document.body, <App />)
7+
mount(document.body, <Toggle />)
8+
mount(document.body, <ListId />)

examples/jsx/src/vite-env.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/// <reference types="vite/client" />
2-
/// <reference types="redom-jsx" />

pnpm-lock.yaml

Lines changed: 13 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)