-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSlidingDots.tsx
More file actions
61 lines (47 loc) · 2.03 KB
/
SlidingDots.tsx
File metadata and controls
61 lines (47 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { hooks } from 'botframework-webchat';
import React, { memo, useCallback, useEffect, useRef } from 'react';
import { useRefFrom } from 'use-ref-from';
import useAssetURL from './private/useAssetURL';
const { useLocalizer, useShouldReduceMotion } = hooks;
type SlidingDotsProps = Readonly<{ className: string }>;
const SlidingDots = ({ className }: SlidingDotsProps) => {
const [shouldReduceMotion] = useShouldReduceMotion();
const [url] = useAssetURL('sliding dots');
const localize = useLocalizer();
const objectElementRef = useRef<HTMLObjectElement>(null);
const altText = localize('TYPING_INDICATOR_ALT');
const shouldReduceMotionRef = useRefFrom(shouldReduceMotion);
const pauseAnimations = useCallback(() => {
const contentDocument = objectElementRef.current?.contentDocument;
const svgElement = contentDocument?.documentElement;
const { SVGSVGElement } = contentDocument?.defaultView || {};
SVGSVGElement && svgElement instanceof SVGSVGElement && svgElement.pauseAnimations();
}, [objectElementRef]);
const unpauseAnimations = useCallback(() => {
const contentDocument = objectElementRef.current?.contentDocument;
const svgElement = contentDocument?.documentElement;
const { SVGSVGElement } = contentDocument?.defaultView || {};
SVGSVGElement && svgElement instanceof SVGSVGElement && svgElement.unpauseAnimations();
}, [objectElementRef]);
const pauseOrUnpauseAnimations = useCallback(
() => (shouldReduceMotionRef.current ? pauseAnimations() : unpauseAnimations()),
[pauseAnimations, shouldReduceMotionRef, unpauseAnimations]
);
useEffect(pauseOrUnpauseAnimations, [
pauseOrUnpauseAnimations,
// Call "pauseOrUnpauseAnimations()" when "shouldReduceMotion" change.
shouldReduceMotion
]);
return (
<object
aria-label={altText}
className={className}
data={url.href}
onLoad={pauseOrUnpauseAnimations}
ref={objectElementRef}
type="image/svg+xml"
/>
);
};
SlidingDots.displayName = 'SlidingDots';
export default memo(SlidingDots);