When floating loading spinner isn't visible, don't render it

This commit is contained in:
Scott Nonnenberg 2022-05-26 12:18:17 -07:00 committed by GitHub
parent ce7a2ded14
commit 6b66dad493
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 26 deletions

View file

@ -25,14 +25,5 @@
@include timeline-floating-header-node;
margin-top: 12px;
padding: 6px;
&--visible {
opacity: 1;
}
&--hidden {
opacity: 0;
transition: opacity 0.25s ease-out 0.3s;
}
}
}

View file

@ -0,0 +1,41 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as React from 'react';
import { storiesOf } from '@storybook/react';
import { isBoolean } from 'lodash';
import { boolean } from '@storybook/addon-knobs';
import { setupI18n } from '../../util/setupI18n';
import enMessages from '../../../_locales/en/messages.json';
import type { PropsType } from './TimelineFloatingHeader';
import { TimelineFloatingHeader } from './TimelineFloatingHeader';
const i18n = setupI18n('en', enMessages);
function booleanOr(value: boolean | undefined, defaultValue: boolean): boolean {
return isBoolean(value) ? value : defaultValue;
}
const createProps = (overrideProps: Partial<PropsType> = {}): PropsType => ({
isLoading: boolean('isLoading', booleanOr(overrideProps.isLoading, false)),
style: overrideProps.style,
visible: boolean('visible', booleanOr(overrideProps.visible, false)),
i18n,
timestamp: overrideProps.timestamp || Date.now(),
});
const stories = storiesOf('Components/TimelineFloatingHeader', module);
stories.add('Visible', () => {
return <TimelineFloatingHeader {...createProps({ visible: true })} />;
});
stories.add('Loading', () => {
return (
<TimelineFloatingHeader
{...createProps({ visible: true, isLoading: true })}
/>
);
});

View file

@ -4,29 +4,67 @@
import classNames from 'classnames';
import type { CSSProperties, ReactElement } from 'react';
import React, { useEffect, useState } from 'react';
import { animated, useSpring } from '@react-spring/web';
import type { LocalizerType } from '../../types/Util';
import { TimelineDateHeader } from './TimelineDateHeader';
import { Spinner } from '../Spinner';
export type PropsType = Readonly<{
i18n: LocalizerType;
isLoading: boolean;
style?: CSSProperties;
timestamp: number;
visible: boolean;
}>;
export const TimelineFloatingHeader = ({
i18n,
isLoading,
style,
timestamp,
visible,
}: Readonly<{
i18n: LocalizerType;
isLoading: boolean;
style?: CSSProperties;
timestamp: number;
visible: boolean;
}>): ReactElement => {
}: PropsType): ReactElement => {
const [hasRendered, setHasRendered] = useState(false);
const [showSpinner, setShowSpinner] = useState(isLoading);
useEffect(() => {
setHasRendered(true);
}, []);
const [spinnerStyles, spinnerSpringRef] = useSpring(
() => ({
delay: 300,
duration: 250,
from: { opacity: 1 },
to: { opacity: 0 },
onRest: {
opacity: ({ value }) => {
if (value === 0) {
setShowSpinner(false);
}
},
},
}),
[isLoading]
);
useEffect(() => {
if (isLoading) {
spinnerSpringRef.stop();
spinnerSpringRef.set({ opacity: 1 });
setShowSpinner(true);
}
if (!isLoading && showSpinner) {
spinnerSpringRef.start();
}
if (!isLoading && !showSpinner) {
spinnerSpringRef.stop();
}
}, [isLoading, showSpinner, spinnerSpringRef]);
return (
<div
className={classNames(
@ -38,16 +76,14 @@ export const TimelineFloatingHeader = ({
style={style}
>
<TimelineDateHeader floating i18n={i18n} timestamp={timestamp} />
<div
className={classNames(
'TimelineFloatingHeader__spinner-container',
`TimelineFloatingHeader__spinner-container--${
isLoading ? 'visible' : 'hidden'
}`
)}
>
<Spinner direction="on-background" size="20px" svgSize="small" />
</div>
{showSpinner && (
<animated.div
className="TimelineFloatingHeader__spinner-container"
style={spinnerStyles}
>
<Spinner direction="on-background" size="20px" svgSize="small" />
</animated.div>
)}
</div>
);
};