-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[Tabs] TabIndicator width is being calculated before fonts are loaded #7187
Comments
I have noticed that issue many times. Any idea how to fix it at the library level? I thought that if this was going to be a painful issue, I would have make so that the default font (not the one loaded) would match the spacing of the loaded one as much as possible. |
For the Tabs, there's two possible solutions that I can think of.
An idea for fixing this globally would be to create a component that listens for a font to be loaded. It could broadcast a global event which other components could listen to. What are your thoughts on these? |
Yeah, we could try calling it a few time right after the first mount hoping for the better (the font to be loaded). This is a naive and poorly performant solution. Let's see if we can do better.
I'm not sure we can find something that works across our supported browsers. Oh yes, there is fontfaceobserver. This issue makes me think that we need a strong story around font loading. This article is a must-read on the topic. This tool is interesting regarding reducing width jump. It can be used to make is imperceptible. Terminology:
|
I've thought about this problem for a bit, and upon weighing my options, I decided that implementing an 'initial' indicator (for the first render) on the tab itself, and then using an absolutely positioned indicator on subsequent renders (once the tab index changes). The two indicators are mutually exclusive: once the tab index changes, you transition from using the 'initial' indicator to the one absolutely positioned with respect to all the tabs. By relying on the width of the tab, the 'initial' indicator automatically reflows when content changes -- so the timing FOUT issue fizzles out. The strongest upside to this approach is that you don't have to watch fonts or add in some external library that handles watching resize events on DOM elements. Trying to implement watchers for either of those is bound to be filled with corner cases, especially when you're trying to support many different browsers. You do need to keep an internal boolean in the Tabs component state to keep track of if you have a changed tab index or not. Before: After: Here's my implementation but it'd be pretty easy to adapt the concepts: Initially shown indicator: New state to keep track of: |
@petermikitsh Thanks for sharing your approach but I don't think that we want to go into that direction. We need to address the root of the issue. It's not ok to have the font "jump" that way. Once we solve it, the tab issue is solved for free. |
@oliviertassinari To my understanding (and please correct if I'm wrong here) the indicator width is calculated before the custom font is loaded. So suppose this is how you have your DOM configured, with respect to getting your fonts initialized: <html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
<style type="text/css">
html, body {
font-family: 'Roboto', sans-serif;
}
</style>
</head>
<body>
...
</body>
</html> The browser will first render with But since you calculated the width of the indicator when I think it ultimately comes down to being a limitation of the browser. The browser asynchronously reflowing when a font loads is something you cannot control. But you can control when and how you calculate the indicator width. I think you'd have to be watching for when the font is loaded, and then recalculate the indicator styles. That would probably mean having an interface for consumers to tell you what custom font they use in their |
Yes, that's what is happening.
I don't agree, following my previous link regarding how to handle font loading: #7187 (comment)
I need to implement one of these solutions at work to be sure but I believe we can close the issue with: fix the font loading, no need for extra code complexity on the tabs component (that have a cost, like extra payload) |
We went with changing the implementing to work server side. For the first server side render, we are using a width: 100% under the active tab. When the client side reconcile, we switch back to measuring the tab position. As raised by #13165, we could push the approach one step further. Instead of using the tab position for positioning the indicator as soon as possible on the client side, we could wait for an index change event, providing more time for the fonts to load. The current workaround is to trigger an indicator position update with the |
Hello!
I believe I've found a small bug. I'm happy to take a look at fixing this, but I could use some direction on what would be the best approach.
Problem description
The width of
TabIndicator
s seem to be calculated before fonts are loaded. Here's an example of what I'm seeing:Notice the indicator is wider than the radial background. Here is an example of the background on
Tabs
usingHelvetica
:Both changing tabs and resizing the window fires events that cause the indicator to resize itself correctly.
Link to minimal working code that reproduces the issue
https://github.com/zachwolf/tab-spacing-demo
Versions
Thanks!
The text was updated successfully, but these errors were encountered: