Skip to content

Commit

Permalink
feat: finished useListTransition(useTransitionGroup)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyoki committed Dec 30, 2021
1 parent d2fb662 commit 1289cff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './useTransition';
export * from './useSwitchTransition';
export * from './useTransitionGroup';
export * from './useListTransition';
export * from './Transition';
export * from './SwitchTransition';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Fragment, useEffect, useState} from 'react';
import {Fragment, useEffect, useRef, useState} from 'react';
import {Stage} from '..';
import {insertArray} from '../helpers/insertArray';
import {setAnimationFrameTimeout} from '../helpers/setAnimationFrameTimeout';
Expand All @@ -16,11 +16,12 @@ type ItemWithKey<Item> = {
index: number;
};

export function useTransitionGroup<Item>(list: Array<Item>, timeout: number) {
export function useListTransition<Item>(list: Array<Item>, timeout: number) {
const keyRef = useRef(0);
// change list to our list form with extra information.
const initialList: Array<ItemWithState<Item>> = list.map((item, key) => ({
item,
key,
key: keyRef.current,
stage: 'enter',
}));

Expand All @@ -38,15 +39,18 @@ export function useTransitionGroup<Item>(list: Array<Item>, timeout: number) {

// 1 add new items into list state
if (newItemsWithIndex.length > 0) {
setListState((prevListState) => {
return newItemsWithIndex.reduce((prev, {item, index}, i) => {
return insertArray(prev, index, {
item,
key: prevListState.length + 1 + i,
stage: 'from',
});
}, prevListState);
});
keyRef.current++;
setListState((prevListState) =>
newItemsWithIndex.reduce(
(prev, {item, index}, i) =>
insertArray(prev, index, {
item,
key: keyRef.current,
stage: 'from',
}),
prevListState
)
);
}

// 2 enter those new items immediatly
Expand Down Expand Up @@ -86,8 +90,6 @@ export function useTransitionGroup<Item>(list: Array<Item>, timeout: number) {
);
}, timeout);
}

console.log(listState);
},
[list, listState, timeout]
);
Expand Down

0 comments on commit 1289cff

Please sign in to comment.