1
1
import { StoryObj } from "@storybook/react" ;
2
2
import { useEffect , useState } from "react" ;
3
- import { TypedEasing , useAnimation } from "../../src" ;
3
+ import { TypedEasing , TypedKeyframe , useAnimation } from "../../src" ;
4
4
5
5
export default { component : useAnimation } ;
6
6
@@ -269,23 +269,38 @@ const Bar = ({
269
269
value,
270
270
i,
271
271
height,
272
+ keep,
272
273
} : {
273
274
value : number ;
274
275
i : number ;
275
276
height : number ;
277
+ keep : boolean ;
276
278
} ) => {
279
+ const target : TypedKeyframe = {
280
+ height : `${ value } px` ,
281
+ transform : `translateY(-${ value } px)` ,
282
+ opacity : String ( 1 - i * 0.025 ) ,
283
+ } ;
277
284
const animate = useAnimation (
278
- {
279
- height : `${ value } px` ,
280
- transform : `translateY(-${ value } px)` ,
281
- opacity : String ( 1 - i * 0.025 ) ,
282
- } ,
283
- { duration : 150 , easing : "ease-out" , delay : i * 100 }
285
+ keep
286
+ ? target
287
+ : [
288
+ {
289
+ height : 0 ,
290
+ transform : "translateY(0px)" ,
291
+ opacity : target . opacity ,
292
+ } ,
293
+ target ,
294
+ ] ,
295
+ { duration : 150 , easing : "ease-out" , delay : i * 100 , fill : "both" }
284
296
) ;
285
297
286
298
useEffect ( ( ) => {
299
+ if ( keep ) {
300
+ animate . persist ( ) ;
301
+ }
287
302
animate . play ( ) ;
288
- } , [ value ] ) ;
303
+ } , [ value , keep ] ) ;
289
304
290
305
return (
291
306
< rect
@@ -304,21 +319,42 @@ export const Bars: StoryObj = {
304
319
const init = ( ) =>
305
320
Array . from ( { length : 30 } ) . map ( ( ) => 300 * Math . random ( ) ** 2 ) ;
306
321
const [ rects , setRects ] = useState ( init ) ;
322
+ const [ keep , setKeep ] = useState ( false ) ;
307
323
308
324
const width = 800 ;
309
325
const height = 400 ;
310
326
const margin = 10 ;
311
327
const maxBarHeight = height - margin * 2 ;
328
+
329
+ const refresh = ( ) => setRects ( init ( ) ) ;
330
+
312
331
return (
313
332
< >
314
333
< div >
315
- < button onClick = { ( ) => setRects ( init ( ) ) } > refresh</ button >
334
+ < button onClick = { refresh } > refresh</ button >
335
+ < label >
336
+ < input
337
+ type = "checkbox"
338
+ checked = { keep }
339
+ onChange = { ( e ) => {
340
+ setKeep ( e . target . checked ) ;
341
+ refresh ( ) ;
342
+ } }
343
+ />
344
+ keep
345
+ </ label >
316
346
</ div >
317
347
< div >
318
348
< svg width = { width } height = { height } >
319
349
< g transform = { `translate(${ margin } ,${ margin } )` } >
320
350
{ rects . map ( ( v , i ) => (
321
- < Bar key = { i } i = { i } value = { v } height = { maxBarHeight } />
351
+ < Bar
352
+ key = { i }
353
+ i = { i }
354
+ value = { v }
355
+ height = { maxBarHeight }
356
+ keep = { keep }
357
+ />
322
358
) ) }
323
359
</ g >
324
360
</ svg >
0 commit comments