Skip to content

Commit

Permalink
perf(lazy-container): optimize lazyContainer code
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Nov 3, 2020
1 parent e79e540 commit 0f4b847
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 97 deletions.
3 changes: 0 additions & 3 deletions .browserslistrc

This file was deleted.

4 changes: 3 additions & 1 deletion CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

### ⚡ Performance Improvements

- 菜单性能继续优化
- 菜单性能继续优化,更流畅
- 优化懒加载组件及示例

### 🎫 Chores

Expand All @@ -13,6 +14,7 @@
### 🐛 Bug Fixes

- 修复升级之后 table 类型问题
- 修复分割菜单且左侧菜单没有数据时候,继续展示上一次子菜单的问题

## 2.0.0-rc.8 (2020-11-2)

Expand Down
79 changes: 20 additions & 59 deletions src/components/Container/src/LazyContainer.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<template>
<transition-group v-bind="$attrs" ref="elRef" :name="transitionName" :tag="tag">
<transition-group
class="lazy-container"
v-bind="$attrs"
ref="elRef"
:name="transitionName"
:tag="tag"
mode="out-in"
>
<div key="component" v-if="isInit">
<slot :loading="loading" />
</div>
<div key="skeleton">
<div key="skeleton" v-else name="lazy-skeleton">
<slot name="skeleton" v-if="$slots.skeleton" />
<Skeleton v-else />
</div>
Expand All @@ -12,19 +19,9 @@
<script lang="ts">
import type { PropType } from 'vue';
import {
defineComponent,
reactive,
onMounted,
ref,
unref,
onUnmounted,
toRef,
toRefs,
} from 'vue';
import { defineComponent, reactive, onMounted, ref, toRef, toRefs } from 'vue';
import { Skeleton } from 'ant-design-vue';
import { useRaf } from '/@/hooks/event/useRaf';
import { useTimeout } from '/@/hooks/core/useTimeout';
import { useIntersectionObserver } from '/@/hooks/event/useIntersectionObserver';
interface State {
Expand All @@ -36,33 +33,32 @@
name: 'LazyContainer',
components: { Skeleton },
props: {
// 等待时间,如果指定了时间,不论可见与否,在指定时间之后自动加载
// Waiting time, if the time is specified, whether visible or not, it will be automatically loaded after the specified time
timeout: {
type: Number as PropType<number>,
default: 0,
// default: 8000,
},
// 组件所在的视口,如果组件是在页面容器内滚动,视口就是该容器
// The viewport where the component is located. If the component is scrolling in the page container, the viewport is the container
viewport: {
type: (typeof window !== 'undefined' ? window.HTMLElement : Object) as PropType<
HTMLElement
>,
default: () => null,
},
// 预加载阈值, css单位
// Preload threshold, css unit
threshold: {
type: String as PropType<string>,
default: '0px',
},
// 视口的滚动方向, vertical代表垂直方向,horizontal代表水平方向
// The scroll direction of the viewport, vertical represents the vertical direction, horizontal represents the horizontal direction
direction: {
type: String as PropType<'vertical' | 'horizontal'>,
default: 'vertical',
},
// 包裹组件的外层容器的标签名
// The label name of the outer container that wraps the component
tag: {
type: String as PropType<string>,
default: 'div',
Expand Down Expand Up @@ -105,23 +101,11 @@
function init() {
state.loading = true;
requestAnimationFrameFn(() => {
state.isInit = true;
emit('init');
});
}
function requestAnimationFrameFn(callback: () => any) {
// Prevent waiting too long without executing the callback
// Set the maximum waiting time
useTimeout(() => {
if (state.isInit) return;
callback();
state.isInit = true;
emit('init');
}, props.maxWaitingTime || 80);
const { requestAnimationFrame } = useRaf();
return requestAnimationFrame;
}
function initIntersectionObserver() {
Expand Down Expand Up @@ -165,31 +149,8 @@
});
</script>
<style lang="less">
.lazy-container-enter {
opacity: 0;
}
.lazy-container-enter-to {
opacity: 1;
}
.lazy-container-enter-from,
.lazy-container-enter-active {
position: absolute;
top: 0;
.lazy-container {
width: 100%;
transition: opacity 0.3s 0.2s;
}
.lazy-container-leave {
opacity: 1;
}
.lazy-container-leave-to {
opacity: 0;
}
.lazy-container-leave-active {
transition: opacity 0.5s;
height: 100%;
}
</style>
12 changes: 6 additions & 6 deletions src/components/Container/src/collapse/CollapseContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
<CollapseTransition :enable="canExpan">
<Skeleton v-if="loading" />
<div class="collapse-container__body" v-else v-show="show">
<!-- <LazyContainer :timeout="lazyTime" v-if="lazy">
<LazyContainer :timeout="lazyTime" v-if="lazy">
<slot />
<template #skeleton>
<slot name="lazySkeleton" />
</template>
</LazyContainer> -->
<slot />
</LazyContainer>
<slot v-else />
</div>
</CollapseTransition>
</div>
Expand All @@ -28,15 +28,15 @@
import CollapseHeader from './CollapseHeader.vue';
import { Skeleton } from 'ant-design-vue';
// import LazyContainer from '../LazyContainer';
import LazyContainer from '../LazyContainer.vue';
import { triggerWindowResize } from '/@/utils/event/triggerWindowResizeEvent';
// hook
import { useTimeout } from '/@/hooks/core/useTimeout';
export default defineComponent({
components: {
Skeleton,
// LazyContainer,
LazyContainer,
CollapseHeader,
CollapseTransition,
},
Expand Down Expand Up @@ -75,7 +75,7 @@
// 延时加载时间
lazyTime: {
type: Number as PropType<number>,
default: 3000,
default: 0,
},
},
setup(props) {
Expand Down
4 changes: 4 additions & 0 deletions src/components/ContextMenu/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export default defineComponent({
const state = reactive({
show: false,
});

onMounted(() => {
nextTick(() => {
state.show = true;
});
});

onUnmounted(() => {
const el = unref(wrapRef);
el && document.body.removeChild(el);
Expand Down Expand Up @@ -61,6 +63,7 @@ export default defineComponent({

handler && handler();
}

function renderContent(item: ContextMenuItem) {
const { icon, label } = item;

Expand All @@ -72,6 +75,7 @@ export default defineComponent({
</span>
);
}

function renderMenuItem(items: ContextMenuItem[]) {
return items.map((item, index) => {
const { disabled, label, children, divider = false } = item;
Expand Down
1 change: 1 addition & 0 deletions src/components/Preview/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export default defineComponent({
</div>
);
};

return () => {
return (
imgState.show && (
Expand Down
50 changes: 30 additions & 20 deletions src/router/menus/modules/demo/comp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ const menu: MenuModule = {
name: '动画组件',
},

{
path: 'modal',
name: '弹窗扩展',
},
{
path: 'drawer',
name: '抽屉扩展',
},
{
path: 'desc',
name: '详情组件',
},
{
path: 'qrcode',
name: '二维码组件',
},
{
path: 'strength-meter',
name: '密码强度组件',
},
{
path: 'scroll',
name: '滚动组件',
Expand All @@ -36,21 +56,19 @@ const menu: MenuModule = {
},
],
},
{
path: 'modal',
name: '弹窗扩展',
},
{
path: 'drawer',
name: '抽屉扩展',
},
{
path: 'desc',
name: '详情组件',
},
{
path: 'lazy',
name: '懒加载组件',
children: [
{
path: 'basic',
name: '基础示例',
},
{
path: 'transition',
name: '动画效果',
},
],
},
{
path: 'verify',
Expand All @@ -66,14 +84,6 @@ const menu: MenuModule = {
},
],
},
{
path: 'qrcode',
name: '二维码组件',
},
{
path: 'strength-meter',
name: '密码强度组件',
},
],
},
};
Expand Down
21 changes: 20 additions & 1 deletion src/router/routes/modules/demo/comp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,32 @@ export default {
title: '详情组件',
},
},

{
path: '/lazy',
name: 'lazyDemo',
component: () => import('/@/views/demo/comp/lazy/index.vue'),
redirect: '/comp/lazy/basic',
meta: {
title: '懒加载组件',
},
children: [
{
path: 'basic',
name: 'BasicLazyDemo',
component: () => import('/@/views/demo/comp/lazy/index.vue'),
meta: {
title: '基础示例',
},
},
{
path: 'transition',
name: 'BasicTransitionDemo',
component: () => import('/@/views/demo/comp/lazy/Transition.vue'),
meta: {
title: '动画效果',
},
},
],
},
{
path: '/verify',
Expand Down
Loading

0 comments on commit 0f4b847

Please sign in to comment.