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(block): fix rendering tied to named-slot content #10449

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 9 additions & 10 deletions packages/calcite-components/src/components/block/block.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,25 @@ describe("calcite-block", () => {

it("can display/hide content", async () => {
const page = await newE2EPage();
await page.setContent("<calcite-block><div>some content</div></calcite-block>");
let element = await page.find("calcite-block");
let content = await page.find(`calcite-block >>> .${CSS.content}`);
await page.setContent(
html`<calcite-block heading="heading" description="description"><div>Hello world!</div></calcite-block>`,
);
await skipAnimations(page);
await page.waitForChanges();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This waitForChanges shouldn't be necessary.


const element = await page.find("calcite-block");
const content = await page.find(`calcite-block >>> section.${CSS.content}`);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found something weird here. When using the >>> piercing selector, it was finding some other CSS.content class that is a div. Adding section in front of this fixed it but Its also concerning it was finding this element which it shouldn't find. Using an id is fine too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it can be confusing, but this is working as expected (see https://stenciljs.com/docs/end-to-end-testing#find-an-element-in-the-shadow-dom). This also came up in a Stencil issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I didn't know it searched all shadow roots. good to know!

expect(content).not.toBeNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions should already be covered in other tests.

expect(await element.getProperty("open")).toBe(false);
expect(await content.isVisible()).toBe(false);

element.setProperty("open", true);
await page.waitForChanges();
element = await page.find("calcite-block[open]");
content = await page.find(`calcite-block >>> .${CSS.content}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨🧹✨


expect(element).toBeTruthy();
expect(await content.isVisible()).toBe(true);

element.setProperty("open", false);
await page.waitForChanges();
element = await page.find("calcite-block[open]");
content = await page.find(`calcite-block >>> .${CSS.content}`);

expect(element).toBeNull();
expect(await content.isVisible()).toBe(false);
});

Expand Down
78 changes: 37 additions & 41 deletions packages/calcite-components/src/components/block/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ import {
VNode,
Watch,
} from "@stencil/core";
import {
ConditionalSlotComponent,
connectConditionalSlotComponent,
disconnectConditionalSlotComponent,
} from "../../utils/conditionalSlot";
import {
focusFirstTabbable,
getSlotted,
toAriaBoolean,
slotChangeHasAssignedElement,
} from "../../utils/dom";
import { focusFirstTabbable, toAriaBoolean, slotChangeHasAssignedElement } from "../../utils/dom";
import {
InteractiveComponent,
InteractiveContainer,
Expand Down Expand Up @@ -71,7 +61,6 @@ import { BlockMessages } from "./assets/block/t9n";
})
export class Block
implements
ConditionalSlotComponent,
InteractiveComponent,
LocalizedComponent,
T9nComponent,
Expand Down Expand Up @@ -233,6 +222,12 @@ export class Block
updateMessages(this, this.effectiveLocale);
}

@State() hasIcon = false;

@State() hasControl = false;

@State() hasMenuActions = false;

@State() hasContentStart = false;

@State() hasEndActions = false;
Expand All @@ -248,7 +243,6 @@ export class Block
// --------------------------------------------------------------------------

connectedCallback(): void {
connectConditionalSlotComponent(this);
connectLocalized(this);
connectMessages(this);

Expand All @@ -258,7 +252,6 @@ export class Block
disconnectedCallback(): void {
disconnectLocalized(this);
disconnectMessages(this);
disconnectConditionalSlotComponent(this);
}

async componentWillLoad(): Promise<void> {
Expand Down Expand Up @@ -314,6 +307,18 @@ export class Block
this.calciteBlockToggle.emit();
};

private controlSlotChangeHandler = (event: Event): void => {
this.hasControl = slotChangeHasAssignedElement(event);
};

private menuActionsSlotChangeHandler = (event: Event): void => {
this.hasMenuActions = slotChangeHasAssignedElement(event);
};

private iconSlotChangeHandler = (event: Event): void => {
this.hasIcon = slotChangeHasAssignedElement(event);
};

private actionsEndSlotChangeHandler = (event: Event): void => {
this.hasEndActions = slotChangeHasAssignedElement(event);
};
Expand All @@ -338,8 +343,6 @@ export class Block
private renderLoaderStatusIcon(): VNode[] {
const { loading, messages, status } = this;

const hasSlottedIcon = !!getSlotted(this.el, SLOTS.icon);

return loading ? (
<div class={CSS.icon} key="loader">
<calcite-loader inline label={messages.loading} />
Expand All @@ -356,25 +359,24 @@ export class Block
scale="s"
/>
</div>
) : hasSlottedIcon ? (
<div class={CSS.icon} key="icon-slot">
<slot key="icon-slot" name={SLOTS.icon} />
) : (
<div class={CSS.icon} hidden={!this.hasIcon} key="icon-slot">
<slot key="icon-slot" name={SLOTS.icon} onSlotchange={this.iconSlotChangeHandler} />
</div>
) : null;
);
}

private renderActionsEnd(): VNode {
return (
<div class={CSS.actionsEnd}>
<div class={CSS.actionsEnd} hidden={!this.hasEndActions}>
<slot name={SLOTS.actionsEnd} onSlotchange={this.actionsEndSlotChangeHandler} />
</div>
);
}

private renderContentStart(): VNode {
const { hasContentStart } = this;
return (
<div class={CSS.contentStart} hidden={!hasContentStart}>
<div class={CSS.contentStart} hidden={!this.hasContentStart}>
<slot name={SLOTS.contentStart} onSlotchange={this.handleContentStartSlotChange} />
</div>
);
Expand Down Expand Up @@ -422,7 +424,6 @@ export class Block
render(): VNode {
const {
collapsible,
el,
loading,
open,
heading,
Expand All @@ -446,8 +447,6 @@ export class Block
</header>
);

const hasControl = !!getSlotted(el, SLOTS.control);
const hasMenuActions = !!getSlotted(el, SLOTS.headerMenuActions);
const collapseIcon = open ? ICONS.opened : ICONS.closed;

const headerNode = (
Expand Down Expand Up @@ -477,21 +476,18 @@ export class Block
) : (
headerContent
)}
{hasControl ? (
<div aria-labelledby={IDS.header} class={CSS.controlContainer}>
<slot name={SLOTS.control} />
</div>
) : null}
{hasMenuActions ? (
<calcite-action-menu
flipPlacements={menuFlipPlacements ?? ["top", "bottom"]}
label={messages.options}
overlayPositioning={this.overlayPositioning}
placement={menuPlacement}
>
<slot name={SLOTS.headerMenuActions} />
</calcite-action-menu>
) : null}
<div aria-labelledby={IDS.header} class={CSS.controlContainer} hidden={!this.hasControl}>
<slot name={SLOTS.control} onSlotchange={this.controlSlotChangeHandler} />
</div>
<calcite-action-menu
flipPlacements={menuFlipPlacements ?? ["top", "bottom"]}
hidden={!this.hasMenuActions}
label={messages.options}
overlayPositioning={this.overlayPositioning}
placement={menuPlacement}
>
<slot name={SLOTS.headerMenuActions} onSlotchange={this.menuActionsSlotChangeHandler} />
</calcite-action-menu>
{this.renderActionsEnd()}
</div>
);
Expand Down
Loading