Skip to content

Commit 76f0c6d

Browse files
Merge branch 'feature/viewport-related-lifecycle-hooks'
2 parents 1602d19 + dd96003 commit 76f0c6d

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

src/component.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const Component = (name = required('name'), config = required('config')) => {
102102
}
103103

104104
this.parent = parentComponent
105-
this[symbols.wrapper] = parentEl
105+
this[symbols.holder] = parentEl
106106

107107
Object.defineProperties(this, {
108108
componentId: {
@@ -163,6 +163,37 @@ const Component = (name = required('name'), config = required('config')) => {
163163
configurable: false,
164164
})
165165

166+
Object.defineProperty(this, symbols.wrapper, {
167+
value: this[symbols.children][0],
168+
writable: false,
169+
enumerable: false,
170+
configurable: false,
171+
})
172+
173+
if (config.hooks && config.hooks.attach) {
174+
this[symbols.wrapper].node.on('inBounds', () => {
175+
this.lifecycle.state = 'attach'
176+
})
177+
}
178+
179+
if (config.hooks && config.hooks.detach) {
180+
this[symbols.wrapper].node.on('outOfBounds', (node, { previous }) => {
181+
if (previous > 0) this.lifecycle.state = 'detach'
182+
})
183+
}
184+
185+
if (config.hooks && config.hooks.enter) {
186+
this[symbols.wrapper].node.on('inViewport', () => {
187+
this.lifecycle.state = 'enter'
188+
})
189+
}
190+
191+
if (config.hooks && config.hooks.enter) {
192+
this[symbols.wrapper].node.on('outOfViewport', () => {
193+
this.lifecycle.state = 'exit'
194+
})
195+
}
196+
166197
Object.defineProperty(this, symbols.slots, {
167198
value: this[symbols.children].filter((child) => child[symbols.isSlot]),
168199
writable: false,

src/lib/lifecycle.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ import { Log } from './log.js'
1919
import { emit, privateEmit } from './hooks.js'
2020
import symbols from './symbols.js'
2121

22-
const states = ['init', 'ready', 'focus', 'unfocus', 'destroy']
22+
const states = [
23+
'init', // fired upon component instantiation
24+
'ready', // fired when component instantiated, reactivity setup done and template spawned
25+
'focus', // fired when receiving focus (can occur multiple times)
26+
'unfocus', // fired when losing focus (can occur multiple times)
27+
'destroy', // fired when component is destroyed and removed
28+
'attach', // fired when entering the viewport margin and attached to the render tree
29+
'detach', // fired when leaving the viewport margin and detached from the render tree
30+
'enter', // fired when entering the visible viewport
31+
'exit', // fired when leaving the visible viewport
32+
]
2333

2434
export default {
2535
previous: null,

src/lib/symbols.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export default {
33
cursorTagStart: Symbol('cursorTagStart'),
44
computedKeys: Symbol('computedKeys'),
55
destroy: Symbol('destroy'),
6+
holder: Symbol('holder'),
67
id: Symbol('id'),
78
identifier: Symbol('identifier'),
89
index: Symbol('index'),

src/router/router.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export const navigate = async function () {
149149
view = view({ props }, holder, this)
150150
}
151151
} else {
152-
holder = view[symbols.wrapper]
152+
holder = view[symbols.holder]
153153
}
154154

155155
this[symbols.children].push(view)
@@ -223,11 +223,11 @@ const removeView = async (route, view, transition) => {
223223
if (Array.isArray(transition)) {
224224
for (let i = 0; i < transition.length; i++) {
225225
i === transition.length - 1
226-
? await setOrAnimate(view[symbols.wrapper], transition[i])
227-
: setOrAnimate(view[symbols.wrapper], transition[i])
226+
? await setOrAnimate(view[symbols.holder], transition[i])
227+
: setOrAnimate(view[symbols.holder], transition[i])
228228
}
229229
} else {
230-
await setOrAnimate(view[symbols.wrapper], transition)
230+
await setOrAnimate(view[symbols.holder], transition)
231231
}
232232
}
233233

0 commit comments

Comments
 (0)