And another thing that I noticed is that assistive technologies such as screen readers, they handle focus changes very, very, very well. Like I had tried different approaches such as using an ARIA alert or having an ARIA label that you update occasionally, but it was always that odd one out, that odd browser that wouldn't work, or that odd screen reader or that platform, like there was always something somewhere not quite working, but the only thing I found that works very, very reliably is focus changes. This works on every single browser, on every single screen reader, and on every single everything that I have ever tested.
So let's kind of have a look at this demo a bit to understand what's actually going on. So let me just walk you through the code. Here we've just got some input handling, and we draw the ball. The rendering logic is important, so I'm not showing that. And then what we do here is the actual swapping. So we swap around the purpose of these two proxy elements that we've got. And then what I do is the new one that's about to be shown, I update the text value, the ARIA label of the announcer, and I set it to visible. And then the other one, I set that to invisible.
Let's make it hidden, and then we do the focus, which is actually the swap. And so let's have a look at what happens when that moves around. The main thing, the first thing you want to look out for is this style property. You'll see one of them is always on display block, which means visible, and the other one is on display none, hidden. As you see, as I move left and right, these keep switching back and forth. Block, none, block, none, block, none, block, none, block, none, block, none. The next thing you want to look out for is this text value. So I always have to update the text value of the next one I'm about to show. So here, if I want to go to the right, I need to update this to position two.
Comments