-
Notifications
You must be signed in to change notification settings - Fork 34
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
Menu: add additional keydown events and tests #100
Menu: add additional keydown events and tests #100
Conversation
@far-fetched the latest commits removed the focus action, and pass I've not implemented an In the end, the two failing tests were a simple fix: because we are using keys to close the menu, I needed to place focus on the button so I've added a test to check |
Here is existing react docs for menu you can check public API for menu items component. We should be really strict (as long as possible) and keep the same public api. Now we expose something really specific like |
Sorry, I think I misunderstood your previous. I agree about sticking the to public API, hence the comment about deviation from the React/Vue implementation. Now I am clear, I think we can just remove |
Probably we need some code for some tests. |
@far-fetched, latest commit removed |
9f44fa4
to
1f90b65
Compare
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.
Thanks for the PR, and for your patience until I returned!
should we move assertions into
accessibility-assertions
Let's leave it alone for right now; I would actually like us to move the test helpers to the addon-test-support
directory so that they are grouped together in a location that makes sense for testing and can be consumed by users of this addon in their own tests. That's a refactor that I would love help with, but should be a separate effort. I'll write up a card for it!
Overall the changes look good, but we should drop the package.json
changes and do that separately if they're needed at all.
The data-test-
selector bit is a personal preference, but one that I think this addon should establish a firm stance on. IMO addons shouldn't include stuff like that in the code they ship to users; what data-test-
attributes the end-user wants is their business, and I don't love the idea of polluting the element with additional stuff like that. As soon as we include those attribute in the addon
tree, they are public API and removing them is a breaking change. If we were, for example, to move toward using accessibility roles to query elements for testing purposes, we now need to do a semver major bump because we want to refactor our testing infrastructure, which doesn't feel great. It does mean that we need to add data-test-
attributes explicitly in our tests, but lines of code are cheap and it allows our tests to be more explicit, which is a good thing!
|
Thank you! It was just the break I needed to shake off some burn-out and come back refreshed.
Yup, this is absolutely something that we need to address; the tests are different depending on who wrote them 😅 I opened #107 to facilitate a discussion about it and we'll see where things land
Hmm... that's strange. Browsing by commits on this PR, I just see a single commit with changes to the I would recommend preforming a rebase where you re-create the commit off of the current state of |
Yeah, that's because I performed a rebase after merging master into this. I'll have another crack at it and shout if I get stuck. 👍🏻 |
case Keys.Space: | ||
if (this.args.searchTaskIsRunning) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
return this.args.search(event.key); | ||
} | ||
// eslint-disable-next-line no-fallthrough | ||
case Keys.Enter: |
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.
To clarify the intended behavior: Space
will select an item, unless we're currently searching for an item to activate?
Is that the expected behavior from an accessibility perspective?
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.
Yes, that's the expected behaviour. I'm not 100% certain about accessibility, because I was unable to find examples that included search functionality.
This implementation tries to mirror that in the Vue package of headlessui, for the keydown on a menu-item:
...
// @ts-expect-error Fallthrough is expected here
case Keys.Space:
if (api.searchQuery.value !== '') {
event.preventDefault()
event.stopPropagation()
return api.search(event.key)
}
...
@alexlafroscia @roomman I've been following this PR from the outside, but I haven't dug into any of the detail. Are there outstanding items to address before merging this? |
@GavinJoyce, scrap my last. I need to do a small refactor to remove a "data-test" attribute from one template and drop it into a test instead. I will make sure that's done early next week. Sorry for my hasty, initial reply. |
No worries, thanks so much for the PR |
FYI, if you rebase the tests should pass |
refactor(menu): removed focus action and passed setReturnFocus to menu/items test(menu): fixed failing keydown tests test(menu): close the menu with Escape, and return focus to another element refactor(menu): removed `setReturnFocus`
7d99f2c
to
aaa7c54
Compare
@GavinJoyce sorry this PR has ended up being a bit scrappy for the git log but hopefully it's okay to merge, at last! 🤞🏻 |
Thanks @roomman! 🍻 |
This PR adds some additional
<Menu>
keydown interactions, along with updated tests. A few notes / questions:The majority of changes are to
menu-test.js
. I went through the headless ui docs and added any missing tests forSpace
,Escape
,End
, andTab
. I've also tried to standardise the commentary a bit, so it's easier for others to jump.The introduction of
Space
events created issues with the search feature so I've added a condition toonKeyDown
inmenu/items
, to check if the search task is running or not, and to handleevent.key
accordingly. This required an additional argument (searchTaskIsRunning
) to be yielded down frommenu
.While cross referencing headlessui tests, I noticed that we are missing a default behaviour for this component: on close, focus should return to
menu/button
. I’ve added something to the close action inmenu
, to return focus. For now, it includes a condition to check if the button element exists prior to calling focus. This is because there are some tests that either don’t have a button (seeit should not focus button when does not exist
) or have a button that’s not amenu/button
(seecontrolling open/close programmatically
). TBH, I’m a bit unclear why we’d have a menu without renderingmenu/button
so if people (@far-fetched, @alexlafroscia - ps hope you enjoyed the honeymoon!) can feedback I can refactor as needed?I added the
data-test-headlessui-menu-button
attribute tomenu/button
to enable the newgetMenuButton()
to follow the same query selector pattern as headlessui. It did occur to me that we could do the same formenu/items
(iedata-test-headlessui-menu-items
) rather than hardcodingdata-test-menu-button
anddata-test-menu-items
into every test? If you are happy with this suggestion I can add a further commit to this, or follow up with a new PR.For consistency with
<Dialog>
and<ListBox>
, should we move assertions intoaccessibility-assertions
? Again, very happy to commit again, or PR if preferred.Thanks 🙏🏼