Skip to content

Commit 04f06c2

Browse files
Update index.js
fix: add null check + lint
1 parent f7b4a3f commit 04f06c2

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

src/index.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
import React, { useEffect, useRef, useState } from "react";
2-
import PropTypes from "prop-types";
3-
import * as clampy_ from "@clampy-js/clampy/dist/clampy.umd.js";
4-
import * as elementResizeDetectorMaker_ from "element-resize-detector";
1+
import React, { useEffect, useRef, useState } from 'react'
2+
import PropTypes from 'prop-types'
3+
import * as clampy_ from '@clampy-js/clampy/dist/clampy.umd.js'
4+
import * as elementResizeDetectorMaker_ from 'element-resize-detector'
55

66
const elementResizeDetectorMaker =
7-
elementResizeDetectorMaker_.default || elementResizeDetectorMaker_;
8-
const clampy = clampy_.default || clampy_;
7+
elementResizeDetectorMaker_.default || elementResizeDetectorMaker_
8+
const clampy = clampy_.default || clampy_
99

1010
function Clampy(props) {
11-
const [opacity, setOpacity] = useState(0);
12-
let containerRef = useRef(null);
13-
let initialContent;
11+
const [opacity, setOpacity] = useState(0)
12+
let containerRef = useRef(null)
13+
let initialContent
1414
if (!initialContent) {
15-
initialContent = props.children;
15+
initialContent = props.children
1616
}
1717

18-
let resizeDetector = elementResizeDetectorMaker({ strategy: "scroll" });
18+
let resizeDetector = elementResizeDetectorMaker({ strategy: 'scroll' })
1919

2020
const onResize = () => {
21-
truncate();
22-
};
21+
truncate()
22+
}
2323

2424
const truncate = () => {
25-
setOpacity(0);
2625
const options = {
2726
clamp: props.clampSize,
2827
truncationChar: props.truncationChar,
@@ -31,51 +30,54 @@ function Clampy(props) {
3130
// however this can leads to unexpected results so we need to explicitely
3231
// disable it.
3332
useNativeClamp: false
34-
};
33+
}
3534

36-
if (initialContent) {
37-
containerRef.current.innerHTML = initialContent;
35+
if (initialContent && containerRef.current) {
36+
containerRef.current.innerHTML = initialContent
3837
}
39-
clampy.clamp(containerRef.current, options);
40-
setOpacity(1);
41-
};
38+
if (containerRef.current) {
39+
setOpacity(0)
40+
clampy.clamp(containerRef.current, options)
41+
setOpacity(1)
42+
}
43+
}
4244

4345
useEffect(() => {
44-
truncate();
46+
truncate()
4547

4648
resizeDetector.listenTo(containerRef.current, () => {
47-
truncate();
48-
});
49+
truncate()
50+
})
4951

50-
window.addEventListener("resize", onResize);
52+
window.addEventListener('resize', onResize)
5153

5254
return () => {
5355
// cleanup
54-
resizeDetector.removeAllListeners(containerRef.current);
55-
window.removeEventListener("resize", onResize);
56-
};
57-
}, [initialContent, props, truncate, onResize, resizeDetector]);
56+
resizeDetector.removeAllListeners(containerRef.current)
57+
window.removeEventListener('resize', onResize)
58+
}
59+
}, [])
5860

5961
return React.createElement(
60-
"div",
62+
'div',
6163
{
6264
ref: containerRef,
6365
style: { opacity: opacity }
6466
},
6567
props.children
66-
);
68+
)
6769
}
6870

6971
Clampy.propTypes = {
7072
children: PropTypes.node,
7173
clampSize: PropTypes.string,
7274
truncationChar: PropTypes.string
73-
};
75+
}
7476

7577
Clampy.defaultProps = {
76-
clampSize: "auto", // Default clamp size based on available height
77-
truncationChar: "…"
78+
clampSize: 'auto', // Default clamp size based on available height
79+
truncationChar: '…'
7880
// truncationHTML: ''
79-
};
81+
}
8082

81-
export default Clampy;
83+
export default Clampy

0 commit comments

Comments
 (0)