Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract html into separate module #103

Merged
merged 1 commit into from
Jun 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Let's create an input box that changes the content of a textbox in real time.
First we import `choo` and create a new instance:
```js
const choo = require('choo')
const html = require('choo/html')
const app = choo()
```

Expand All @@ -135,7 +136,7 @@ Then we create a new view. It has an `h1` tag which displays the current title,
and an `<input>` field which sends the current value of the text box on every
input:
```js
const mainView = (params, state, send) => choo.view`
const mainView = (params, state, send) => html`
<main>
<h1>${state.title}</h1>
<input
Expand Down Expand Up @@ -165,6 +166,7 @@ document.body.appendChild(tree)
And all together now:
```js
const choo = require('choo')
const html = require('choo/html')
const app = choo()

app.model({
Expand All @@ -174,7 +176,7 @@ app.model({
}
})

const mainView = (params, state, send) => choo.view`
const mainView = (params, state, send) => html`
<main>
<h1>${state.title}</h1>
<input
Expand Down Expand Up @@ -337,7 +339,7 @@ Views are also passed the `send` function, which they can use to dispatch action

```javascript
const view = (params, state, send) => {
return choo.view`
return html`
<div>
<h1>Total todos: ${state.todos.length}</h1>
<button onclick=${(e) => send('add', { payload: {title: 'demo'})}>Add</button>
Expand Down Expand Up @@ -477,10 +479,11 @@ links they comprise most of what can be done on web pages.
```js
const choo = require('choo')
const http = require('choo/http')
const html = require('choo/html')
const app = choo()

function view (params, state, send) {
return choo.view`
return html`
<form onsubmit=${onSubmit}>
<fieldset>
<label>username</label>
Expand Down Expand Up @@ -520,7 +523,7 @@ app.start()
If you want a form element to be selected when it's loaded, add the
[`autofocus`][html-input] property.
```js
const view = choo.view`
const view = html`
<form>
<input type="text" autofocus>
</form>
Expand All @@ -534,7 +537,7 @@ is clicked, the click event is caught, and the value of `href` is passed into
the router causing a state change. If you want to disable this behavior, set
`app.start({ href: false })`.
```js
const nav = choo.view`
const nav = html`
<a href="/">home</a>
<a href="/first-link">first link</a>
<a href="/second-link">second link</a>
Expand Down Expand Up @@ -573,10 +576,11 @@ In order to make our `choo` app call `app.start()` in the browser and be
`require()`-able in Node, we check if [`module.parent`][module-parent] exists:
```js
const choo = require('choo')
const html = require('choo/html')
const app = choo()

app.router((route) => [
route('/', (params, state, send) => choo.view`
route('/', (params, state, send) => html`
<h1>${state.message}</h1>
`)
])
Expand All @@ -602,10 +606,11 @@ dehydrated DOM nodes to make them dynamic, rather than a new DOM tree and
attaching it to the DOM.
```js
const choo = require('choo')
const html = require('choo/html')
const app = choo()

app.router((route) => [
route('/', (params, state, send) => choo.view`
route('/', (params, state, send) => html`
<h1 id="app-root">${state.message}</h1>
`)
])
Expand Down Expand Up @@ -641,11 +646,6 @@ arguments:
a signature of `(action, state, send)` where `send` is a reference to
`app.send()`

