Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Experiment: Replicate Navigation block using directives (Alpinejs) #44289
Experiment: Replicate Navigation block using directives (Alpinejs) #44289
Changes from 4 commits
7de6de2
cc19f6a
c44fbb7
c406a3d
473f140
a134608
0a0598f
b4ebb34
31871e8
df54ee4
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few things here with the
$focus.within($refs.items)
:The
$focus.within($refs.items).first()
part is not triggered when you focus on the hamburguer menu and useEnter
to open the menu.I wonder what directive/pattern we could use here to take into account both the click and the keyboard. Maybe a simple
x-effect
that reacts toopen === true
to make it more declarative?$focus.within($refs.items).first()
is focusing the last element to me! I have no idea why.ezgif-4-d303afd612.mp4
On close, the
micromodal
version focus the hamburguer menu (shown in video above). We should include that.What do you think about the
$focus.within()
API. Was it easy to understand? Would it be an easier way to deal with the focus?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about these two points. It seems to be working for me. Or am I doing something wrong?
https://www.loom.com/share/a144d46e28e6403ca77e2fa7674c87f2
Regarding this, I believed it was working 🤷 I have just pushed a commit fixing it using
x-ref
and$focus
again. Not sure if it is the best way though, maybe we could use just something likedocument.querySelector(".hamburger").focus()
.For me, it was easy to understand but maybe it was just because there is an example really similar to this use case in the docs. If that wasn't the case, I would have probably struggled to understand it.
Regarding if there is a better way to handle the focus, maybe for these cases using the directives is not 100% necessary and we could use just
document.querySelector(".hamburger").focus()
as mentioned above.Anyway, the functions of the
$focus
seem easy to use to me and cover many use cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you're right. Alpine uses
tappable
, and the list of focusable elements is not the same as the one in micromodal, so maybe that's why. Don't worry about it. It's not important.I've moved the focus to an
x-effect
directive (which is similar to React'suseEffect
) in 0a0598f to make it more declarative. It seems to work fine, but could you please check it out?x-effect="open === true ? $focus.within($refs.items).first() : $focus.focus($refs.hamburger)
I couldn't use
if ... else
inside thex-effect
. I'm not sure if I was doing something wrong.Good point!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I missed the
x-effect
comment sorry. It seems clearer this way. However, if I am not mistaken when the page is loadedopen = false
so it leads to focusing on the hamburger even if the user hasn't interacted with the menu. I assume this shouldn't be the case right? The only thing I can think of to solve it is to change the initial state of open to undefined or an empty string and change the conditional to only focus on true /false:This way it seems to work for me.
And using the
if ... else
is working for me 🤷There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, true. That's a bummer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I've discovered why. One of the items has
tabindex="0"
in the HTML. I don't know where did it come from.So it is not a problem for this experiment, don't worry 🙂
This file was deleted.