Skip to content
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

fix(header-dropdown): click list item to close #973

Merged
merged 6 commits into from
Jan 31, 2025

Conversation

max-umain
Copy link
Collaborator

@max-umain max-umain commented Jan 20, 2025

Describe pull-request

Adds div to list of event sources that close the header dropdown.

Adds an event emitter to header-dropdown-list-item that is handled in header-dropdown, where it closes the dropdown.

Adds "pointer-events: none" for relevant inner elements to not interrupt event to close header dropdown. Thank you to @theJohnnyMe for help with the solution :)

Issue Linking:

How to test

  1. Go to https://pr-210.d2vh2lmnzgzrkl.amplifyapp.com/
  2. Click the avatar in the top right corner to open the header dropdown
  3. Click on the settings icon or text, and notice that now it closes the dropdown

Checklist before submission

  • I have added unit tests for my changes (if applicable)
  • All existing tests pass
  • I have updated the documentation (if applicable)
  • Not breaking production behavior
  • Behavior available in storybook with documented descriptions (if applicable)
  • npm run build-all without errors

Suggested test steps

  • Browser testing (Chrome, Safari, Firefox)
  • Keyboard operability
  • Interactive elements have labels.
  • Storybook controls
  • Design/controls/props is aligned with other components
  • Dark/light mode and variants
  • Input fields – values should be displayed properly
  • Events

Screenshots

None

Additional context

I'm not sure if this is a very good solution, adding div as a source of event that closes the dropdown. But the way the dropdown is currently closed is handled like this (when the click is done inside the header-dropdown). I imagine a better solution would be to somehow specify the user clicks anywhere in a dropdown row, it closes it, without specifying that it should be a div.

The reason I added div specifically is because in the ticket for this PR, the problem was mentioned to be in React, and after investigating I found that the element that is clicked is a next Link (which is compiled to a div).

Copy link
Contributor

github-actions bot commented Jan 20, 2025

Playwright test results

passed  789 passed
skipped  1 skipped

Details

stats  790 tests across 134 suites
duration  2 minutes, 18 seconds
commit  40ed740

Skipped tests

src/components/table/table/test/expandable-row-autocollapse/expandable-row-autocollapse.e2e.ts › tds-table-expandable-row-autoCollapse › NEEDS FIXING: expanding one row collapses the others when autoCollapse is true

Copy link

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-973.d3fazya28914g3.amplifyapp.com

Copy link
Collaborator

@Lunkan89 Lunkan89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, it works as intended by my testing, just wondering if we need a & button if we add div? since div is the wrapping element? what element are is the source when clicking on the icon or text? should we be more specific, can we check if it is an header-item?

@max-umain max-umain force-pushed the fix/header-dropdown-click-event branch from e1aaef6 to bf72d61 Compare January 21, 2025 10:26
@max-umain
Copy link
Collaborator Author

Nice, it works as intended by my testing, just wondering if we need a & button if we add div? since div is the wrapping element? what element are is the source when clicking on the icon or text? should we be more specific, can we check if it is an header-item?

@Lunkan89 I tested it just now, and having only div causes it to not work when clicking outside of the text or icon

@max-umain max-umain marked this pull request as draft January 21, 2025 12:46
@max-umain max-umain force-pushed the fix/header-dropdown-click-event branch 2 times, most recently from 65ad727 to 555b33a Compare January 21, 2025 13:01
@max-umain max-umain changed the title fix(header): click on div to close header-dropdown fix(header-dropdown): click list item to close Jan 21, 2025
@max-umain
Copy link
Collaborator Author

max-umain commented Jan 21, 2025

@Lunkan89 @theJohnnyMe Thanks for your feedback! I updated the code now, to instead emit an event from header-dropdown-list-item. So now, if a list item (row) is clicked, it closes the dropdown.

Not sure if we always want clicks on list items to close the dropdown though... What do you think?

@max-umain max-umain force-pushed the fix/header-dropdown-click-event branch from 555b33a to 8e84437 Compare January 21, 2025 16:19
@nathalielindqvist
Copy link
Contributor

@Lunkan89 @theJohnnyMe Thanks for your feedback! I updated the code now, to instead emit an event from header-dropdown-list-item. So now, if a list item (row) is clicked, it closes the dropdown.

Not sure if we always want clicks on list items to close the dropdown though... What do you think?

