-
Notifications
You must be signed in to change notification settings - Fork 22
/
useDetectTruncate.ts
63 lines (50 loc) · 1.64 KB
/
useDetectTruncate.ts
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
import { RefCallback, useCallback, useEffect, useRef, useState } from 'react'
export type UseDetectTruncateReturn = {
textRef: RefCallback<HTMLParagraphElement | null>
truncated: boolean
}
// This hook detects when text is truncated.
//
// To use it, simply call this hook, and then assign the `textRef` to the
// appropriate element. The `truncated` value will be updated when the text is
// truncated.
export const useDetectTruncate = (): UseDetectTruncateReturn => {
const [truncated, setTruncated] = useState(false)
// Detect when the recipient wraps to the next page to change arrow.
const [refSet, setRefSet] = useState(0)
const _textRef = useRef<HTMLParagraphElement | null>(null)
const textRef: RefCallback<HTMLParagraphElement> = useCallback((node) => {
_textRef.current = node
setRefSet((refSet) => refSet + 1)
}, [])
useEffect(() => {
if (typeof window === 'undefined') {
return
}
const onResize = () => {
if (!_textRef.current) {
return
}
const { offsetWidth, scrollWidth } = _textRef.current
setTruncated(scrollWidth > offsetWidth)
}
// Trigger initial set.
onResize()
// Observe changes to the container and child elements.
const observer = new ResizeObserver(onResize)
if (_textRef.current) {
observer.observe(_textRef.current)
}
// Trigger re-render when window is resized.
window.addEventListener('resize', onResize)
return () => {
observer.disconnect()
window.removeEventListener('resize', onResize)
}
// Trigger re-render when refs are set.
}, [refSet])
return {
textRef,
truncated,
}
}