mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
fix(component): affine loading (#1887)
This commit is contained in:
@@ -1,20 +0,0 @@
|
|||||||
import {
|
|
||||||
StyledLoading,
|
|
||||||
StyledLoadingItem,
|
|
||||||
StyledLoadingWrapper,
|
|
||||||
} from './styled';
|
|
||||||
|
|
||||||
export const Loading = ({ size = 40 }: { size?: number }) => {
|
|
||||||
return (
|
|
||||||
<StyledLoadingWrapper size={size}>
|
|
||||||
<StyledLoading>
|
|
||||||
<StyledLoadingItem size={size} />
|
|
||||||
<StyledLoadingItem size={size} />
|
|
||||||
<StyledLoadingItem size={size} />
|
|
||||||
<StyledLoadingItem size={size} />
|
|
||||||
</StyledLoading>
|
|
||||||
</StyledLoadingWrapper>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Loading;
|
|
||||||
@@ -1,7 +1,22 @@
|
|||||||
import { styled } from '@affine/component';
|
import { styled } from '@affine/component';
|
||||||
|
import { AffineLoading } from '@affine/component/affine-loading';
|
||||||
import { useTranslation } from '@affine/i18n';
|
import { useTranslation } from '@affine/i18n';
|
||||||
|
import { memo, Suspense } from 'react';
|
||||||
|
|
||||||
import Loading from './Loading';
|
export const Loading = memo(function Loading() {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
height: '180px',
|
||||||
|
width: '180px',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Suspense>
|
||||||
|
<AffineLoading loop={true} autoplay={true} autoReverse={true} />
|
||||||
|
</Suspense>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// Used for the full page loading
|
// Used for the full page loading
|
||||||
const StyledLoadingContainer = styled('div')(() => {
|
const StyledLoadingContainer = styled('div')(() => {
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
import Loading from './Loading';
|
import { Loading } from './PageLoading';
|
||||||
export * from './PageLoading';
|
export * from './PageLoading';
|
||||||
export default Loading;
|
export default Loading;
|
||||||
|
|||||||
@@ -1,94 +0,0 @@
|
|||||||
import { styled } from '@affine/component';
|
|
||||||
|
|
||||||
// Inspired by https://codepen.io/graphilla/pen/rNvBMYY
|
|
||||||
export const StyledLoadingWrapper = styled('div', {
|
|
||||||
shouldForwardProp: prop => {
|
|
||||||
return !['size'].includes(prop as string);
|
|
||||||
},
|
|
||||||
})<{ size?: number }>(({ size = 40 }) => {
|
|
||||||
return {
|
|
||||||
width: size * 4,
|
|
||||||
height: size * 4,
|
|
||||||
position: 'relative',
|
|
||||||
};
|
|
||||||
});
|
|
||||||
export const StyledLoading = styled('div')`
|
|
||||||
position: absolute;
|
|
||||||
left: 25%;
|
|
||||||
top: 50%;
|
|
||||||
transform: rotateX(55deg) rotateZ(-45deg);
|
|
||||||
@keyframes slide {
|
|
||||||
0% {
|
|
||||||
transform: translate(var(--sx), var(--sy));
|
|
||||||
}
|
|
||||||
65% {
|
|
||||||
transform: translate(var(--ex), var(--sy));
|
|
||||||
}
|
|
||||||
95%,
|
|
||||||
100% {
|
|
||||||
transform: translate(var(--ex), var(--ey));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
export const StyledLoadingItem = styled('div')<{ size: number }>(
|
|
||||||
({ size = 40 }) => {
|
|
||||||
return `
|
|
||||||
position: absolute;
|
|
||||||
width: ${size}px;
|
|
||||||
height: ${size}px;
|
|
||||||
background: #9dacf9;
|
|
||||||
animation: slide 0.9s cubic-bezier(0.65, 0.53, 0.59, 0.93) infinite;
|
|
||||||
|
|
||||||
&::before,
|
|
||||||
&::after {
|
|
||||||
content: '';
|
|
||||||
position: absolute;
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
&::before {
|
|
||||||
background: #5260b9;
|
|
||||||
transform: skew(0deg, -45deg);
|
|
||||||
right: 100%;
|
|
||||||
top: 50%;
|
|
||||||
}
|
|
||||||
|
|
||||||
&::after {
|
|
||||||
background: #6880ff;
|
|
||||||
transform: skew(-45deg, 0deg);
|
|
||||||
top: 100%;
|
|
||||||
right: 50%;
|
|
||||||
}
|
|
||||||
|
|
||||||
&:nth-of-type(1) {
|
|
||||||
--sx: 50%;
|
|
||||||
--sy: -50%;
|
|
||||||
--ex: 150%;
|
|
||||||
--ey: 50%;
|
|
||||||
}
|
|
||||||
|
|
||||||
&:nth-of-type(2) {
|
|
||||||
--sx: -50%;
|
|
||||||
--sy: -50%;
|
|
||||||
--ex: 50%;
|
|
||||||
--ey: -50%;
|
|
||||||
}
|
|
||||||
|
|
||||||
&:nth-of-type(3) {
|
|
||||||
--sx: 150%;
|
|
||||||
--sy: 50%;
|
|
||||||
--ex: 50%;
|
|
||||||
--ey: 50%;
|
|
||||||
}
|
|
||||||
|
|
||||||
&:nth-of-type(4) {
|
|
||||||
--sx: 50%;
|
|
||||||
--sy: 50%;
|
|
||||||
--ex: -50%;
|
|
||||||
--ey: -50%;
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
@@ -1,18 +1,30 @@
|
|||||||
import { useTheme } from '@mui/material';
|
import { useTheme } from '@mui/material';
|
||||||
|
import type { FC } from 'react';
|
||||||
|
|
||||||
import { InternalLottie } from '../internal-lottie';
|
import { InternalLottie } from '../internal-lottie';
|
||||||
import dark from './loading-black.json';
|
import dark from './loading-black.json';
|
||||||
import light from './loading-white.json';
|
import light from './loading-white.json';
|
||||||
|
|
||||||
export const AffineLoading = () => {
|
export type AffineLoadingProps = {
|
||||||
|
loop?: boolean;
|
||||||
|
autoplay?: boolean;
|
||||||
|
autoReverse?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const AffineLoading: FC<AffineLoadingProps> = ({
|
||||||
|
loop = false,
|
||||||
|
autoplay = false,
|
||||||
|
autoReverse = false,
|
||||||
|
}) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const isDark = theme.palette.mode === 'dark';
|
const isDark = theme.palette.mode === 'dark';
|
||||||
return (
|
return (
|
||||||
<InternalLottie
|
<InternalLottie
|
||||||
key={isDark ? 'dark' : 'light'}
|
key={isDark ? 'dark' : 'light'}
|
||||||
options={{
|
options={{
|
||||||
loop: false,
|
loop,
|
||||||
autoplay: false,
|
autoplay,
|
||||||
|
autoReverse,
|
||||||
animationData: isDark ? light : dark,
|
animationData: isDark ? light : dark,
|
||||||
rendererSettings: {
|
rendererSettings: {
|
||||||
preserveAspectRatio: 'xMidYMid slice',
|
preserveAspectRatio: 'xMidYMid slice',
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import { lottieAtom } from '@affine/jotai';
|
import { lottieAtom } from '@affine/jotai';
|
||||||
import { useAtomValue } from 'jotai';
|
import { useAtomValue } from 'jotai';
|
||||||
|
import type { AnimationItem } from 'lottie-web';
|
||||||
import type { FC } from 'react';
|
import type { FC } from 'react';
|
||||||
import { useEffect, useRef } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
|
|
||||||
type CustomLottieProps = {
|
type CustomLottieProps = {
|
||||||
options: {
|
options: {
|
||||||
loop?: boolean | number | undefined;
|
loop?: boolean | number | undefined;
|
||||||
|
autoReverse?: boolean | undefined;
|
||||||
autoplay?: boolean | undefined;
|
autoplay?: boolean | undefined;
|
||||||
animationData: any;
|
animationData: any;
|
||||||
rendererSettings?: {
|
rendererSettings?: {
|
||||||
@@ -26,17 +28,53 @@ export const InternalLottie: FC<CustomLottieProps> = ({
|
|||||||
height,
|
height,
|
||||||
}) => {
|
}) => {
|
||||||
const element = useRef<HTMLDivElement>(null);
|
const element = useRef<HTMLDivElement>(null);
|
||||||
const lottieInstance = useRef<any>();
|
const lottieInstance = useRef<AnimationItem>();
|
||||||
const lottie = useAtomValue(lottieAtom);
|
const lottie = useAtomValue(lottieAtom);
|
||||||
|
const directionRef = useRef<1 | -1>(1);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const callback = () => {
|
||||||
|
if (!lottieInstance.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const frame = lottieInstance.current.currentFrame.toFixed(0);
|
||||||
|
if (frame === '1' || frame === '0') {
|
||||||
|
directionRef.current = 1;
|
||||||
|
lottieInstance.current.setDirection(directionRef.current);
|
||||||
|
lottieInstance.current.goToAndStop(0, true);
|
||||||
|
lottieInstance.current.play();
|
||||||
|
} else {
|
||||||
|
directionRef.current = -1;
|
||||||
|
lottieInstance.current.setDirection(directionRef.current);
|
||||||
|
lottieInstance.current.goToAndStop(
|
||||||
|
lottieInstance.current.totalFrames - 1,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
lottieInstance.current.play();
|
||||||
|
}
|
||||||
|
};
|
||||||
if (element.current) {
|
if (element.current) {
|
||||||
lottieInstance.current = lottie.loadAnimation({
|
if (options.autoReverse && options.autoplay) {
|
||||||
...options,
|
lottieInstance.current = lottie.loadAnimation({
|
||||||
container: element.current,
|
...options,
|
||||||
});
|
autoplay: false,
|
||||||
|
loop: false,
|
||||||
|
container: element.current,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
lottieInstance.current = lottie.loadAnimation({
|
||||||
|
...options,
|
||||||
|
container: element.current,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (options.autoReverse) {
|
||||||
|
lottieInstance.current.addEventListener('complete', callback);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return () => {
|
return () => {
|
||||||
|
if (options.autoReverse) {
|
||||||
|
lottieInstance.current?.removeEventListener('complete', callback);
|
||||||
|
}
|
||||||
lottieInstance.current?.destroy();
|
lottieInstance.current?.destroy();
|
||||||
};
|
};
|
||||||
}, [lottie, options]);
|
}, [lottie, options]);
|
||||||
|
|||||||
@@ -7,4 +7,19 @@ export default {
|
|||||||
component: AffineLoading,
|
component: AffineLoading,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Default: StoryFn = () => <AffineLoading />;
|
export const Default: StoryFn = ({ width, loop, autoplay, autoReverse }) => (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
width: width,
|
||||||
|
height: width,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<AffineLoading loop={loop} autoplay={autoplay} autoReverse={autoReverse} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
Default.args = {
|
||||||
|
width: 100,
|
||||||
|
loop: true,
|
||||||
|
autoplay: true,
|
||||||
|
autoReverse: true,
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user