|
1 | | -import { useState, useEffect, useRef, useCallback } from 'react' |
| 1 | +import { useState, useEffect, useRef, useLayoutEffect } from 'react' |
2 | 2 |
|
3 | | -export const use_compacting = (max_compact_step = 4) => { |
| 3 | +export const use_compacting = () => { |
4 | 4 | const container_ref = useRef<HTMLDivElement>(null) |
5 | 5 | const [compact_step, set_compact_step] = useState(0) |
6 | 6 | const [thresholds, set_thresholds] = useState<Record<number, number>>({}) |
7 | 7 |
|
8 | | - const report_width = useCallback((width: number, step: number) => { |
| 8 | + useLayoutEffect(() => { |
| 9 | + if (!container_ref.current) return |
| 10 | + const container = container_ref.current |
| 11 | + |
| 12 | + const original_width = container.style.width |
| 13 | + const original_max_width = container.style.maxWidth |
| 14 | + const original_flex_shrink = container.style.flexShrink |
| 15 | + const original_transition = container.style.transition |
| 16 | + |
| 17 | + container.style.transition = 'none' |
| 18 | + container.style.width = 'max-content' |
| 19 | + container.style.maxWidth = 'none' |
| 20 | + container.style.flexShrink = '0' |
| 21 | + |
| 22 | + const width = container.getBoundingClientRect().width |
| 23 | + |
| 24 | + container.style.width = original_width |
| 25 | + container.style.maxWidth = original_max_width |
| 26 | + container.style.flexShrink = original_flex_shrink |
| 27 | + container.style.transition = original_transition |
| 28 | + |
9 | 29 | set_thresholds((prev) => { |
10 | 30 | const threshold = Math.ceil(width) |
11 | | - if (prev[step] == threshold) return prev |
12 | | - return { ...prev, [step]: threshold } |
| 31 | + if (prev[compact_step] == threshold) return prev |
| 32 | + return { ...prev, [compact_step]: threshold } |
13 | 33 | }) |
14 | | - }, []) |
| 34 | + }, [compact_step]) |
15 | 35 |
|
16 | 36 | useEffect(() => { |
17 | 37 | const observer = new ResizeObserver((entries) => { |
18 | | - const width = entries[0].contentRect.width |
| 38 | + const container = entries[0].target as HTMLElement |
| 39 | + const width = container.getBoundingClientRect().width |
19 | 40 |
|
20 | | - // Try to expand (reduce compaction) |
21 | 41 | for (let i = 0; i < compact_step; i++) { |
22 | 42 | if (thresholds[i] !== undefined && width >= thresholds[i]) { |
23 | 43 | set_compact_step(i) |
24 | 44 | return |
25 | 45 | } |
26 | 46 | } |
27 | 47 |
|
28 | | - // Try to shrink (increase compaction) |
29 | 48 | if (thresholds[0] !== undefined && width < thresholds[0]) { |
30 | 49 | if (compact_step == 0) { |
31 | 50 | set_compact_step(1) |
32 | | - } else if (compact_step < max_compact_step) { |
| 51 | + } else { |
33 | 52 | if ( |
34 | 53 | thresholds[compact_step] !== undefined && |
35 | 54 | width < thresholds[compact_step] |
36 | 55 | ) { |
37 | | - set_compact_step(compact_step + 1) |
| 56 | + if (thresholds[compact_step] < thresholds[compact_step - 1]) { |
| 57 | + set_compact_step(compact_step + 1) |
| 58 | + } |
38 | 59 | } |
39 | 60 | } |
40 | 61 | } |
41 | 62 | }) |
42 | 63 |
|
43 | 64 | if (container_ref.current) observer.observe(container_ref.current) |
44 | 65 | return () => observer.disconnect() |
45 | | - }, [thresholds, compact_step, max_compact_step]) |
| 66 | + }, [thresholds, compact_step]) |
46 | 67 |
|
47 | 68 | return { |
48 | 69 | container_ref, |
49 | | - compact_step, |
50 | | - report_width |
| 70 | + compact_step |
51 | 71 | } |
52 | 72 | } |
0 commit comments