Skip to content

Commit 4ca5e80

Browse files
authored
fix ownsElement to account for portals (#3951)
Closes #3950.
1 parent 8b9a89b commit 4ca5e80

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

assets/js/phoenix_live_view/view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2176,7 +2176,7 @@ export default class View {
21762176
}
21772177

21782178
ownsElement(el) {
2179-
let parentViewEl = el.closest(PHX_VIEW_SELECTOR);
2179+
let parentViewEl = DOM.closestViewEl(el);
21802180
return (
21812181
el.getAttribute(PHX_PARENT_ID) === this.id ||
21822182
(parentViewEl && parentViewEl.id === this.id) ||

test/e2e/support/portal.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ defmodule Phoenix.LiveViewTest.E2E.PortalLive do
6060
this.liveSocket.execJS(this.el, this.el.dataset.hide);
6161
this.cleanup && this.cleanup();
6262
},
63+
},
64+
InsidePortal: {
65+
mounted() {
66+
this.js().setAttribute(this.el, "data-portalhook-mounted", "true");
67+
}
6368
}
6469
}
6570
})
@@ -145,6 +150,8 @@ defmodule Phoenix.LiveViewTest.E2E.PortalLive do
145150
<p>DOM patching works as expected: {@count}</p>
146151
<.button phx-click={JS.patch("/portal?param=#{@param_next}")}>Patch this LiveView</.button>
147152
</.modal>
153+
154+
<div id="hook-test" phx-hook="InsidePortal">This should get a data attribute</div>
148155
</.portal>
149156
150157
<.portal id="portal-source-2" target="#app-portal">

test/e2e/tests/portal.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,14 @@ test("tooltip example", async ({ page }) => {
103103
await expect(page.locator("#tooltip-example-portal")).toBeHidden();
104104
await expect(page.locator("#tooltip-example-no-portal")).toBeVisible();
105105
});
106+
107+
test("teleported hook works correctly", async ({ page }) => {
108+
// https://github.com/phoenixframework/phoenix_live_view/issues/3950
109+
await page.goto("/portal?tick=false");
110+
await syncLV(page);
111+
112+
await expect(page.locator("#hook-test")).toHaveAttribute(
113+
"data-portalhook-mounted",
114+
"true",
115+
);
116+
});

0 commit comments

Comments
 (0)