Skip to content

Commit d7f32cf

Browse files
Merge pull request #68 from linked-planet/dev
added centered loading spinner and time table custom header width han…
2 parents b9c57d4 + 1ea68f0 commit d7f32cf

File tree

4 files changed

+332
-279
lines changed

4 files changed

+332
-279
lines changed

library/src/components/LoadingSpinner.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { type CSSProperties } from "react"
1+
import type { CSSProperties } from "react"
22
import { twMerge } from "tailwind-merge"
33
type Size = "xsmall" | "small" | "medium" | "large" | "xlarge"
44

@@ -44,3 +44,25 @@ export function LoadingSpinner({
4444
/>
4545
)
4646
}
47+
48+
export function LoadingSpinnerCentered({
49+
style,
50+
className,
51+
size,
52+
}: {
53+
style?: React.CSSProperties
54+
className?: string
55+
size?: Size
56+
}) {
57+
return (
58+
<div
59+
className={twMerge(
60+
"absolute inset-0 flex justify-center items-center",
61+
className,
62+
)}
63+
style={style}
64+
>
65+
<LoadingSpinner size={size} />
66+
</div>
67+
)
68+
}

library/src/components/timetable/TimeTableHeader.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dayjs.extend(weekOfYear)
66
dayjs.extend(weekYear)
77
dayjs.extend(localeData)
88
import 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
1212
import "dayjs/locale/de"
@@ -22,6 +22,7 @@ import type {
2222
TimeTableViewType,
2323
} from "./TimeTable"
2424
import type { TimeFrameDay } from "./TimeTableConfigStore"
25+
import useResizeObserver, { type ObservedSize } from "use-resize-observer"
2526

2627
const 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

7476
export 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

Comments
 (0)