1
+ // @ts -ignore
1
2
import React , { Children , ReactElement , ReactNode , useState } from 'react'
2
3
import { callbackify } from 'util'
3
4
import { styled } from '../stitches.config'
@@ -24,32 +25,35 @@ const Explanation = ({ content, children }: ExplanationProps) => {
24
25
)
25
26
}
26
27
27
- console . log ( "children" , children )
28
- const traverse = ( ele : JSX . Element [ ] | JSX . Element | string , callback : ( ele : string ) => void ) => {
28
+ console . log ( 'children' , children )
29
+ const traverse = (
30
+ ele : JSX . Element [ ] | JSX . Element | string ,
31
+ callback : ( ele : string ) => void
32
+ ) => {
29
33
if ( typeof ele === 'string' ) {
30
34
// console.log("ele is string:", ele)
31
- callback ( ele ) ;
32
- return ;
35
+ callback ( ele )
36
+ return
33
37
}
34
38
if ( Array . isArray ( ele ) ) {
35
39
// console.log("ele is array:", ele)
36
- ele . forEach ( ( subEle ) => traverse ( subEle , callback ) ) ;
40
+ ele . forEach ( ( subEle ) => traverse ( subEle , callback ) )
37
41
} else if ( React . isValidElement ( ele ) ) {
38
42
// console.log("ele is react element:", ele)
39
- if ( ele . props . hasOwnProperty ( 'children' ) ) {
43
+ if ( ele . props . includes ( 'children' ) ) {
40
44
// Only if the props of this react element has a children prop.
41
45
// Might not in some cases, e.g. when ele.type === "img"
42
- traverse ( ( ele as JSX . Element ) . props . children , callback ) ;
46
+ traverse ( ( ele as JSX . Element ) . props . children , callback )
43
47
}
44
48
}
45
49
}
46
50
47
51
// Traverse the component tree to count total length of text.
48
- let textLength = 0 ;
52
+ let textLength = 0
49
53
traverse ( children , ( text ) => {
50
54
textLength += text . length
51
55
} )
52
-
56
+
53
57
const handleOnClick = ( event ) => {
54
58
// prevents collapsing the parent component
55
59
event . stopPropagation ( )
@@ -60,17 +64,18 @@ const Explanation = ({ content, children }: ExplanationProps) => {
60
64
const threshold = 1000
61
65
if ( ( children . length > 1 || textLength > threshold ) && ! isExpanded ) {
62
66
let previewTextFound = false
63
- let previewText = ""
67
+ let previewText = ''
64
68
65
- // Traverse the component tree and find the first text node.
69
+ // Traverse the component tree and find the first text node.
66
70
traverse ( children , ( text : string ) => {
67
71
if ( ! previewTextFound ) {
68
72
previewText = text
69
73
previewTextFound = true
70
74
}
71
75
} )
72
76
73
- previewText . substring ( 0 , threshold )
77
+ previewText
78
+ . substring ( 0 , threshold )
74
79
// trims all non-letter characters from end of string
75
80
// this ensures the truncated paragraph looks good with the ellipsis (...)
76
81
. replace ( / [ ^ a - z ] + $ / gi, '' )
0 commit comments