77 * @position Storybook demonstrations for List consumers
88 */
99
10- import { useMemo , useRef , useState , type MouseEvent } from 'react' ;
10+ import { useMemo , useState } from 'react' ;
1111import * as stylex from '@stylexjs/stylex' ;
1212import type { Meta , StoryObj } from '@storybook/react' ;
1313import { List , ListItem } from '@astryxdesign/core/List' ;
@@ -21,11 +21,6 @@ import {Heading, Text} from '@astryxdesign/core/Text';
2121import { VStack } from '@astryxdesign/core/Layout' ;
2222import { Button } from '@astryxdesign/core/Button' ;
2323import { ContextMenu } from '@astryxdesign/core/ContextMenu' ;
24- // The selection hold is internal to core (no public API records it yet).
25- import {
26- holdSelection ,
27- type SelectionHold as RowSelectionHold ,
28- } from '../../../packages/core/src/List/selectionHold' ;
2924import {
3025 Cog6ToothIcon ,
3126 BellIcon ,
@@ -417,121 +412,6 @@ export const HeaderInFlexParent: Story = {
417412 } ,
418413} ;
419414
420- function SelectionHoldDemo ( ) {
421- const hold = useRef < RowSelectionHold | null > ( null ) ;
422- const slider = useRef < HTMLInputElement | null > ( null ) ;
423- const [ open , setOpen ] = useState < string | null > ( null ) ;
424- const [ progress , setProgress ] = useState ( 0 ) ;
425- const push = ( label : string ) => ( event : MouseEvent ) => {
426- // In the same task as the activation that pushes.
427- hold . current = holdSelection ( event . target ) ;
428- setProgress ( 0 ) ;
429- setOpen ( label ) ;
430- } ;
431- const settle = async (
432- to : 0 | 1 ,
433- timing ?: Parameters < RowSelectionHold [ 'animate' ] > [ 1 ] ,
434- { followSlider = false } : { followSlider ?: boolean } = { } ,
435- ) => {
436- const current = hold . current ;
437- // A slow pop shows where it is on the slider (written to the input, no
438- // render per frame), so a finger that takes the slider over mid-flight
439- // continues from the value on screen. (Off for the regular pop, whose
440- // proof counts every paint.)
441- const following = { frame : 0 } ;
442- const follow = ( ) => {
443- if ( current != null && slider . current != null ) {
444- slider . current . value = String ( current . presentation ( ) ) ;
445- }
446- following . frame = requestAnimationFrame ( follow ) ;
447- } ;
448- if ( followSlider ) {
449- following . frame = requestAnimationFrame ( follow ) ;
450- }
451- // No hold under a mouse: the screen just pops.
452- const arrived = current == null || ( await current . animate ( to , timing ) ) ;
453- cancelAnimationFrame ( following . frame ) ;
454- if ( ! arrived ) {
455- // Interrupted (a finger took the progress over mid-flight): the hold
456- // stays where the finger left it.
457- return ;
458- }
459- if ( to === 1 ) {
460- setOpen ( null ) ;
461- current ?. end ( true ) ;
462- current ?. focus ( { preventScroll : true } ) ;
463- hold . current = null ;
464- } else {
465- current ?. end ( false ) ;
466- setProgress ( 0 ) ;
467- }
468- } ;
469- return (
470- < VStack gap = { 4 } >
471- < List hasDividers >
472- { [ 'Inbox' , 'Sent' , 'Drafts' ] . map ( label => (
473- < ListItem
474- key = { label }
475- label = { label }
476- onClick = { push ( label ) }
477- endContent = { < Icon icon = { ChevronRightIcon } /> }
478- />
479- ) ) }
480- </ List >
481- { open != null && (
482- < VStack gap = { 2 } data-testid = "pushed-screen" >
483- < Text type = "label" > { open } (pushed)</ Text >
484- < Button label = "Back" onClick = { ( ) => void settle ( 1 ) } />
485- < Button
486- label = "Back, slowly"
487- onClick = { ( ) =>
488- void settle (
489- 1 ,
490- { durationMs : 3000 , curve : [ 0 , 0 , 1 , 1 ] } ,
491- { followSlider : true } ,
492- )
493- }
494- />
495- < label >
496- Pop progress
497- < input
498- ref = { slider }
499- type = "range"
500- min = { 0 }
501- max = { 1 }
502- step = { 0.01 }
503- value = { progress }
504- onChange = { event => {
505- const value = Number ( event . target . value ) ;
506- setProgress ( value ) ;
507- hold . current ?. setProgress ( value ) ;
508- } }
509- />
510- </ label >
511- < Button label = "Finish pop" onClick = { ( ) => void settle ( 1 ) } />
512- < Button label = "Cancel pop" onClick = { ( ) => void settle ( 0 ) } />
513- </ VStack >
514- ) }
515- </ VStack >
516- ) ;
517- }
518-
519- export const SelectionHold : Story = {
520- render : ( ) => < SelectionHoldDemo /> ,
521- parameters : {
522- docs : {
523- description : {
524- story :
525- 'A row tapped to push stays selected while the pushed screen covers it ' +
526- 'and deselects as the screen pops (UIKit’s push from a table row), ' +
527- 'driven here by buttons standing in for a navigation stack: Back runs ' +
528- 'the in-transition deselect, the slider scrubs it like an edge swipe, ' +
529- 'and Cancel selects the row again.' ,
530- } ,
531- } ,
532- } ,
533- } ;
534-
535415function AnimatedChangesDemo ( ) {
536416 const [ rows , setRows ] = useState ( [
537417 'Inbox' ,
0 commit comments