Skip to content

Commit 9c7aa23

Browse files
committed
Mobx reactive property working with lit
1 parent 071d29f commit 9c7aa23

File tree

10 files changed

+87
-46
lines changed

10 files changed

+87
-46
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"editor.formatOnSave": true
2+
"editor.formatOnSave": true,
3+
"appService.defaultWebAppToDeploy": "/subscriptions/53e3928f-5a21-47c1-80e8-4021fac7cae7/microsoft.web/sites/subscriptions/53e3928f-5a21-47c1-80e8-4021fac7cae7/resourceGroups/testing-werner/providers/Microsoft.Web/sites/typescripttemplate",
4+
"appService.deploySubpath": "build"
35
}

public/index.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
<meta name="description" content="Web site created using create-snowpack-app" />
99
<link rel="stylesheet" type="text/css" href="/index.css" />
1010
<link rel="stylesheet" type="text/css" href="/style.css" />
11-
<script type="module" src="/dist/index.js"></script>
1211

12+
<script type="module" src="/dist/index.js"></script>
1313
<script type="module" src="/dist/components/name-tag.js"></script>
1414
<script type="module" src="/dist/components/observable-timer.js"></script>
15+
<!-- <script type="module" src="/dist/components/my-element-component.js"></script> -->
1516

1617
<title>Werners typescript template</title>
1718
</head>
@@ -23,6 +24,9 @@
2324
<header>
2425
<div class="container">
2526
<h1 id="welcomeText">Welcome to the todo list</h1>
27+
28+
<b>Import tasks</b>-<b>Export tasks</b>-<b>Clear tasks</b>
29+
2630
</div>
2731
</header>
2832

@@ -34,6 +38,11 @@ <h1 id="welcomeText">Welcome to the todo list</h1>
3438
<observable-timer></observable-timer>
3539
</div>
3640

41+
<!-- <div class="container">
42+
<hr>
43+
<my-element2></my-element2>
44+
</div> -->
45+
3746
<footer>
3847
<div class="container">
3948
<p>Git repository: <a href="https://github.com/WernerWaage/typescript-todo">typescript-todo</a></p>

src/components/countdown.component.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/components/list/index.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { html, TemplateResult } from 'lit';
2+
import { customElement, property } from 'lit/decorators.js';
3+
import { observable, action } from 'mobx';
4+
import { MobxLitElement } from '@adobe/lit-mobx';
5+
6+
// create a mobx observable
7+
class Counter {
8+
@observable
9+
public count = 0;
10+
11+
@action
12+
public increment() {
13+
this.count++;
14+
}
15+
}
16+
17+
// create instance that can be shared across components
18+
const counter = new Counter();
19+
20+
// create a new custom element, and use the base MobxLitElement class
21+
// alternatively you can use the MobxReactionUpdate mixin, e.g. `class MyElement extends MobxReactionUpdate(LitElement)`
22+
@customElement('my-element2')
23+
export class MyElement extends MobxLitElement {
24+
25+
private counter = counter;
26+
27+
// any observables accessed in the render method will now trigger an update
28+
public render(): TemplateResult {
29+
return html`
30+
Count is ${this.counter.count}
31+
<br />
32+
<button @click=${this.incrementCount}>Add</button>
33+
`;
34+
}
35+
36+
private incrementCount() {
37+
console.log("incrementing count " + this.counter.count);
38+
this.counter.increment();
39+
// this.requestUpdate()
40+
}
41+
}

