-
Notifications
You must be signed in to change notification settings - Fork 8
/
Spectrum.tsx
112 lines (107 loc) 路 2.59 KB
/
Spectrum.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
107
108
109
110
111
112
import * as React from 'react';
import DrawLine from './DrawLine';
export interface RenderWordProps {
key: number;
style: React.CSSProperties;
}
type SpectrumProps = {
/**
* Width of the placeholder container
*/
width?: number;
/**
* Possible colors of words, will be picked randomly
*/
colors?: Array<string>;
/**
* Possible widths of words, will be picked randomly
*/
wordWidths?: Array<number>;
/**
* Possible distance between words, will be picked randomly
*/
wordDistances?: Array<number>;
/**
* Height of every word placeholder
*/
wordHeight?: number;
/**
* Border radius of every word
*/
wordRadius?: number;
/**
* Distance(margin) between the lines
*/
lineDistance?: number;
/**
* Lines per paragraph
* if there are multiple paragraphs, all of them will have same number of lines
*/
linesPerParagraph?: number;
/**
* Number of paragraphs in the placeholder
*/
paragraphs?: number;
/**
* Distance(margin) between the paragraphs
*/
paragraphDistance?: number;
/**
* Show less words in the last line for more natural feel
*/
truncateLastLine?: boolean;
/**
* Render word with customizations
*/
renderWord?: React.FC<RenderWordProps>;
};
const Spectrum = ({
width = 500,
colors = ['#eee'],
wordWidths = [30, 60, 90, 120, 150],
wordDistances = [4, 8, 12],
wordHeight = 12,
wordRadius = 20,
linesPerParagraph = 8,
lineDistance = 12,
paragraphs = 1,
paragraphDistance = 24,
truncateLastLine = true,
renderWord = ({ key, style }: RenderWordProps): React.ReactElement => (
<span key={key} style={style} />
),
}: SpectrumProps): React.ReactElement => {
return (
<>
{new Array(paragraphs).fill(true).map((_, i) => {
const lines = new Array(linesPerParagraph).fill(true).map((__, j) => (
<div data-line={j} key={j}>
<DrawLine
width={width}
colors={colors}
wordWidths={wordWidths}
wordDistances={wordDistances}
wordHeight={wordHeight}
wordRadius={wordRadius}
lineDistance={lineDistance}
truncate={truncateLastLine ? j === linesPerParagraph - 1 : false}
renderWord={renderWord}
/>
</div>
));
return (
<div
data-paragraph={i}
key={i}
style={{
marginBottom: paragraphDistance,
fontSize: 0,
}}>
{lines}
</div>
);
})}
</>
);
};
export default React.memo(Spectrum);