Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/elements/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ export class LexicalEditorElement extends HTMLElement {
// Skip if the contenteditable already owns focus — the update would be a
// no-op but still triggers a full style/layout pass on pages with large
// DOMs.
if (this.#isContentFocused) return

this.editor.focus(() => this.#onFocus())
if (this.editor && !this.#isContentFocused) {
this.editor.focus(() => this.#onFocus())
}
}

get #isContentFocused() {
Expand Down
17 changes: 17 additions & 0 deletions test/browser/tests/editor/focus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,21 @@ test.describe("Focus", () => {

await expect(editor.content).toBeFocused()
})

test("focus() on an uninitialized editor is a safe no-op", async ({ page }) => {
const thrown = await page.evaluate(async () => {
await customElements.whenDefined("lexxy-editor")

const editor = document.createElement("lexxy-editor")

try {
editor.focus()
return null
} catch (error) {
return error.message
}
})

expect(thrown).toBeNull()
})
})
Loading