Skip to content

Commit 6fb77b7

Browse files
authored
docs: update with latest fixes (MithrilJS#2116)
1 parent b9f65c2 commit 6fb77b7

28 files changed

+269
-82
lines changed

Diff for: docs/animation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ Note that the `onbeforeremove` hook only fires on the element that loses its `pa
102102

103103
When creating animations, it's recommended that you only use the `opacity` and `transform` CSS rules, since these can be hardware-accelerated by modern browsers and yield better performance than animating `top`, `left`, `width`, and `height`.
104104

105-
It's also recommended that you avoid the `box-shadow` rule and selectors like `:nth-child`, since these are also resource intensive options. If you want to animate a `box-shadow`, consider [putting the `box-shadow` rule on a pseudo element, and animate that element's opacity instead](http://tobiasahlin.com/blog/how-to-animate-box-shadow/). Other things that can be expensive include large or dynamically scaled images and overlapping elements with different `position` values (e.g. an absolute postioned element over a fixed element).
105+
It's also recommended that you avoid the `box-shadow` rule and selectors like `:nth-child`, since these are also resource intensive options. If you want to animate a `box-shadow`, consider [putting the `box-shadow` rule on a pseudo element, and animate that element's opacity instead](http://tobiasahlin.com/blog/how-to-animate-box-shadow/). Other things that can be expensive include large or dynamically scaled images and overlapping elements with different `position` values (e.g. an absolute positioned element over a fixed element).

Diff for: docs/code-of-conduct.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
5555
## Enforcement
5656

5757
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58-
reported by contacting the project team at [[email protected]](mailto:[email protected]?subject=Mithril Code of Conduct). All
58+
reported by contacting the project team at [[email protected]](mailto:[email protected]?subject=Mithril%20Code%20of%20Conduct). All
5959
complaints will be reviewed and investigated and will result in a response that
6060
is deemed necessary and appropriate to the circumstances. The project team is
6161
obligated to maintain confidentiality with regard to the reporter of an incident.

Diff for: docs/components.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ var ComponentWithHooks = {
6161
oncreate: function(vnode) {
6262
console.log("DOM created")
6363
},
64+
onbeforeupdate: function(vnode, old) {
65+
return true
66+
},
6467
onupdate: function(vnode) {
6568
console.log("DOM updated")
6669
},
@@ -74,9 +77,6 @@ var ComponentWithHooks = {
7477
onremove: function(vnode) {
7578
console.log("removing DOM element")
7679
},
77-
onbeforeupdate: function(vnode, old) {
78-
return true
79-
},
8080
view: function(vnode) {
8181
return "hello"
8282
}
@@ -177,7 +177,7 @@ They can be consumed in the same way regular components can.
177177
m.render(document.body, m(closureComponent))
178178

179179
// EXAMPLE: via m.mount
180-
m.mount(document.body, closuresComponent)
180+
m.mount(document.body, closureComponent)
181181

182182
// EXAMPLE: via m.route
183183
m.route(document.body, "/", {

Diff for: docs/contributing.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ To send a pull request:
2222

2323
- fork the repo (button at the top right in GitHub)
2424
- clone the forked repo to your computer (green button in GitHub)
25+
- Switch to the `next` branch (run `git checkout next`)
2526
- create a feature branch (run `git checkout -b the-feature-branch-name`)
2627
- make your changes
27-
- run the tests (run `npm t`)
28+
- run the tests (run `npm test`)
29+
- push your changes to your fork
2830
- submit a pull request (go to the pull requests tab in GitHub, click the green button and select your feature branch)
2931

3032

Diff for: docs/css.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Nowadays there are [a lot of CSS-in-JS libraries with various degrees of robustn
8787

8888
The main problem with many of these libraries is that even though they require a non-trivial amount of transpiler tooling and configuration, they also require sacrificing code readability in order to work, e.g. `<a class={classnames(styles.button, styles.danger)}></a>` vs `<a class="button danger"></a>` (or `m("a.button.danger")` if we're using hyperscript).
8989

90-
Often sacrifices also need to be made at time of debugging, when mapping rendered CSS class names back to their source. Often all you get in browser developer tools is a class like `button_fvp6zc2gdj35evhsl73ffzq_0 danger_fgdl0s2a5fmle5g56rbuax71_0` with useless source maps (or worse, entirely criptic class names).
90+
Often sacrifices also need to be made at time of debugging, when mapping rendered CSS class names back to their source. Often all you get in browser developer tools is a class like `button_fvp6zc2gdj35evhsl73ffzq_0 danger_fgdl0s2a5fmle5g56rbuax71_0` with useless source maps (or worse, entirely cryptic class names).
9191

9292
Another common issue is lack of support for less basic CSS features such as `@keyframes` and `@font-face`.
9393

Diff for: docs/generate.js

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ try {fs.mkdirSync("./dist/archive/v" + version)} catch (e) {/* ignore */}
1212
var guides = fs.readFileSync("docs/nav-guides.md", "utf-8")
1313
var methods = fs.readFileSync("docs/nav-methods.md", "utf-8")
1414

15-
var index = fs.readFileSync("docs/index.md", "utf-8")
16-
fs.writeFileSync("README.md", index.replace(/(\]\()(.+?)\.md(\))/g, "$1http://mithril.js.org/$2.html$3"), "utf-8")
17-
1815
generate("docs")
1916

2017
function generate(pathname) {

Diff for: docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ var Splash = {
225225

226226
As you can see, this component simply renders a link to `#!/hello`. The `#!` part is known as a hashbang, and it's a common convention used in Single Page Applications to indicate that the stuff after it (the `/hello` part) is a route path.
227227

228-
Now that we going to have more than one screen, we use `m.route` instead of `m.mount`.
228+
Now that we're going to have more than one screen, we use `m.route` instead of `m.mount`.
229229

230230
```javascript
231231
m.route(root, "/splash", {

Diff for: docs/installation.md

+59-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- [CDN](#cdn)
44
- [NPM](#npm)
5+
- [Quick start with Webpack](#quick-start-with-webpack)
6+
- [TypeScript](#typescript)
57

68
### CDN
79

@@ -15,30 +17,57 @@ If you're new to Javascript or just want a very simple setup to get your feet we
1517

1618
### NPM
1719

18-
#### Quick start with Webpack
20+
```bash
21+
$> npm install mithril --save
22+
```
23+
24+
---
1925

26+
### Quick start with Webpack
27+
28+
1. Initialize the directory as an npm package
2029
```bash
21-
# 1) install
22-
npm install mithril --save
30+
$> npm init --yes
31+
```
2332

24-
npm install webpack --save
33+
2. install required tools
34+
```bash
35+
$> npm install mithril --save
36+
$> npm install webpack webpack-cli --save-dev
37+
```
2538

26-
# 2) add this line into the scripts section in package.json
27-
# "scripts": {
28-
# "start": "webpack src/index.js bin/app.js --watch"
29-
# }
39+
3. Add a "start" entry to the scripts section in `package.json`.
40+
```js
41+
{
42+
// ...
43+
"scripts": {
44+
"start": "webpack src/index.js --output bin/app.js -d --watch"
45+
}
46+
}
47+
```
3048

31-
# 3) create an `src/index.js` file
49+
3. Create `src/index.js`
50+
```js
51+
import m from "mithril";
3252

33-
# 4) create an `index.html` file containing `<script src="bin/app.js"></script>`
53+
m.render(document.body, "hello world");
54+
```
3455

35-
# 5) run bundler
36-
npm start
56+
4. create `index.html`
57+
```html
58+
<!DOCTYPE html>
59+
<body>
60+
<script src="bin/app.js"></script>
61+
</body>
62+
```
3763

38-
# 6) open `index.html` in the (default) browser
39-
open index.html
64+
5. run bundler
65+
```bash
66+
$> npm start
4067
```
4168

69+
6. open `index.html` in a browser
70+
4271
#### Step by step
4372

4473
For production-level projects, the recommended way of installing Mithril is to use NPM.
@@ -78,7 +107,7 @@ Most browser today do not natively support modularization systems (CommonJS or E
78107
A popular way for creating a bundle is to setup an NPM script for [Webpack](https://webpack.js.org/). To install Webpack, run this from the command line:
79108

80109
```bash
81-
npm install webpack --save-dev
110+
npm install webpack webpack-cli --save-dev
82111
```
83112

84113
Open the `package.json` that you created earlier, and add an entry to the `scripts` section:
@@ -87,7 +116,7 @@ Open the `package.json` that you created earlier, and add an entry to the `scrip
87116
{
88117
"name": "my-project",
89118
"scripts": {
90-
"start": "webpack src/index.js bin/app.js -d --watch"
119+
"start": "webpack src/index.js --output bin/app.js -d --watch"
91120
}
92121
}
93122
```
@@ -149,8 +178,8 @@ If you open bin/app.js, you'll notice that the Webpack bundle is not minified, s
149178
{
150179
"name": "my-project",
151180
"scripts": {
152-
"start": "webpack src/index.js bin/app.js -d --watch",
153-
"build": "webpack src/index.js bin/app.js -p",
181+
"start": "webpack src/index.js --output bin/app.js -d --watch",
182+
"build": "webpack src/index.js --output bin/app.js -p",
154183
}
155184
}
156185
```
@@ -231,3 +260,15 @@ If you don't have the ability to run a bundler script due to company security po
231260
// if a CommonJS environment is not detected, Mithril will be created in the global scope
232261
m.render(document.body, "hello world")
233262
```
263+
264+
---
265+
266+
### TypeScript
267+
268+
TypeScript type definitions are available from DefinitelyTyped. They can be installed with:
269+
270+
```bash
271+
$> npm install @types/mithril --save-dev
272+
```
273+
274+
For example usage, to file issues or to discuss TypeScript related topics visit: https://github.com/MithrilJS/mithril.d.ts

Diff for: docs/integrating-libs.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Integrating with Other Libraries
2+
Integration with third party libraries or vanilla javascript code can be achieved via [lifecycle methods](lifecycle-methods.md).
3+
4+
- [Usage](#usage)
5+
6+
### Usage
7+
```javascript
8+
var FullCalendar = {
9+
10+
oncreate: function (vnode) {
11+
console.log('FullCalendar::oncreate')
12+
$(vnode.dom).fullCalendar({
13+
// put your initial options and callbacks here
14+
})
15+
16+
Object.assign(vnode.attrs.parentState, {fullCalendarEl: vnode.dom})
17+
},
18+
19+
// Consider that the lib will modify this parent element in the DOM (e.g. add dependent class attribute and values).
20+
// As long as you return the same view results here, mithril will not
21+
// overwrite the actual DOM because it's always comparing old and new VDOM
22+
// before applying DOM updates.
23+
view: function (vnode) {
24+
return m('div')
25+
},
26+
27+
onbeforeremove: function (vnode) {
28+
// Run any destroy / cleanup methods here.
29+
//E.g. $(vnode.state.fullCalendarEl).fullCalendar('destroy')
30+
}
31+
}
32+
33+
m.mount(document.body, {
34+
view: function (vnode) {
35+
return [
36+
m('h1', 'Calendar'),
37+
m(FullCalendar, {parentState: vnode.state}),
38+
m('button', {onclick: prev}, 'Mithril Button -'),
39+
m('button', {onclick: next}, 'Mithril Button +')
40+
41+
]
42+
43+
function next() {
44+
$(vnode.state.fullCalendarEl).fullCalendar('next')
45+
}
46+
47+
function prev() {
48+
$(vnode.state.fullCalendarEl).fullCalendar('prev')
49+
}
50+
51+
}
52+
53+
})
54+
55+
```
56+
57+
Running example [flems: FullCalendar](https://fl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BVeoAJkGgBOfBjoO1bJqykAZrmhaUrSx6AEdrE7CRat4er1ClJqdrQNqOroVwTHez7eriU7P10YNxF0cGPt4CRbvqB68Cepa8E1KkIu+36tua3aQZcVIQjxl4oYSZI4YgBHcYgtHpokWbMYQUoNNKIA)

Diff for: docs/jsx.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ npm install babel-core babel-loader babel-preset-es2015 babel-plugin-transform-r
9898

9999
Create a `.babelrc` file:
100100

101-
```
101+
```json
102102
{
103103
"presets": ["es2015"],
104104
"plugins": [
@@ -121,7 +121,7 @@ module.exports = {
121121
filename: 'app.js',
122122
},
123123
module: {
124-
loaders: [{
124+
rules: [{
125125
test: /\.js$/,
126126
exclude: /node_modules/,
127127
loader: 'babel-loader'
@@ -130,6 +130,8 @@ module.exports = {
130130
}
131131
```
132132

133+
For those familiar with Webpack already, please note that adding the Babel options to the `babel-loader` section of your `webpack.config.js` will throw an error, so you need to include them in the separate `.babelrc` file.
134+
133135
This configuration assumes the source code file for the application entry point is in `src/index.js`, and this will output the bundle to `bin/app.js`.
134136

135137
To run the bundler, setup an npm script. Open `package.json` and add this entry under `"scripts"`:
@@ -189,7 +191,7 @@ JSX is useful for teams where HTML is primarily written by someone without Javas
189191
Hyperscript is the compiled representation of JSX. It's designed to be readable and can also be used as-is, instead of JSX (as is done in most of the documentation). Hyperscript tends to be terser than JSX for a couple of reasons:
190192

191193
- it does not require repeating the tag name in closing tags (e.g. `m("div")` vs `<div></div>`)
192-
- static attributes can be written using CSS selector syntax (i.e. `m("a.button")` vs `<div class="button"></div>`
194+
- static attributes can be written using CSS selector syntax (i.e. `m("a.button")` vs `<a class="button"></a>`)
193195

194196
In addition, since hyperscript is plain Javascript, it's often more natural to indent than JSX:
195197

Diff for: docs/keys.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ m("div", [
180180

181181
#### Avoid passing model data directly to components if the model uses `key` as a data property
182182

183-
The `key` property may appear in your data model in a way that conflicts with Mithril's key logic. For example, a component may represent an entity whose `key` property is expected to change over time. This can lead to components receiving the wrong data, re-initialise, or change positions unexpectedly. If your data model uses the `key` property, make sure to wrap the data such that Mithril doesn't misinterpret it as a rendering instruction:
183+
The `key` property may appear in your data model in a way that conflicts with Mithril's key logic. For example, a component may represent an entity whose `key` property is expected to change over time. This can lead to components receiving the wrong data, re-initialize, or change positions unexpectedly. If your data model uses the `key` property, make sure to wrap the data such that Mithril doesn't misinterpret it as a rendering instruction:
184184

185185
```javascript
186186
// Data model

Diff for: docs/layout.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ <h1><img src="logo.svg"> Mithril <small>[version]</small></h1>
1616
<a href="index.html">Guide</a>
1717
<a href="api.html">API</a>
1818
<a href="https://gitter.im/MithrilJS/mithril.js">Chat</a>
19-
<a href="https://github.com/MithrilJS/mithril.js">Github</a>
19+
<a href="https://github.com/MithrilJS/mithril.js">GitHub</a>
2020
</nav>
2121
</section>
2222
</header>

Diff for: docs/learning-mithril.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Learning Mithril
2+
3+
Links to Mithril learning content:
4+
5+
- [Mithril 0-60](https://scrimba.com/playlist/playlist-34)

Diff for: docs/mount.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ m.mount(element, {view: function () {return m(Component, attrs)}})
3535

3636
### Signature
3737

38-
`m.mount(element, component)`
38+
`m.mount(element, Component)`
3939

4040
Argument | Type | Required | Description
4141
----------- | -------------------- | -------- | ---
4242
`element` | `Element` | Yes | A DOM element that will be the parent node to the subtree
43-
`component` | `Component|null` | Yes | The [component](components.md) to be rendered. `null` unmounts the tree and cleans up internal state.
43+
`Component` | `Component|null` | Yes | The [component](components.md) to be rendered. `null` unmounts the tree and cleans up internal state.
4444
**returns** | | | Returns nothing
4545

4646
[How to read signatures](signatures.md)
@@ -49,7 +49,9 @@ Argument | Type | Required | Description
4949

5050
### How it works
5151

52-
Similar to [`m.render()`](render.md), the `m.mount()` method takes a component and mounts a corresponding DOM tree into `element`. If `element` already has a DOM tree mounted via a previous `m.mount()` call, the component is diffed against the previous vnode tree and the existing DOM tree is modified only where needed to reflect the changes. Unchanged DOM nodes are not touched at all.
52+
`m.mount(element, Component)`, when called renders the component into the element and subscribe the `(element, Component)` pair to the redraw subsystem. That tree will be re-rendered when [manual](redraw.md) or [automatic](autoredraw.md) redraws are triggered.
53+
54+
On redraw, the new vDOM tree is compared (or "diffed") with the old one, and the existing DOM tree is modified only where needed to reflect the changes. Unchanged DOM nodes are not touched at all.
5355

5456
#### Replace a component
5557

@@ -73,7 +75,7 @@ In contrast, traversing a javascript data structure has a much more predictable
7375

7476
### Differences from m.render
7577

76-
A component rendered via `m.mount` automatically auto-redraws in response to view events, `m.redraw()` calls or `m.request()` calls. Vnodes rendered via `m.render()` do not.
78+
A component rendered via `m.mount` [automatically redraws](autoredraw.md) in response to view events, `m.redraw()` calls or `m.request()` calls. Vnodes rendered via `m.render()` do not.
7779

7880
`m.mount()` is suitable for application developers integrating Mithril widgets into existing codebases where routing is handled by another library or framework, while still enjoying Mithril's auto-redrawing facilities.
7981

Diff for: docs/nav-guides.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- [Animation](animation.md)
1010
- [Testing](testing.md)
1111
- [Examples](examples.md)
12+
- [Integrating with Other Libraries](integrating-libs.md)
13+
- [Learning Mithril](learning-mithril.md)
1214
- Key concepts
1315
- [Vnodes](vnodes.md)
1416
- [Components](components.md)

Diff for: docs/promise.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ This example also illustrates another benefit of smaller functions: we reused th
302302

303303
### Why not callbacks
304304

305-
Callbacks are another mechanism for working with asynchrounous computations, and are indeed more adequate to use if an asynchronous computation may occur more than one time (for example, an `onscroll` event handler).
305+
Callbacks are another mechanism for working with asynchronous computations, and are indeed more adequate to use if an asynchronous computation may occur more than one time (for example, an `onscroll` event handler).
306306

307307
However, for asynchronous computations that only occur once in response to an action, promises can be refactored more effectively, reducing code smells known as pyramids of doom (deeply nested series of callbacks with unmanaged state being used across several closure levels).
308308

Diff for: docs/redraw.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
Updates the DOM after a change in the application data layer.
1212

13-
You DON'T need to call it if data is modified within the execution context of an event handler defined in a Mithril view, or after request completion when using `m.request`/`m.jsonp`.
13+
You DON'T need to call it if data is modified within the execution context of an event handler defined in a Mithril view, or after request completion when using `m.request`/`m.jsonp`. The [autoredraw](autoredraw.md) system, which is built on top of `m.redraw()` will take care of it.
1414

1515
You DO need to call it in `setTimeout`/`setInterval`/`requestAnimationFrame` callbacks, or callbacks from 3rd party libraries.
1616

0 commit comments

Comments
 (0)