Skip to content

Commit 9c9a052

Browse files
committed
wip: recalculate AdjustLabelFix when propeerties (e.g. label) changes
1 parent fb56b6b commit 9c9a052

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

packages/webui/src/client/ui/ClockView/DirectorScreen.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,15 @@ function DirectorScreenRender({
411411
minLetterSpacing={0}
412412
hardCutText={true}
413413
/>
414-
<span className="director-screen__body__segment__countdown">
415-
<CurrentPartOrSegmentRemaining
416-
currentPartInstanceId={playlist.currentPartInfo?.partInstanceId || null}
417-
heavyClassName="overtime"
418-
preferSegmentTime={true}
419-
/>
420-
</span>
414+
{playlist.currentPartInfo?.partInstanceId ? (
415+
<span className="director-screen__body__segment__countdown">
416+
<CurrentPartOrSegmentRemaining
417+
currentPartInstanceId={playlist.currentPartInfo?.partInstanceId || null}
418+
heavyClassName="overtime"
419+
preferSegmentTime={true}
420+
/>
421+
</span>
422+
) : null}
421423
</div>
422424
{currentPartInstance && currentShowStyleBaseId ? (
423425
<>
@@ -444,7 +446,7 @@ function DirectorScreenRender({
444446
fontSize: '1.4em',
445447
minFontWidth: 32,
446448
maxFontWidth: 90,
447-
minLetterSpacing: 2,
449+
minLetterSpacing: 0,
448450
}}
449451
/>
450452
</div>
@@ -544,7 +546,7 @@ function DirectorScreenRender({
544546
fontSize: '1.4em',
545547
minFontWidth: 32,
546548
maxFontWidth: 90,
547-
minLetterSpacing: 2,
549+
minLetterSpacing: 0,
548550
}}
549551
/>
550552
) : (

packages/webui/src/client/ui/util/AdjustLabelFit.tsx

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef, CSSProperties } from 'react'
1+
import React, { useEffect, useRef, CSSProperties, useLayoutEffect } from 'react'
22

33
export interface AdjustLabelFitProps {
44
/**
@@ -82,6 +82,8 @@ export const AdjustLabelFit: React.FC<AdjustLabelFitProps> = ({
8282
}) => {
8383
const labelRef = useRef<HTMLSpanElement>(null)
8484
const containerRef = useRef<HTMLDivElement>(null)
85+
const prevLabelRef = useRef<string>(label)
86+
8587
// If label is longer than 140 characters, cut it off
8688
if (label.length > 140) {
8789
label = label.slice(0, 137) + '...'
@@ -106,6 +108,21 @@ export const AdjustLabelFit: React.FC<AdjustLabelFitProps> = ({
106108
...(fontSizeValue ? { fontSize: fontSizeValue } : {}),
107109
}
108110

111+
const resetLabelStyles = () => {
112+
if (labelRef.current) {
113+
labelRef.current.style.letterSpacing = '0px'
114+
labelRef.current.style.fontVariationSettings = ''
115+
labelRef.current.textContent = label
116+
labelRef.current.style.wordBreak = 'normal'
117+
labelRef.current.style.whiteSpace = 'nowrap'
118+
119+
// Remove any child spans if they were added in previous calculations
120+
while (labelRef.current.firstChild) {
121+
labelRef.current.removeChild(labelRef.current.firstChild)
122+
}
123+
}
124+
}
125+
109126
const adjustTextToFit = () => {
110127
const labelElement = labelRef.current
111128
const containerElement = containerRef.current
@@ -114,7 +131,8 @@ export const AdjustLabelFit: React.FC<AdjustLabelFitProps> = ({
114131

115132
const DEFAULT_WIDTH = 100
116133
const DEFAULT_OPTICAL_SIZE = 10
117-
labelElement.style.letterSpacing = '0px'
134+
135+
resetLabelStyles()
118136

119137
// Apply the new width setting
120138
labelElement.style.fontVariationSettings = `'opsz' ${DEFAULT_OPTICAL_SIZE}, 'wdth' ${DEFAULT_WIDTH}`
@@ -135,9 +153,7 @@ export const AdjustLabelFit: React.FC<AdjustLabelFitProps> = ({
135153
// Force reflow to ensure measurements are accurate
136154
void labelElement.offsetWidth
137155

138-
// Measure the container and text widths
139156
const containerWidth = containerElement.clientWidth
140-
141157
// Remeasure after font size adjustment
142158
void labelElement.offsetWidth
143159
const newTextWidth = labelElement.getBoundingClientRect().width
@@ -200,7 +216,18 @@ export const AdjustLabelFit: React.FC<AdjustLabelFitProps> = ({
200216
}
201217
}
202218

219+
// Synchronous layout calculation before paint
220+
useLayoutEffect(() => {
221+
// Check if label has changed
222+
if (prevLabelRef.current !== label) {
223+
prevLabelRef.current = label
224+
resetLabelStyles()
225+
adjustTextToFit()
226+
}
227+
}, [label])
228+
203229
useEffect(() => {
230+
// Initial adjustment
204231
const adjustmentTimer = requestAnimationFrame(() => {
205232
adjustTextToFit()
206233
})
@@ -210,27 +237,31 @@ export const AdjustLabelFit: React.FC<AdjustLabelFitProps> = ({
210237
const handleResize = () => {
211238
cancelAnimationFrame(resizeTimer)
212239
resizeTimer = requestAnimationFrame(() => {
213-
// Reset all styles first before recalculating
214-
if (labelRef.current) {
215-
labelRef.current.style.letterSpacing = '0px'
216-
labelRef.current.style.fontVariationSettings = ''
217-
labelRef.current.textContent = label
218-
// Reset the word break and white space properties
219-
labelRef.current.style.wordBreak = 'normal'
220-
labelRef.current.style.whiteSpace = 'nowrap'
221-
}
240+
resetLabelStyles()
222241
adjustTextToFit()
223242
})
224243
}
225244

245+
// Properties change
246+
const handlePropsChange = () => {
247+
resetLabelStyles()
248+
adjustTextToFit()
249+
}
250+
226251
// Adjust on window resize
227252
window.addEventListener('resize', handleResize)
253+
254+
// Run adjustment when width or font settings change
255+
if (width || fontFamily || fontSize || minFontWidth || maxFontWidth || minLetterSpacing) {
256+
handlePropsChange()
257+
}
258+
228259
return () => {
229260
window.removeEventListener('resize', handleResize)
230261
cancelAnimationFrame(adjustmentTimer)
231-
cancelAnimationFrame(resizeTimer)
262+
if (resizeTimer) cancelAnimationFrame(resizeTimer)
232263
}
233-
}, [label, width, fontFamily, fontSize, minFontWidth, maxFontWidth, minLetterSpacing])
264+
}, [width, fontFamily, fontSize, minFontWidth, maxFontWidth, minLetterSpacing])
234265

235266
return (
236267
<div ref={containerRef} className={`adjust-label-fit ${className}`} style={finalContainerStyle}>

0 commit comments

Comments
 (0)