pinafore/routes/_components/Layout.html

61 lines
1.4 KiB
HTML

<:Window bind:innerHeight='innerHeight'/>
<Nav page={{page}} />
<div class="container" on:scroll="onScroll(event)" ref:node>
<main>
<slot></slot>
</main>
</div>
<script>
import Nav from './Nav.html';
import { virtualListStore } from '../_utils/virtualListStore'
import debounce from 'lodash/debounce'
import throttle from 'lodash/throttle'
const SCROLL_EVENT_DELAY = 300
const RESIZE_EVENT_DELAY = 700
export default {
oncreate() {
this.observe('innerHeight', debounce(() => {
// respond to window resize events
this.store.set({
offsetHeight: this.refs.node.offsetHeight
})
}, RESIZE_EVENT_DELAY))
this.store.set({
scrollTop: this.refs.node.scrollTop,
scrollHeight: this.refs.node.scrollHeight,
offsetHeight: this.refs.node.offsetHeight
})
},
components: {
Nav
},
store: () => virtualListStore,
events: {
scroll(node, callback) {
const onScroll = throttle(callback, SCROLL_EVENT_DELAY, {
leading: true,
trailing: true
})
node.addEventListener('scroll', onScroll);
return {
teardown() {
node.removeEventListener('scroll', onScroll);
}
};
}
},
methods: {
onScroll(event) {
this.store.set({
scrollTop: event.target.scrollTop,
scrollHeight: event.target.scrollHeight
})
}
}
};
</script>