@@ -13,10 +13,11 @@ import {
1313/**
1414 * Initializes a ScrollView $vm into tracking.
1515 *
16+ * @param {Object } state - ScrollView tracking state.
1617 * @param {Object } ScrollView - The ScrollView $vm
1718 * @returns {Object } Component locations and ScrollViews being tracked.
1819 */
19- export const initializeScrollview = ( { _uid, $children } : ScrollviewComponent ) => {
20+ export const initializeScrollview = ( state : State , { _uid, $children } : ScrollviewComponent ) => {
2021 return $children . reduce ( ( data , { $el, $vnode : { key : child } } ) => {
2122 const position = getElPosition ( $el )
2223 data . locations . push ( {
@@ -25,10 +26,26 @@ export const initializeScrollview = ({ _uid, $children }: ScrollviewComponent) =
2526 component : child
2627 } )
2728 data . tracking [ child ] = false
29+ setLastComponent ( state , { key : child , position } )
2830 return data
2931 } , { locations : [ ] , tracking : { } } )
3032}
3133
34+ /**
35+ * Stores data for the last component on the page.
36+ *
37+ * @param {Object } State - ScrollView tracking state.
38+ * @param {Object } ComponentData - An object containing the key and position of a component.
39+ * @returns {Object } null
40+ */
41+ export const setLastComponent = ( state : State , { key, position } : { key : ComponentKey , position : ComponentPosition } ) => {
42+ if ( ! state . lastComponent . key || ! state . lastComponent . position ) {
43+ state . lastComponent = { key, position }
44+ } else if ( state . lastComponent . position . top < position . top ) {
45+ state . lastComponent = { key, position }
46+ }
47+ }
48+
3249/**
3350 * Attaches the scroll listener.
3451 *
0 commit comments