Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perfectly Fitting Text to Container in React Blog Post #146

Merged
merged 28 commits into from
Oct 22, 2024

Conversation

gggritso
Copy link
Member

Hello.

Copy link

vercel bot commented Oct 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sentry-engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 22, 2024 3:56pm

@gggritso gggritso marked this pull request as ready for review October 18, 2024 17:35
@gggritso gggritso requested review from a team and JonasBa October 18, 2024 17:35
gggritso and others added 2 commits October 21, 2024 11:39
Co-authored-by: Jonas <jonas@badalic.com>
Co-authored-by: Jonas <jonas@badalic.com>
Copy link

@DominikB2014 DominikB2014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a great read! It really illustrates how a problem that seems so simple on the surface, is much more complicated then it appears. I think you did a good job putting in the right details, without making it too complicated to read.

- `useLayoutEffect` fires because the font size changed. It checks the elements and finds that the child underflows the parent by a lot. It's too small! It updates the font size bounds to 50px and 100px respectively. It sets the new font size to halfway between the bounds (75px)
- `useLayoutEffect` fires because the font size changed. It checks the elements and find that the child is almost the same size as the parent, within 5px in width. We're done! Stop iterating

Note: Iteration only stops when the element _fits inside_, on that _tick_ of the algorithm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Fits inside" here seems a bit ambiguous - when I read that, I think Item A can contain Item B with any amount of excess space to fit more stuff. Rather than something being a "correct fit." The first lacks the precision that I think you're trying to express? Maybe tie this back to the requirements you set out earlier (near-perfect fit within 1-2%) so that it's a bit more clear when the iterations stop.

As I read this more, the whole Note feels somewhat redundant with the useLayoutEffect bullet point. But similar feedback on that bullet point - does the interaction stop at 5px, or does it stop at a specific percent of closeness (which happens to be 5px in this example)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha! I clarified this paragraph. The idea is that the algorithm looks for a close-enough fit, but only stops if the fit is close and the content underflows the parent. This way we never cut off the content


In the case of a page resize, they _do_ have to run all at once, but in my opinion that experience is still acceptable. Waiting a few hundred milliseconds on page resize is normal, and there are other more expensive operations running during a resize anyway.

Success! I can ship, and reduce the max iteration count to 20, just to be generous.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reducing the count to be generous feels weird. Maybe "set the max iteration count to 20, just to be generous"? I think I know what you're trying to get at here - the max measured iteration count was 10, so setting it to 20 is generous in that context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair! I removed this paragraph and added more details to the paragraph below to explain the 20

@gggritso gggritso merged commit f430309 into main Oct 22, 2024
6 checks passed
@gggritso gggritso deleted the feat/auto-fit-text-to-container-in-react branch October 22, 2024 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants