react-animated-text-writer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,199 @@
1
+ import React from "react";
2
+
3
+ import "./AnimatedTextWriter.css";
4
+
5
+ const sTags = [' ', '<', '&'];
6
+ const eTags = ['', '>', ';'];
7
+
8
+ const getIncrement = (content: string, index: number, fistCall=true) => {
9
+ let increment = 1;
10
+ let decrement = 0;
11
+ const currentChar = content.substring(index, index+1);
12
+
13
+ const tstart = sTags.indexOf(currentChar);
14
+ if (tstart !== -1) {
15
+ if (eTags[tstart].length === 0) {
16
+ increment = 2;
17
+ decrement = 1;
18
+ if (fistCall === false) {
19
+ increment = 1;
20
+ decrement = 0;
21
+ }
22
+ } else {
23
+ const tend = content.indexOf(eTags[tstart], index);
24
+ if (tend !== -1) {
25
+ increment = tend - index + 1;
26
+ }
27
+ }
28
+
29
+ const nextChar = content.substring(index+increment-decrement, index+increment-decrement+1);
30
+
31
+ if (sTags.includes(nextChar)) {
32
+ increment += getIncrement(content, index+increment-decrement, false);
33
+ }
34
+ }
35
+
36
+ return increment;
37
+ };
38
+
39
+ interface AnimatedTextWriterProps {
40
+ content?: string;
41
+ prefix?: string;
42
+ suffix?: string;
43
+ replacablePrefix?: string;
44
+ codePrefix?: string;
45
+ codeSuffix?: string;
46
+ delay?: number;
47
+ startDelay?: number;
48
+ displayCursor?: boolean;
49
+ displayCursorEnd?: boolean;
50
+ displayCodeWrapper?: boolean;
51
+ displayCodeLineNumber?: boolean;
52
+ displayCodeLineNumberMax?: number;
53
+ className?: string;
54
+ cursorColor?: string;
55
+ cursorLineHeight?: string;
56
+ codeWrapperClasses?: string;
57
+ codeWrapperStyle?: string | React.CSSProperties;
58
+ codeWrapperWhiteSpace?: "auto" | "nowrap";
59
+ displayClickMoreButtonAndPause?: boolean;
60
+ displayFullContentOnClickMoreButton?: boolean;
61
+ clickMoreHeaderText?: string;
62
+ clickMoreHeaderClassString?: string;
63
+ viewMoreButtonText?: string;
64
+ viewLessButtonText?: string;
65
+ showContentAuto?: boolean;
66
+ sx?: React.CSSProperties;
67
+ }
68
+
69
+ const AnimatedTextWriter = ({
70
+ content = 'This is a Sample Text',
71
+ prefix = '',
72
+ suffix = '',
73
+ replacablePrefix = '',
74
+ codePrefix = '',
75
+ codeSuffix = '',
76
+ delay = 250,
77
+ startDelay = 1000,
78
+ displayCursor = true,
79
+ displayCursorEnd = true,
80
+ displayCodeWrapper = true,
81
+ displayCodeLineNumber = true,
82
+ displayCodeLineNumberMax = 10,
83
+ className = '',
84
+ cursorColor = '#000',
85
+ cursorLineHeight = '1rem',
86
+ codeWrapperClasses = '',
87
+ codeWrapperStyle = '',
88
+ codeWrapperWhiteSpace = 'auto',
89
+ displayClickMoreButtonAndPause = false,
90
+ displayFullContentOnClickMoreButton = false,
91
+ clickMoreHeaderText = '',
92
+ clickMoreHeaderClassString = '',
93
+ viewMoreButtonText = 'More',
94
+ viewLessButtonText = 'Less',
95
+ showContentAuto = false,
96
+ sx={}
97
+ } : AnimatedTextWriterProps) => {
98
+ const [currentIndex, setCurrentIndex] = React.useState(0);
99
+ const [currentContent, setCurrentContent] = React.useState('');
100
+ const [enabled, setEnabled] = React.useState(!displayClickMoreButtonAndPause);
101
+ const [showContent, setShowContent] = React.useState(showContentAuto);
102
+ const [showContentDisappear, setShowContentDisappear] = React.useState(!showContentAuto);
103
+ const [startTime, setStartTime] = React.useState(0);
104
+ const [nextIteration, setNextIteration] = React.useState(0);
105
+ const elementRef = React.useRef<HTMLDivElement>(null);
106
+ let timerControl:NodeJS.Timeout | undefined;
107
+
108
+ const codeWrapperPrefix = displayCodeWrapper ? `<div class="animated-text-writer-code-container ${displayCodeLineNumber ? '' : 'hide-line-number'} ${displayCodeLineNumberMax ? 'n'+displayCodeLineNumberMax : ''} ${codeWrapperWhiteSpace === 'auto' ? (displayCodeLineNumber ? 'nowrap' : '' ) : (codeWrapperWhiteSpace === 'nowrap' ? 'nowrap' : '')} ${codeWrapperClasses}" style="${codeWrapperStyle}">` : '';
109
+ const codeWrapperSuffix = displayCodeWrapper ? '</div>' : '';
110
+ const cursorContent = `<span class="animated-text-writer-blinking-cursor" style="color: ${cursorColor}; line-height: ${cursorLineHeight}">𝙸</span>`;
111
+
112
+
113
+ React.useEffect(() => {
114
+ const currentMillis = new Date().getTime();
115
+ if (startTime === 0) {
116
+ setCurrentContent(cursorContent);
117
+ setStartTime(currentMillis);
118
+ }
119
+
120
+ if (startTime + startDelay > currentMillis) {
121
+ if (enabled) {
122
+ setTimeout(() => {
123
+ setNextIteration(nextIteration+1);
124
+ }, delay);
125
+ }
126
+ return;
127
+ }
128
+
129
+ setCurrentContent(prefix + (nextIteration === 0 ? replacablePrefix : '') + codeWrapperPrefix + codePrefix + (content.length && currentIndex < content.length ? content.substring(0, currentIndex) + (displayCursor && enabled ? cursorContent : '') : '') + codeSuffix + codeWrapperSuffix + suffix);
130
+ if (currentIndex < content.length) {
131
+ const increment = getIncrement(content, currentIndex);
132
+
133
+ setTimeout(() => {
134
+ setCurrentIndex(currentIndex + increment);
135
+ }, delay);
136
+ } else {
137
+ setCurrentContent(prefix + codeWrapperPrefix + codePrefix + content + (displayCursorEnd ? cursorContent : '') + codeSuffix + codeWrapperSuffix + suffix);
138
+ }
139
+ }, [
140
+ nextIteration, currentIndex, enabled,
141
+ startTime,
142
+ startDelay,
143
+ prefix,
144
+ replacablePrefix,
145
+ codeWrapperPrefix,
146
+ codePrefix,
147
+ content,
148
+ displayCursor,
149
+ cursorContent,
150
+ codeSuffix,
151
+ codeWrapperSuffix,
152
+ suffix,
153
+ delay,
154
+ displayCursorEnd
155
+ ]
156
+ );
157
+
158
+
159
+ const clickMoreLessButton = () => {
160
+ if (displayFullContentOnClickMoreButton) {
161
+ if (!showContent) {
162
+ setShowContentDisappear(false);
163
+ setShowContent(true);
164
+ if (timerControl) {
165
+ clearTimeout(timerControl);
166
+ }
167
+ } else {
168
+ document.documentElement.style.setProperty('--client-height', elementRef.current!.clientHeight + 'px');
169
+ setShowContentDisappear(true);
170
+ timerControl = setTimeout(() => {
171
+ setShowContent(false);
172
+ setShowContentDisappear(false);
173
+ }, 1800);
174
+ }
175
+ } else {
176
+ setEnabled(true);
177
+ }
178
+ }
179
+
180
+ return (
181
+ <div className={className} style={sx}>
182
+ {enabled && (<div dangerouslySetInnerHTML={{__html: currentContent}} />)}
183
+ {!enabled && (
184
+ <h4 className={clickMoreHeaderClassString} onClick={clickMoreLessButton} style={{ cursor: 'pointer' }}>
185
+ {clickMoreHeaderText}
186
+ <button className='displayMoreButton buttonButton'
187
+ onClick={clickMoreLessButton}
188
+ >
189
+ <span className={`buttonText ${showContent ? 'showLess' : 'showMore'}`} >{showContent ? viewLessButtonText : viewMoreButtonText}</span>
190
+ <div className="bouncingDotAnimation"> <div></div><div></div> <div></div> </div>
191
+ </button>
192
+ </h4>
193
+ )}
194
+ {displayFullContentOnClickMoreButton && showContent && (<div ref={elementRef} style={{ animation: showContentDisappear ? 'showly-disappear 2s ease' : 'showly-appear 2s ease', overflow: 'hidden', marginTop: '-2px' }} dangerouslySetInnerHTML={{__html: content}} />)}
195
+ </div>
196
+ );
197
+ };
198
+
199
+ export default AnimatedTextWriter;
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import AnimatedTextWriter from "./components/AnimatedTextWriter";
2
+
3
+ export default AnimatedTextWriter;
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES5",
4
+ "lib": ["DOM", "DOM.Iterable", "ESNext", "ES2020"],
5
+ "allowJs": true,
6
+ "module": "ESNext",
7
+ "moduleResolution": "node",
8
+ "resolveJsonModule": true,
9
+ "isolatedModules": true,
10
+ "noEmit": true,
11
+ "jsx": "react-jsx",
12
+ "declaration": true,
13
+ "rootDir": "./src",
14
+ "outDir": "./dist",
15
+ "strict": true,
16
+ "noFallthroughCasesInSwitch": true,
17
+ "allowSyntheticDefaultImports": true,
18
+ "esModuleInterop": true,
19
+ "skipLibCheck": true,
20
+ "forceConsistentCasingInFileNames": true
21
+ },
22
+ "include": ["src"]
23
+ }