Post

How do you make a button that doesn't change it's width when it's in a loading state? Use CSS Grid to stack items, and visibility: hidden to hide/show spinner! This approach allows the largest item to size the button, and keeps the button text accessible

19 reposts299 likes

Please don't do this. Never hide context to the user while something is loading. You're much better showing the loading state over the content than hiding the content. Here's an example of my loading button:

In isolation maybe but it feels very natural in real-world situations. It’s far less unnerving than having content disappear during a mutation or action.

Yeah I agree that there are better UI patterns than this. It’s the same with labels that disappear as you start typing

Great video, thanks Wes! I’d go one step further and use both opacity and visibility, as then you’re able to transition between the two for a nice fade effect. Also, small perf hack, if you go from opacity: 0.001 -> 1 instead of 0 ->1, it can use GPU to render. (I stole that from GSAP years ago)

Just take an image of the base button, send it to a Cloudflare Worker, calculate the exact width based on screen size, send back a new loading button and replace it with HTMX. ez

I was thinking about this further. The reason I have continued to do it this way is CSS Grid isn’t available in React Native. I recognize this is a web tip. Most people won’t have that constraint.

I think ideally you'd use aria-live to announce that a save is happening - but as always, it depends on what you are doing

I see what you’re getting at. I’ve never really worried about updating the text to “Saving…” but that makes sense. I’m no accessibility expert, happy to learn another nugget.

Re: accessibility: while this is ok, it's generally better to use actual text (probably a `.sr-only` span) than `aria-label`. Text is more easily picked up by ATs universally, and can also be translated to other languages more easily than HTML attributes. Plus: don't use ARIA if you don't have to.

Nice! I used the opacity: 0; + abs positioning trick on a project recently but definitely was a bit concerned about a11y. I dig this method, very elegant

Fun fact, if you play back the video at 0.25x the speed not only can I hear every other word, but it makes you sound like you are drunkenly mansplaining CSS

😟 It’s too long, I’ve skeeted like 10 times about how I’d pay for longer and better quality uploads. Isn’t this the platform where we’re allowed to post links? I just wanna share what I made and I can’t upload it here

I hate replies like these. I use Shadcn, but are you not curious how things work? Do you not hit issues where you need to position things or do custom CSS?

I realize my joke came across differently than I intended. Sorry for causing any upset, that wasn't my goal. I absolutely love to get into details on how things work. I learned a lot of front-end stuff from you and will continue to do so.