Skip to content

Commit 3814fc8

Browse files
committed
revert: useBreakpoint
1 parent 67939d3 commit 3814fc8

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

components/_util/eagerComputed.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { watchEffect, shallowRef } from 'vue';
2+
import type { ComputedRef } from 'vue';
3+
export declare type ComputedGetter<T> = (...args: any[]) => T;
4+
export default function eagerComputed<T>(fn: ComputedGetter<T>) {
5+
const result = shallowRef<T>();
6+
watchEffect(
7+
() => {
8+
result.value = fn();
9+
},
10+
{
11+
flush: 'sync', // needed so updates are immediate.
12+
},
13+
);
14+
15+
return result as any as ComputedRef<T>;
16+
}

components/_util/hooks/useBreakpoint.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import type { Ref } from 'vue';
2-
import { getCurrentInstance, onMounted, onUnmounted, ref } from 'vue';
2+
import { onMounted, onUnmounted, ref } from 'vue';
33
import type { ScreenMap } from '../../_util/responsiveObserve';
44
import ResponsiveObserve from '../../_util/responsiveObserve';
55

6-
function useBreakpoint(refreshOnChange = ref(true)): Ref<ScreenMap> {
6+
function useBreakpoint(): Ref<ScreenMap> {
77
const screens = ref<ScreenMap>({});
88
let token = null;
9-
const instance = getCurrentInstance();
109
onMounted(() => {
1110
token = ResponsiveObserve.subscribe(supportScreens => {
1211
screens.value = supportScreens;
13-
if (refreshOnChange?.value) {
14-
instance.update();
15-
}
1612
});
1713
});
1814

components/avatar/Avatar.tsx

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { responsiveArray } from '../_util/responsiveObserve';
1010
import useConfigInject from '../_util/hooks/useConfigInject';
1111
import ResizeObserver from '../vc-resize-observer';
1212
import { useInjectSize } from '../_util/hooks/useSize';
13+
import eagerComputed from '../_util/eagerComputed';
1314

1415
export type AvatarSize = 'large' | 'small' | 'default' | number | ScreenSizeMap;
1516

@@ -54,13 +55,8 @@ const Avatar = defineComponent({
5455
const size = computed(() => {
5556
return props.size === 'default' ? groupSize.value : props.size;
5657
});
57-
const needResponsive = computed(() =>
58-
Object.keys(typeof size.value === 'object' ? size.value || {} : {}).some(key =>
59-
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].includes(key),
60-
),
61-
);
62-
const screens = useBreakpoint(needResponsive);
63-
const responsiveSize = computed(() => {
58+
const screens = useBreakpoint();
59+
const responsiveSize = eagerComputed(() => {
6460
if (typeof props.size !== 'object') {
6561
return undefined;
6662
}

components/grid/index.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import Row from './Row';
22
import Col from './Col';
3-
import useInternalBreakpoint from '../_util/hooks/useBreakpoint';
3+
import useBreakpoint from '../_util/hooks/useBreakpoint';
44

5-
// Do not export params
6-
function useBreakpoint() {
7-
return useInternalBreakpoint();
8-
}
95
export type { RowProps } from './Row';
106

117
export type { ColProps, ColSize } from './Col';

components/list/index.tsx

+4-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import useConfigInject from '../_util/hooks/useConfigInject';
1717
import useBreakpoint from '../_util/hooks/useBreakpoint';
1818
import type { Breakpoint } from '../_util/responsiveObserve';
1919
import { responsiveArray } from '../_util/responsiveObserve';
20+
import eagerComputed from '../_util/eagerComputed';
2021

2122
export { ListItemProps } from './Item';
2223
export type { ListItemMetaProps } from './ItemMeta';
@@ -43,7 +44,7 @@ export const listProps = () => ({
4344
bordered: PropTypes.looseBool,
4445
dataSource: PropTypes.array,
4546
extra: PropTypes.any,
46-
grid: { type: Object as PropType<ListGridType>, default: undefined },
47+
grid: { type: Object as PropType<ListGridType>, default: undefined as ListGridType },
4748
itemLayout: String as PropType<ListItemLayout>,
4849
loading: {
4950
type: [Boolean, Object] as PropType<boolean | (SpinProps & HTMLAttributes)>,
@@ -200,14 +201,9 @@ const List = defineComponent({
200201
return dd;
201202
});
202203

203-
const needResponsive = computed(() =>
204-
Object.keys(props.grid || {}).some(key =>
205-
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].includes(key),
206-
),
207-
);
208-
const screens = useBreakpoint(needResponsive);
204+
const screens = useBreakpoint();
209205

210-
const currentBreakpoint = computed(() => {
206+
const currentBreakpoint = eagerComputed(() => {
211207
for (let i = 0; i < responsiveArray.length; i += 1) {
212208
const breakpoint: Breakpoint = responsiveArray[i];
213209
if (screens.value[breakpoint]) {

0 commit comments

Comments
 (0)