Skip to content

Commit 65d7f4b

Browse files
authored
Merge pull request #379 from GetStream/attachment-sizing-warning
feat: Warn if invalid attachment sizing properties are detected
2 parents 3b22272 + 3937422 commit 65d7f4b

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed
59.2 KB
Loading

docusaurus/docs/Angular/components/AttachmentListComponent.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import AttachmentsScreenshot from "../assets/attachments-screenshot.png";
22
import ImageSizingScreenshot1 from "../assets/image-sizing-screenshot-1.png";
33
import ImageSizingScreenshot2 from "../assets/image-sizing-screenshot-2.png";
44
import ImageSizingScreenshot3 from "../assets/image-sizing-screenshot-3.png";
5+
import AttachmentSizeWarning from "../assets/attachment-size-warning.png";
56

67
The `AttachmentList` component displays the attachments of a message. The following attachments are supported:
78

@@ -51,9 +52,9 @@ The following section details how the width and height of images and videos uplo
5152

5253
You can control the maximum size of images and videos with the [`--str-chat__attachment-max-width`](../theming/component-variables.mdx) CSS variable (available only in [theme-v2](../theming/introduction.mdx)). The value of this variable must be a value that can be computed to a valid pixel value using the [`getComputedStyle`](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle) method (for example: `300px`, `10rem`, `calc(300px - var(--margin))`, but not `100%`). If you provide an invalid value, the image and video sizing can break, which can lead to scrolling issues inside the message list (for example the message list isn't scrolled to the bottom when you open a channel).
5354

54-
:::tip
55-
If you're unsure about the value you provided for `--str-chat__attachment-max-width`, send an image/video to a channel, then inspect the `img`/`video` element inside the message list using your browser's development tools. Inside the **computed** properties of the element, you should see that `max-height` and `max-width` are set to a valid pixel value.
56-
:::
55+
If you set an invalid value to the variable, you'll see a warning on the browser's console:
56+
57+
<img src={AttachmentSizeWarning} width="800" />
5758

5859
#### File size optimization
5960

projects/stream-chat-angular/src/lib/attachment-configuration.service.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ export class AttachmentConfigurationService<
7272
attachment.image_url ||
7373
'') as string
7474
);
75+
const displayWarning = location === 'gallery' || location === 'single';
7576
const { sizeRestriction, height } = this.getSizingRestrictions(
7677
url,
77-
element
78+
element,
79+
displayWarning
7880
);
7981

8082
if (sizeRestriction) {
@@ -111,9 +113,11 @@ export class AttachmentConfigurationService<
111113
let thumbUrl = undefined;
112114
if (attachment.thumb_url && this.shouldGenerateVideoThumbnail) {
113115
const url = new URL(attachment.thumb_url);
116+
const displayWarning = true;
114117
const { sizeRestriction, height } = this.getSizingRestrictions(
115118
url,
116-
element
119+
element,
120+
displayWarning
117121
);
118122

119123
if (sizeRestriction) {
@@ -183,7 +187,11 @@ export class AttachmentConfigurationService<
183187
url.searchParams.set('w', sizeRestriction.width.toString());
184188
}
185189

186-
private getSizingRestrictions(url: URL, htmlElement: HTMLElement) {
190+
private getSizingRestrictions(
191+
url: URL,
192+
htmlElement: HTMLElement,
193+
displayWarning = false
194+
) {
187195
const urlParams = url.searchParams;
188196
const originalHeight = Number(urlParams.get('oh')) || 1;
189197
const originalWidth = Number(urlParams.get('ow')) || 1;
@@ -219,6 +227,11 @@ export class AttachmentConfigurationService<
219227
}
220228
} else {
221229
sizeRestriction = undefined;
230+
if (displayWarning) {
231+
console.warn(
232+
`Invalid value set for height/max-height and/or max-width for HTML element, this can cause scrolling issues inside the message list, more info https://getstream.io/chat/docs/sdk/angular/components/AttachmentListComponent/#image-and-video-sizing`
233+
);
234+
}
222235
}
223236

224237
return { sizeRestriction, height };

0 commit comments

Comments
 (0)