When the span tag is inserted into a contenteditable element on iOS Safari, the cursor automatically shifts to the

One interesting feature allows users to click on a tag and have it added to a contenteditable div. This function works smoothly initially, but when toggling back and forth, the cursor unexpectedly jumps to the end of the div.

When a user adds a variable for the first time, the cursor behaves as expected.

https://i.sstatic.net/NzI5y.png

However, when the user removes and re-adds the tag, an issue arises.

https://i.sstatic.net/N7I7P.png

This behavior is specific to iOS Safari and can be attributed to the following code:

insertTextAtCursor = (text, paste = false) => {
    const sel = window.getSelection();
    // Ensure focus on the compose element
    this.compose.focus(); // reference to contenteditable div

    if (sel.getRangeAt && sel.rangeCount) {
      const range = sel.getRangeAt(0);
      range.deleteContents();
      if (paste) {
        range.insertNode(document.createTextNode(text));
        range.selectNode(document.createTextNode(text));
      } else {
        range.insertNode(text);
        range.selectNode(text);
      }
      // After deleting and replacing with HTML, clean up any empty nodes left behind
      const newRange = range.cloneRange();
      range.setEndBefore(text);
      newRange.setStartAfter(text);
      newRange.setEndAfter(text);
      sel.removeAllRanges();
      sel.addRange(newRange);
    }
  }

Edit: The cursor enlarges significantly after adding the display: inline-block property.

https://i.sstatic.net/dBNFt.png

HTML:

<div id="variable-message-input-message-fb39fc57-7909-47e2-9de8-042e70eee3ad" class="variable-message__compose" contenteditable="true" name="message" placeholder="" style="
/* display: inline-block; */"><span spellcheck="false" contenteditable="false" id="span-First Name" class="variable-message__variable"><i>{</i><div>First</div><b>_</b><div>Name</div><i>}</i><span class="variable-message__close"></span></span></div>

CSS:

background-color: #fcfcfc;
border: 1px solid #e3e3e3;
border-top-left-radius: .2rem;
border-top-right-radius: .2rem;
color: #404040;
min-height: 4rem;
padding: 1rem 1.2rem;

Answer №1

Here is a suggestion for your CSS:

[contenteditable] {
    display: inline-block;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The props are not receiving the margin property

I am working on a styled component that needs to receive a prop to determine if it should have a margin on the left or right. It seems that there is a syntax issue that I am struggling with. Here is the code snippet: const FormDiv = styled.div` displa ...

Navigating File Paths in Node.js

My directory structure is as follows: Main > models > file1.ejs | |> routes > file2.ejs In my code, I'm trying to require file1 from file2 like this: const variable = require("../models/file1.ejs). But what if I don't want to ...

Access to the Express Node.js server is restricted to the machine that is currently hosting the server

I am facing a beginner issue with using express. I am on Ubuntu 14.04 and created a new directory where I ran "express" in the terminal to set up a project template. Following that, I ran "npm install" to install the dependencies. I then made changes to &a ...

Can "Operator" be used as a function parameter?

Take a look at this code snippet: const myFunction = (x, y, z) => { //Some code here }; $('whatever').on( event, function(){ myFunction( event.originalEvent && event.originalEvent.target ); //What is this operator doing? }); Can yo ...

Ensure that an async function's result is resolved before rendering react routes

I've been working on setting up an authentication service in my single-page application using React in the routes file. The goal is to check if a user is trying to access a private path, and verify that the token stored locally is valid. However, I&ap ...

Order an array in Javascript based on the day of the month

I am trying to create a unique function that changes the order of a string based on the day of the month. For instance, if the input string is "HELLO" and today's date is the 1st of April, the output should be "LHEOL". On the 2nd of April, it should ...

The target of the event in Internet Explorer 6

I am currently facing a challenge with an event handler that I have attached to an element in order to capture a bubbled click event. The key issue here is the need for a dependable method to retrieve the element that captured the event, rather than the el ...

Retrieving and assigning data values within an Angular JS service

Here is the service I created to fetch user details: angular.module('app') .factory('userDetailService', function($http) { var userData = {}; function getUserDetails(userId) { if (userId) { return $http.get ...

No display of Tailwind updates on local server in Safari browser using NextJS and a Mac M1

For my personal project with NextJS, I am using Tailwind CSS, but I am experiencing frequent issues with updating styles. It seems like there is a 50/50 chance that Tailwind will apply the styles to the component properly, and sometimes I have to go throug ...

How to implement a "callWhenReady" function in JavaScript

I am new to javascript and currently working on restructuring my prototype code. The current setup involves multiple levels of nested callbacks, making it difficult to read and manage. I am aiming for a cleaner approach similar to the following: GoogleMap ...

Is it Possible to Achieve Callbacks From AJAX to PHP Despite the Inability to Serialize Closures?

Question How can I incorporate a PHP callback passed via AJAX, where the callback is executed by the page requested through AJAX? The Scenario Comments are submitted through AJAX with parameters serialized and encrypted for security. The issue arises wh ...

Vue.js 2 components encountering issue with parent-child relationship due to undefined item

I recently started working with Vue and encountered the error referenceError: items is not defined. Can anyone help me figure out why this error is occurring or provide some guidance? Upon initial inspection of the code, it seems like the issue may be rel ...

The functionality of XMLHttpRequest becomes unreliable when dealing with GET parameters

I'm attempting to retrieve the DOM of a specific page. var req = new XMLHttpRequest(); req.open( 'GET', '/sport-hobby-kultura?adListing-visualPaginator-page=2&adListing-url=sport-hobby-kultura&do=adListing-visualPaginator-showP ...

Aligning characters according to Unicode standards

I'm looking to showcase a Unicode character at the center of a button. Below is the code snippet: <div class="col-xs-1"> <button class="btn btn-tool"> <span></span> </button> </div> span:before { ...

Convert the jade file to an HTML file while keeping the original file name

I'm currently attempting to configure Jade in a way that allows me to save my Jade files as HTML files while retaining the same file name. For example, I would like the file views/index.jade to be saved as dist/index.html This should apply to all ad ...

Utilize Angular 6 [ngClass] to dynamically add a class composed of multiple variables through direct string concatenation within [ngClass]

Currently, I am working with Angular 6 and I have the following variables: var1 = 'my'; var2 = '-'; var3 = 'class'; I am trying to achieve something like this: <div [ngClass]='var1 + var2 + var3'></div> ...

Updating the DOM using jQuery after receiving an AJAX response

I'm grappling with the most effective way to modify the DOM after receiving an AJAX response. I prefer to showcase the code rather than attempt to articulate my dilemma. // page contains several checkboxes $("input[type='checkbox']").live(& ...

How to eliminate the check box feature in angular-ivh-treeview

I have successfully implemented a tree using angular-ivh-treeview with objects based on the code provided here: https://github.com/iVantage/angular-ivh-treeview You can view the running sample of the tree by visiting: http://jsbin.com/rigepe/1 The tree w ...

execute a function after submitting the form

I need to implement a function that will be triggered when submitting a form. This function should work for all forms contained within the div with the id "#js_ajax_form". ( The JavaScript code functions properly in Chrome console // Here is the HTML sni ...

The focus() matcher in Jest encountered an error when testing the Vite-powered React application

After transitioning from create-react-app to Vite and incorporating RTL for creating and unit testing React applications, I encountered test failures relating to the hasFocus() matcher. The issue arose when attempting to verify if input fields (email, pass ...