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

refactor(editor): Migrate BreakpointsObserver component to Vue 3 syntax (no-changelog) #9758

Merged
merged 2 commits into from
Jun 18, 2024
Merged
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
136 changes: 63 additions & 73 deletions packages/editor-ui/src/components/BreakpointsObserver.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
</span>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
<script setup lang="ts">
import { ref, computed, onMounted, onBeforeUnmount, nextTick } from 'vue';
import { BREAKPOINT_SM, BREAKPOINT_MD, BREAKPOINT_LG, BREAKPOINT_XL } from '@/constants';
import { useUIStore } from '@/stores/ui.store';
import { getBannerRowHeight } from '@/utils/htmlUtils';
import { useDebounce } from '@/composables/useDebounce';

/**
* matching element.io https://element.eleme.io/#/en-US/component/layout#col-attributes
Expand All @@ -16,85 +19,72 @@ import { BREAKPOINT_SM, BREAKPOINT_MD, BREAKPOINT_LG, BREAKPOINT_XL } from '@/co
* lg >= 1200
* xl >= 1920
*/
interface Props {
valueXS?: number;
valueXL?: number;
valueLG?: number;
valueMD?: number;
valueSM?: number;
valueDefault?: number;
}

import { useUIStore } from '@/stores/ui.store';
import { getBannerRowHeight } from '@/utils/htmlUtils';
import { useDebounce } from '@/composables/useDebounce';

export default defineComponent({
name: 'BreakpointsObserver',
props: ['valueXS', 'valueXL', 'valueLG', 'valueMD', 'valueSM', 'valueDefault'],
setup() {
const { callDebounced } = useDebounce();
return { callDebounced };
},
data() {
return {
width: window.innerWidth,
};
},
computed: {
bp(): string {
if (this.width < BREAKPOINT_SM) {
return 'XS';
}
const props = defineProps<Props>();

if (this.width >= BREAKPOINT_XL) {
return 'XL';
}
const { callDebounced } = useDebounce();
const width = ref(window.innerWidth);

if (this.width >= BREAKPOINT_LG) {
return 'LG';
}

if (this.width >= BREAKPOINT_MD) {
return 'MD';
}

return 'SM';
},

value(): number | undefined {
if (this.valueXS !== undefined && this.width < BREAKPOINT_SM) {
return this.valueXS;
}
const bp = computed(() => {
if (width.value < BREAKPOINT_SM) {
return 'XS';
}
if (width.value >= BREAKPOINT_XL) {
return 'XL';
}
if (width.value >= BREAKPOINT_LG) {
return 'LG';
}
if (width.value >= BREAKPOINT_MD) {
return 'MD';
}
return 'SM';
});

if (this.valueXL !== undefined && this.width >= BREAKPOINT_XL) {
return this.valueXL;
}
const value = computed(() => {
if (props.valueXS && width.value < BREAKPOINT_SM) {
return props.valueXS;
}
if (props.valueXL && width.value >= BREAKPOINT_XL) {
return props.valueXL;
}
if (props.valueLG && width.value >= BREAKPOINT_LG) {
return props.valueLG;
}
if (props.valueMD && width.value >= BREAKPOINT_MD) {
return props.valueMD;
}
if (props.valueSM) {
return props.valueSM;
}
return props.valueDefault;
});

if (this.valueLG !== undefined && this.width >= BREAKPOINT_LG) {
return this.valueLG;
}
const onResize = () => {
void callDebounced(onResizeEnd, { debounceTime: 50 });
};

if (this.valueMD !== undefined && this.width >= BREAKPOINT_MD) {
return this.valueMD;
}
const onResizeEnd = async () => {
width.value = window.innerWidth;
await nextTick();

if (this.valueSM !== undefined) {
return this.valueSM;
}
const bannerHeight = await getBannerRowHeight();
useUIStore().updateBannersHeight(bannerHeight);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could have the uiStore initialized at root level to no longer make this function call every time we resize?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, updated

};

return this.valueDefault;
},
},
created() {
window.addEventListener('resize', this.onResize);
},
beforeUnmount() {
window.removeEventListener('resize', this.onResize);
},
methods: {
onResize() {
void this.callDebounced(this.onResizeEnd, { debounceTime: 50 });
},
async onResizeEnd() {
this.width = window.innerWidth;
await this.$nextTick();
onMounted(() => {
window.addEventListener('resize', onResize);
});

const bannerHeight = await getBannerRowHeight();
useUIStore().updateBannersHeight(bannerHeight);
},
},
onBeforeUnmount(() => {
window.removeEventListener('resize', onResize);
});
</script>
Loading