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(UIShell): loss of focus on menu (new) #13796

Merged
merged 5 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6544,6 +6544,9 @@ Map {
"onOverlayClick": Object {
"type": "func",
},
"onSideNavBlur": Object {
"type": "func",
},
"onToggle": Object {
"type": "func",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function HeaderContainer({
const [isSideNavExpandedState, setIsSideNavExpandedState] =
useState(isSideNavExpanded);

useWindowEvent('keydown', (event) => {
useWindowEvent('keydown', (event: KeyboardEvent) => {
if (match(event, keys.Escape)) {
setIsSideNavExpandedState(false);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/react/src/components/UIShell/SideNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface SideNavProps extends ComponentProps<'nav'> {
addFocusListeners?: boolean | undefined;
addMouseListeners?: boolean | undefined;
onOverlayClick?: MouseEventHandler<HTMLDivElement> | undefined;
onSideNavBlur?: () => void | undefined;
}

function SideNavRenderFunction(
Expand All @@ -61,6 +62,7 @@ function SideNavRenderFunction(
addFocusListeners = true,
addMouseListeners = true,
onOverlayClick,
onSideNavBlur,
...other
}: SideNavProps,
ref: ForwardedRef<HTMLElement>
Expand Down Expand Up @@ -151,6 +153,11 @@ function SideNavRenderFunction(
if (!event.currentTarget.contains(event.relatedTarget)) {
handleToggle(event, false);
}
if (!event.currentTarget.contains(event.relatedTarget) && expanded) {
if (onSideNavBlur) {
onSideNavBlur();
}
}
};
eventHandlers.onKeyDown = (event) => {
if (match(event, keys.Escape)) {
Expand Down Expand Up @@ -254,6 +261,12 @@ SideNav.propTypes = {
*/
onOverlayClick: PropTypes.func,

/**
* An optional listener that is called a callback to collapse the SideNav
*/

onSideNavBlur: PropTypes.func,

/**
* An optional listener that is called when an event that would cause
* toggling the SideNav occurs.
Expand Down
19 changes: 14 additions & 5 deletions packages/react/src/components/UIShell/UIShell.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ export const HeaderBaseWNavigation = () => (
<SideNav
aria-label="Side navigation"
expanded={isSideNavExpanded}
isPersistent={false}>
isPersistent={false}
onSideNavBlur={onClickSideNavExpand}>
<SideNavItems>
<HeaderSideNavItems>
<HeaderMenuItem href="#">Link 1</HeaderMenuItem>
Expand Down Expand Up @@ -329,7 +330,8 @@ export const HeaderBaseWNavigationAndActions = () => (
<SideNav
aria-label="Side navigation"
expanded={isSideNavExpanded}
isPersistent={false}>
isPersistent={false}
onSideNavBlur={onClickSideNavExpand}>
<SideNavItems>
<HeaderSideNavItems>
<HeaderMenuItem href="#">Link 1</HeaderMenuItem>
Expand Down Expand Up @@ -393,7 +395,10 @@ export const HeaderBaseWNavigationActionsAndSideNav = () => (
<SwitcherIcon size={20} />
</HeaderGlobalAction>
</HeaderGlobalBar>
<SideNav aria-label="Side navigation" expanded={isSideNavExpanded}>
<SideNav
aria-label="Side navigation"
expanded={isSideNavExpanded}
onSideNavBlur={onClickSideNavExpand}>
<SideNavItems>
<HeaderSideNavItems hasDivider={true}>
<HeaderMenuItem href="#">Link 1</HeaderMenuItem>
Expand Down Expand Up @@ -479,7 +484,10 @@ export const HeaderBaseWSideNav = () => (
<HeaderName href="#" prefix="IBM">
[Platform]
</HeaderName>
<SideNav aria-label="Side navigation" expanded={isSideNavExpanded}>
<SideNav
aria-label="Side navigation"
expanded={isSideNavExpanded}
onSideNavBlur={onClickSideNavExpand}>
<SideNavItems>
<SideNavMenu renderIcon={Fade} title="Category title">
<SideNavMenuItem href="https://www.carbondesignsystem.com/">
Expand Down Expand Up @@ -901,7 +909,8 @@ export const SideNavRailWHeader = () => (
isRail
expanded={isSideNavExpanded}
onOverlayClick={onClickSideNavExpand}
href="#main-content">
href="#main-content"
onSideNavBlur={onClickSideNavExpand}>
<SideNavItems>
<SideNavMenu renderIcon={Fade} title="Category title">
<SideNavMenuItem href="https://www.carbondesignsystem.com/">
Expand Down