src/components/observable-timer-base.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@ import {
66
observable,
77
} from 'mobx'
88
export class ObservableTimer {
9-
public secondsPassed: number = -1
9+
@observable
10+
public secondsPassed: number = 0
11+
12+
@action
13+
public increment() {
14+
this.secondsPassed++;
15+
}
16+
17+
@action
18+
public resetTimer() {
19+
this.secondsPassed =0;
20+
}
21+
1022
constructor() {
1123
makeAutoObservable(this, {
1224
secondsPassed: observable,
1325
handleSecondsUpdated: action,
14-
staticSeconds: computed,
26+
minutesPassed: computed,
27+
increase: action,
28+
resetTimer: action,
1529
})
1630

1731
let intervalId = setInterval(() => {
@@ -20,26 +34,17 @@ export class ObservableTimer {
2034
}
2135

2236
// Computed value example
23-
public get staticSeconds() {
24-
return 5 * this.secondsPassed
37+
public get minutesPassed() {
38+
return this.secondsPassed / 60;
2539
}
2640

2741
increase() {
28-
console.log('increasing ' + this.secondsPassed)
42+
// console.log('increasing ' + this.secondsPassed)
2943
this.secondsPassed += 1
3044
}
3145

32-
reset() {
33-
this.secondsPassed = 0
34-
}
35-
3646
// Change the seconds from outside the class (eg. not the timer)
3747
public handleSecondsUpdated(secondsPassed: number) {
38-
console.log('Handling change')
3948
this.secondsPassed = secondsPassed
4049
}
41-
}
42-
43-
// const TimerView = observer(({ timer }) => (
44-
// <button onClick={() => timer.reset()}>Seconds passed: {timer.secondsPassed}</button>
45-
// ))
50+
}

src/components/observable-timer.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LitElement, html } from 'lit'
1+
import { html } from 'lit'
22
import {map} from 'lit/directives/map.js';
33
import {
44
makeObservable,
@@ -10,8 +10,9 @@ import {
1010
import { MobxLitElement } from '@adobe/lit-mobx'
1111
import { ObservableTimer } from './observable-timer-base'
1212

13-
export class ObservableTimerView extends LitElement {
13+
export class ObservableTimerView extends MobxLitElement {
1414
// Properties
15+
@observable
1516
public secondsPassed = myTimer.secondsPassed
1617

1718
constructor() {
@@ -33,30 +34,21 @@ export class ObservableTimerView extends LitElement {
3334

3435

3536
render() {
36-
const resetbutton = html`<button
37-
@click=${() => this.forceUpdate(myTimer.secondsPassed)}
38-
>
39-
Test ${myTimer.staticSeconds}
40-
</button><br><button @click="${this.getNewVal}">get new value</button>`
37+
const resetbutton = html`<button @click=${() => this.forceUpdate(0)} >Reset timer </button>`
4138
return html`
4239
<div>
4340
This is an observable timer!
44-
<p>${this.secondsPassed} seconds</p>
41+
<p>${myTimer.secondsPassed} seconds since last refresh</p>
4542
<br />${resetbutton}
4643
</div>
4744
`
4845
}
4946

5047

51-
getNewVal(){
52-
let newVal = Math.floor(Math.random()*10);
53-
this.secondsPassed = newVal;
54-
}
55-
56-
5748
forceUpdate(newValue: number) {
58-
this.secondsPassed = newValue
59-
this.requestUpdate()
49+
// this.secondsPassed = newValue
50+
// this.requestUpdate()
51+
myTimer.resetTimer()
6052
return null
6153
}
6254
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { html, css, LitElement } from 'lit'
22
import { customElement, property } from 'lit/decorators.js'
33

4+
// This example shows a simple component with an input parameter(name) and css styling that does not bubble up to the parent.
45
@customElement('simple-greeting')
56
export class SimpleGreeting extends LitElement {
67
static styles = css`
78
p {
89
color: blue;
910
}
1011
`
11-
1212
@property()
1313
name = 'Somebody'
1414

1515
render() {
1616
return html`<p>Hello, ${this.name}!</p>`
1717
}
18-
}
18+
}

src/components/todo.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { MobxLitElement } from '@adobe/lit-mobx'
12
import { LitElement, html, css, PropertyValues } from 'lit'
23
import { customElement, state, property, query } from 'lit/decorators.js'
34
import type { Task } from 'src/types/task.type'
@@ -17,7 +18,7 @@ export function loadTasks(): Task[] {
1718
}
1819

1920
@customElement('todo-list')
20-
export class ToDoList extends LitElement {
21+
export class ToDoList extends MobxLitElement {
2122
static styles = css`
2223
.completed {
2324
text-decoration-line: line-through;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { customElement, property } from 'lit/decorators.js'
1111
import { MobxLitElement } from '@adobe/lit-mobx'
1212

1313
// Lit element components:
14-
import './components/simple-greeting/simple-greeting'
14+
import './components/simple-greeting'
1515
import './components/todo.component'
1616
import { Timer } from './classes/timer.class'
1717

0 commit comments

Comments
 (0)