In the case that a user successfully slots in another dropdown as a list item it might be problematic if the main dropdown closes when a user clicks on the slotted dropdown in an attempt to open it. We might be shooting ourselves in the foot by harshly specifying that all list items closes the dropdown.

@max-umain
Copy link
Collaborator Author

@Lunkan89 @theJohnnyMe Thanks for your feedback! I updated the code now, to instead emit an event from header-dropdown-list-item. So now, if a list item (row) is clicked, it closes the dropdown.
Not sure if we always want clicks on list items to close the dropdown though... What do you think?

In the case that a user successfully slots in another dropdown as a list item it might be problematic if the main dropdown closes when a user clicks on the slotted dropdown in an attempt to open it. We might be shooting ourselves in the foot by harshly specifying that all list items closes the dropdown.

That's a good point. For that reason perhaps it's better with the solution in the first commit... However, perhaps the element that is clicked to open the inner dropdown would also be a div, if it is then it will close the outer dropdown when trying to open the inner. I'll look into this.

Perhaps another solution would be to add a prop to the header-dropdown-list-item component such as closeOnClick which is true by default, but that could be set to false in case one wants to e.g. open an inner dropdown when clicking on the list item. Could this work?

@max-umain max-umain force-pushed the fix/header-dropdown-click-event branch from 8e84437 to 97c2696 Compare January 22, 2025 11:24
Copy link
Contributor

@nathalielindqvist nathalielindqvist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do believe this would work, however I'm wondering about the implementation this would require. From what i can understand the user would have to listen to the newly created event 'closeDropdownFromListItem' on all list items in the header dropdown except the one/few that would potentially open something else, have I understood that correctly? If yes, it would mean a lot of repeating event listeners for a lot of list items. Maybe it would be more desirable to have something the user can opt out of instead, like a "don't close dropdown when this list item is clicked" or similar. Or have I completely misunderstood your implementation? 🙃

@max-umain
Copy link
Collaborator Author

@nathalielindqvist
I'm not sure about the events... I was thinking a prop that, when not passed to the list item, causes the dropdown to close when the list item is clicked. However, if the prop is passed to a list item, it will keep it open. So the default case, when nothing is specified, is to close on click. So my example was a prop called closeOnClick that is true by default, and then the user can set it to false in the few cases it's necessary. But, the opposite would work equally well, such as a prop called keepOpenOnClick that is set to false by default, and can be set to true if necessary :)

@theJohnnyMe
Copy link
Contributor

theJohnnyMe commented Jan 28, 2025

OK, let's go back to the initial issue. The problem is that when the user clicks directly on the icon or text in the anchor tag, the dropdown does not close. I believe the reason is that we have code like this:

<a href="/settings"><tds-icon name="settings" size="16px" class="hydrated"></tds-icon><div class="tds-u-pl1">Settings</div> </a>

Instead of

<a href="/settings">Settings</a>

So when you click on an Icon or a text, you are clicking on it, not on the a or button that handleSlotteItemClick function expects.

In the browser I did a small test by setting pointer-events: none as CSS rule on tds-icon and div and then there are no issues. Please see if you can do this small change in CSS of the header-dropdown-list-item.scss, I think it should do the trick.
Something like this:
::slotted(a), ::slotted(button) { tds-icon, div { pointer-events: none; } }

Another, no so much desired way, would be to try to add tds-icon and div to the list of eventSources in handleSlottedItemClick.

As I see the user has not requested control to toggle off or on logic for the closing of dropdown so let's keep that discussion out.

@max-umain max-umain force-pushed the fix/header-dropdown-click-event branch from a6409eb to 5421d64 Compare January 29, 2025 08:49
@max-umain max-umain marked this pull request as ready for review January 30, 2025 09:55
@max-umain max-umain force-pushed the fix/header-dropdown-click-event branch 2 times, most recently from 826e3f1 to b09b942 Compare January 31, 2025 09:54
@max-umain
Copy link
Collaborator Author

Demo available here now: https://pr-210.d2vh2lmnzgzrkl.amplifyapp.com/

Copy link
Contributor

@theJohnnyMe theJohnnyMe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one! LGTM

@max-umain max-umain force-pushed the fix/header-dropdown-click-event branch from b09b942 to 40ed740 Compare January 31, 2025 12:01
@max-umain max-umain merged commit 10d54ca into develop Jan 31, 2025
4 checks passed
@max-umain max-umain deleted the fix/header-dropdown-click-event branch January 31, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants