~emersion/public-inbox

1

[gamja] Focus composer on visibilitychange

Hubert HIRTZ
Details
Message ID
<c60bd69b-5f8b-01e0-8b5b-c605fc5cb65c@cea.fr>
DKIM signature
missing
Download raw message
When clicking a link in gamja, it sends the user to a new tab.  When the 
user comes back, it expects the composer to be focused, but the clicked 
link is focused.

Solution:
https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

     document.addEventListener('visibilitychange', () => {
         if (!document.hidden) {
             document.getElementById('composer').text.focus()
         }
     })

I'd like to send a patch but I'm not sure what's the idiomatic way of 
adding an event listener on document with preact. The above lines would 
be very out-of-place.
Details
Message ID
<7HUcwWFFovkJ80MRBuajIDwD5PAlClolAT-DlVcnHCfABsH8O_ZnqrSX4NQtfUvrrOhQlfwSSBQ_4hWBsByvIIwRToKLzm51dBXs6SSkwHs=@emersion.fr>
In-Reply-To
<c60bd69b-5f8b-01e0-8b5b-c605fc5cb65c@cea.fr> (view parent)
DKIM signature
pass
Download raw message
On Tuesday, September 7th, 2021 at 09:37, Hubert HIRTZ <hubert.hirtz.ocre@cea.fr> wrote:

> When clicking a link in gamja, it sends the user to a new tab.  When the
> user comes back, it expects the composer to be focused, but the clicked
> link is focused.
>
> Solution:
> https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
>
>      document.addEventListener('visibilitychange', () => {
>          if (!document.hidden) {
>              document.getElementById('composer').text.focus()
>          }
>      })

I'd rather not use visibility change events if possible, because this has
privacy implications. Browsers might not trigger the events reliably or at all.

I'd rather not mess with focus, this will subtly break all kind of things.
Focus handling in gamja is quite complicated because we want to allow users to
navigate within the buffer view with the keyboard (not only Up/Down/PgUp/PgDown
but also Tab/Shift+Tab). Also we don't want to bring up the virtual keyboard
when switching to a buffer on mobile (and there's no good way to detect whether
the keyboard is physical or virtual). For these reasons the buffer view is
focused in general, and we switch focus to the composer on keypress or on
paste.

Some of this logic is handled in the Composer component [1], which installs
the window event handlers when mounted and uninstalls them when unmounted.

I guess we could add a special case for links in here, but I'm worried about
not being able to hit Enter when a link is focused (browsers might have other
keybindings too, e.g. to open the right-click menu).

Maybe we should just blur() the focus when the user clicks on a link in the
composer?

[1]: https://git.sr.ht/~emersion/gamja/tree/76f097e8a8dcb10d21b8a0a7213d4ea02c2d4eb0/item/components/composer.js#L119
Reply to thread Export thread (mbox)