-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: BoldNumber text resizing (#18820)
- Loading branch information
1 parent
d65a775
commit e94bca5
Showing
11 changed files
with
119 additions
and
101 deletions.
There are no files selected for viewing
Binary file modified
BIN
+99 Bytes
(100%)
frontend/__snapshots__/exporter-exporter--trends-number-insight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1 Byte
(100%)
frontend/__snapshots__/scenes-app-insights--trends-number--webkit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-12 Bytes
(100%)
frontend/__snapshots__/scenes-app-insights--trends-number-edit--webkit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-25 Bytes
(100%)
frontend/__snapshots__/scenes-app-insights--trends-number-edit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1 Byte
(100%)
frontend/__snapshots__/scenes-app-insights--trends-number.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
frontend/src/scenes/insights/views/BoldNumber/Textfit.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Meta, StoryFn, StoryObj } from '@storybook/react' | ||
|
||
import { Textfit } from './Textfit' | ||
|
||
type Story = StoryObj<typeof Textfit> | ||
const meta: Meta<typeof Textfit> = { | ||
title: 'Lemon UI/TextFit', | ||
component: Textfit, | ||
tags: ['autodocs'], | ||
args: { | ||
min: 20, | ||
max: 150, | ||
children: '10000000', | ||
}, | ||
} | ||
export default meta | ||
|
||
const Template: StoryFn<typeof Textfit> = (props) => { | ||
return ( | ||
<div className="resize w-100 h-50 overflow-hidden border rounded"> | ||
<Textfit {...props} /> | ||
</div> | ||
) | ||
} | ||
|
||
export const Basic: Story = Template.bind({}) |
108 changes: 51 additions & 57 deletions
108
frontend/src/scenes/insights/views/BoldNumber/Textfit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,77 @@ | ||
// Adapted from https://github.com/malte-wessel/react-textfit | ||
// which is no longer maintained and does not support React 18 | ||
import { useRef } from 'react' | ||
import useResizeObserver from 'use-resize-observer' | ||
|
||
import { useEffect, useRef, useState } from 'react' | ||
|
||
// Calculate width without padding. | ||
const innerWidth = (el: HTMLDivElement): number => { | ||
const style = window.getComputedStyle(el, null) | ||
// Hidden iframe in Firefox returns null, https://github.com/malte-wessel/react-textfit/pull/34 | ||
if (!style) { | ||
return el.clientWidth | ||
} | ||
|
||
return ( | ||
el.clientWidth - | ||
parseInt(style.getPropertyValue('padding-left'), 10) - | ||
parseInt(style.getPropertyValue('padding-right'), 10) | ||
) | ||
export type TextfitProps = { | ||
min: number | ||
max: number | ||
children: string | ||
} | ||
|
||
const assertElementFitsWidth = (el: HTMLDivElement, width: number): boolean => el.scrollWidth - 1 <= width | ||
|
||
const Textfit = ({ min, max, children }: { min: number; max: number; children: React.ReactNode }): JSX.Element => { | ||
export const Textfit = ({ min, max, children }: TextfitProps): JSX.Element => { | ||
const parentRef = useRef<HTMLDivElement>(null) | ||
const childRef = useRef<HTMLDivElement>(null) | ||
|
||
const [fontSize, setFontSize] = useState<number>() | ||
const fontSizeRef = useRef<number>(min) | ||
|
||
let resizeTimer: NodeJS.Timeout | ||
|
||
const handleWindowResize = (): void => { | ||
const updateFontSize = (size: number): void => { | ||
fontSizeRef.current = size | ||
childRef.current!.style.fontSize = `${size}px` | ||
} | ||
|
||
const handleResize = (): void => { | ||
clearTimeout(resizeTimer) | ||
resizeTimer = setTimeout(() => { | ||
const el = parentRef.current | ||
const wrapper = childRef.current | ||
const parent = parentRef.current | ||
const child = childRef.current | ||
|
||
if (!parent || !child) { | ||
return | ||
} | ||
|
||
if (el && wrapper) { | ||
const originalWidth = innerWidth(el) | ||
let mid | ||
let low = min | ||
let high = max | ||
|
||
let mid | ||
let low = min | ||
let high = max | ||
while (low <= high) { | ||
mid = Math.floor((low + high) / 2) | ||
updateFontSize(mid) | ||
const childRect = child.getBoundingClientRect() | ||
const parentRect = parent.getBoundingClientRect() | ||
|
||
while (low <= high) { | ||
mid = Math.floor((low + high) / 2) | ||
setFontSize(mid) | ||
const childFitsParent = childRect.width <= parentRect.width && childRect.height <= parentRect.height | ||
|
||
if (assertElementFitsWidth(wrapper, originalWidth)) { | ||
low = mid + 1 | ||
} else { | ||
high = mid - 1 | ||
} | ||
if (childFitsParent) { | ||
low = mid + 1 | ||
} else { | ||
high = mid - 1 | ||
} | ||
mid = Math.min(low, high) | ||
} | ||
mid = Math.min(low, high) | ||
|
||
// Ensure we hit the user-supplied limits | ||
mid = Math.max(mid, min) | ||
mid = Math.min(mid, max) | ||
// Ensure we hit the user-supplied limits | ||
mid = Math.max(mid, min) | ||
mid = Math.min(mid, max) | ||
|
||
setFontSize(mid) | ||
} | ||
}, 10) | ||
updateFontSize(mid) | ||
}, 50) | ||
} | ||
|
||
useEffect(() => { | ||
window.addEventListener('resize', handleWindowResize) | ||
return () => window.removeEventListener('resize', handleWindowResize) | ||
}, []) | ||
|
||
useEffect(() => handleWindowResize(), [parentRef, childRef]) | ||
useResizeObserver<HTMLDivElement>({ | ||
ref: parentRef, | ||
onResize: () => handleResize(), | ||
}) | ||
|
||
return ( | ||
// eslint-disable-next-line react/forbid-dom-props | ||
<div ref={parentRef} style={{ lineHeight: 1, fontSize: fontSize }}> | ||
{/* eslint-disable-next-line react/forbid-dom-props */} | ||
<div ref={childRef} style={{ whiteSpace: 'nowrap', display: 'inline-block' }}> | ||
<div | ||
ref={parentRef} | ||
className="w-full h-full flex items-center justify-center" | ||
// eslint-disable-next-line react/forbid-dom-props | ||
style={{ lineHeight: 1, fontSize: fontSizeRef.current }} | ||
> | ||
<div ref={childRef} className="whitespace-nowrap"> | ||
{children} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default Textfit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters