Skip to content

Commit fa9f1f4

Browse files
committed
feat: layout sider support dynamic breakpoint
1 parent adbc39b commit fa9f1f4

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

components/layout/Sider.tsx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import classNames from '../_util/classNames';
22
import type { PropType, ExtractPropTypes, CSSProperties } from 'vue';
3-
import { inject, defineComponent, ref, watch, onMounted, onBeforeUnmount, provide } from 'vue';
3+
import {
4+
watchEffect,
5+
inject,
6+
defineComponent,
7+
ref,
8+
watch,
9+
onMounted,
10+
onBeforeUnmount,
11+
provide,
12+
} from 'vue';
413
import PropTypes from '../_util/vue-types';
514
import { tuple } from '../_util/type';
615
import initDefaultProps from '../_util/props-util/initDefaultProps';
@@ -114,18 +123,25 @@ export default defineComponent({
114123
siderHook && siderHook.addSider(uniqueId);
115124

116125
onMounted(() => {
117-
if (typeof window !== 'undefined') {
118-
const { matchMedia } = window;
119-
if (matchMedia! && props.breakpoint && props.breakpoint in dimensionMaxMap) {
120-
mql = matchMedia(`(max-width: ${dimensionMaxMap[props.breakpoint]})`);
121-
try {
122-
mql.addEventListener('change', responsiveHandler);
123-
} catch (error) {
124-
mql.addListener(responsiveHandler);
126+
watchEffect(() => {
127+
try {
128+
mql?.removeEventListener('change', responsiveHandler);
129+
} catch (error) {
130+
mql?.removeListener(responsiveHandler);
131+
}
132+
if (typeof window !== 'undefined') {
133+
const { matchMedia } = window;
134+
if (matchMedia! && props.breakpoint && props.breakpoint in dimensionMaxMap) {
135+
mql = matchMedia(`(max-width: ${dimensionMaxMap[props.breakpoint]})`);
136+
try {
137+
mql.addEventListener('change', responsiveHandler);
138+
} catch (error) {
139+
mql.addListener(responsiveHandler);
140+
}
141+
responsiveHandler(mql);
125142
}
126-
responsiveHandler(mql);
127143
}
128-
}
144+
});
129145
});
130146
onBeforeUnmount(() => {
131147
try {

components/layout/demo/fixed-sider.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ When dealing with long content, a fixed sider can provide a better user experien
1818
</docs>
1919
<template>
2020
<a-layout has-sider>
21-
<a-layout-sider :style="{ overflow: 'auto', height: '100vh', position: 'fixed', left: 0 }">
21+
<a-layout-sider
22+
:style="{ overflow: 'auto', height: '100vh', position: 'fixed', left: 0, top: 0, bottom: 0 }"
23+
>
2224
<div class="logo" />
2325
<a-menu v-model:selectedKeys="selectedKeys" theme="dark" mode="inline">
2426
<a-menu-item key="1">

0 commit comments

Comments
 (0)