Skip to content

Commit

Permalink
perf: entered
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed Apr 6, 2024
1 parent a847816 commit d32059e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/hooks/useListTransition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,37 @@ export function useListTransition<Item>(list: Array<Item>, options?: ListTransit
}

// 2 enter those new items immediatly
const fromItems = listState.filter(item => item.status === 'from')
if (
newItemsWithIndex.length === 0
&& listState.some(item => item.status === 'from')
&& fromItems.length > 0
) {
setAnimationFrameTimeout(() => {
setListState(prev =>
prev.map(item => {
if (entered && item.status === 'from') {
setAnimationFrameTimeout(() => {
setListState(prev =>
prev.map(_item =>
_item.key === item.key && _item.status === 'entering'
? { ..._item, ...getState(STATUS.entered) }
: item,
),
)
}, enterTimeout)
}
return ({
...item,
...(item.status === 'from' ? getState(STATUS.entering) : {}),
})
}),
prev.map(item => ({
...item,
...(item.status === 'from' ? getState(STATUS.entering) : {}),
})),
)
})
}

if (entered && fromItems.length > 0) {
setAnimationFrameTimeout(() => {
setListState(prev => {
return prev.map(item =>
(
fromItems.some(fromItem => fromItem.key === item.key)
&& item.status === 'entering'
)
? { ...item, ...getState(STATUS.entered) }
: item,
)
},
)
}, enterTimeout)
}

// 3 leave items from list state
const subtractItemStates = listState.filter(
itemState =>
Expand Down
6 changes: 6 additions & 0 deletions src/viewTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { flushSync } from 'react-dom'
const isClient = typeof window !== 'undefined'
const isSupportViewTransition = isClient && 'startViewTransition' in document

const noop = (fn: any) => fn()

Check warning on line 6 in src/viewTransition.ts

View workflow job for this annotation

GitHub Actions / lint

'noop' is assigned a value but never used. Allowed unused vars must match /^_/u

export function viewTransition() {

}

export function startViewTransition(fn: () => void) {
if (isSupportViewTransition) {
// @ts-expect-error startViewTransition is not in the type definition
Expand Down

0 comments on commit d32059e

Please sign in to comment.