Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make breakpoints use the token feature (#39105)
* Add XXXL size * Update index.zh-CN.md * Update components/list/index.zh-CN.md Co-authored-by: Amumu <yoyo837@hotmail.com> * Try to use `useToken` for building responsiveMap * Try useResponsiveObserve * Fix useResponsiveObserve * Some try * Some try * Some try * Remove impossible test * Remove impossible test * Fix token.screenXSMax * Try to put test back as token.screenXSMax is fixed * Remove impossible test now * Subscribers, subuid and screen are no longer static * reorganize def * chore: not affect no-related file * chore: not affect no-related file * test: update test case * chore: adjust memo logic Co-authored-by: Amumu <yoyo837@hotmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com>
- Loading branch information
1 parent
01ae089
commit 18400fb
Showing
8 changed files
with
150 additions
and
97 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import { render } from '../../../tests/utils'; | ||
import useResponsiveObserve from '../responsiveObserve'; | ||
|
||
describe('Test ResponsiveObserve', () => { | ||
it('test ResponsiveObserve subscribe and unsubscribe', () => { | ||
let responsiveObserveRef: any; | ||
const Demo = () => { | ||
const responsiveObserve = useResponsiveObserve(); | ||
responsiveObserveRef = responsiveObserve; | ||
return null; | ||
}; | ||
render(<Demo />); | ||
const subscribeFunc = jest.fn(); | ||
const token = responsiveObserveRef.subscribe(subscribeFunc); | ||
expect( | ||
responsiveObserveRef.matchHandlers[responsiveObserveRef.responsiveMap.xs].mql.matches, | ||
).toBeTruthy(); | ||
expect(subscribeFunc).toHaveBeenCalledTimes(1); | ||
|
||
responsiveObserveRef.unsubscribe(token); | ||
expect( | ||
responsiveObserveRef.matchHandlers[responsiveObserveRef.responsiveMap.xs].mql.removeListener, | ||
).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,85 @@ | ||
import React from 'react'; | ||
import type { GlobalToken } from '../theme/interface'; | ||
import { useToken } from '../theme/internal'; | ||
|
||
export type Breakpoint = 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs'; | ||
export type BreakpointMap = Record<Breakpoint, string>; | ||
export type ScreenMap = Partial<Record<Breakpoint, boolean>>; | ||
export type ScreenSizeMap = Partial<Record<Breakpoint, number>>; | ||
|
||
export const responsiveArray: Breakpoint[] = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs']; | ||
type SubscribeFunc = (screens: ScreenMap) => void; | ||
|
||
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)', | ||
}; | ||
const getResponsiveMap = (token: GlobalToken): BreakpointMap => ({ | ||
xs: `(max-width: ${token.screenXSMax}px)`, | ||
sm: `(min-width: ${token.screenSM}px)`, | ||
md: `(min-width: ${token.screenMD}px)`, | ||
lg: `(min-width: ${token.screenLG}px)`, | ||
xl: `(min-width: ${token.screenXL}px)`, | ||
xxl: `(min-width: ${token.screenXXL}px)`, | ||
}); | ||
|
||
type SubscribeFunc = (screens: ScreenMap) => void; | ||
const subscribers = new Map<Number, SubscribeFunc>(); | ||
let subUid = -1; | ||
let screens = {}; | ||
export default function useResponsiveObserver() { | ||
const [, token] = useToken(); | ||
const responsiveMap: BreakpointMap = getResponsiveMap(token); | ||
|
||
const responsiveObserve = { | ||
matchHandlers: {} as { | ||
[prop: string]: { | ||
mql: MediaQueryList; | ||
listener: ((this: MediaQueryList, ev: MediaQueryListEvent) => any) | null; | ||
}; | ||
}, | ||
dispatch(pointMap: ScreenMap) { | ||
screens = pointMap; | ||
subscribers.forEach((func) => func(screens)); | ||
return subscribers.size >= 1; | ||
}, | ||
subscribe(func: SubscribeFunc): number { | ||
if (!subscribers.size) this.register(); | ||
subUid += 1; | ||
subscribers.set(subUid, func); | ||
func(screens); | ||
return subUid; | ||
}, | ||
unsubscribe(token: number) { | ||
subscribers.delete(token); | ||
if (!subscribers.size) this.unregister(); | ||
}, | ||
unregister() { | ||
Object.keys(responsiveMap).forEach((screen: Breakpoint) => { | ||
const matchMediaQuery = responsiveMap[screen]; | ||
const handler = this.matchHandlers[matchMediaQuery]; | ||
handler?.mql.removeListener(handler?.listener); | ||
}); | ||
subscribers.clear(); | ||
}, | ||
register() { | ||
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, | ||
}; | ||
// To avoid repeat create instance, we add `useMemo` here. | ||
return React.useMemo(() => { | ||
const subscribers = new Map<Number, SubscribeFunc>(); | ||
let subUid = -1; | ||
let screens = {}; | ||
|
||
listener(mql); | ||
}); | ||
}, | ||
}; | ||
return { | ||
matchHandlers: {} as { | ||
[prop: string]: { | ||
mql: MediaQueryList; | ||
listener: ((this: MediaQueryList, ev: MediaQueryListEvent) => any) | null; | ||
}; | ||
}, | ||
dispatch(pointMap: ScreenMap) { | ||
screens = pointMap; | ||
subscribers.forEach((func) => func(screens)); | ||
return subscribers.size >= 1; | ||
}, | ||
subscribe(func: SubscribeFunc): number { | ||
if (!subscribers.size) this.register(); | ||
subUid += 1; | ||
subscribers.set(subUid, func); | ||
func(screens); | ||
return subUid; | ||
}, | ||
unsubscribe(paramToken: number) { | ||
subscribers.delete(paramToken); | ||
if (!subscribers.size) this.unregister(); | ||
}, | ||
unregister() { | ||
Object.keys(responsiveMap).forEach((screen: Breakpoint) => { | ||
const matchMediaQuery = responsiveMap[screen]; | ||
const handler = this.matchHandlers[matchMediaQuery]; | ||
handler?.mql.removeListener(handler?.listener); | ||
}); | ||
subscribers.clear(); | ||
}, | ||
register() { | ||
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, | ||
}; | ||
|
||
export default responsiveObserve; | ||
listener(mql); | ||
}); | ||
}, | ||
responsiveMap, | ||
}; | ||
}, [token]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters