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

perf(image): disable transition for safari + unwatch observer after it triggers #573

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ module.exports = {
},
},

globals: {
window: true,
},

rules: {
indent: 'off',
'indent-legacy': ['error', 'tab'],
Expand Down
20 changes: 17 additions & 3 deletions src/components/Image/src/Image.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
$s[`shape_${resolvedShape}`],
]"
/>
<m-transition-fade-in @after-enter="afterEnter">
<component
:is="transitionComponent"
@after-enter="afterEnter"
>
<img
v-show="loaded"
:class="{
Expand All @@ -26,7 +29,7 @@
@load="onLoaded"
v-on="$listeners"
>
</m-transition-fade-in>
</component>
<pseudo-window
@resize="throttledResizeHandler"
/>
Expand All @@ -40,6 +43,7 @@ import { MTransitionFadeIn } from '@square/maker/components/TransitionFadeIn';
import { MSkeletonBlock } from '@square/maker/components/Skeleton';
import { MThemeKey, defaultTheme, resolveThemeableProps } from '@square/maker/components/Theme';
import cssValidator from '@square/maker/utils/css-validator';
import { isSafari } from '../../../utils/devices';

const hexagon = 'polygon(50% 0, 93.3012701892219% 25%, 93.3012701892219% 75%, 50% 100%, 6.69872981077807% 75%, 6.69872981077807% 25%)';

Expand Down Expand Up @@ -181,6 +185,14 @@ export default {
shouldGetImageDimensions() {
return this.shape !== 'square';
},

transitionComponent() {
if (!isSafari()) {
return 'm-transition-fade-in';
}
// Don't want transition on Safari for perf reasons
return 'span';
kevinlee11 marked this conversation as resolved.
Show resolved Hide resolved
},
},

watch: {
Expand All @@ -199,7 +211,8 @@ export default {
mounted() {
// Emit image:visible right away if Image is cached,
// since it will just render instead of transitioning in
if (this.loaded) {
// Safari doesn't have a transition, so we'll just trigger it here
if (this.loaded || isSafari()) {
this.$emit('image:visible');
}

Expand Down Expand Up @@ -244,6 +257,7 @@ export default {
methods: {
load() {
this.shouldLoad = true;
observer?.unwatch(this.$el);
},

getImageDimensions() {
Expand Down
17 changes: 17 additions & 0 deletions src/utils/devices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Defines "browser" as having the `window` global
* Checks if the browser supports them
* @return {boolean}
*/
function isBrowser() {
return (typeof window !== 'undefined');
}

/**
* Is it apple safari browser
* @return {boolean}
*/
export function isSafari() {
return isBrowser() && window.navigator.userAgent.includes('Safari')
kevinlee11 marked this conversation as resolved.
Show resolved Hide resolved
&& !window.navigator.userAgent.includes('Chrome');
}
Loading