Skip to content

Commit

Permalink
fix(scrim): handle slotted children correctly (#7477)
Browse files Browse the repository at this point in the history
**Related Issue:** #7032

## Summary

- Support adding slotted content after initial render
- Uses `onSlotChange` to detect slotted elements
- Hides content container via the hidden attribute
- Updates test
  • Loading branch information
driskull authored Aug 8, 2023
1 parent e4017dc commit c5ce008
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
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

0 comments on commit c5ce008

Please sign in to comment.