Skip to content
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

Bento: Assign placeholder and fallback elements to service slot #34310

Merged
merged 4 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion extensions/amp-facebook-comments/1.0/storybook/Basic.amp.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ export const _default = () => {
data-locale={locale}
data-numposts={numPosts}
data-order-by={orderBy}
></amp-facebook-comments>
>
<div placeholder>
<h1>Placeholder</h1>
</div>
</amp-facebook-comments>
);
};

Expand Down
7 changes: 6 additions & 1 deletion src/preact/base-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ const SHADOW_CONTAINER_ATTRS = dict({
'part': 'c',
});

/** @const {string} */
const SERVICE_SLOT_NAME = 'i-amphtml-svc';

/** @const {!JsonObject<string, string>} */
const SERVICE_SLOT_ATTRS = dict({'name': 'i-amphtml-svc'});
const SERVICE_SLOT_ATTRS = dict({'name': SERVICE_SLOT_NAME});

/**
* The same as `applyFillContent`, but inside the shadow.
Expand Down Expand Up @@ -624,6 +627,8 @@ export class PreactBaseElement extends AMP.BaseElement {
SERVICE_SLOT_ATTRS
);
shadowRoot.appendChild(serviceSlot);
this.getPlaceholder()?.setAttribute('slot', SERVICE_SLOT_NAME);
this.getFallback()?.setAttribute('slot', SERVICE_SLOT_NAME);
}
this.container_ = container;

Expand Down
19 changes: 19 additions & 0 deletions test/unit/preact/test-base-element-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ describes.realWin('PreactBaseElement', spec, (env) => {
element = html`
<amp-preact layout="fixed" width="100" height="100">
<div id="child1"></div>
<div placeholder>foo</div>
<div fallback>bar</div>
</amp-preact>
`;
});
Expand All @@ -373,6 +375,23 @@ describes.realWin('PreactBaseElement', spec, (env) => {
).to.have.lengthOf(1);
});

it('should pass placeholder and fallback elements to service slot', async () => {
doc.body.appendChild(element);
await element.buildInternal();
await waitFor(() => component.callCount > 0, 'component rendered');
const serviceSlot = element.shadowRoot.querySelectorAll(
'slot[name="i-amphtml-svc"]'
);
expect(serviceSlot).to.have.lengthOf(1);
const placeholder = element.querySelector('[placeholder]');
const fallback = element.querySelector('[fallback]');
expect(placeholder.getAttribute('slot')).to.equal('i-amphtml-svc');
expect(fallback.getAttribute('slot')).to.equal('i-amphtml-svc');
expect(serviceSlot[0].assignedElements()).to.have.lengthOf(2);
expect(serviceSlot[0].assignedElements()[0]).to.equal(placeholder);
expect(serviceSlot[0].assignedElements()[1]).to.equal(fallback);
});

describe('SSR', () => {
let shadowRoot, container;
let componentEl, serviceSlotEl, styleEl;
Expand Down