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

feat(pie-card): DSW-2426 image opacity when disabled #1946

Merged
merged 15 commits into from
Sep 26, 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
5 changes: 5 additions & 0 deletions .changeset/mean-planets-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@justeattakeaway/pie-card": minor
---

[Added] Ability to set images to 50% opacity when disabled
kevinrodrigues marked this conversation as resolved.
Show resolved Hide resolved
58 changes: 57 additions & 1 deletion packages/components/pie-card/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,62 @@ export class PieCard extends LitElement implements CardProps {
return `padding: ${paddingCSS}`;
}

/**
* Handles the slot change event and applies/removes opacity to images based on the `disabled` state.
*
* @private
*/
private handleSlotChange () {
this.updateImagesOpacity();
}

/**
* Updates opacity of all images (slotted and non-slotted) based on the `disabled` property.
*/
private updateImagesOpacity () {
// Handle slotted images
const slot = this.shadowRoot?.querySelector('slot');

if (slot) {
const slottedElements = slot.assignedElements({ flatten: true });
kevinrodrigues marked this conversation as resolved.
Show resolved Hide resolved

slottedElements.forEach((element) => {
const images = element.querySelectorAll('img');
this.applyOpacityToImages(images);
});
}

// Handle non-slotted direct content images
kevinrodrigues marked this conversation as resolved.
Show resolved Hide resolved
const directImages = this.querySelectorAll('img');
this.applyOpacityToImages(directImages);
}

/**
* Applies or removes opacity from the given images based on the `disabled` property.
*
* @param {NodeListOf<HTMLImageElement>} images - The images to apply opacity to.
*/
private applyOpacityToImages (images: NodeListOf<HTMLImageElement>) {
images.forEach((img) => {
if (this.disabled) {
img.style.opacity = '50%';
} else {
img.style.opacity = '';
}
});
}

/**
* Observes changes in the `disabled` property and triggers the update of images' opacity.
*
* @param changedProperties
*/
updated (changedProperties: Map<string, unknown>) {
kevinrodrigues marked this conversation as resolved.
Show resolved Hide resolved
if (changedProperties.has('disabled')) {
this.updateImagesOpacity(); // Re-apply styles when disabled state changes
}
}

render () {
const {
variant,
Expand Down Expand Up @@ -146,7 +202,7 @@ export class PieCard extends LitElement implements CardProps {
aria-label=${aria?.label || nothing}
aria-disabled=${disabled ? 'true' : 'false'}
style=${paddingCSS || ''}>
<slot></slot>
<slot @slotchange=${this.handleSlotChange}></slot>
</div>
</div>`;
}
Expand Down
27 changes: 27 additions & 0 deletions packages/components/pie-card/test/component/pie-card.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,31 @@ test.describe('PieCard - Component tests', () => {
});
});
});

test.describe('Prop: disabled', () => {
test.describe('when an image exists and the prop `disabled` is set to `true`', () => {
kevinrodrigues marked this conversation as resolved.
Show resolved Hide resolved
test('should set the opacity to 50%', async ({ mount, page }) => {
kevinrodrigues marked this conversation as resolved.
Show resolved Hide resolved
// Arrange
const slottedImageContent = `<div data-test-id="slot-content">
<img alt="Sample Image" src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBA==" />
</div>`;

await mount(PieCard, {
props: {
disabled: true,
} as CardProps,
slots: {
default: slottedImageContent,
},
});

// Act
const component = page.locator('[data-test-id="slot-content"]');
const image = component.locator('img');

// Assert the image has the correct opacity
await expect(image).toHaveCSS('opacity', '0.5');
});
});
});
});
Loading