-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
Focus & Modal #229
Comments
You may find the code below useful: export let focusAfterClosed = null; let dialogNode;
And then, add this at the top and at the bottom of your Modal:
|
@akiarostami Thank you for this. Without doubt modal context should trap focus within it. However, there are a couple of reasons why this was not included in
I'm curious to hear more of your thought my reasoning above. In the meantime, would it be helpful if I introduce a |
@vnphanquang Thanks for taking the time and writing this thoughtful answer.
|
Thanks very much for the follow-up @akiarostami. Very glad to hear that you are finding
Yeah, i agree; in that use case it wasn't the best UX, but i didn't have a say in the decision. And honestly it kinda made sense for that particular application at the time. But the point is still, i don't want to force any opinion upon lib users as i can never predict what this is being used for.
Same as above, my view is that I should only provide the bare minimum, and an interface that allows features like I'm imaging something like this Click to see code<!-- MyCustomModal.svelte -->
<script lang="ts">
import {
type ModalInstance,
// optimized, tree-shakable pieces from counter libs (svelte-put/*)
escape,
clickoutside,
focustrap,
lockscroll,
} from '@svelte-put/modal';
import { movable } from '@svelte-put/movable';
// injected prop by lib
export let modal: ModalInstance;
</script>
<div class="modal" use:focustrap use:movable use:clickoutside use:escape use:lockscroll>
<!-- content -->
</div>
Totally. HTML Dialog, however, is now supported in all major browsers. I think it's probably time we adopt it. Lots of good things it does for free, but i'm not entirely sure how extendable it can be. Experimentation is needed. Also not quite related but I've been impressed by Melt UI and perhaps there is inspiration worth taking from there. Your point of view is very valuable to the the redesign of this package. Please do provide feedback about the code sample above. Thank you! |
You're very kind, @vnphanquang. Your recipe solution looks very clean. I've recently seen Melt UI, but I haven't played with it yet. Now that you're recommending it, then I feel I should! |
First of all, wonderful set of components! Thank you for creating these.
There is an issue with the Modal component. Let's say we have a button that opens a Modal (e.g. «Open ConfirmationModal» button in your Modal documentation page). If the user uses keyboard / tab to move focus to that button, the user then can keep pressing «space» and it would keep opening new Modals.
Modals in general should save the last focused element, and then use a focus trap to prevent users from changing focus. In order to create a Focus Trap, you can simply bracket the dialog node with two invisible, focusable nodes (
<div tablindex="0" />
). While this dialog is open, we use these to make sure that focus never leaves the document even if dialogNode is the first or last node.The text was updated successfully, but these errors were encountered: