Skip to content

Commit

Permalink
fix: dismiss nav dropdown on anchor click (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstaley committed Nov 16, 2021
1 parent 7861e6a commit 7a04f76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cuddly-impalas-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hashicorp/react-subnav': minor
---

Automatically dismiss open navigation dropdown menus when the user clicks on an anchor element.
8 changes: 7 additions & 1 deletion packages/subnav/partials/MenuItemsDefault/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ class NavLinkWithDropdown extends React.Component {
// If we're not collapsed, and the click is outside the component,
// we should ensure that the modal closes
const isClickOutside = !this.parentRef.current.contains(event.target)
if (isClickOutside) this.setState({ isCollapsed: true })
// If we're not collapsed, and the click is on an anchor element with an
// href attribute, we're likely performing a navigation, and should ensure
// that the modal closes
const isClickNavigation = event.target.tagName === 'A' && event.target.href
if (isClickOutside || isClickNavigation) {
this.setState({ isCollapsed: true })
}
}

componentDidMount() {
Expand Down

1 comment on commit 7a04f76

@vercel
Copy link

@vercel vercel bot commented on 7a04f76 Nov 16, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.