Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
botandrose-machine committed Sep 13, 2024
1 parent a836b8b commit 23791a0
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/core/bardo.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export class Bardo {

replaceCurrentPermanentElementWithClone(permanentElement) {
const clone = permanentElement.cloneNode(true)
const shouldRefocus = document.activeElement == permanentElement
permanentElement.replaceWith(clone)
if (shouldRefocus) clone.focus()
}

replacePlaceholderWithPermanentElement(permanentElement) {
Expand Down
4 changes: 0 additions & 4 deletions src/core/drive/morphing_page_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export class MorphingPageRenderer extends PageRenderer {
dispatch("turbo:morph", { detail: { currentElement, newElement } })
}

async preservingPermanentElements(callback) {
return await callback()
}

get renderMethod() {
return "morph"
}
Expand Down
4 changes: 0 additions & 4 deletions src/core/frames/morphing_frame_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ export class MorphingFrameRenderer extends FrameRenderer {

morphChildren(currentElement, newElement)
}

async preservingPermanentElements(callback) {
return await callback()
}
}
6 changes: 1 addition & 5 deletions src/core/morphing.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ class DefaultIdiomorphCallbacks {
this.#beforeNodeMorphed = beforeNodeMorphed || (() => true)
}

beforeNodeAdded = (node) => {
return !(node.id && node.hasAttribute("data-turbo-permanent") && document.getElementById(node.id))
}

beforeNodeMorphed = (currentElement, newElement) => {
if (currentElement instanceof Element) {
if (!currentElement.hasAttribute("data-turbo-permanent") && this.#beforeNodeMorphed(currentElement, newElement)) {
if (this.#beforeNodeMorphed(currentElement, newElement)) {
const event = dispatch("turbo:before-morph-element", {
cancelable: true,
target: currentElement,
Expand Down
6 changes: 5 additions & 1 deletion src/core/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class Snapshot {
return anchor ? this.element.querySelector(`[id='${anchor}'], a[name='${anchor}']`) : null
}

getElementById(id) {
return this.element.querySelector(`#${id}`)
}

get isConnected() {
return this.element.isConnected
}
Expand All @@ -42,7 +46,7 @@ export class Snapshot {

for (const currentPermanentElement of this.permanentElements) {
const { id } = currentPermanentElement
const newPermanentElement = snapshot.getPermanentElementById(id)
const newPermanentElement = snapshot.getElementById(id)
if (newPermanentElement) {
permanentElementMap[id] = [currentPermanentElement, newPermanentElement]
}
Expand Down
8 changes: 5 additions & 3 deletions src/tests/fixtures/page_refresh.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
const application = Application.start()

addEventListener("focusin", ({ target }) => {
if (target instanceof HTMLInputElement && !target.hasAttribute("data-turbo-permanent")) {
if (target instanceof HTMLInputElement && target.hasAttribute("data-toggle-permanent-on-focus")) {
target.toggleAttribute("data-turbo-permanent", true)

target.addEventListener("focusout", () => target.toggleAttribute("data-turbo-permanent", false), { once: true })
target.addEventListener("focusout", () => {
target.toggleAttribute("data-turbo-permanent", false)
}, { once: true })
}
})

Expand Down Expand Up @@ -114,7 +116,7 @@ <h3>Element with Stimulus controller</h3>
<form method="get" data-turbo-action="replace" oninput="this.requestSubmit()">
<label>
Search
<input name="query">
<input id="query" name="query" data-toggle-permanent-on-focus>
</label>
<button>Form with params to refresh the page</button>
</form>
Expand Down
2 changes: 1 addition & 1 deletion src/tests/functional/page_refresh_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ test("it preserves focus across morphs", async ({ page }) => {
await nextEventNamed(page, "turbo:render", { renderMethod: "morph" })

await expect(input).toBeFocused()
await expect(input).toHaveValue("Preserve me")
await expect(input).toHaveValue("")
})

test("it preserves focus and the [data-turbo-permanent] element's value across morphs", async ({ page }) => {
Expand Down

0 comments on commit 23791a0

Please sign in to comment.