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 firePhxChangeWhileComposing opt to live_socket #3582

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions assets/js/phoenix_live_view/live_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@
* @param {Object} [opts.localStorage] - An optional Storage compatible object
* Useful for when LiveView won't have access to `localStorage`.
* See `opts.sessionStorage` for examples.
* @param {boolean} [opts.firePhxChangeWhileComposing] - If set to `true`, ensures that
* `phx-change` events are triggered even while the user is composing input using IME
* (Input Method Editor). This is determined by the `e.isComposing` property on keyboard events,
* which is `true` when the user is in the process of entering composed characters (e.g., typing
* in languages like Japanese, Chinese, etc.). By default, `phx-change` will not fire during a
* composition session to prevent potential issues, especially on Safari, where input updates
* during composition can cause unexpected behavior. For more information, see:
*
* - https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/isComposing
* - https://github.com/phoenixframework/phoenix_live_view/issues/3322
*
* Defaults to `false`.
*/

import {
Expand Down Expand Up @@ -159,6 +171,7 @@ export default class LiveSocket {
this.failsafeJitter = opts.failsafeJitter || FAILSAFE_JITTER
this.localStorage = opts.localStorage || window.localStorage
this.sessionStorage = opts.sessionStorage || window.sessionStorage
this.firePhxChangeWhileComposing = opts.firePhxChangeWhileComposing || false
this.boundTopLevelEvents = false
this.boundEventNames = new Set()
this.serverCloseRef = null
Expand Down Expand Up @@ -901,11 +914,8 @@ export default class LiveSocket {
}
let phxChange = this.binding("change")
let input = e.target
// do not fire phx-change if we are in the middle of a composition session
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/isComposing
// Safari has issues if the input is updated while composing
// see https://github.com/phoenixframework/phoenix_live_view/issues/3322
if(e.isComposing){

if(!this.firePhxChangeWhileComposing && e.isComposing){
const key = `composition-listener-${type}`
if(!DOM.private(input, key)){
DOM.putPrivate(input, key, true)
Expand Down
Loading