1
1
import { useCallback , useRef } from "react"
2
2
3
- const initialRect = {
4
- width : - 1 ,
5
- tabRight : - 1 ,
6
- }
7
-
8
- const hasChanged = ( prevValues , values ) => {
9
- if ( prevValues . width === initialRect . width ) return true
10
- if ( prevValues . width === values . width && prevValues . tabRight !== values . tabRight ) return true
11
- return prevValues . width !== values . width
12
- }
3
+ const initialScrollWidth = - 1
4
+ const initialClientWidth = - 1
13
5
14
6
export default ( parentRef , ref , target , callback , deps ) => {
15
- const prevValuesRef = useRef ( initialRect )
7
+ const prevScrollWidthRef = useRef ( initialScrollWidth )
8
+ const prevClientWidthRef = useRef ( initialClientWidth )
16
9
17
10
return useCallback ( ( ) => {
18
11
if ( ! ref . current || ! target . current || ! parentRef || ! parentRef . current ) return
@@ -24,42 +17,25 @@ export default (parentRef, ref, target, callback, deps) => {
24
17
const container = parentRef . current
25
18
const { right : containerRight , left : containerLeft } = container . getBoundingClientRect ( )
26
19
27
- if ( ! prevValuesRef . current . expandedStaticWidth ) {
28
- prevValuesRef . current . expandedStaticWidth = draggableLeft - containerLeft
29
- }
30
-
31
20
const { right : tabRight , width : tabWidth } = target . current . getBoundingClientRect ( )
32
21
33
- if ( ! hasChanged ( prevValuesRef . current , { width, tabRight } ) ) return
22
+ if (
23
+ prevScrollWidthRef . current === draggableTabs . scrollWidth &&
24
+ prevClientWidthRef . current === width
25
+ )
26
+ return
27
+ prevScrollWidthRef . current = draggableTabs . scrollWidth
28
+ prevClientWidthRef . current = width
34
29
35
30
const padRight = width - containerRight
36
31
if (
37
32
tabRight < width - padRight &&
38
- containerLeft +
39
- prevValuesRef . current . expandedStaticWidth +
40
- ( tabRight - draggableLeft ) +
41
- padRight >
33
+ containerLeft + ( draggableLeft - containerLeft ) + ( tabRight - draggableLeft ) + padRight >
42
34
width
43
35
)
44
36
return
45
37
46
- prevValuesRef . current = {
47
- ...prevValuesRef . current ,
48
- width,
49
- tabRight,
50
- containerRight,
51
- }
52
-
53
- if ( tabRight >= containerRight && ! prevValuesRef . current . collapse ) {
54
- prevValuesRef . current . collapse = true
55
- return callback ( true )
56
- }
57
- if (
58
- tabRight + tabWidth < containerRight &&
59
- ( prevValuesRef . current . collapse || typeof prevValuesRef . current . collapse === "undefined" )
60
- ) {
61
- prevValuesRef . current . collapse = false
62
- return callback ( false )
63
- }
38
+ if ( tabRight >= containerRight ) return callback ( true )
39
+ if ( tabRight + tabWidth < containerRight ) return callback ( false )
64
40
} , deps )
65
41
}
0 commit comments