openui5 icon indicating copy to clipboard operation
openui5 copied to clipboard

Setting Focus in onAfterShow Fails When Navigating via JS window.history.go

Open boghyon opened this issue 8 years ago • 11 comments

The API reference states that the application can set the focus in afterShow event. This is great and works fine. There is only one problem which can be observed only when the app navigates back programmatically via window.history.go(-1). When the user navigates back natively with the browser button, the focus is set as intended.

Tested browsers/devices(OK/FAIL)

  • Chrome: FAIL
  • FF: FAIL
  • Edge: FAIL
  • Safari: FAIL

URL (minimal example if possible)

https://embed.plnkr.co/wp6yes

Steps to reproduce the problem

  1. Open the above sample page. The sap.m.App has autoFocus="false" and in onAfterShow, the Input is supposed to have the focus after navigation finishes.
  2. In Home.controller.js, replace
    const onAfterShow = () => requestAnimationFrame(() => target.focus());
    with
    const onAfterShow = () => target.focus();
  3. In the preview panel, click on Navigate >.
  4. Navigate back by pressing Alt/⌘ + Left arrow or click on the native back button.
    Focus is set on the Input again (expected behavior).
  5. Reload the preview panel, and click on Navigate > again.
  6. Navigate back, but this time, by clicking on the navButton from the Page header toolbar which calls window.history.go(-1) in the controller.

What is the expected result?

Focus is set on the Input, same as when navigated back natively.

What happens instead?

Focus is set on the first element (Button instead of Input).

Any other information? (attach screenshot if possible)

This seems to be related to the behavior mentioned in this talk given by Jake Archibald at JSConf this year (really good btw.) where the difference between JS and native trigger is explained.

My current workaround is just to call focus() within setTimout or requestAnimationFrame queuing the task to be performed later.

boghyon avatar Nov 27 '18 18:11 boghyon

Hello @boghyon , Thank you for sharing this finding. I've created an internal incident 1870488179. The status of the issue will be updated here in GitHub. Regards,

GeorgiValkanov avatar Nov 28 '18 13:11 GeorgiValkanov

Hey @boghyon,

I tried reproducing this issue but apparently failed. I followed your steps in Chrome 71 on macOS as well as Chrome 70 and Edge 41 on Windows 10 and failed to get the focus on anything but the input control after navigation:

navigationfocusissue2306

Can you please check whether you can still reproduce the issue? (I know it's been only two days) If yes, could you please share more details about your environment, like OS and browser versions used?

Thanks and best regards, Merlin

RandomByte avatar Nov 29 '18 12:11 RandomByte

Hi @RandomByte,

Thanks for looking into this issue. I see in the GIF that requestAnimationFrame is still there. That's my current workaround to solve the problem. Following the 2nd step, you just need to replace the line with const onAfterShow = () => target.focus();.

I should've forked the plunk (since it's referenced in many other places), replaced that line, and then shared the fork here. Sorry about that.

boghyon avatar Nov 29 '18 14:11 boghyon

Oh sorry, I think I just missed that step! :disappointed:

I can perfectly reproduce this issue now. I just forwarded it to the responsible colleague and he will get in touch with you as soon as possible.

Best regards, Merlin

RandomByte avatar Nov 29 '18 14:11 RandomByte

Hi @boghyon, did you set the autoFocus property to false when testing? I found the following statement in the documentation: "If necessary, the afterShow event can be used to focus another element, only if autoFocus is set to false."

Regards, Mihail.

myordanov avatar Nov 30 '18 07:11 myordanov

@myordanov Yes, as stated in the issue description, and also in the provided sample code.

boghyon avatar Nov 30 '18 07:11 boghyon

Hi @boghyon, ok then I may propose to try using the afterNavigate event of the NavContainer. It's fired last and all you do then should be executed correctly without the need of workarounds(delays) in your code.

Regards, Mihail.

myordanov avatar Nov 30 '18 09:11 myordanov

@myordanov Thank you for the proposal but I regard this as another workaround, and unfortunately not a good one:

  • afterNavigate is not fired when the Page initially shows, hence no initial focus when the app loads.
  • afterNavigate is fired every time when the NavContainer navigates, hence focusing the target element that is often not displayed in the UI doing unnecessary work --> bug-prone.
  • afterNavigate's handler is often defined separated from the view that contains the target element to focus. In the case of the sample code above:
    • I'll need to create an extra controller for the root view having <App afterNavigate=".onAfterNavigate" ..., define the handler in that controller, awkwardly grab the target element which is in another view, and call focus() on it.
    • Or I could use EventBus in the root controller, publish an event in onAfterNavigate, and the view containing the target control subscribes to that event.

In comparison, and from application developer's point of view, a proper working afterShow would be more helpful in creating cleaner and maintainable code.


Or something more declarative instead of all of this: Add initialFocus to sap.ui.core.mvc.View similar to the initialFocus in sap.m.Dialog:

https://github.com/SAP/openui5/blob/8ca34da5ba24c00fecd0d3ae789e6743726252ba/src/sap.m/src/sap/m/Dialog.js#L311-L315

Once defined, the target element would get the focus every time when the view is shown. This would be much more intuitive for application developers.

boghyon avatar Nov 30 '18 11:11 boghyon

Hi @boghyon, fixing the issue wouldn't take much effort I guess, but your proposal for identical initialFocus property makes more sense to me. We'll discuss it and I'll get back to you with the final decision (fix or feature).

Thanks, Mihail.

myordanov avatar Nov 30 '18 11:11 myordanov

I think this afterShow issue should be definitely fixed, though, regardless whether the initialFocus is added or not (would be great of course).

The fact that its handler produces a different result, depending on whether the navigation was triggered via JS or natively, is something that should be fixed imho.

boghyon avatar Nov 30 '18 12:11 boghyon

About the initialFocus proposal: Since a single screen can display multiple views at once (e.g. in a master-detail scenario), I wonder if it's not a better solution to introduce the feature to targets & routes instead - Similar to using the title property in targets:

"routes": [
  {
    "name": "masterDetail",
    "pattern": "items/{itemId}",
    "target": [
      "master",
      "detail"
    ],
    "titleTarget": "master",
    "initialFocusTarget": "detail"
  }
],
"targets": {
  "master": {
    "viewName": "Master",
    "controlAggregation": "beginColumnPages",
    "title": "...",
    "initialFocus": "someControlIdInMaster"
  },
  "detail": {
    "viewName": "Detail",
    "controlAggregation": "midColumnPages",
    "title": "...",
    "initialFocus": "someControlIdInDetail"
  }
}

As soon as multiple targets are displayed, the corresponding control could receive the focus, following the same heuristic described in titleChanged.

In the case above, it's the control with id="someControlIdInDetail". If the initialFocusTarget is not defined, then the control from the first target that has initialFocus defined. Any thoughts?

boghyon avatar Jan 04 '19 14:01 boghyon