Skip to content

Commit e9775a0

Browse files
committed
cleanup api
1 parent a0237c7 commit e9775a0

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

API.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
| Method | Description |
66
| :--- | :--- |
7-
| `add(Class, root?)` | Register a class as a new custom-tag and provide options for it. |
7+
| `add(Class)` | Register a class as a new custom-tag and provide options for it. |
8+
| `init(root?)` | Initialize all components (optionally starating at a root node in the DOM). This is called automatically when an <App></App> component is added. |
89
| `escape(String)` | Escapes HTML characters from a string (based on [he][3]). |
910
| `sanitize(Object)` | Escapes all the strings found in an object literal. |
1011
| `match(Node, Selector)` | Match the given node against a selector or any matching parent of the given node. This is useful when trying to locate a node from the actual node that was interacted with. |

index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class Tonic {
2929
return el.matches(s) ? el : el.closest(s)
3030
}
3131

32-
static add (c, root) {
32+
static add (c, isReady) {
3333
c.prototype._props = Object.getOwnPropertyNames(c.prototype)
34-
if (!c.name || c.name.length === 1) throw Error('Mangling detected, see guide. https://github.com/hxoht/tonic/blob/master/HELP.md.')
34+
if (!c.name || c.name.length === 1) throw Error('Mangling detected. https://github.com/heapwolf/tonic/blob/master/HELP.md')
3535

3636
const name = Tonic._splitName(c.name)
3737
Tonic.registry[name.toUpperCase()] = Tonic[c.name] = c
@@ -45,23 +45,22 @@ class Tonic {
4545
Tonic.styleNode = document.head.appendChild(styleTag)
4646
}
4747

48-
if (!root) return
49-
Tonic._constructTags(root)
48+
if (isReady || c.name === 'App') Tonic.init(document.firstElementChild)
5049
}
5150

52-
static _constructTags (node, states = {}) { /* eslint-disable no-new */
51+
static init (node = document.firstElementChild, states = {}) {
5352
node = node.firstElementChild
5453

5554
while (node) {
5655
const tagName = node.tagName
5756

58-
if (Tonic.tags.includes(tagName)) {
57+
if (Tonic.tags.includes(tagName)) { /* eslint-disable no-new */
5958
new Tonic.registry[tagName]({ node, state: states[node.id] })
6059
node = node.nextElementSibling
6160
continue
6261
}
6362

64-
Tonic._constructTags(node, states)
63+
Tonic.init(node, states)
6564
node = node.nextElementSibling
6665
}
6766
}
@@ -108,7 +107,7 @@ class Tonic {
108107
const oldProps = JSON.parse(JSON.stringify(this.props))
109108
this.props = Tonic.sanitize(typeof o === 'function' ? o(this.props) : o)
110109
if (!this.root) throw new Error('.reRender called on destroyed component, see guide.')
111-
Tonic._constructTags(this.root, this._setContent(this.root, this.render()))
110+
Tonic.init(this.root, this._setContent(this.root, this.render()))
112111
this.updated && this.updated(oldProps)
113112
}
114113

@@ -205,7 +204,7 @@ class Tonic {
205204
this.children = [...this.root.childNodes].map(node => node.cloneNode(true))
206205
this.children.__children__ = true
207206
this._setContent(this.root, this.render())
208-
Tonic._constructTags(this.root)
207+
Tonic.init(this.root)
209208
const style = this.stylesheet && this.stylesheet()
210209

211210
if (style && !Tonic.registry[this.root.tagName].styled) {

test/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ test('compose sugar (this.children)', t => {
338338
`
339339

340340
Tonic.add(ComponentG)
341-
Tonic.add(ComponentH, document.body)
341+
Tonic.add(ComponentH)
342+
Tonic.init(document.body)
342343

343344
const g = document.querySelector('component-g')
344345
const children = g.querySelectorAll('.child')
@@ -388,7 +389,10 @@ test('check that composed elements use (and re-use) their initial innerHTML corr
388389

389390
Tonic.add(ComponentJ)
390391
Tonic.add(ComponentK)
391-
Tonic.add(ComponentI, document.body)
392+
Tonic.add(ComponentI)
393+
Tonic.init()
394+
395+
t.comment('Uses init() instead of <app>')
392396

393397
const i = document.querySelector('component-i')
394398
const kTags = i.getElementsByTagName('component-k')
@@ -463,7 +467,7 @@ test('mixed order declaration', t => {
463467
Tonic.add(ComponentA)
464468
Tonic.add(ComponentC)
465469
Tonic.add(ComponentB)
466-
Tonic.add(App, document.body)
470+
Tonic.add(App)
467471

468472
{
469473
const div = document.querySelector('.app')

0 commit comments

Comments
 (0)