123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293import React,{ ReactElement, useState }from"react";import{ AppBar, AppBarAction, AppBarNav }from"@react-md/app-bar";import{ Avatar }from"@react-md/avatar";import avatarVariables from"@react-md/avatar/dist/scssVariables";import{ List, ListItem }from"@react-md/list";import{ MenuSVGIcon, MoreVertSVGIcon }from"@react-md/material-icons";import{ useScrollListener }from"@react-md/utils";import people from"./people";import AppBarTitle from"./AppBarTitle";import styles from"./AnimatingAppBar.module.scss";typeCSSProperties= React.CSSProperties &{"--offset":string;};constCOLORS= Object.keys(avatarVariables["rmd-avatar-colors"]);const transformedPeople = people.map((name, i)=>({
id: name.toLowerCase().replace(//g,"-"),
name,
avatar: name.substring(0,1),
color:COLORS[i %COLORS.length],}));constHEIGHT=96;// this is used along with the current `scrollTop` value to make// the animation happen a bit slower. The number has no real significance// and just looked "decent"constSCROLL_MULTIPLIER=0.314;exportdefaultfunctionDemo(): ReactElement {const[height, setHeight]=useState(`${HEIGHT}px`);const[ref, setRef]=useState<HTMLDivElement |null>(null);useScrollListener({
element: ref,onScroll:()=>{if(!ref){return;}const{ scrollTop }= ref;const remaining = Math.min(
Math.max(HEIGHT- scrollTop *SCROLL_MULTIPLIER,0),HEIGHT);const nextHeight =`${remaining}px`;if(height !== nextHeight){setHeight(nextHeight);}},});const style: CSSProperties ={"--offset": height,};return(<divstyle={style}className={styles.container}><AppBarid="animating-app-bar"className={styles.header}fixedtheme="clear"><AppBarNavid="animating-app-bar-nav"aria-label="Navigation"><MenuSVGIcon/></AppBarNav><AppBarTitleclassName={styles.title}>Animating App Bar</AppBarTitle><AppBarActionid="animating-app-bar-kebab"aria-label="Actions"first><MoreVertSVGIcon/></AppBarAction></AppBar><divclassName={styles.content}ref={setRef}><List>{transformedPeople.map(({ id, name, avatar, color }, i)=>(<ListItemid={`person-${i}`}key={id}leftAddon={<Avatarcolor={color}>{avatar}</Avatar>}leftAddonType="avatar">{name}</ListItem>))}</List></div></div>);}