Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Update com-editor to use auto-growing textarea
...under hood
  • Loading branch information
gordonbrander committed May 30, 2024
commit f39ecee59afbeed06dcefd546b05fb75179d29e2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {LitElement, html, css} from 'lit-element'
import {LitElement, html, css, PropertyValueMap} from 'lit-element'
import {customElement, property} from 'lit/decorators.js'
import {base} from '../styles'

Expand All @@ -8,7 +8,17 @@ const styles = css`
}

.editor {
background: transparent;
display: block;
border: none;
padding: 0;
width: 100%;
font-size: var(--body-size, 16px);
font-family: var(--body-font, sans-serif);
line-height: var(--body-line, 1.5em);
min-height: var(--body-line, 1.5em);
overflow: hidden;
resize: none;

&:focus {
outline: none;
Expand All @@ -23,8 +33,28 @@ export class ComEditor extends LitElement {
@property({type: String}) value = ''

render() {
const oninput = (event: InputEvent) => {
const textarea = event.target as HTMLTextAreaElement
this.value = textarea.value
}

return html`
<div class="editor" contenteditable="plaintext-only">${this.value}</div>
<textarea class="editor" @input=${oninput}>${this.value}</textarea>
`
}

#updateTextareaHeight() {
const textarea = this.shadowRoot?.querySelector('.editor') as HTMLTextAreaElement
textarea.style.height = '0px'
const height = textarea.scrollHeight
textarea.style.height = `${height}px`
}

protected updated(
changedProperties: Map<PropertyKey, any>
): void {
if (changedProperties.has('value')) {
this.#updateTextareaHeight()
}
}
}