@@ -6,7 +6,7 @@ dayjs.extend(weekOfYear)
66dayjs . extend ( weekYear )
77dayjs . extend ( localeData )
88import type React from "react"
9- import { Fragment , type RefObject , useRef } from "react"
9+ import { Fragment , type RefObject , useCallback , useRef , useState } from "react"
1010
1111// if more locales then english and germans are needed, we need to enable them first here
1212import "dayjs/locale/de"
@@ -22,6 +22,7 @@ import type {
2222 TimeTableViewType ,
2323} from "./TimeTable"
2424import type { TimeFrameDay } from "./TimeTableConfigStore"
25+ import useResizeObserver , { type ObservedSize } from "use-resize-observer"
2526
2627const headerTimeSlotFormat : { [ viewType in TimeTableViewType ] : string } = {
2728 hours : "HH:mm" ,
@@ -69,6 +70,7 @@ export type CustomHeaderRowTimeSlotProps<
6970 entries : TimeTableEntry < G , I > [ ]
7071 slotsArray : readonly Dayjs [ ]
7172 tableCellRef : RefObject < HTMLTableCellElement >
73+ tableCellWidth : number
7274}
7375
7476export type CustomHeaderRowHeaderProps <
@@ -358,7 +360,22 @@ function CustomHeaderRowCell<
358360 header : ( props : CustomHeaderRowHeaderProps < G , I > ) => JSX . Element
359361 }
360362} ) {
361- const ref = useRef < HTMLTableCellElement > ( null )
363+ // this is the same as in the TableCell component
364+ const tableCellRef = useRef < HTMLTableCellElement > ( null )
365+ const [ tableCellWidth , setTableCellWidth ] = useState (
366+ tableCellRef . current ?. offsetWidth ?? 70 ,
367+ )
368+
369+ const resizeCallback = useCallback ( ( observedSize : ObservedSize ) => {
370+ setTableCellWidth ( observedSize . width ?? 70 )
371+ } , [ ] )
372+ useResizeObserver ( {
373+ ref : tableCellRef ,
374+ onResize : resizeCallback ,
375+ box : "border-box" ,
376+ round : ( n : number ) => n , // we don't need rounding here
377+ } )
378+ //
362379
363380 return (
364381 < th
@@ -367,7 +384,7 @@ function CustomHeaderRowCell<
367384 className = { `${ headerCellBaseClassname } ${
368385 isLastOfDay ? "after:border-l-2" : "after:border-l"
369386 } `}
370- ref = { ref }
387+ ref = { tableCellRef }
371388 >
372389 { customHeaderRow . timeSlot ( {
373390 timeSlot,
@@ -377,7 +394,8 @@ function CustomHeaderRowCell<
377394 viewType,
378395 entries,
379396 slotsArray,
380- tableCellRef : ref ,
397+ tableCellRef,
398+ tableCellWidth,
381399 } ) }
382400 </ th >
383401 )
0 commit comments