|
1 | 1 | import { useState } from 'react' |
2 | | -import { SwitchTransition } from 'transition-hooks' |
| 2 | +import { useListTransition } from 'transition-hooks' |
| 3 | +// import { startViewTransition } from 'transition-hooks/viewTransition' |
3 | 4 |
|
4 | 5 | export function App() { |
5 | | - const [show, setShow] = useState(false) |
| 6 | + const [list, setList] = useState([ |
| 7 | + { id: 1, text: '1' }, |
| 8 | + { id: 2, text: '2' }, |
| 9 | + ]) |
| 10 | + |
| 11 | + const { transitionList } = useListTransition(list, { |
| 12 | + timeout: 1000, |
| 13 | + keyExtractor: i => i.id, |
| 14 | + }) |
| 15 | + |
| 16 | + const updateLastItem = () => { |
| 17 | + const newList = [...list] |
| 18 | + newList[newList.length - 2] = { |
| 19 | + id: newList[newList.length - 2].id, |
| 20 | + text: `${newList[newList.length - 2].text}test`, |
| 21 | + } |
| 22 | + setList(newList) |
| 23 | + } |
| 24 | + |
| 25 | + const addItem = () => { |
| 26 | + setList(l => [...l, { id: l.length + 1, text: (l.length + 1).toString() }]) |
| 27 | + } |
6 | 28 |
|
7 | 29 | return ( |
8 | 30 | <div> |
9 | | - <SwitchTransition state={show}> |
10 | | - {(state, { status }) => { |
| 31 | + <div style={{ display: 'flex', gap: 4, marginBottom: 8 }}> |
| 32 | + <button onClick={addItem}>add</button> |
| 33 | + <button onClick={updateLastItem}>update last item</button> |
| 34 | + </div> |
| 35 | + <ul> |
| 36 | + {transitionList((item, { key, simpleStatus }) => { |
11 | 37 | return ( |
12 | | - <div |
| 38 | + <li |
13 | 39 | style={{ |
14 | | - transition: 'opacity 0.3s', |
15 | | - opacity: status === 'entering' || status === 'entered' ? 1 : 0, |
| 40 | + position: simpleStatus === 'exit' ? 'absolute' : 'relative', |
| 41 | + opacity: simpleStatus === 'enter' ? 1 : 0, |
| 42 | + transform: |
| 43 | + simpleStatus === 'enter' |
| 44 | + ? 'translateX(0)' |
| 45 | + : 'translateX(20px)', |
| 46 | + transition: 'all .6s', |
| 47 | + // viewTransitionName: simpleStatus === 'enter' ? `transition-list-${key}` : '', |
16 | 48 | }} |
17 | 49 | > |
18 | | - Hello Word {state ? 'true' : 'false'} |
19 | | - </div> |
| 50 | + - {item.text} |
| 51 | + </li> |
20 | 52 | ) |
21 | | - }} |
22 | | - </SwitchTransition> |
23 | | - <button onClick={() => setShow(!show)}>toggle</button> |
| 53 | + })} |
| 54 | + </ul> |
24 | 55 | </div> |
25 | 56 | ) |
26 | 57 | } |
0 commit comments