pinafore/routes/_components/VirtualListItem.html
2018-01-17 19:16:04 -08:00

68 lines
2 KiB
HTML

<div class="virtual-list-item {{shown ? 'shown' : ''}}"
virtual-list-key="{{key}}"
ref:node
style="transform: translate3d(0, {{offset}}px, 0);"
>
<:Component {component} virtualProps="{{props}}" />
</div>
<style>
.virtual-list-item {
position: absolute;
top: 0;
opacity: 0;
pointer-events: none;
/* will-change: transform; */ /* causes jank in mobile Firefox */
}
.shown {
opacity: 1;
pointer-events: auto;
}
</style>
<script>
import { virtualListStore } from '../_utils/virtualListStore'
import { AsyncLayout } from '../_utils/AsyncLayout'
import { mark, stop } from '../_utils/marks'
let updateItemHeights = {}
let promise = Promise.resolve()
const asyncLayout = new AsyncLayout(node => node.getAttribute('virtual-list-key'))
export default {
oncreate() {
let key = this.get('key')
// TODO: implement batchUpdate
// TODO: fix resize on media
asyncLayout.observe(key, this.refs.node, (rect) => {
updateItemHeights[key] = rect.height
promise = promise.then(() => {
// update all item heights in one microtask batch for better perf
let updatedKeys = Object.keys(updateItemHeights)
if (!updatedKeys.length) {
return
}
mark('batch update VirtualListItem')
// batch all updates to itemHeights for better perf
let itemHeights = this.store.get('itemHeights')
for (key of updatedKeys) {
itemHeights[key] = updateItemHeights[key]
}
this.store.set({
itemHeights: itemHeights
})
updateItemHeights = {}
stop('batch update VirtualListItem')
})
})
},
ondestroy() {
let key = this.get('key')
asyncLayout.unobserve(key, this.refs.node)
delete updateItemHeights[key]
},
store: () => virtualListStore,
computed: {
'shown': ($itemHeights, key) => $itemHeights[key] > 0
}
}
</script>