-
Notifications
You must be signed in to change notification settings - Fork 82
[Modal] focus gets trapped on close #558
Comments
Hi @frodesigns thank you for reporting! Tried to create a content like below, but couldn't reproduce the problem. Would you try forking https://codesandbox.io/s/github/carbon-design-system/carbon-web-components/tree/master/examples/codesandbox/basic, put your content and see if the problem reproduces? If so, could you please share the fork? Some movie shots of the user interaction you described will also be a help. Thanks! <bx-modal id="modal">
<bx-modal-header>
<bx-modal-close-button></bx-modal-close-button>
<bx-modal-label>Label (Optional)</bx-modal-label>
<bx-modal-heading>Modal Title</bx-modal-heading>
</bx-modal-header>
<bx-modal-body>
<form>
<bx-form-item>
<bx-date-picker>
<bx-date-picker-input kind="single" label-text="Date picker">
</bx-date-picker-input>
</bx-date-picker>
</bx-form-item>
<bx-form-item>
<bx-input id="input" label-text="input"></bx-input>
</bx-form-item>
</form>
</bx-modal-body>
<bx-modal-footer>
<bx-btn kind="secondary" data-modal-close>Cancel</bx-btn>
<bx-btn kind="primary">Save</bx-btn>
</bx-modal-footer>
</bx-modal>
<script type="module" src="src/index.js"></script>
<script type="text/javascript">
document.addEventListener('click', event => {
if (event.target.id === 'launcher') {
document.getElementById('modal').open = true;
}
});
document.addEventListener('bx-modal-beingclosed', event => {
const input = document.getElementById('input');
if (!input.value) {
event.preventDefault();
}
});
</script> |
I also can't reproduce this way, may have to look at another approach. |
I see, another way for further investigation is opening console.log('this:', this);
console.log('target:', target);
console.log('oldContains:', oldContains);
console.log('relatedTarget:', relatedTarget);
console.log('currentContains:', currentContains); The focus wrapping (trapping) runs when |
Bug reviewed in the Dec 15, 2020 meeting. Waiting to hear from the adopter with additional info. |
No additional info received. Closing. |
@asudoh Sorry for the long delay between comments, but to follow up on this yes Steps:
Maybe it has something to do with having created a custom component to wrap the Carbon modal components, and passing the content down an extra layer of slots? |
Fireline will try to replicate the issue. Review when done. |
@frodesigns I created a Code Sandbox based on the example code above https://zrumj.sse.codesandbox.io/ but I haven't been able to replicate the focus trapping issue. Would you be able to modify the sandbox into a reduced test case for easier testing and debugging? |
Actually, I think I am able to replicate it in the storybook instance:
I am also noticing in your sandbox (https://zrumj.sse.codesandbox.io/), that the modal appears briefly before fading away. This is another bug we have been dealing with. |
does updating the package version resolve the first issue for you? it looks like the storybook is using package version 1.13.3, and I can only replicate the behavior in the sandbox by downgrading to 1.13.3 for the the second issue, is this happening when you dismiss the modal? would you be able to provide a recording of the behavior for example? I think if this is a separate issue we may need to capture it in a new ticket |
Yes, the issue still occurs in 1.16.0. There must be a flaw in the logic for focus trapping, possibly related to how we have a custom component for our modal that wraps the carbon modal components, and passes the body content down an extra layer of slots. For the other issue, it is on load (or anytime we revisit the page with the modal). Modal starts visible and fades out even though it should be closed/hidden initially. Particularly noticeable in Firefox |
oh I see, from what I can tell after some more testing, the focus trap issue was not appearing when clicking on the form margin below the inputs, but it would appear when clicking underneath the input and the margin. will need to look more into this I haven't been able to replicate the second issue on my end but ultimately I think it can be contained in a separate ticket |
Interesting. For me, it happens every time I load your sandbox: https://codesandbox.io/s/zrumj |
I'm not sure if I'm misunderstanding but this is what I see when I load the sandbox: Screen.Recording.2021-08-17.at.12.32.59.mov |
screen-capture.mp4 |
A couple suggestions:
|
opened #689 to potentially resolve the current issue in the meantime |
Tested version 1.16.1 containing fix #689, focus trapping issue is still present unfortunately. |
can you provide the steps to replicate the issue? you can view the steps we took to test this in the recorded videos in #689 |
would you be able to create a reduced test case in code sandbox? That would really help with testing and debugging especially when the issue can't be replicated in storybook. You can follow the instructions outlined earlier #558 (comment) |
I tried to create a test case before in the sandbox, but I was having too much trouble trying to re-create our component structure. We have a custom component we created as a wrapper for the modal, with a slot that passes content to the modal body. Here is a quick example: <catalyst-modal>
<bx-date-picker></bx-date-picker>
<!-- other inputs here -->
</catalyst-modal> Here is a gist containing the content of the I am fairly certain this has to do with having an extra level of slot nesting that the focus trapping code is having trouble handling. The focus trap needs to handle any amount of shadowRoot nesting. |
thank you for the additional context about how you're using the modal, I was trying to figure that out
I think you might be right with this hunch, I can take a deeper look once I'm able to create a reproducible test case |
after a bit of testing I was finally able to identify an issue or at least something similar. it seems that in your use case when to get around this, I think we would have to traverse up the DOM to determine if any of the Is there any particular reason for wrapping the Carbon modal as you showed in your earlier gist? |
Yes, the main reason is to standardize our usage of the carbon modal for all implementations. It also avoids having to import each individual sub-component of the modal for every instance. |
given that the issue is not reproducible in the storybook, it seems that the Carbon modal's focus wrap behavior is working as expected when self-contained. The modal's focus wrap behavior currently doesn't support custom wrappers or containers so it won't extend beyond the Carbon modal itself. I'm not sure if there are plans to extend the focus wrap behavior in the Carbon modal beyond its current scope in the meantime I think the workaround would be to use the Carbon modal as-is (without a wrapper) or to implement a separate modal with custom focus wrap behavior |
Disappointing to hear, but I guess we will have to live with it or find some other workaround. Thank you for all of your time spent on this. |
Closing based on the comments above. |
When dealing with form inputs within a bx-modal and clicking our Submit button (
<bx-btn data-modal-close>
triggers the beforeclose event), validations are run, preventDefault is called for errors, and focus becomes trapped on the Submit button and the modal close X button, and we are unable to click into inputs to re-focus. This has been observed with both the bx-input and bx-date-picker.I've narrowed it down that I don't think any of our project's code, such as the validation, is causing this. It must be something in the bx-modal code related to the beforeclose or close events.
The text was updated successfully, but these errors were encountered: