-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtable-width-fix.tsx
More file actions
60 lines (48 loc) · 1.93 KB
/
table-width-fix.tsx
File metadata and controls
60 lines (48 loc) · 1.93 KB
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
'use client'
import { useEffect } from 'react'
/**
* Component to fix table column width synchronization
* Ensures that tbody rows use the same column widths as thead
*/
export default function TableWidthFix() {
useEffect(() => {
const syncTableColumnWidths = () => {
const tables = document.querySelectorAll('main table')
tables.forEach((table) => {
const thead = table.querySelector('thead')
const tbody = table.querySelector('tbody')
if (thead && tbody) {
const theadCells = Array.from(thead.querySelectorAll('th, td'))
if (theadCells.length === 0) return
// Get computed widths from thead cells
const theadWidths = theadCells.map((cell) => {
const computed = window.getComputedStyle(cell)
return computed.width
})
// Apply thead column widths to all tbody rows
const allTbodyRows = tbody.querySelectorAll('tr')
allTbodyRows.forEach((row) => {
const rowCells = Array.from(row.querySelectorAll('td, th'))
rowCells.forEach((cell, cellIndex) => {
if (theadWidths[cellIndex]) {
;(cell as HTMLElement).style.width = theadWidths[cellIndex]
;(cell as HTMLElement).style.minWidth = theadWidths[cellIndex]
;(cell as HTMLElement).style.maxWidth = theadWidths[cellIndex]
}
})
})
}
})
}
// Run on mount and after a short delay to ensure DOM is ready
syncTableColumnWidths()
const timeoutId = setTimeout(syncTableColumnWidths, 100)
// Also run when window is resized
window.addEventListener('resize', syncTableColumnWidths)
return () => {
clearTimeout(timeoutId)
window.removeEventListener('resize', syncTableColumnWidths)
}
}, [])
return null // This component doesn't render anything
}