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

Input: add support for HTML autofocus attribute #686

Merged
merged 3 commits into from
Nov 12, 2024

Conversation

itsmenewbie03
Copy link
Contributor

No description provided.

@robsontenorio
Copy link
Owner

robsontenorio commented Oct 30, 2024

@itsmenewbie03 I see two related PR for this autofocus.

Could you give a try with this ? So we can avoid dispatching events.

https://alpinejs.dev/plugins/intersect#x-intersect

This way the change can be done exclusively on Input component to make it autofocus when is visible on view port , including modals and drawer.

@itsmenewbie03
Copy link
Contributor Author

@robsontenorio I ended up with two PRs as the solution for having autofocus on modals is a bit complex

I'll take a look on that plugin and update my PR 💜

@itsmenewbie03
Copy link
Contributor Author

@robsontenorio
The current Modal implementation seems to not work with the intersect plugin.

{{-- Input.php --}}
@if( $attributes->whereStartsWith('autofocus')->first() )
    x-ref="myInput"
    @php
        $focus_mode = array_keys($attributes->whereStartsWith('autofocus')->getAttributes())[0] 
    @endphp
    @if($focus_mode == 'autofocus')
        x-intersect.full="$refs.myInput.focus();console.log('autofocus triggered!')"
    @elseif ($focus_mode == 'autofocus.visible')
        x-intersect.threshold.100="$refs.myInput.focus();console.log('autofocus.visible triggered!')"
    @elseif ($focus_mode == 'autofocus.empty')
        {{-- NOTE: use intersect here to focus if empty --}} 
    @endif
    x-intersect:leave="$refs.myInput.blur();console.log('blurring due to :leave')"
@endif

{{-- index.blade.php --}}
<x-modal wire:model="myModal3" persistent>
    <x-input label="Name" placeholder="Your name" icon="o-user" hint="Your full name" autofocus.visible />
    <x-slot:actions>
        <x-button label="Cancel" @click="$wire.myModal3 = false" />
    </x-slot:actions>
</x-modal>

I'm expecting to see the log message once the modal is visible. However, it executes immediately on page load.
image

@robsontenorio robsontenorio changed the title feat: add support for HTML autofocus attribute Input: add support for HTML autofocus attribute Nov 11, 2024
@robsontenorio
Copy link
Owner

Thanks, let's go ahead with basic autofocus. The modal thing is very tricky.

@robsontenorio robsontenorio merged commit ea59606 into robsontenorio:main Nov 12, 2024
@robsontenorio robsontenorio mentioned this pull request Nov 12, 2024
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.

2 participants