Skip to content

Commit

Permalink
[Lit] render DSD attributes based on shadowRootOptions (#6351)
Browse files Browse the repository at this point in the history
* [Lit] render DSD attributes based on `shadowRootOptions`

## Changes

- Update `@astrojs/lit`’s `server.js` to properly render elements with `delegatesFocus: false` set in their `shadowRootOptions`.
- Logic is based on `@lit-labs/ssr` [latest implementation as found here](https://github.com/lit/lit/blob/b0c3f82ef0f97326a205e77e7e1043b75a5cc53f/packages/labs/ssr/src/lib/render-value.ts#L738)

## Testing

A test was added to ensure an element with `delegatesFocus` set to true has this attribute properly included in the rendered static markup.

* chore: add changeset
  • Loading branch information
hrmcdonald committed Feb 24, 2023
1 parent 5aa6580 commit 26bf12e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/old-fireants-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/lit': patch
---

Render DSD attributes based on `shadowRootOptions`
6 changes: 5 additions & 1 deletion packages/integrations/lit/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ function* render(Component, attrs, slots) {
yield `>`;
const shadowContents = instance.renderShadow({});
if (shadowContents !== undefined) {
yield '<template shadowroot="open" shadowrootmode="open">';
const { mode = 'open', delegatesFocus } = instance.shadowRootOptions ?? {};
// `delegatesFocus` is intentionally allowed to coerce to boolean to
// match web platform behavior.
const delegatesfocusAttr = delegatesFocus ? ' shadowrootdelegatesfocus' : '';
yield `<template shadowroot="${mode}" shadowrootmode="${mode}"${delegatesfocusAttr}>`;
yield* shadowContents;
yield '</template>';
}
Expand Down
11 changes: 11 additions & 0 deletions packages/integrations/lit/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,15 @@ describe('renderToStaticMarkup', () => {
expect($(tagName).attr('attr1')).to.equal(attr1);
expect($(`${tagName} template`).text()).to.contain(`Hello ${prop1}`);
});

it('should render DSD attributes based on shadowRootOptions', async () => {
const tagName = 'lit-component';
customElements.define(tagName, class extends LitElement {
static shadowRootOptions = {...LitElement.shadowRootOptions, delegatesFocus: true};
});
const render = await renderToStaticMarkup(tagName);
expect(render).to.deep.equal({
html: `<${tagName}><template shadowroot=\"open\" shadowrootmode=\"open\" shadowrootdelegatesfocus><!--lit-part--><!--/lit-part--></template></${tagName}>`,
});
});
});

0 comments on commit 26bf12e

Please sign in to comment.