Skip to content

Commit

Permalink
fix(tip): fix rendering tied to named-slot content (#10470)
Browse files Browse the repository at this point in the history
**Related Issue:** #6059

## Summary

- remove use of `getSlotted` utility
- replace with `slotchange` event and `@State` variables to update the
display of elements.
- existing tests should suffice
  • Loading branch information
driskull authored Oct 1, 2024
1 parent 06105ac commit d95825c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const CSS = {
imageFrame: "image-frame",
content: "content",
info: "info",
infoNoThumbnail: "info--no-thumbnail",
};

export const ICONS = {
Expand Down
7 changes: 4 additions & 3 deletions packages/calcite-components/src/components/tip/tip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@
.info {
@apply py-0 px-4;
inline-size: 70%;
&:only-child {
@apply w-full px-0;
}
}

.info--no-thumbnail {
@apply w-full px-0;
}

::slotted(p) {
Expand Down
29 changes: 13 additions & 16 deletions packages/calcite-components/src/components/tip/tip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import {
VNode,
Watch,
} from "@stencil/core";
import {
ConditionalSlotComponent,
connectConditionalSlotComponent,
disconnectConditionalSlotComponent,
} from "../../utils/conditionalSlot";
import { getSlotted } from "../../utils/dom";
import { connectLocalized, disconnectLocalized, LocalizedComponent } from "../../utils/locale";
import {
connectMessages,
Expand All @@ -26,6 +20,7 @@ import {
} from "../../utils/t9n";
import { constrainHeadingLevel, Heading, HeadingLevel } from "../functional/Heading";
import { logger } from "../../utils/logger";
import { slotChangeHasAssignedElement } from "../../utils/dom";
import { TipMessages } from "./assets/tip/t9n";
import { CSS, ICONS, SLOTS } from "./resources";

Expand All @@ -46,7 +41,7 @@ logger.deprecated("component", {
shadow: true,
assetsDirs: ["assets"],
})
export class Tip implements ConditionalSlotComponent, LocalizedComponent, T9nComponent {
export class Tip implements LocalizedComponent, T9nComponent {
// --------------------------------------------------------------------------
//
// Properties
Expand Down Expand Up @@ -108,6 +103,8 @@ export class Tip implements ConditionalSlotComponent, LocalizedComponent, T9nCom

@State() defaultMessages: TipMessages;

@State() hasThumbnail = false;

@State() effectiveLocale = "";

@Watch("effectiveLocale")
Expand All @@ -122,7 +119,6 @@ export class Tip implements ConditionalSlotComponent, LocalizedComponent, T9nCom
// --------------------------------------------------------------------------

connectedCallback(): void {
connectConditionalSlotComponent(this);
connectLocalized(this);
connectMessages(this);
}
Expand All @@ -132,7 +128,6 @@ export class Tip implements ConditionalSlotComponent, LocalizedComponent, T9nCom
}

disconnectedCallback(): void {
disconnectConditionalSlotComponent(this);
disconnectLocalized(this);
disconnectMessages(this);
}
Expand Down Expand Up @@ -160,6 +155,10 @@ export class Tip implements ConditionalSlotComponent, LocalizedComponent, T9nCom
this.calciteTipDismiss.emit();
};

private handleThumbnailSlotChange = (event: Event): void => {
this.hasThumbnail = slotChangeHasAssignedElement(event);
};

// --------------------------------------------------------------------------
//
// Render Methods
Expand Down Expand Up @@ -195,18 +194,16 @@ export class Tip implements ConditionalSlotComponent, LocalizedComponent, T9nCom
}

renderImageFrame(): VNode {
const { el } = this;

return getSlotted(el, SLOTS.thumbnail) ? (
<div class={CSS.imageFrame} key="thumbnail">
<slot name={SLOTS.thumbnail} />
return (
<div class={CSS.imageFrame} hidden={!this.hasThumbnail} key="thumbnail">
<slot name={SLOTS.thumbnail} onSlotchange={this.handleThumbnailSlotChange} />
</div>
) : null;
);
}

renderInfoNode(): VNode {
return (
<div class={CSS.info}>
<div class={{ [CSS.info]: true, [CSS.infoNoThumbnail]: !this.hasThumbnail }}>
<slot />
</div>
);
Expand Down
10 changes: 5 additions & 5 deletions packages/calcite-components/src/demos/tip-manager.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<img
slot="thumbnail"
width="1000"
height="600"
height="200"
src="./_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
Expand Down Expand Up @@ -89,7 +89,7 @@
<img
slot="thumbnail"
width="1000"
height="600"
height="200"
src="./_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
Expand Down Expand Up @@ -125,7 +125,7 @@
<img
slot="thumbnail"
width="1000"
height="600"
height="200"
src="./_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
Expand All @@ -145,7 +145,7 @@
<img
slot="thumbnail"
width="1000"
height="600"
height="200"
src="./_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
Expand All @@ -160,7 +160,7 @@
<img
slot="thumbnail"
width="1000"
height="600"
height="200"
src="./_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
Expand Down
28 changes: 14 additions & 14 deletions packages/calcite-components/src/demos/tip/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ <h2>With thumbnail</h2>
<img
slot="thumbnail"
width="1000"
height="600"
src="./_assets/images/placeholder.svg"
height="200"
src="../_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
<p>
Expand Down Expand Up @@ -51,8 +51,8 @@ <h2>Without heading</h2>
<img
slot="thumbnail"
width="1000"
height="600"
src="./_assets/images/placeholder.svg"
height="200"
src="../_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
<p>This tip is how a tip should appear without a dismissible button as well as without a heading.</p>
Expand All @@ -67,8 +67,8 @@ <h2>Without heading, close-disabled</h2>
<img
slot="thumbnail"
width="1000"
height="600"
src="./_assets/images/placeholder.svg"
height="200"
src="../_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
<p>This tip is how a tip should appear without a dismissible button as well as without a heading.</p>
Expand Down Expand Up @@ -103,8 +103,8 @@ <h2>Tip Group with thumbnails</h2>
<img
slot="thumbnail"
width="1000"
height="600"
src="./_assets/images/placeholder.svg"
height="200"
src="../_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
<p>
Expand All @@ -118,8 +118,8 @@ <h2>Tip Group with thumbnails</h2>
<img
slot="thumbnail"
width="1000"
height="600"
src="./_assets/images/placeholder.svg"
height="200"
src="../_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
<p>
Expand All @@ -137,8 +137,8 @@ <h2>Tip Group with thumbnails, close-disabled</h2>
<img
slot="thumbnail"
width="1000"
height="600"
src="./_assets/images/placeholder.svg"
height="200"
src="../_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
<p>
Expand All @@ -152,8 +152,8 @@ <h2>Tip Group with thumbnails, close-disabled</h2>
<img
slot="thumbnail"
width="1000"
height="600"
src="./_assets/images/placeholder.svg"
height="200"
src="../_assets/images/placeholder.svg"
alt="This is an image of nature."
/>
<p>
Expand Down

0 comments on commit d95825c

Please sign in to comment.