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

Support styling with :part() #762

Merged
merged 3 commits into from
Nov 17, 2024
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
5 changes: 5 additions & 0 deletions .changeset/lucky-llamas-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@responsive-image/wc': minor
---

Support styling with :part(img)
10 changes: 10 additions & 0 deletions apps/docs/src/frameworks/wc.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ import '@responsive-image/wc';
> [!Important]
> As the `src` input is an `ImageData` object, you cannot pass this as an HTML _attribute_. Web components only support string based attributes. So make sure to set it as a _property_ using JavaScript. In case of [Lit](https://lit.dev/), you would use the dot prefix notation for [property expressions](https://lit.dev/docs/templates/expressions/#property-expressions): ``html`<responsive-image .src=${image} ></responsive-image>\` ``.

### Styling

To style the `<img>` element which is within the web component's Shadow DOM, you can use the [::part(img) selector](https://developer.mozilla.org/en-US/docs/Web/CSS/::part):

```css
responsive-image::part(img) {
box-shadow: 3px 3px 5px gray;
}
```

## Local images

To process [local images](../usage/local-images.md) you will need to setup one of the [build plugins](../build/index.md) depending on your app's setup.
Expand Down
1 change: 1 addition & 0 deletions packages/wc/src/responsive-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export class ResponsiveImage extends LitElement {
/>`,
)}
<img
part="img"
width=${ifDefined(this.imgWidth)}
height=${ifDefined(this.imgHeight)}
class=${classMap(classes)}
Expand Down
11 changes: 11 additions & 0 deletions packages/wc/test/responsive-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ describe('ResponsiveImage', () => {
'some',
);
});

test('it exposes img with part', async () => {
const el = await fixture<ResponsiveImage>(
html`<responsive-image .src=${defaultImageData} />`,
);

expect(el.shadowRoot?.querySelector('img')).toHaveAttribute(
'part',
'img',
);
});
});

describe('responsive layout', () => {
Expand Down