-
Notifications
You must be signed in to change notification settings - Fork 616
fix(fast-html): fix event bindings inside f-when not firing after hydration #7327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mohamedmansour
wants to merge
14
commits into
microsoft:main
Choose a base branch
from
mohamedmansour:fix/hydration-under-conditions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d681f8e
fix: Fix event bindings inside f-when not firing after hydration
mohamedmansour 8a6e756
add more tests
mohamedmansour eb09ecd
Fix tests
mohamedmansour ea1a800
Apply suggestions from code review
mohamedmansour be54d2c
Merge branch 'main' into fix/hydration-under-conditions
mohamedmansour 53982ca
fix(fast-html): bind event handlers to host element inside repeat dir…
d2d473e
Update packages/fast-element/src/templating/html-binding-directive.ts
mohamedmansour 8d56ed5
Add f-when false test case
mohamedmansour c4a0b81
Merge branch 'fix/hydration-under-conditions' of https://github.com/m…
mohamedmansour 3c7b703
Merge remote-tracking branch 'origin/pr-7327' into fix/hydration-unde…
mohamedmansour 5eba2ed
fix(fast-html): respect explicit falsy values in f-when during hydration
mohamedmansour 96d7c75
Merge remote-tracking branch 'upstream/main' into fix/hydration-under…
mohamedmansour b062869
revert: remove repeat event binding context walk-up
mohamedmansour 37fd13a
refactor: merge when-false-explicit fixture into when-event
mohamedmansour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-element-ccc364c3-3d9a-4477-a877-812d7fc72bff.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "patch", | ||
| "comment": "Address hydration mismatches when a template is provided but no boundaries exist, this will skip creation of a new view.", | ||
| "packageName": "@microsoft/fast-element", | ||
| "email": "hello@mohamedmansour.com", | ||
| "dependentChangeType": "none" | ||
| } |
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-html-1cbbfa63-0099-407f-9cd2-36fe3f44f3c3.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "Address hydration mismatches when a template is provided but no boundaries exist, this will skip creation of a new view.", | ||
| "packageName": "@microsoft/fast-html", | ||
| "email": "hello@mohamedmansour.com", | ||
| "dependentChangeType": "none" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en-US"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title></title> | ||
| <script type="module" src="./main.ts"></script> | ||
| </head> | ||
| <body> | ||
| <f-template name="repeat-event-element"> | ||
| <template> | ||
| <ul> | ||
| <f-repeat value="{{item in items}}"> | ||
| <li> | ||
| <span class="name">{{item.name}}</span> | ||
| <button class="toggle" data-name="{{item.name}}" @click="{onItemClick(e)}">Toggle</button> | ||
| <f-when value="{{item.expanded}}"> | ||
| <span class="detail">Details for {{item.name}}</span> | ||
| </f-when> | ||
| </li> | ||
| </f-repeat> | ||
| </ul> | ||
| </template> | ||
| </f-template> | ||
| <!-- No SSR: let FAST render everything client-side --> | ||
| <repeat-event-element></repeat-event-element> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { RenderableFASTElement, TemplateElement } from "@microsoft/fast-html"; | ||
| import { FASTElement, observable } from "@microsoft/fast-element"; | ||
|
|
||
| interface ListItem { | ||
| name: string; | ||
| expanded: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Minimal repro for two issues with event bindings inside f-repeat: | ||
| * | ||
| * 1. @click on a <button> inside <f-repeat> does not fire after hydration. | ||
| * 2. <f-when> inside <f-repeat> does not re-evaluate when item properties | ||
| * change via array replacement (e.g. immutable update pattern). | ||
| */ | ||
| export class RepeatEventElement extends FASTElement { | ||
| @observable | ||
| items: ListItem[] = [ | ||
| { name: "Alpha", expanded: false }, | ||
| { name: "Beta", expanded: false }, | ||
| { name: "Charlie", expanded: false }, | ||
| ]; | ||
|
|
||
| public lastClicked: string = ""; | ||
|
|
||
| public onItemClick(e: Event): void { | ||
| const target = e.currentTarget as HTMLElement; | ||
| const name = target.dataset.name ?? ""; | ||
| this.lastClicked = name; | ||
| console.log("onItemClick fired:", name); | ||
|
|
||
| // Immutable update: replace array with new objects toggling expanded. | ||
| this.items = this.items.map((item) => { | ||
| if (item.name !== name) return item; | ||
| return { ...item, expanded: !item.expanded }; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| RenderableFASTElement(RepeatEventElement).defineAsync({ | ||
| name: "repeat-event-element", | ||
| templateOptions: "defer-and-hydrate", | ||
| }); | ||
|
|
||
| TemplateElement.define({ | ||
| name: "f-template", | ||
| }); |
11 changes: 11 additions & 0 deletions
11
packages/fast-html/test/fixtures/repeat-event/repeat-event.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <ul> | ||
| <f-repeat value="{{item in items}}"> | ||
| <li> | ||
| <span class="name">{{item.name}}</span> | ||
| <button class="toggle" data-name="{{item.name}}" @click="{onItemClick(e)}">Toggle</button> | ||
| <f-when value="{{item.expanded}}"> | ||
| <span class="detail">Details for {{item.name}}</span> | ||
| </f-when> | ||
| </li> | ||
| </f-repeat> | ||
| </ul> |
7 changes: 7 additions & 0 deletions
7
packages/fast-html/test/fixtures/repeat-event/repeat-event.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "items": [ | ||
| { "name": "Alpha", "expanded": false }, | ||
| { "name": "Beta", "expanded": false }, | ||
| { "name": "Charlie", "expanded": false } | ||
| ] | ||
| } |
135 changes: 135 additions & 0 deletions
135
packages/fast-html/test/fixtures/repeat-event/repeat-event.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { expect, test } from "@playwright/test"; | ||
| import type { RepeatEventElement } from "./main.js"; | ||
|
|
||
| test.describe("f-repeat with event bindings and inner f-when", () => { | ||
| test("@click on button inside f-repeat should fire after hydration", async ({ | ||
| page, | ||
| }) => { | ||
| await page.goto("/fixtures/repeat-event/"); | ||
|
|
||
| const element = page.locator("repeat-event-element"); | ||
| const buttons = element.locator("button.toggle"); | ||
|
|
||
| // All 3 buttons should be rendered from SSR | ||
| await expect(buttons).toHaveCount(3); | ||
|
|
||
| // Debug: inspect how the event binding resolved | ||
| const debug = await element.evaluate((el) => { | ||
| const sr = el.shadowRoot!; | ||
| const btn = sr.querySelector("button.toggle") as any; | ||
| // Collect all keys stored on the button by FAST | ||
| const fastKeys = Object.keys(btn).filter( | ||
| (k) => k.startsWith("fast") || k.startsWith("__") || k.length > 20, | ||
| ); | ||
| // Check for the controller reference stored by event binding | ||
| const allKeys = Object.getOwnPropertyNames(btn); | ||
| const controllerKeys = allKeys.filter((k) => { | ||
| try { | ||
| return btn[k] && typeof btn[k] === "object" && "source" in btn[k]; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }); | ||
| return { | ||
| fastKeys, | ||
| controllerKeys, | ||
| allNonStandardKeys: allKeys.filter( | ||
| (k) => | ||
| !k.startsWith("on") && | ||
| !HTMLButtonElement.prototype.hasOwnProperty(k) && | ||
| k !== "constructor", | ||
| ).slice(0, 10), | ||
| }; | ||
| }); | ||
| console.log("Button FAST state:", JSON.stringify(debug)); | ||
|
|
||
| // Click the first button — event binding should work | ||
| await buttons.nth(0).click(); | ||
|
|
||
| // Check for console messages | ||
| const messages: string[] = []; | ||
| page.on("console", (msg) => messages.push(msg.text())); | ||
|
|
||
| await buttons.nth(0).click(); | ||
| await page.waitForTimeout(200); | ||
|
|
||
| console.log("Console messages after click:", messages); | ||
|
|
||
| // Verify the click handler fired | ||
| const lastClicked = await element.evaluate( | ||
| (el: RepeatEventElement) => el.lastClicked, | ||
| ); | ||
| console.log("lastClicked:", lastClicked); | ||
| expect(lastClicked).toBe("Alpha"); | ||
| }); | ||
|
|
||
| test("clicking button should toggle expanded and show detail via f-when", async ({ | ||
| page, | ||
| }) => { | ||
| await page.goto("/fixtures/repeat-event/"); | ||
|
|
||
| const element = page.locator("repeat-event-element"); | ||
| const buttons = element.locator("button.toggle"); | ||
| const details = element.locator("span.detail"); | ||
|
|
||
| // Initially no details visible (all expanded=false) | ||
| await expect(details).toHaveCount(0); | ||
|
|
||
| // Click Alpha toggle | ||
| await buttons.nth(0).click(); | ||
|
|
||
| // f-when should re-evaluate: Alpha's detail should appear | ||
| await expect(details).toHaveCount(1); | ||
| await expect(details.nth(0)).toHaveText("Details for Alpha"); | ||
|
|
||
| // Verify the item's expanded state | ||
| await expect( | ||
| element.evaluate((el: RepeatEventElement) => { | ||
| return el.items.map((i) => ({ | ||
| name: i.name, | ||
| expanded: i.expanded, | ||
| })); | ||
| }), | ||
| ).resolves.toEqual([ | ||
| { name: "Alpha", expanded: true }, | ||
| { name: "Beta", expanded: false }, | ||
| { name: "Charlie", expanded: false }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("clicking same button twice should toggle detail off", async ({ | ||
| page, | ||
| }) => { | ||
| await page.goto("/fixtures/repeat-event/"); | ||
|
|
||
| const element = page.locator("repeat-event-element"); | ||
| const buttons = element.locator("button.toggle"); | ||
| const details = element.locator("span.detail"); | ||
|
|
||
| // Click Alpha on | ||
| await buttons.nth(0).click(); | ||
| await expect(details).toHaveCount(1); | ||
|
|
||
| // Click Alpha off | ||
| await buttons.nth(0).click(); | ||
| await expect(details).toHaveCount(0); | ||
| }); | ||
|
|
||
| test("clicking different buttons should show multiple details", async ({ | ||
| page, | ||
| }) => { | ||
| await page.goto("/fixtures/repeat-event/"); | ||
|
|
||
| const element = page.locator("repeat-event-element"); | ||
| const buttons = element.locator("button.toggle"); | ||
| const details = element.locator("span.detail"); | ||
|
|
||
| // Expand Alpha and Charlie | ||
| await buttons.nth(0).click(); | ||
| await buttons.nth(2).click(); | ||
|
|
||
| await expect(details).toHaveCount(2); | ||
| await expect(details.nth(0)).toHaveText("Details for Alpha"); | ||
| await expect(details.nth(1)).toHaveText("Details for Charlie"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en-US"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title></title> | ||
| <script type="module" src="./main.ts"></script> | ||
| </head> | ||
| <body> | ||
| <!-- Template uses "serverOnly" which is NOT a property on test-element. | ||
| This simulates <if condition="serverOnly"> where the server state has serverOnly=true | ||
| but the client class doesn't know about it. --> | ||
| <f-template name="test-element"> | ||
| <template><f-when value="{{serverOnly}}"><button @click="{handleClick()}">Click me</button></f-when></template> | ||
| </f-template> | ||
| <!-- SSR: condition was true, so button is rendered with hydration markers --> | ||
| <test-element id="when-event-show"> | ||
| <template shadowrootmode="open"><!--fe-b$$start$$0$$MRl5Rw6tl3$$fe-b--><button data-fe-b-0>Click me</button><!--fe-b$$end$$0$$MRl5Rw6tl3$$fe-b--></template> | ||
| </test-element> | ||
| <!-- SSR: condition was false, so no content between markers --> | ||
| <test-element id="when-event-hide"> | ||
| <template shadowrootmode="open"><!--fe-b$$start$$0$$MRl5Rw6tl3$$fe-b--><!--fe-b$$end$$0$$MRl5Rw6tl3$$fe-b--></template> | ||
| </test-element> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { RenderableFASTElement, TemplateElement } from "@microsoft/fast-html"; | ||
| import { FASTElement } from "@microsoft/fast-element"; | ||
|
|
||
| // This element intentionally does NOT define "serverOnly" as a property. | ||
| // The f-when condition references "serverOnly" which only exists in server state, | ||
| // simulating the real-world scenario where <if condition="..."> uses server-only data. | ||
| class TestElement extends FASTElement { | ||
| public clickCount: number = 0; | ||
|
|
||
| public handleClick = (): void => { | ||
| this.clickCount++; | ||
| console.log("clicked:" + this.clickCount); | ||
| }; | ||
| } | ||
| RenderableFASTElement(TestElement).defineAsync({ | ||
| name: "test-element", | ||
| templateOptions: "defer-and-hydrate", | ||
| }); | ||
|
|
||
| TemplateElement.define({ | ||
| name: "f-template", | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <f-when value="{{serverOnly}}"><button @click="{handleClick()}">Click me</button></f-when> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.