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

fix(scrim): handle slotted children correctly #7477

Merged
merged 3 commits into from
Aug 8, 2023
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
13 changes: 10 additions & 3 deletions packages/calcite-components/src/components/scrim/scrim.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,21 @@ describe("calcite-scrim", () => {
expect(clickSpy).toHaveReceivedEventTimes(1);
});

it("does not render content if the default slot if it is empty", async () => {
it("does not display content if the default slot if it is empty", async () => {
const page = await newE2EPage();

await page.setContent(`<calcite-scrim></calcite-scrim>`);

const contentNode = await page.find(`calcite-scrim >>> .${CSS.content}`);
expect(contentNode).toHaveAttribute("hidden");

const scrim = await page.find("calcite-scrim");
scrim.innerHTML = "<p>Content added after initialization</p>";
await page.waitForChanges();

expect(contentNode).toBeNull();
const content = await page.find("p");
expect(content).toBeTruthy();
expect(await content.isVisible()).toBe(true);
expect(contentNode).not.toHaveAttribute("hidden");
});

it("renders content in the default slot has content", async () => {
Expand Down
30 changes: 18 additions & 12 deletions packages/calcite-components/src/components/scrim/scrim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ScrimMessages } from "./assets/scrim/t9n";
import { CSS, BREAKPOINTS } from "./resources";
import { createObserver } from "../../utils/observers";
import { Scale } from "../interfaces";
import { slotChangeHasAssignedElement } from "../../utils/dom";

/**
* @slot - A slot for adding custom content, primarily loading information.
Expand Down Expand Up @@ -75,6 +76,8 @@ export class Scrim implements LocalizedComponent, T9nComponent {
updateMessages(this, this.effectiveLocale);
}

@State() hasContent = false;

//--------------------------------------------------------------------------
//
// Lifecycle
Expand Down Expand Up @@ -104,21 +107,20 @@ export class Scrim implements LocalizedComponent, T9nComponent {
// --------------------------------------------------------------------------

render(): VNode {
const { el, loading, messages } = this;
const hasContent = el.innerHTML.trim().length > 0;
const loaderNode = loading ? (
<calcite-loader label={messages.loading} ref={this.storeLoaderEl} scale={this.loaderScale} />
) : null;
const contentNode = hasContent ? (
<div class={CSS.content}>
<slot />
</div>
) : null;
const { hasContent, loading, messages } = this;

return (
<div class={CSS.scrim}>
{loaderNode}
{contentNode}
{loading ? (
<calcite-loader
label={messages.loading}
ref={this.storeLoaderEl}
scale={this.loaderScale}
/>
) : null}
<div class={CSS.content} hidden={!hasContent}>
<slot onSlotchange={this.handleDefaultSlotChange} />
</div>
</div>
);
}
Expand All @@ -129,6 +131,10 @@ export class Scrim implements LocalizedComponent, T9nComponent {
//
// --------------------------------------------------------------------------

private handleDefaultSlotChange = (event: Event): void => {
this.hasContent = slotChangeHasAssignedElement(event);
};

private storeLoaderEl = (el: HTMLCalciteLoaderElement): void => {
this.loaderEl = el;
this.handleResize();
Expand Down