When floating loading spinner isn't visible, don't render it
This commit is contained in:
parent
ce7a2ded14
commit
6b66dad493
3 changed files with 94 additions and 26 deletions
|
@ -25,14 +25,5 @@
|
||||||
@include timeline-floating-header-node;
|
@include timeline-floating-header-node;
|
||||||
margin-top: 12px;
|
margin-top: 12px;
|
||||||
padding: 6px;
|
padding: 6px;
|
||||||
|
|
||||||
&--visible {
|
|
||||||
opacity: 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
&--hidden {
|
|
||||||
opacity: 0;
|
|
||||||
transition: opacity 0.25s ease-out 0.3s;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 })}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
|
@ -4,29 +4,67 @@
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import type { CSSProperties, ReactElement } from 'react';
|
import type { CSSProperties, ReactElement } from 'react';
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
|
import { animated, useSpring } from '@react-spring/web';
|
||||||
|
|
||||||
import type { LocalizerType } from '../../types/Util';
|
import type { LocalizerType } from '../../types/Util';
|
||||||
import { TimelineDateHeader } from './TimelineDateHeader';
|
import { TimelineDateHeader } from './TimelineDateHeader';
|
||||||
import { Spinner } from '../Spinner';
|
import { Spinner } from '../Spinner';
|
||||||
|
|
||||||
|
export type PropsType = Readonly<{
|
||||||
|
i18n: LocalizerType;
|
||||||
|
isLoading: boolean;
|
||||||
|
style?: CSSProperties;
|
||||||
|
timestamp: number;
|
||||||
|
visible: boolean;
|
||||||
|
}>;
|
||||||
|
|
||||||
export const TimelineFloatingHeader = ({
|
export const TimelineFloatingHeader = ({
|
||||||
i18n,
|
i18n,
|
||||||
isLoading,
|
isLoading,
|
||||||
style,
|
style,
|
||||||
timestamp,
|
timestamp,
|
||||||
visible,
|
visible,
|
||||||
}: Readonly<{
|
}: PropsType): ReactElement => {
|
||||||
i18n: LocalizerType;
|
|
||||||
isLoading: boolean;
|
|
||||||
style?: CSSProperties;
|
|
||||||
timestamp: number;
|
|
||||||
visible: boolean;
|
|
||||||
}>): ReactElement => {
|
|
||||||
const [hasRendered, setHasRendered] = useState(false);
|
const [hasRendered, setHasRendered] = useState(false);
|
||||||
|
const [showSpinner, setShowSpinner] = useState(isLoading);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setHasRendered(true);
|
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 (
|
return (
|
||||||
<div
|
<div
|
||||||
className={classNames(
|
className={classNames(
|
||||||
|
@ -38,16 +76,14 @@ export const TimelineFloatingHeader = ({
|
||||||
style={style}
|
style={style}
|
||||||
>
|
>
|
||||||
<TimelineDateHeader floating i18n={i18n} timestamp={timestamp} />
|
<TimelineDateHeader floating i18n={i18n} timestamp={timestamp} />
|
||||||
<div
|
{showSpinner && (
|
||||||
className={classNames(
|
<animated.div
|
||||||
'TimelineFloatingHeader__spinner-container',
|
className="TimelineFloatingHeader__spinner-container"
|
||||||
`TimelineFloatingHeader__spinner-container--${
|
style={spinnerStyles}
|
||||||
isLoading ? 'visible' : 'hidden'
|
>
|
||||||
}`
|
<Spinner direction="on-background" size="20px" svgSize="small" />
|
||||||
)}
|
</animated.div>
|
||||||
>
|
)}
|
||||||
<Spinner direction="on-background" size="20px" svgSize="small" />
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue