# Textarea in widget (locked leaf type)

**URL:** <https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198>\
**Category:** Uncategorized\
**Created:** [February 26, 2016, 4:51pm UTC](https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198 "2016-02-26T16:51:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![forresto](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/forresto/32/108_2.png) [@forresto](https://discuss.prosemirror.net/u/forresto)\
**Post date:** [February 26, 2016, 4:51pm UTC](https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198/1 "2016-02-26T16:51:06Z")

</div>

I’m looking at refactoring our widgets. Now they are [overlays with absolute positioning](http://discuss.prosemirror.net/t/the-grid-ed-overlay-widgets/150), but putting them in the PM content flow will simplify things.

But when a widget has a textarea or input, I can’t focus and type there. The text goes to the previous PM position, even though the textarea has focus.

Noticed the same issue with @peteb’s widgets: ![](https://discuss.prosemirror.net/uploads/secondsite/original/1X/0500608053ab249edda292e69aca969479a0ed7a.gif)

---

<div class="post-metadata">

**Author:** ![peteb](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/peteb/32/155_2.png) [@peteb](https://discuss.prosemirror.net/u/peteb)\
**Post date:** [February 26, 2016, 5:10pm UTC](https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198/2 "2016-02-26T17:10:00Z")

</div>

You are right. For my application that isn’t a problem because my editor is being used to build HTML content not data entry. Another issue I found in FF is that you sometimes get a NS\_ERROR\_FAILURE. It doesn’t affect anything and apparently has something to do with focus in FF.

That also made me wonder if text entered in input fields is even tracked by PM.

---

<div class="post-metadata">

**Author:** ![forresto](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/forresto/32/108_2.png) [@forresto](https://discuss.prosemirror.net/u/forresto)\
**Post date:** [February 26, 2016, 5:37pm UTC](https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198/3 "2016-02-26T17:37:15Z")

</div>

@marijn ideally I could give the widget container schema a property to specify “I’m responsible for the DOM in here.”

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [February 29, 2016, 3:33pm UTC](https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198/4 "2016-02-29T15:33:08Z")

</div>

With [this patch](https://github.com/ProseMirror/prosemirror/commit/d293d71e1344de873b5d5f6ef00a3ec4b883ed62) the editor won’t interpret key (and composition/input) events when it isn’t focused. That should address this problem.

---

<div class="post-metadata">

**Author:** ![forresto](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/forresto/32/108_2.png) [@forresto](https://discuss.prosemirror.net/u/forresto)\
**Post date:** [March 24, 2016, 7:43pm UTC](https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198/5 "2016-03-24T19:43:14Z")

</div>

Still seems to be some issues when triple-click selecting text in an input. Also any selection of text in an iframe widget.

---

<div class="post-metadata">

**Author:** ![etclub](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/etclub/32/1884_2.png) [@etclub](https://discuss.prosemirror.net/u/etclub)\
**Post date:** [September 25, 2024, 8:32am UTC](https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198/6 "2024-09-25T08:32:08Z")

</div>

I found this quite old but related post. I can focus and type anything in the textarea, but when I press `delete` or `backspace`, the editor content changes, which I am not sure is bug or not.

Here is my plugin code

```auto
new Plugin({
        state: {
          init(){},
          apply(tr, set, old, state){
            const dom = document.createElement('div')
            const textarea = document.createElement('textarea')
            textarea.onKeyPress = function(e){
              e.stopPropagation()
            }
            dom.appendChild(textarea)
            return DecorationSet.create(state.doc, [
              Decoration.widget(0, dom)
            ])
            
          }
        },

```

And the issue can be reproduced by:

1. in prosemirror editor, type some text, like `textarea in widget`
2. select part of the text
3. focus in the textarea, and type anything, everything works
4. now press `delete` or `backspace`, the selected text will be deleted, and the textarea becomes empty.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [September 25, 2024, 9:06am UTC](https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198/7 "2024-09-25T09:06:16Z")

</div>

You’ll want to provide a [`stopEvent`](https://prosemirror.net/docs/ref/#view.Decoration%5Ewidget%5Espec.stopEvent) option for the widget. Also, `onKeyPress` (with capital letters) isn’t going to have any effect, and you want `onkeydown` anyway, because that’s the event that the editor handlers.

Furthermore, if you recreate your widget every time `apply` is called, that will indeed reset the textarea whenever something happens in the editor, so that’s probably not a good idea. Using a function, instead of a DOM node, when creating the decoration, and making sure you keep passing the exact same function value, can prevent this.

---

<div class="post-metadata">

**Author:** ![etclub](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/etclub/32/1884_2.png) [@etclub](https://discuss.prosemirror.net/u/etclub)\
**Post date:** [September 25, 2024, 10:06am UTC](https://discuss.prosemirror.net/t/textarea-in-widget-locked-leaf-type/198/8 "2024-09-25T10:06:39Z")

</div>

It seems that I have been ignoring this option before, although I used it in nodeviews. Thank you for pointing out my mistake and the helpful instruction.
