2019-05-22 23:22:09 +08:00
|
|
|
export type Breakpoint = 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs';
|
|
|
|
export type BreakpointMap = Partial<Record<Breakpoint, string>>;
|
2020-01-08 17:14:29 +08:00
|
|
|
export type ScreenMap = Partial<Record<Breakpoint, boolean>>;
|
2019-05-22 23:22:09 +08:00
|
|
|
|
|
|
|
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)',
|
|
|
|
};
|
|
|
|
|
2020-01-08 17:14:29 +08:00
|
|
|
type SubscribeFunc = (screens: ScreenMap) => void;
|
2019-05-22 23:22:09 +08:00
|
|
|
|
|
|
|
let subscribers: Array<{
|
|
|
|
token: string;
|
|
|
|
func: SubscribeFunc;
|
|
|
|
}> = [];
|
|
|
|
let subUid = -1;
|
|
|
|
let screens = {};
|
|
|
|
|
|
|
|
const responsiveObserve = {
|
2019-12-23 18:33:08 +08:00
|
|
|
matchHandlers: {},
|
2020-01-08 17:14:29 +08:00
|
|
|
dispatch(pointMap: ScreenMap) {
|
2019-05-22 23:22:09 +08:00
|
|
|
screens = pointMap;
|
|
|
|
subscribers.forEach(item => {
|
|
|
|
item.func(screens);
|
|
|
|
});
|
2020-05-24 23:52:25 +08:00
|
|
|
return subscribers.length >= 1;
|
2019-05-22 23:22:09 +08:00
|
|
|
},
|
|
|
|
subscribe(func: SubscribeFunc) {
|
|
|
|
if (subscribers.length === 0) {
|
|
|
|
this.register();
|
|
|
|
}
|
|
|
|
const token = (++subUid).toString();
|
|
|
|
subscribers.push({
|
2019-08-14 18:21:24 +08:00
|
|
|
token,
|
|
|
|
func,
|
2019-05-22 23:22:09 +08:00
|
|
|
});
|
|
|
|
func(screens);
|
|
|
|
return token;
|
|
|
|
},
|
|
|
|
unsubscribe(token: string) {
|
|
|
|
subscribers = subscribers.filter(item => item.token !== token);
|
|
|
|
if (subscribers.length === 0) {
|
|
|
|
this.unregister();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unregister() {
|
2019-12-23 18:33:08 +08:00
|
|
|
Object.keys(responsiveMap).forEach((screen: Breakpoint) => {
|
|
|
|
const matchMediaQuery = responsiveMap[screen]!;
|
|
|
|
const handler = this.matchHandlers[matchMediaQuery];
|
|
|
|
if (handler && handler.mql && handler.listener) {
|
|
|
|
handler.mql.removeListener(handler.listener);
|
|
|
|
}
|
|
|
|
});
|
2019-05-22 23:22:09 +08:00
|
|
|
},
|
|
|
|
register() {
|
2019-12-23 18:33:08 +08:00
|
|
|
Object.keys(responsiveMap).forEach((screen: Breakpoint) => {
|
|
|
|
const matchMediaQuery = responsiveMap[screen]!;
|
|
|
|
const listener = ({ matches }: { matches: boolean }) => {
|
|
|
|
this.dispatch({
|
|
|
|
...screens,
|
|
|
|
[screen]: matches,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const mql = window.matchMedia(matchMediaQuery);
|
|
|
|
mql.addListener(listener);
|
|
|
|
this.matchHandlers[matchMediaQuery] = {
|
|
|
|
mql,
|
|
|
|
listener,
|
|
|
|
};
|
2019-12-23 23:24:47 +08:00
|
|
|
|
|
|
|
listener(mql);
|
2019-12-23 18:33:08 +08:00
|
|
|
});
|
2019-05-22 23:22:09 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default responsiveObserve;
|