| 538 | |
| 539 | // TODO: make this component a reusable utility? |
| 540 | function ShimmerText(props: DetailTriggerProps) { |
| 541 | let {children, isPending} = props; |
| 542 | let {isExpanded, responseStatusRef} = useContext(ResponseStatusContext)!; |
| 543 | |
| 544 | // Do the animation in JS rather than CSS so we can synchronize across elements. |
| 545 | let shimmerRef = useCallback( |
| 546 | (el: HTMLSpanElement | null) => { |
| 547 | if (el && !window.matchMedia('(prefers-reduced-motion: reduce)').matches) { |
| 548 | let duration = 1500; |
| 549 | let delay = 300; |
| 550 | let animation = el.animate( |
| 551 | [ |
| 552 | {transform: `translateX(calc(-50% - ${SPREAD}))`, offset: 0}, |
| 553 | {transform: `translateX(calc(50% + ${SPREAD}))`, offset: 1 - delay / duration}, |
| 554 | {transform: `translateX(calc(50% + ${SPREAD}))`, offset: 1} |
| 555 | ], |
| 556 | { |
| 557 | duration: 1500 + delay, |
| 558 | iterations: Infinity, |
| 559 | easing: 'cubic-bezier(0.41, 0.01, 0.43, 0.99)', |
| 560 | fill: 'both', |
| 561 | pseudoElement: '::after' |
| 562 | } |
| 563 | ); |
| 564 | animation[shimmerSym] = true; |
| 565 | |
| 566 | // If there is an existing shimmer animation already happening, synchronize with it. |
| 567 | // If there is only one, then start from the beginning of the animation. |
| 568 | let existingAnim = responseStatusRef?.current |
| 569 | ?.getAnimations({subtree: true}) |
| 570 | .find(anim => anim[shimmerSym] && anim !== animation); |
| 571 | if (existingAnim) { |
| 572 | animation.startTime = existingAnim.startTime; |
| 573 | animation.play(); |
| 574 | } |
| 575 | return () => animation.cancel(); |
| 576 | } |
| 577 | }, |
| 578 | [responseStatusRef] |
| 579 | ); |
| 580 | |
| 581 | return ( |
| 582 | <span className={style({position: 'relative', display: 'inline-block'})}> |
| 583 | <span className={style({display: 'inline-block'})}>{children}</span> |
| 584 | {/* inert clone of the children with shimmer effect mask. */} |
| 585 | {isPending && isExpanded && ( |
| 586 | <span inert ref={shimmerRef} className={shimmer}> |
| 587 | {children} |
| 588 | </span> |
| 589 | )} |
| 590 | </span> |
| 591 | ); |
| 592 | } |
| 593 | |
| 594 | // Inner disclosure. |
| 595 | const detailTriggerStyles = style({ |