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

Add x-trap noinitialfocus modifier #3977

Merged

Conversation

richcarni
Copy link
Contributor

Adds a new x-trap modifier: .noinitialfocus to trap focus without giving focus to the first element in the focus trap

Test included

Relates to discussion #2806

Use case is to be able to create a modal with the option of not focusing the first input. For example, Wire Element Modal is a Livewire component for creating modals, which uses x-trap, and currently has no mechanism (besides tricks like hidden inputs) to create a modal that opens without focusing its first input: wire-elements/modal#367

Adds a new x-trap modifier: .noinitialfocus to trap focus without
giving focus to the first element in the focus trap

Test included

Relates to discussion alpinejs#2806
@ekwoka
Copy link
Contributor

ekwoka commented Jan 12, 2024

How does this handle (or communicate to devs) that the focus may now behave unpredictably for assistive technologies?

Also, update docs :)

@richcarni
Copy link
Contributor Author

Thanks for the feedback @ekwoka

I'll have to do some testing around how this affects the behaviour of assistive technologies. Will report back and update documentation accordingly

@richcarni
Copy link
Contributor Author

After testing and consideration of how this feature isn't compatible with assistive technologies, I've come to the conclusion that there are better solutions to the problem I thought it solved (see #2806)

Therefore, unless someone wants to argue to the contrary, I'll close this PR

@calebporzio
Copy link
Collaborator

I like the addition - I've heard of cases where people want to disable autofocus on mobile dialogs because autofocusing automatically pulls up the keyboard when you open a modal and that's not ideal.

Anyhow, I like it and changed it to .noautofocus

Thanks!

@calebporzio calebporzio merged commit ad94a86 into alpinejs:main Jan 21, 2024
1 check passed
@richcarni
Copy link
Contributor Author

Apologies for not getting around to writing the documentation @calebporzio

I didn't end up closing this because I realised in my use case, which is for a Livewire modal, autofocus has to be implemented by the modal because it has no contents when the page is first loaded. So you end up with an ugly switch in focus. More annoyingly my first field is an address autocompletion, which incurs a fee if activated

Anyway, thanks for merging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants