Skip to content

Commit f77faed

Browse files
authored
[Chat] Fixed the issue where image placeholder doesn't work for Safari Browser (#4846)
1 parent c1dd9bd commit f77faed

File tree

16 files changed

+42
-9
lines changed

16 files changed

+42
-9
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "patch",
3+
"area": "fix",
4+
"workstream": "Inline Image",
5+
"comment": "Fixed the issue where image loading placeholder doesn't work for safari",
6+
"packageName": "@azure/communication-react",
7+
"email": "109105353+jpeng-ms@users.noreply.github.com",
8+
"dependentChangeType": "patch"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "patch",
3+
"area": "fix",
4+
"workstream": "Inline Image",
5+
"comment": "Fixed the issue where image loading placeholder doesn't work for safari",
6+
"packageName": "@azure/communication-react",
7+
"email": "109105353+jpeng-ms@users.noreply.github.com",
8+
"dependentChangeType": "patch"
9+
}

packages/chat-component-bindings/src/messageThreadSelector.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ const processChatMessageContent = (message: ChatMessageWithStatus): string | und
145145
// else in loading or success state
146146
img.setAttribute('src', src);
147147
}
148+
setImageWidthAndHeight(img);
148149
}
149150
});
150151
content = document.body.innerHTML;
@@ -208,6 +209,16 @@ const extractAttachmentContentTypeFromName = (name?: string): string => {
208209
return contentType;
209210
};
210211

212+
const setImageWidthAndHeight = (img?: HTMLImageElement): void => {
213+
if (img) {
214+
// define aspect ratio explicitly to prevent image not being displayed correctly
215+
// in safari, this includes image placeholder for loading state
216+
const width = img.width;
217+
const height = img.height;
218+
img.style.aspectRatio = `${width}/${height}`;
219+
}
220+
};
221+
211222
/* @conditional-compile-remove(file-sharing-teams-interop) @conditional-compile-remove(file-sharing-acs) */
212223
const extractAttachmentsMetadata = (message: ChatMessageWithStatus): { attachments?: AttachmentMetadata[] } => {
213224
let attachments: AttachmentMetadata[] = [];

packages/react-components/src/components/styles/MessageThread.styles.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,13 @@ export const useChatMessageCommonStyles = makeStyles({
357357
},
358358
bodyWithPlaceholderImage: {
359359
'& img[src=""]': {
360-
display: 'block',
360+
backgroundColor: tokens.colorNeutralBackground1Selected,
361+
// this ensures safari won't have default rendering when image source
362+
// becomes invalid such as empty string in this case
363+
fontSize: '0',
361364
position: 'relative',
362-
marginBottom: '5px'
365+
clipPath: 'inset(0.3px)',
366+
display: 'flex'
363367
},
364368
'& img[src=""]:after': {
365369
backgroundColor: tokens.colorNeutralBackground1Selected,

packages/react-components/tests/browser/ImageOverlay.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ betaTest.describe('ImageOverlay tests', () => {
1313
messageType: 'chat',
1414
senderId: 'user3',
1515
content:
16-
'<p>How should I design my new house?</p><p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="166.5625" height="250" id="SomeImageId1" style="vertical-align:bottom"></p><p><img alt="image" src="images/inlineImageExample2.png" itemscope="png" width="374.53183520599254" height="250" id="SomeImageId2" style="vertical-align:bottom"></p><p>&nbsp;</p>',
16+
'<p>How should I design my new house?</p><p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="166" height="250" id="SomeImageId1" style="vertical-align:bottom; aspect-ratio: 166 / 250;"></p><p><img alt="image" src="images/inlineImageExample2.png" itemscope="png" width="374" height="250" id="SomeImageId2" style="vertical-align:bottom; aspect-ratio: 374 / 250"></p><p>&nbsp;</p>',
1717
senderDisplayName: 'Miguel Garcia',
1818
messageId: Math.random().toString(),
1919
createdOn: new Date('2019-04-13T00:00:00.000+08:09'),

packages/react-components/tests/browser/MessageThread.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ betaTest.describe('MessageThread inline image tests', () => {
2626
{
2727
messageType: 'chat',
2828
senderId: 'user3',
29-
content: `<p>How should I design my new house?</p><p><img alt="image" src="${imgSrc}" itemscope="png" width="374.53183520599254" height="250" id="SomeImageId2" style="vertical-align:bottom"></p><p>&nbsp;</p>`,
29+
content: `<p>How should I design my new house?</p><p><img alt="image" src="${imgSrc}" itemscope="png" width="374" height="250" id="SomeImageId2" style="vertical-align:bottom; aspect-ratio: 374 / 250"></p><p>&nbsp;</p>`,
3030
senderDisplayName: 'Miguel Garcia',
3131
messageId: Math.random().toString(),
3232
createdOn: new Date('2019-04-13T00:00:00.000+08:09'),

packages/react-composites/tests/app/chat/FakeAdapterApp.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const sendRemoteInlineImageMessage = (
145145
sequenceId: `${thread.messages.length}`,
146146
version: '0',
147147
content: {
148-
message: `<p>Test</p><p><img alt="image" src="${imgSrcPreview}" itemscope="png" width="200" height="300" id="SomeImageId1" style="vertical-align:bottom"></p><p>&nbsp;</p>`,
148+
message: `<p>Test</p><p><img alt="image" src="${imgSrcPreview}" itemscope="png" width="200" height="300" id="SomeImageId1" style="vertical-align:bottom; aspect-ratio: 200 / 300;"></p><p>&nbsp;</p>`,
149149
attachments: [
150150
{
151151
id: 'SomeImageId1',

packages/storybook/stories/MessageThread/placeholdermessages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const GenerateMockNewChatMessageWithInlineImage = (): ChatMessage => {
7676
...UserThree,
7777
messageId: Math.random().toString(),
7878
content:
79-
'<p>Check out this image:&nbsp;</p>\r\n<p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="250" height="375" id="SomeImageId" style="vertical-align:bottom"></p><p>&nbsp;</p>\r\n',
79+
'<p>Check out this image:&nbsp;</p>\r\n<p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="250" height="375" id="SomeImageId" style="vertical-align:bottom; aspect-ratio: 250 / 375;"></p><p>&nbsp;</p>\r\n',
8080
createdOn: new Date('2020-04-13T00:00:00.000+07:01'),
8181
mine: false,
8282
attached: false,

packages/storybook/stories/MessageThread/snippets/WithInlineImageMessage.snippet.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const MessageThreadWithInlineImageExample: () => JSX.Element = () => {
7373
messageType: 'chat',
7474
senderId: 'user3',
7575
content:
76-
'<p>How should I design my new house?</p><p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="166.5625" height="250" id="SomeImageId1" style="vertical-align:bottom"></p><p><img alt="image" src="images/inlineImageExample2.png" itemscope="png" width="374.53183520599254" height="250" id="SomeImageId2" style="vertical-align:bottom"></p><p>&nbsp;</p>',
76+
'<p>How should I design my new house?</p><p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="166" height="250" id="SomeImageId1" style="vertical-align:bottom; aspect-ratio: 166 / 250"></p><p><img alt="image" src="images/inlineImageExample2.png" itemscope="png" width="374" height="250" id="SomeImageId2" style="vertical-align:bottom; aspect-ratio: 374 / 250"></p><p>&nbsp;</p>',
7777
senderDisplayName: 'Miguel Garcia',
7878
messageId: Math.random().toString(),
7979
createdOn: new Date('2019-04-13T00:00:00.000+08:09'),

packages/storybook8/stories/Components/MessageThread/placeholdermessages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const GenerateMockNewChatMessageWithInlineImage = (): ChatMessage => {
7676
...UserThree,
7777
messageId: Math.random().toString(),
7878
content:
79-
'<p>Check out this image:&nbsp;</p>\r\n<p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="250" height="375" id="SomeImageId" style="vertical-align:bottom"></p><p>&nbsp;</p>\r\n',
79+
'<p>Check out this image:&nbsp;</p>\r\n<p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="250" height="375" id="SomeImageId" style="vertical-align:bottom; aspect-ratio: 250 / 375;"></p><p>&nbsp;</p>\r\n',
8080
createdOn: new Date('2020-04-13T00:00:00.000+07:01'),
8181
mine: false,
8282
attached: false,

packages/storybook8/stories/Components/MessageThread/snippets/WithInlineImageMessage.snippet.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const MessageThreadWithInlineImageExample: () => JSX.Element = () => {
6969
messageType: 'chat',
7070
senderId: 'user3',
7171
content:
72-
'<p>How should I design my new house?</p><p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="166.5625" height="250" id="SomeImageId1" style="vertical-align:bottom"></p><p><img alt="image" src="images/inlineImageExample2.png" itemscope="png" width="374.53183520599254" height="250" id="SomeImageId2" style="vertical-align:bottom"></p><p>&nbsp;</p>',
72+
'<p>How should I design my new house?</p><p><img alt="image" src="images/inlineImageExample1.png" itemscope="png" width="166" height="250" id="SomeImageId1" style="vertical-align:bottom; aspect-ratio: 166 / 250"></p><p><img alt="image" src="images/inlineImageExample2.png" itemscope="png" width="374" height="250" id="SomeImageId2" style="vertical-align:bottom; aspect-ratio: 374 / 250"></p><p>&nbsp;</p>',
7373
senderDisplayName: 'Miguel Garcia',
7474
messageId: Math.random().toString(),
7575
createdOn: new Date('2019-04-13T00:00:00.000+08:09'),

0 commit comments

Comments
 (0)