### choo.view\`html\`
Tagged template string HTML builder. See
[`yo-yo`](https://github.com/maxogden/yo-yo) for full documentation. Views
should be passed to `app.router()`

### app.router(params, state, send)
Creates a new router. See
[`sheet-router`](https://github.com/yoshuawuyts/sheet-router) for full
Expand Down Expand Up @@ -676,6 +676,11 @@ following values:
changes (eg `localhost/#posts/123`). Enabling this option automatically
disables `opts.history` and `opts.href`.

### choo/html\`html\`
Tagged template string HTML builder. See
[`yo-yo`](https://github.com/maxogden/yo-yo) for full documentation. Views
should be passed to `app.router()`

## Errors
### Could not find DOM node (#id) to update
This means that a re-render of the DOM was triggered before the first render
Expand Down
4 changes: 2 additions & 2 deletions examples/http/views/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const choo = require('../../../')
const html = require('../../../html')

module.exports = function (params, state, send) {
const error = state.app.error[0]
const title = state.api.title
return choo.view`
return html`
<section>
<h1>${title}</h1>
<h2>Latest error: ${error}</h2>
Expand Down
6 changes: 3 additions & 3 deletions examples/mailbox/elements/email-list.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const dateformat = require('dateformat')
const choo = require('../../../')
const html = require('../../../html')

module.exports = function (params, state, send) {
const mailbox = params.mailbox
const messages = state[mailbox].messages
return choo.view`
return html`
<div>
<div class="db cf w-100">
<div class="fl mb3 w-25 mt0 b">Date</th>
Expand All @@ -20,7 +20,7 @@ module.exports = function (params, state, send) {
}

function createMessage (message, mailbox) {
return choo.view`
return html`
<div class="db cf w-100">
<a href="${'/' + mailbox + '/' + message.id}">
<div class="fl mb3 w-25 f6 link">
Expand Down
6 changes: 3 additions & 3 deletions examples/mailbox/elements/email.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const choo = require('../../../')
const html = require('../../../html')

module.exports = function (params, state, send) {
const mailbox = params.mailbox
Expand All @@ -8,15 +8,15 @@ module.exports = function (params, state, send) {
return String(msg.id) === message
})[0]

return choo.view`
return html`
<div>
${email ? createEmail(email) : 'error: no email found'}
</div
`
}

function createEmail (message) {
return choo.view`
return html`
<div class="mail">
<dl>
<dt>From</dt>
Expand Down
4 changes: 2 additions & 2 deletions examples/mailbox/elements/empty-mailbox.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const choo = require('../../../')
const html = require('../../../html')

module.exports = function (params, state, send) {
return choo.view`
return html`
<section>
<p>Select a mailbox</p>
</section>
Expand Down
12 changes: 6 additions & 6 deletions examples/mailbox/elements/mailbox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const dateformat = require('dateformat')
const choo = require('../../../')
const html = require('../../../html')

module.exports = function () {
return function (params, state, send) {
Expand All @@ -12,7 +12,7 @@ module.exports = function () {
return String(msg.id) === message
})[0]

return choo.view`
return html`
<section class="fl mt4 w-80 db">
<div>
${createHeader()}
Expand All @@ -26,7 +26,7 @@ module.exports = function () {
</section>
`
} else {
return choo.view`
return html`
<section class="fl mt4 w-80 db">
${createHeader()}
${messages.map(function (msg) {
Expand All @@ -39,7 +39,7 @@ module.exports = function () {
}

function createHeader () {
return choo.view`
return html`
<div class="db cf w-100">
<div class="fl mb3 w-25 mt0 b">Date</th>
<div class="fl mb3 w-25 mt0 b">Subject</th>
Expand All @@ -50,7 +50,7 @@ function createHeader () {
}

function createMessage (message, mailbox) {
return choo.view`
return html`
<div class="db cf w-100">
<a href="${'/' + mailbox + '/' + message.id}">
<div class="fl mb3 w-25 f6 link">
Expand All @@ -65,7 +65,7 @@ function createMessage (message, mailbox) {
}

function createEmail (message) {
return choo.view`
return html`
<div class="mail">
<dl>
<dt>From</dt>
Expand Down
6 changes: 3 additions & 3 deletions examples/mailbox/elements/nav.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const choo = require('../../../')
const html = require('../../../html')

const mailboxes = [ 'inbox', 'spam', 'sent' ]

module.exports = function (params, state, send) {
return choo.view`
return html`
<aside class="fl mt4 w-20 db">
<ul>
<li>
Expand All @@ -19,7 +19,7 @@ module.exports = function (params, state, send) {
}

function createLi (mailbox, messages) {
return choo.view`
return html`
<li class="mt4 f6">
<a href="/${mailbox}">
${mailbox.charAt(0).toUpperCase() + mailbox.slice(1)}
Expand Down
4 changes: 2 additions & 2 deletions examples/mailbox/elements/pathname.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const pathname = require('pathname-match')
const choo = require('../../../')
const html = require('../../../html')

module.exports = function (params, state, send) {
const location = state.app.location
return choo.view`
return html`
<span class="fl mt4 w-100 f4 b">
URL: ${pathname(location) || '/'}
</span>
Expand Down
4 changes: 2 additions & 2 deletions examples/mailbox/views/email.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const choo = require('../../../')
const html = require('../../../html')

const emailList = require('../elements/email-list')
const pathname = require('../elements/pathname')
const email = require('../elements/email')
const nav = require('../elements/nav')

module.exports = function (params, state, send) {
return choo.view`
return html`
<main class="mw5 mw7-ns center cf">
${pathname(params, state, send)}
${nav(params, state, send)}
Expand Down
4 changes: 2 additions & 2 deletions examples/mailbox/views/empty.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const choo = require('../../../')
const html = require('../../../html')

const empty = require('../elements/empty-mailbox')
const pathname = require('../elements/pathname')
const nav = require('../elements/nav')

module.exports = function (params, state, send) {
return choo.view`
return html`
<main class="mw5 mw7-ns center cf">
${pathname(params, state, send)}
${nav(params, state, send)}
Expand Down
4 changes: 2 additions & 2 deletions examples/mailbox/views/mailbox.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const choo = require('../../../')
const html = require('../../../html')

const emailList = require('../elements/email-list')
const pathname = require('../elements/pathname')
const nav = require('../elements/nav')

module.exports = function (params, state, send) {
return choo.view`
return html`
<main class="mw5 mw7-ns center cf">
${pathname(params, state, send)}
${nav(params, state, send)}
Expand Down
4 changes: 2 additions & 2 deletions examples/server-rendering/views/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const choo = require('../../../')
const html = require('../../../html')

module.exports = function (params, state, send) {
const serverMessage = state.message.server
Expand All @@ -8,7 +8,7 @@ module.exports = function (params, state, send) {
assert.equal(typeof serverMessage, 'string', 'server should be a string')
assert.equal(typeof clientMessage, 'string', 'client should be a string')

return choo.view`
return html`
<section id="app-root">
<h1>server message: ${serverMessage}</h1>
<h1>client message: ${clientMessage}</h1>
Expand Down
3 changes: 2 additions & 1 deletion examples/sse/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const choo = require('../../')
const html = require('../../html')

const app = choo()
app.model(createModel())
Expand All @@ -10,7 +11,7 @@ const tree = app.start()
document.body.appendChild(tree)

function mainView (params, state, send) {
return choo.view`
return html`
<div>${state.logger.msg}</div>
`
}
Expand Down
4 changes: 2 additions & 2 deletions examples/sse/views/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const choo = require('../../../')
const html = require('../../../html')

module.exports = function (params, state, send) {
const error = state.app.error[0]
const title = state.api.title
return choo.view`
return html`
<section>
<h1>${title}</h1>
<h2>Latest error: ${error}</h2>
Expand Down
3 changes: 2 additions & 1 deletion examples/title/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const choo = require('../../')
const html = require('../../html')

const app = choo()
app.model({
Expand All @@ -15,7 +16,7 @@ app.model({
})

const mainView = (params, state, send) => {
return choo.view`
return html`
<main class="app">
<h1>${state.input.title}</h1>
<label>Set the title</label>
Expand Down
1 change: 1 addition & 0 deletions html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('yo-yo')
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const assert = require('assert')
const xtend = require('xtend')
const yo = require('yo-yo')

choo.view = yo
module.exports = choo

// framework for creating sturdy web applications
Expand Down
Loading