Skip to content

Commit e3befd0

Browse files
docs(website): add components docs
1 parent 9be114a commit e3befd0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1202
-329
lines changed

logo2.psd

223 KB
Binary file not shown.

packages/node_modules/overmind-components/src/Element.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ export class Element {
66
parent: any
77
context: any
88
keys: any
9-
events: any
109
constructor(tag, props, children) {
1110
this.tag = tag
1211
this.props = props || {}
1312
this.children = children
1413
this.keys = {}
15-
this.events = {}
1614
}
1715
mount(parent, context) {
1816
this.parent = parent
@@ -254,8 +252,10 @@ export class Element {
254252
return this.el
255253
}
256254
unmount() {
257-
for (let eventType in this.events) {
258-
this.el.removeEventListener(eventType, this.events[eventType])
255+
for (let prop in this.props) {
256+
if (this.context.events.isEventAttr(this.props[prop])) {
257+
this.context.events.unregister(prop.substr(2).toLowerCase(), this.el)
258+
}
259259
}
260260

261261
return this

packages/node_modules/overmind-components/src/Events.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export class Events {
3838
}
3939
},
4040
})
41-
this.events[type].get(node)(eventToDispatch)
41+
this.events[type].get(node) &&
42+
this.events[type].get(node)(eventToDispatch)
4243

4344
if (breakOut) {
4445
return
@@ -51,4 +52,7 @@ export class Events {
5152

5253
this.events[type].set(element, cb)
5354
}
55+
unregister(type, element) {
56+
this.events[type].delete(element)
57+
}
5458
}

packages/node_modules/overmind-components/src/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function useRef() {
7474
}
7575

7676
let hook = (node) => {
77-
;(hook as any).target = node
77+
;(hook as any).current = node
7878
}
7979

8080
Component.current.addHook(hook)

packages/node_modules/overmind/src/index.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ export class Overmind<Config extends Configuration> implements Configuration {
228228
)
229229
})
230230
} else {
231-
const execution = this.createExecution(name, action)
232-
this.eventHub.emit(EventType.ACTION_START, execution)
233-
this.eventHub.emit(EventType.OPERATOR_START, {
234-
...execution,
231+
const execution = {
232+
...this.createExecution(name, action),
235233
operatorId: 0,
236234
type: 'action',
237-
})
235+
}
236+
this.eventHub.emit(EventType.ACTION_START, execution)
237+
this.eventHub.emit(EventType.OPERATOR_START, execution)
238238
const scopedTree = this.proxyStateTree.getScopedTree()
239239
scopedTree.startMutationTracking()
240240
const result = action(
@@ -247,22 +247,19 @@ export class Overmind<Config extends Configuration> implements Configuration {
247247
this.eventHub.emit(EventType.OPERATOR_END, {
248248
...execution,
249249
isAsync: result instanceof Promise,
250-
operatorId: 0,
251250
result: undefined,
252251
})
253252
this.eventHub.emit(EventType.ACTION_END, execution)
254253
const mutations = scopedTree.clearMutationTracking()
255254
mutations.length &&
256255
this.eventHub.emit(EventType.MUTATIONS, {
257256
...execution,
258-
operatorId: 0,
259257
mutations,
260258
})
261259
scopedTree.startMutationTracking()
262260
scopedTree.addMutationListener((mutation) => {
263261
this.eventHub.emit(EventType.MUTATIONS, {
264262
...execution,
265-
operatorId: 0,
266263
mutations: [mutation],
267264
})
268265
setTimeout(() => {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Components
2+
3+
Overmind ships with its own view layer, called **overmind-components**. It delivers an experience where the connection and tracking of state between Overmind and the components is completely transparent.
4+
5+
## actions
6+
7+
All components receives a **prop** called **actions**. These are all the actions you have defined in your Overmind application.
8+
9+
```marksy
10+
h(Example, { name: "api/components_actions"})
11+
```
12+
13+
## component
14+
15+
All components are defined as plain functions.
16+
17+
```marksy
18+
h(Example, { name: "api/components_component"})
19+
```
20+
21+
## events
22+
23+
Overmind components uses event delegation to optimize event management. There is only a single event listener for each type of event, but the event acts as if it was triggered on the element you defined it. That means **currentTarget** points to the element where you registered the listener and **target** points to the element that caused the event to trigger.
24+
25+
Any event you want to manage you just prefix with **on**, like *onClick*, *onInput* or *onSubmit*.
26+
27+
```marksy
28+
h(Example, { name: "api/components_events"})
29+
```
30+
31+
```marksy
32+
h(Notice, null, "Even though event delegation is used **stopPropagation** still acts as expected")
33+
```
34+
35+
## jsx
36+
37+
The syntax used to define UI composition is called **jsx**. This is included in Typescript and can be added with the [babel plugin for jsx](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx) if using traditional Javascript.
38+
39+
Jsx is **not** html. Actually jsx is transformed into the following Javascript:
40+
41+
```marksy
42+
h(Example, { name: "api/components_jsx"})
43+
```
44+
45+
That means everything is Javascript at the end of the day and that is exactly what makes jsx so powerful. There is no special template syntax and limitations of that. You can use plain Javascript to do whatever you want. Jsx is just a more declarative way of expressing the element and component composition.
46+
47+
## keys
48+
49+
Keys is a way to uniquely identify elements and components. This becomes useful in two scenarios:
50+
51+
1. To cache elements and components in lists to reduce amount of reconciliation required. It is a good convention to always give keys when you work with lists
52+
2. Force a component to unmount and mount a new one every time the key change
53+
54+
```marksy
55+
h(Example, { name: "api/components_keys"})
56+
```
57+
58+
## props
59+
60+
You can pass properties to components. The component is optimized in such a way that it will only update if any of the props change, or if any accessed state changes. You can also pass functions as props.
61+
62+
```marksy
63+
h(Example, { name: "api/components_props"})
64+
```
65+
66+
## render
67+
68+
The render function allows you to render your UI structure to a target element. You also pass the render function an Overmind application instance which is how state and actions is exposed to all components.
69+
70+
```marksy
71+
h(Example, { name: "api/components_render"})
72+
```
73+
74+
## state
75+
76+
All components receives a **prop** called **state**. This is all the state defined in your Overmind application instance. Any state your component accesses will cause the component to render again when changed.
77+
78+
```marksy
79+
h(Example, { name: "api/components_state"})
80+
```
81+
82+
## style
83+
84+
With **jsx** you define styles as an object. That does not mean you should define all the styling of your application this way. There are several solutions for managing CSS and styling, though when you do want to inline styles you define it as an object.
85+
86+
```marksy
87+
h(Example, { name: "api/components_style"})
88+
```
89+
90+
## useEffect
91+
92+
The **useEffect** hook allows you to run arbitrary logic when the component mounts and optionally whenever it rerenders. You can also control when the effect runs again by referencing one or multiple values. If the values change, the effect runs again. You can return a function from the effect which runs whenever the effect is rererun or the component unmounts. This is typically used to clean up the effect.
93+
94+
```marksy
95+
h(Example, { name: "api/components_useEffect"})
96+
```
97+
98+
## useRef
99+
100+
Sometimes you need to reference an actual element. Maybe you need to mount something from a library on the element or you want to mutate it directly. To get a reference to the element use **useRef**.
101+
102+
```marksy
103+
h(Example, { name: "api/components_useRef"})
104+
```
105+
106+
## useState
107+
108+
To define and manage internal state in components you can use the **useState** hook.
109+
110+
```marksy
111+
h(Example, { name: "api/components_useState"})
112+
```
113+

packages/overmind-website/api/log.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/overmind-website/api/reaction.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/overmind-website/demos.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
{
33
"title": "Simple app",
44
"sandboxes": {
5-
"react":
6-
"https://codesandbox.io/embed/qv1n35yrlj?fontsize=12&codemirror=1&module=%2Fsrc%2FPosts.jsx",
7-
"react_ts":
8-
"https://codesandbox.io/embed/0yz8k00orv?fontsize=12&codemirror=1&module=%2Fsrc%2FPosts.tsx",
9-
"angular":
10-
"https://stackblitz.com/edit/overmind-demo1-angular-next?embed=1&file=src/app/app.component.html",
11-
"vue":
12-
"https://codesandbox.io/embed/k094r7rmyr?fontsize=12&codemirror=1&module=%2Fsrc%2FPosts.vue"
5+
"components": "https://codesandbox.io/embed/qv1n35yrlj?fontsize=12&codemirror=1&module=%2Fsrc%2FPosts.jsx",
6+
"react": "https://codesandbox.io/embed/qv1n35yrlj?fontsize=12&codemirror=1&module=%2Fsrc%2FPosts.jsx",
7+
"react_ts": "https://codesandbox.io/embed/0yz8k00orv?fontsize=12&codemirror=1&module=%2Fsrc%2FPosts.tsx",
8+
"angular": "https://stackblitz.com/edit/overmind-demo1-angular-next?embed=1&file=src/app/app.component.html",
9+
"vue": "https://codesandbox.io/embed/k094r7rmyr?fontsize=12&codemirror=1&module=%2Fsrc%2FPosts.vue"
1310
}
1411
}
1512
]

packages/overmind-website/examples/api/app_options_devtools.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { tsAppIndex } from '../templates'
2-
3-
export default (ts, view) =>
1+
export default (ts) =>
42
ts
53
? [
64
{

0 commit comments

Comments
 (0)