-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds FASTElementRenderer to fast-ssr (#5613)
* adds FASTElementRenderer as a ElementRenderer implementation * adding test * adding eslint for file extensions * re-structure tests to better support dependency structures * Fixing tests for FASTElementRenderer.matchesClass * adding code comments * Update packages/web-components/fast-ssr/src/element-renderer/element-renderer.ts Co-authored-by: Jane Chu <[email protected]> Co-authored-by: nicholasrice <[email protected]> Co-authored-by: Jane Chu <[email protected]>
- Loading branch information
1 parent
e097a1f
commit 9ff9657
Showing
13 changed files
with
110 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
module.exports = { | ||
extends: ["@microsoft/eslint-config-fast-dna", "prettier"], | ||
rules: { | ||
"import/extensions": ["error", "always"], | ||
}, | ||
}; |
This file contains 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
6 changes: 2 additions & 4 deletions
6
...onents/fast-ssr/test/playwright.config.ts → ...components/fast-ssr/playwright.config.cjs
This file contains 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 |
---|---|---|
@@ -1,11 +1,9 @@ | ||
// playwright.config.ts | ||
import { PlaywrightTestConfig } from "@playwright/test"; | ||
const config: PlaywrightTestConfig = { | ||
module.exports = { | ||
testDir: "./dist/esm", | ||
webServer: { | ||
command: "npm run test-server", | ||
port: 8080, | ||
timeout: 120 * 1000, | ||
reuseExistingServer: false, | ||
}, | ||
}; | ||
export default config; |
This file contains 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
17 changes: 17 additions & 0 deletions
17
packages/web-components/fast-ssr/src/element-renderer/elemenent-renderer.spec.ts
This file contains 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,17 @@ | ||
import "@lit-labs/ssr/lib/install-global-dom-shim.js"; | ||
import { FASTElement } from "@microsoft/fast-element"; | ||
import { expect, test } from '@playwright/test'; | ||
import { FASTElementRenderer } from "./element-renderer.js"; | ||
|
||
test.describe("FASTElementRenderer", () => { | ||
test.describe("should have a 'matchesClass' method", () => { | ||
test("that returns true when invoked with a class that extends FASTElement ", () => { | ||
class MyElement extends FASTElement {} | ||
expect(FASTElementRenderer.matchesClass(MyElement)).toBe(true); | ||
}); | ||
test("that returns false when invoked with a class that does not extend FASTElement ", () => { | ||
class MyElement extends HTMLElement {} | ||
expect(FASTElementRenderer.matchesClass(MyElement)).toBe(false); | ||
}); | ||
}) | ||
}) |
74 changes: 74 additions & 0 deletions
74
packages/web-components/fast-ssr/src/element-renderer/element-renderer.ts
This file contains 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,74 @@ | ||
import { ElementRenderer, RenderInfo } from "@lit-labs/ssr"; | ||
import { FASTElement } from "@microsoft/fast-element"; | ||
|
||
export class FASTElementRenderer extends ElementRenderer { | ||
/** | ||
* The element instance represented by the {@link FASTElementRenderer}. | ||
*/ | ||
public readonly element!: FASTElement; | ||
|
||
/** | ||
* Tests a constructor to determine if it should be managed by a {@link FASTElementRenderer}. | ||
* @param ctor - The constructor to test. | ||
*/ | ||
public static matchesClass(ctor: typeof HTMLElement): boolean { | ||
return ctor.prototype instanceof FASTElement; | ||
} | ||
|
||
/** | ||
* Indicate to the {@link FASTElementRenderer} that the instance should execute DOM connection behavior. | ||
*/ | ||
public connectedCallback(): void { | ||
this.element.connectedCallback(); | ||
} | ||
|
||
/** | ||
* Constructs a new {@link FASTElementRenderer}. | ||
* @param tagName - the tag-name of the element to create. | ||
*/ | ||
constructor(tagName: string) { | ||
super(tagName); | ||
|
||
const ctor: typeof FASTElement | null = customElements.get(this.tagName); | ||
|
||
if (ctor) { | ||
this.element = new ctor(); | ||
} else { | ||
throw new Error( | ||
`FASTElementRenderer was unable to find a constructor for a custom element with the tag name '${tagName}'.` | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Renders the component internals to light DOM instead of shadow DOM. | ||
* @param renderInfo - information about the current rendering context. | ||
*/ | ||
*renderLight(renderInfo: RenderInfo): IterableIterator<string> { | ||
// TODO - this will yield out the element's template using the template renderer, skipping any shadow-DOM specific emission. | ||
yield ""; | ||
} | ||
|
||
/** | ||
* Render the component internals to shadow DOM. | ||
* @param renderInfo - information about the current rendering context. | ||
*/ | ||
*renderShadow(renderInfo: RenderInfo): IterableIterator<string> { | ||
// TODO - this will yield out the element's template using the template renderer | ||
yield ""; | ||
} | ||
|
||
/** | ||
* Indicate to the {@link FASTElementRenderer} that an attribute has been changed. | ||
* @param name - The name of the changed attribute | ||
* @param old - The old attribute value | ||
* @param value - The new attribute value | ||
*/ | ||
attributeChangedCallback( | ||
name: string, | ||
old: string | null, | ||
value: string | null | ||
): void { | ||
this.element.attributeChangedCallback(name, old, value); | ||
} | ||
} |
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
export default "fast-ssr"; | ||
export { FASTElementRenderer } from "./element-renderer/element-renderer.js"; |
This file contains 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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