pinafore/src/routes/_components/virtualList/VirtualListItem.html

82 wiersze
2.6 KiB
HTML

<div class="virtual-list-item list-item {shown ? 'shown' : ''}"
aria-hidden={!shown}
ref:node
style="top: {offset}px;" >
<svelte:component this={component}
virtualProps={props}
virtualIndex={index}
virtualLength={$length}
virtualKey={key}
on:recalculateHeight="doRecalculateHeight()"/>
</div>
<style>
.virtual-list-item {
position: absolute;
width: 100%;
opacity: 0;
pointer-events: none;
transition: opacity 0.2s linear;
contain: content; /* see https://www.w3.org/TR/2018/CR-css-contain-1-20181108/#valdef-contain-content */
}
.virtual-list-item.shown {
opacity: 1;
pointer-events: auto;
}
</style>
<script>
import { virtualListStore } from './virtualListStore.js'
import { registerResizeListener, unregisterResizeListener } from '../../_utils/resize.js'
import { mark, stop } from '../../_utils/marks.js'
import { requestPostAnimationFrame } from '../../_utils/requestPostAnimationFrame.js'
import { observe } from 'svelte-extras'
import { doubleRAF } from '../../_utils/doubleRAF.js'
export default {
oncreate () {
const { key } = this.get()
const node = this.refs.node
requestPostAnimationFrame(() => {
if (!node || !key) {
return
}
mark('VirtualListItem gBCR')
const rect = node.getBoundingClientRect()
stop('VirtualListItem gBCR')
// update all item heights in one batch for better perf
this.store.batchUpdateForRealm('itemHeights', key, rect.height)
})
this.doRecalculateHeight = this.doRecalculateHeight.bind(this)
registerResizeListener(this.doRecalculateHeight)
// this rAF delay is necessary in order to get the fade-in animation
// to consistently show
this.observe('shownBeforeRaf', shownBeforeRaf => {
doubleRAF(() => {
this.set({ shown: shownBeforeRaf })
})
})
},
ondestroy () {
unregisterResizeListener(this.doRecalculateHeight)
},
store: () => virtualListStore,
data: () => ({
shown: false
}),
computed: {
shownBeforeRaf: ({ $itemHeights, key }) => $itemHeights[key] > 0
},
methods: {
observe,
doRecalculateHeight () {
// Recalculate immediately because this is done on-demand, e.g.
// when clicking the "More" button on a spoiler.
const rect = this.refs.node.getBoundingClientRect()
const { key } = this.get()
const { itemHeights } = this.store.get()
itemHeights[key] = rect.height
this.store.setForRealm({ itemHeights })
}
}
}
</script>