You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: autofocus property has no effect on ion-input for desktop #18132
If property autofocus is set to true for an <ion-input> element, the behavior for the <ion-input> element does not change when executing an ionic serve.
Expected behavior:
The app should focus on the <ion-input> element that has the property autofocus set to true.
Steps to reproduce:
Insert <ion-input autofocus="true"></ion-input> to your HTML page
Execute ionic serve in Ionic CLI
Wait for browser to pop up with app and observe behavior
Reacted by Matthew Valenti, Alexander Trakhimenok, Bruno Albano de Souza, jonz94, Pavel Chuchuva, Anthony Bernardo, linjinbo, Robert, Atishay Jain, Pleymor and 2 more
It also can lead to problems on iOS. I was using it in a PWA and when the modal was displayed the entire screen went blank with no way to navigate back since no buttons were available. It looked like the content might have been shifted up above the top because it still appeared in the inspector. Could not figure out a solution and eventually had to stop using the attribute.
I agree that it is important to get it working on desktop because that is the standard behavior for forms in that environment.
I believe this issue is fixed in @ionic/angular version 4.4.0. However, I also encountered the issue that is mentioned by @cjorasch. I opened another issue #18257.
I can confirm this bug with Ionic 5.1.0 on Desktop/Safari, using the React IonInput component within an IonItem, IonContent, IonPage, IonApp (ordered from inside out).
I have the same problem (using @ionic/angular: ^5.5.2) but I can see the focus on the input for a very short time, than the input loose the focus. I have only one autofocus input
@avalanche1 Other things like Svelte-kit.
My main reason starting to use Ionic was far back in time their "Creator" - online GUI editor, which was very handy for replacing wireframing with quick almost-functioning demo that was easy to fully finish offline. But "Creator" is abandoned and stuck on Ionic v3.0, with no replacement.
Still very inconsistent and weird with Ionic 7. For both android and ios. Web now works pretty well actually afaik, consistent rn.
There are two different approaches I found:
set the autofocus property to true on ion-input, make the input-ion-item appear ngIf ionViewDidEnter
set this.myInput.setFocus() when ionViewDidEnter
Both approaches are inconsistent and often do not work
I did not know about the 1., but the 2. is working for me. I'm using this solution since few months now, more exactly I'm putting the this.myInput.setFocus() part inside ionViewWillEnter instead of ionViewDidEnter so that it triggers the keyboard quicker.
Hi, thank you for submitting this issue. We agree that the behavior of the autofocus property can be unexpected.
The autofocus property on Input and Textarea sets the autofocus attribute on the native input element. However, this may not be sufficient for the element to be focused on page load.
One example of this is that if you are navigating to a page with an element with autofocus, the element will load before the page is visible, and it will not be able to focus. Also, each platform has different restrictions on when elements can be focused and if the keyboard will open when an element is focused.
We recommend that developers use the setFocus API with the ionViewDidEnter lifecycle method to focus an element once the view is guaranteed to be visible.
We have added an example to the docs of how to do this in each framework.
We have also updated the documentation for the autofocus property and setFocus method of Input, Textarea, and Searchbar to clarify the autofocus behavior and how to set the focus.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.
Activity
cjorasch commentedon Apr 25, 2019
It also can lead to problems on iOS. I was using it in a PWA and when the modal was displayed the entire screen went blank with no way to navigate back since no buttons were available. It looked like the content might have been shifted up above the top because it still appeared in the inspector. Could not figure out a solution and eventually had to stop using the attribute.
I agree that it is important to get it working on desktop because that is the standard behavior for forms in that environment.
brandyscarney commentedon Apr 26, 2019
Thanks for the issue! I was able to reproduce this for both the native
inputandion-inputinside of anion-app. Outside of that tag it is working properly. Here's the Codepen I made to reproduce: https://codepen.io/brandyscarney/pen/axXZGV?editors=1010msigwart commentedon May 10, 2019
I believe this issue is fixed in @ionic/angular version 4.4.0. However, I also encountered the issue that is mentioned by @cjorasch. I opened another issue #18257.
fabiobeoni commentedon Jun 11, 2020
Hello, I can confirm the bug is still there using ionic core 4.11.x and on android too.
Thanks.
larsblumberg commentedon Jun 11, 2020
I can confirm this bug with Ionic 5.1.0 on Desktop/Safari, using the React
IonInputcomponent within anIonItem,IonContent,IonPage,IonApp(ordered from inside out).Here's the workaround that I'm using:
useIonViewDidEnter(() => {setTimeout(() => inputRef.current.setFocus(), 100)})dreamclass commentedon Jul 2, 2020
got the same issue.
anthony-bernardo commentedon Aug 6, 2020
same issue on firefox linux with react
Workaroud for react :
Huluvu424242 commentedon Sep 26, 2020
Was this issue fixed for ion-button too?
dexster commentedon Dec 10, 2020
Still happens. I'm using plain JS, no framework
https://jsbin.com/jadosag/1/edit?html,js,output
davidecampello commentedon Feb 22, 2021
I have the same problem (using @ionic/angular: ^5.5.2) but I can see the focus on the input for a very short time, than the input loose the focus. I have only one autofocus input
obnijnil commentedon Apr 12, 2021
Still happens. Using @ionic/vue ^5.4.0.
9 remaining items
tattivitorino commentedon Aug 14, 2022
having the same problem with angular.. after all this time.. :(((( i'm running a pwa
here's my input
even tried the this.inputCpf.setFocus() on ngAfterViewInit and nothing? :(((
tattivitorino commentedon Aug 14, 2022
[updating] - it seems like this did the trick.. but why should i wait after view init? autofocus on the input does nothing
iva2k commentedon Aug 28, 2022
avalanche1 commentedon Aug 29, 2022
@iva2k "to other" like what?
gsoulie commentedon Sep 7, 2022
Just in case, it still doesn't work with the following Ionic / Capacitor configuration (tested on Android mobile device + browser)
And there is nothing fancy in the view...
iva2k commentedon Nov 12, 2022
@avalanche1 Other things like Svelte-kit.
My main reason starting to use Ionic was far back in time their "Creator" - online GUI editor, which was very handy for replacing wireframing with quick almost-functioning demo that was easy to fully finish offline. But "Creator" is abandoned and stuck on Ionic v3.0, with no replacement.
JulienLecoq commentedon May 17, 2023
Same issue here, just tried on iOS. autofocus does not work with Angular and Ionic 6.
folsze commentedon Aug 18, 2023
Still very inconsistent and weird with Ionic 7. For both android and ios. Web now works pretty well actually afaik, consistent rn.
There are two different approaches I found:
Both approaches are inconsistent and often do not work
JulienLecoq commentedon Aug 18, 2023
I did not know about the
1., but the2.is working for me. I'm using this solution since few months now, more exactly I'm putting thethis.myInput.setFocus()part insideionViewWillEnterinstead ofionViewDidEnterso that it triggers the keyboard quicker.eliaharris commentedon Sep 7, 2023
Hacky but this custom hook works great. This is react specific.
docs(input, searchbar, textarea): improve docs for managing focus (#2…
mapsandapps commentedon Dec 7, 2023
Hi, thank you for submitting this issue. We agree that the behavior of the
autofocusproperty can be unexpected.The
autofocusproperty on Input and Textarea sets theautofocusattribute on the native input element. However, this may not be sufficient for the element to be focused on page load.One example of this is that if you are navigating to a page with an element with
autofocus, the element will load before the page is visible, and it will not be able to focus. Also, each platform has different restrictions on when elements can be focused and if the keyboard will open when an element is focused.We recommend that developers use the
setFocusAPI with theionViewDidEnterlifecycle method to focus an element once the view is guaranteed to be visible.We have added an example to the docs of how to do this in each framework.
We have also updated the documentation for the
autofocusproperty andsetFocusmethod of Input, Textarea, and Searchbar to clarify the autofocus behavior and how to set the focus.ionitron-bot commentedon Jan 6, 2024
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.