Panel: When IsHiddenOnDismiss is true, Focus does not automatically enter Panel and is not returned when Panel is dismissed
Environment Information
- Package version(s): 6.110.1
- Browser and OS versions: should repro in all browsers
Please provide a reproduction of the bug in a codepen:
- Go to the Panel Example Page
- Using the keyboard, TAB all the way to the "Open Panel" button under the "Panel - Hidden on Dismiss" section
- Press Enter to open the panel. Notice that focus is still on the "Open Panel" button
- TAB over to the X to close the Panel. Notice that focus does not return to the "Open Panel" button
Actual behavior:
When opening the panel, focus stays on the "Open Panel" button. After closing the panel, focus is lost and tabbing starts at the top of the page again.
Expected behavior:
When opening the panel, focus should automatically move into the panel (usually to the X button). If you use the keyboard to open any of the other panels in the example page, you should see this behavior. After closing the panel, focus should return to the "Open Panel" button. This is the behavior of the other panels on this example page.
Explanation of the Issue:
Panel internally uses a FocusTrapZone and it is this FocusTrapZone that performs the logic of moving focus into the Panel and returning focus back to the element that opened the panel when the panel is dismissed. The logic for bringing focus into the Panel when it opens (and remembering the _previouslyFocusedElementOutsideTrapZone) resides in FocusTrapZone's componentDidMount(). The logic for returning focus back to the right element when the panel is dismissed resides in FocusTrapZone's componentWillUnmount() function.
The Panel's isHiddenOnDismiss option makes it so dismissing the panel just hides it instead of destroys it. The issue is that this means its internal FocusTrapZone is NOT unmounted and remounted when dismissing/opening. Therefore, the logic for bringing focus in and returning it doesn't get run.
Possible fix suggestion:
One idea that was suggested is a mini-feature for FocusTrapZone. The idea is to add a "disabled" prop to FocusTrapZone. Panel would compute the value of the new disabled prop as (isHiddenOnDismiss && !isOpen) FocusTrapZone could then have a componentDidUpdate() and if the disabled prop changed, then perform the logic that's currently in componentDidMount()/componentWillUnmount() to bring focus in or return focus, as needed.
(I started implementing this, but then saw the _focusStack variable. I was not confident that I wouldn't be introducing new bugs related to the focus stack, and I wasn't sure if this was the best fix approach design-wise...)
Priorities and help requested:
Are you willing to submit a PR to fix? Yes, but I would like advice and guidance about how to do the right fix.
Requested priority: High / (Blocking?)
Products/sites affected: SharePoint Modern UI -- all Flow Panel related features!
@LevnLime thanks for opening the issue and providing a detailed explanation.
I've been looking through FocusTrapZone and I don't see any issues that could result from this fix. @dzearing is there someone more familiar with FocusTrapZone who could could provide more definitive guidance here?
I consider @bworline and @dagoff FocusTrapZone experts - they've done a lot of work in understanding/tweaking FocusTrapZone behavior in the past.
@LevnLime , the focusStack is from @dagoff. You can chat with him about the nuances regarding that.
In theory, between the unit tests and visual regression tests, there should be enough test coverage to protect you against unintended mistakes.
The focusStack was added to deal with a problem that can result when multiple focus trap zones are on the page. In short, they will start fighting for focus and it gets ugly. The idea of the focusStack is that only the most recently added FocusTrapZone (ie, the one at the top of the stack) should be trapping focus.
I can see two possible solutions:
(1) Change the semantics of FocusTrapZone so that when the state changes from Enabled to Disabled, it gets removed from the focusStack (wherever it was in the stack), and when the state changes from Disabled to Enabled, it gets pushed onto the focusStack.
(2) Change the semantics of FocusTrapZone so that instead of the checking that the current FocusTrapZone is ontop of the focusStack, it checks to see if the current FocusTrapZone is the highest non-disabled FocusTrapZone in the stack before allowing it to steal focus.
I personally recommend (1).
Ok, I chatted with dagoff, and I think my concern with the focus stack is alleviated. However, if we go with this fix of adding a "disabled" property, does it make sense from a design standpoint to have ALL of these properties?
- disabled
- forceFocusInsideTrap
- isClickableOutsideFocusTrap
I felt uncomfortable adding a new "disabled" property next to those existing properties (forceFocusInsideTrap and isClickableOutsideFocusTrap), so I'm approaching the fix in a slightly more contained way. I'm adding logic so that when forceFocusInsideTrap transitions from false -> true, perform the logic of bringing focus in. Also, when forceFocusInsideTrap transitions from true -> false, perform the logic of returning focus back. I'll put up a pull request soon