ant-design/components/_util/responsiveObserve.ts
陈帅 3e32364dc0
New component Descriptions (#14645)
* add new component: DescriptionList

* add warning message

* docs: fix doc typo

* feat: implement the size attribute

* docs: fix doc typo

* refactor: use new name Descriptions

* test: snapshots updated

* feat: support react15

* style: fix code style warring

* style: better var name

* style: better code style

* style: merge css class

* feat: add responsive config

* fix: fix error title

* style: use @border-radius-base

* update snapshot

* feat: set default column

* test: add test script

* style: fix property defaultProps is useless error

* style: more robust code

* style: fix codereview warning

* style: fix review warning

* use responsiveObserveserve

* fix review warning

* bug: add childrenArray copy,prevent changes to incoming parameters

* fix dom error

* fix typo

* fix test

* don't use this

* snapshot updated

* prettier md

* remove descriptions md text

* new rendering method

* doc :add dot

* style: add right border
2019-05-22 23:22:09 +08:00

102 lines
2.4 KiB
TypeScript

// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
let enquire: any;
if (typeof window !== 'undefined') {
const matchMediaPolyfill = (mediaQuery: string) => {
return {
media: mediaQuery,
matches: false,
addListener() {},
removeListener() {},
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
enquire = require('enquire.js');
}
export type Breakpoint = 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs';
export type BreakpointMap = Partial<Record<Breakpoint, string>>;
export const responsiveArray: Breakpoint[] = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
export const responsiveMap: BreakpointMap = {
xs: '(max-width: 575px)',
sm: '(min-width: 576px)',
md: '(min-width: 768px)',
lg: '(min-width: 992px)',
xl: '(min-width: 1200px)',
xxl: '(min-width: 1600px)',
};
type SubscribeFunc = (screens: BreakpointMap) => void;
let subscribers: Array<{
token: string;
func: SubscribeFunc;
}> = [];
let subUid = -1;
let screens = {};
const responsiveObserve = {
dispatch(pointMap: BreakpointMap) {
screens = pointMap;
if (subscribers.length < 1) {
return false;
}
subscribers.forEach(item => {
item.func(screens);
});
return true;
},
subscribe(func: SubscribeFunc) {
if (subscribers.length === 0) {
this.register();
}
const token = (++subUid).toString();
subscribers.push({
token: token,
func: func,
});
func(screens);
return token;
},
unsubscribe(token: string) {
subscribers = subscribers.filter(item => item.token !== token);
if (subscribers.length === 0) {
this.unregister();
}
},
unregister() {
Object.keys(responsiveMap).map((screen: Breakpoint) =>
enquire.unregister(responsiveMap[screen]),
);
},
register() {
Object.keys(responsiveMap).map((screen: Breakpoint) =>
enquire.register(responsiveMap[screen], {
match: () => {
const pointMap = {
...screens,
[screen]: true,
};
this.dispatch(pointMap);
},
unmatch: () => {
const pointMap = {
...screens,
[screen]: false,
};
this.dispatch(pointMap);
},
// Keep a empty destory to avoid triggering unmatch when unregister
destroy() {},
}),
);
},
};
export default responsiveObserve;