forked from lobehub/lobe-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
106 lines (96 loc) · 2.57 KB
/
index.tsx
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use client';
import { useSize } from 'ahooks';
import { shuffle } from 'lodash-es';
import { memo, useCallback, useMemo, useRef } from 'react';
import { DivProps } from '@/types';
import Grid, { type GridProps } from './Grid';
import { useStyles } from './style';
export interface GridBackgroundProps extends DivProps {
animation?: boolean;
animationDuration?: number;
backgroundColor?: string;
colorBack?: string;
colorFront?: string;
flip?: boolean;
random?: boolean;
reverse?: boolean;
showBackground?: boolean;
strokeWidth?: number;
}
const GridBackground = memo<GridBackgroundProps>(
({
flip,
reverse,
showBackground,
backgroundColor,
random,
animationDuration = 8,
className,
colorFront,
colorBack,
strokeWidth,
style,
animation,
...rest
}) => {
const ref = useRef(null);
const size = useSize(ref);
const { styles, cx, theme } = useStyles({ backgroundColor, reverse });
const gridProps: GridProps = useMemo(
() => ({
className: styles.highlight,
color: colorFront || theme.colorText,
strokeWidth,
}),
[reverse, colorFront, strokeWidth],
);
const HighlightGrid = useCallback(() => {
if (!random)
return <Grid style={{ '--duration': `${animationDuration}s` } as any} {...gridProps} />;
const group = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
return (
<>
{shuffle(group).map((item, index) => {
return (
<Grid
key={item}
linePick={item}
style={
{
'--delay': `${index + Math.random()}s`,
'--duration': `${animationDuration}s`,
} as any
}
{...gridProps}
/>
);
})}
</>
);
}, [random, animationDuration, gridProps]);
return (
<div
className={cx(styles.container, className)}
ref={ref}
style={flip ? { transform: 'scaleY(-1)', ...style } : style}
{...rest}
>
<Grid
color={colorBack || theme.colorBorder}
strokeWidth={strokeWidth}
style={{ zIndex: 2 }}
/>
{animation && <HighlightGrid />}
{showBackground && (
<div
className={styles.backgroundContainer}
style={size ? { fontSize: size.width / 80 } : {}}
>
<div className={styles.background} />
</div>
)}
</div>
);
},
);
export default GridBackground;