MCPcopy Create free account
hub / github.com/adobe/react-spectrum / SubmenuAnimation

Function SubmenuAnimation

packages/dev/docs/pages/blog/SubmenuAnimation.tsx:16–368  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14import {useResizeObserver} from '@react-aria/utils';
15
16export function SubmenuAnimation(): JSX.Element {
17 let ref = useRef<HTMLDivElement>(null);
18 let [isSubmenuOpen, setIsSubmenuOpen] = useState(false);
19 let [hovered, setHovered] = useState('Option 1');
20 let isAnimating = useRef(false);
21 let mouseRef = useRef<SVGSVGElement>(null);
22 let [mouseWidth, setMouseWidth] = useState(12);
23 let option1Ref = useRef<SVGTextElement>(null);
24 let option2Ref = useRef<SVGTextElement>(null);
25 let submenuOptionRef = useRef<SVGTextElement>(null);
26
27 let updateWidth = () => {
28 if (ref.current) {
29 setMouseWidth(Math.min(12, (window.innerWidth / 768) * 12));
30 }
31 };
32
33 useResizeObserver({ref: ref, onResize: updateWidth});
34
35 useEffect(() => {
36 let startAnimation = () => {
37 let cancel = animate([
38 {
39 time: 500,
40 perform() {
41 setTimeout(() => {
42 setHovered('Option 1');
43 }, 500);
44 }
45 },
46 {
47 time: 700,
48 perform() {
49 let option1Rect = option1Ref.current!.getBoundingClientRect();
50 let option2Rect = option2Ref.current!.getBoundingClientRect();
51 let x =
52 option1Rect.left + option1Rect.width / 2 - ref.current!.getBoundingClientRect().left;
53 let y =
54 option1Rect.top + option1Rect.height / 2 - ref.current!.getBoundingClientRect().top;
55 let y_target =
56 option2Rect.top + option2Rect.height / 2 - ref.current!.getBoundingClientRect().top;
57 mouseRef.current!.animate(
58 {
59 transform: [`translate(${x}px, ${y}px)`, `translate(${x}px, ${y_target}px)`]
60 },
61 {duration: 1000, fill: 'forwards', easing: 'ease-in-out'}
62 );
63 setTimeout(() => {
64 setHovered('Option 2');
65 setIsSubmenuOpen(true);
66 }, 350);
67 }
68 },
69 {
70 time: 700,
71 perform() {}
72 },
73 {

Callers

nothing calls this directly

Calls 2

useResizeObserverFunction · 0.90
startAnimationFunction · 0.70

Tested by

no test coverage detected