-
Notifications
You must be signed in to change notification settings - Fork 349
refactor(media-query): migrate MediaQuery from Flow to TypeScript #4735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mergify
merged 1 commit into
box:master
from
bonchevskyi:refactor/flow-to-ts-media-query
Aug 6, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
8 changes: 5 additions & 3 deletions
8
...ia-query/__tests__/withMediaQuery.test.js → ...a-query/__tests__/withMediaQuery.test.tsx
This file contains hidden or 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
File renamed without changes.
This file contains hidden or 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,32 @@ | ||
| import { smallScreen, mediumScreen, largeScreen } from '../../styles/variables'; | ||
|
|
||
| export const SIZE_SMALL = smallScreen; | ||
| export const SIZE_MEDIUM = mediumScreen; | ||
| export const SIZE_LARGE = largeScreen; | ||
|
|
||
| export const HOVER = '(hover: hover)'; | ||
| export const ANY_HOVER = '(any-hover: hover)'; | ||
|
|
||
| export const POINTER_FINE = '(pointer: fine)'; | ||
| export const POINTER_COARSE = '(pointer: coarse)'; | ||
|
|
||
| export const ANY_POINTER_FINE = '(any-pointer: fine)'; | ||
| export const ANY_POINTER_COARSE = '(any-pointer: coarse)'; | ||
|
|
||
| export const POINTER_TYPE = { | ||
| fine: 'fine', | ||
| coarse: 'coarse', | ||
| none: 'none', | ||
| } as const; | ||
|
|
||
| export const HOVER_TYPE = { | ||
| hover: 'hover', | ||
| none: 'none', | ||
| } as const; | ||
|
|
||
| export const VIEW_SIZE_TYPE = { | ||
| small: 'small', | ||
| medium: 'medium', | ||
| large: 'large', | ||
| xlarge: 'x-large', | ||
| } as const; |
File renamed without changes.
This file contains hidden or 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,3 @@ | ||
| export { default as useMediaQuery } from './useMediaQuery'; | ||
| export { default as withMediaQuery } from './withMediaQuery'; | ||
| export type { MediaFeatures, MediaHoverType, MediaPointerType, MediaQuery, MediaShape } from './types'; |
This file contains hidden or 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
File renamed without changes.
This file contains hidden or 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,28 @@ | ||
| import { HOVER_TYPE, POINTER_TYPE } from './constants'; | ||
|
|
||
| export type MediaPointerType = (typeof POINTER_TYPE)[keyof typeof POINTER_TYPE]; | ||
| export type MediaHoverType = (typeof HOVER_TYPE)[keyof typeof HOVER_TYPE]; | ||
|
|
||
| export interface MediaFeatures { | ||
| anyHover?: MediaHoverType; | ||
| anyPointer?: MediaPointerType; | ||
| hover?: MediaHoverType; | ||
| maxHeight?: number; | ||
| maxWidth?: number; | ||
| minHeight?: number; | ||
| minWidth?: number; | ||
| pointer?: MediaPointerType; | ||
| } | ||
|
|
||
| export interface MediaShape { | ||
| anyHover: MediaHoverType; | ||
| anyPointer: MediaPointerType; | ||
| hover: MediaHoverType; | ||
| isTouchDevice: boolean; | ||
| pointer: MediaPointerType; | ||
| size: string; | ||
| viewHeight: number; | ||
| viewWidth: number; | ||
| } | ||
|
|
||
| export type MediaQuery = string | MediaFeatures; |
File renamed without changes.
This file contains hidden or 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,98 @@ | ||
| import { toQuery, useMediaQuery as _useMediaQuery } from 'react-responsive'; | ||
| import { | ||
| ANY_HOVER, | ||
| ANY_POINTER_COARSE, | ||
| ANY_POINTER_FINE, | ||
| HOVER, | ||
| HOVER_TYPE, | ||
| POINTER_COARSE, | ||
| POINTER_FINE, | ||
| POINTER_TYPE, | ||
| SIZE_LARGE, | ||
| SIZE_MEDIUM, | ||
| SIZE_SMALL, | ||
| VIEW_SIZE_TYPE, | ||
| } from './constants'; | ||
| import type { MediaQuery, MediaShape } from './types'; | ||
|
|
||
| const getPointerCapabilities = (isFine: boolean, isCoarse: boolean) => { | ||
| if (!isFine && !isCoarse) return POINTER_TYPE.none; | ||
| if (isFine) return POINTER_TYPE.fine; | ||
| return POINTER_TYPE.coarse; | ||
| }; | ||
|
|
||
| const getViewDimensions = () => { | ||
| return { viewWidth: window.innerWidth, viewHeight: window.innerHeight }; | ||
| }; | ||
|
|
||
| /** | ||
| * Formats the media query either as a MediaQuery object or string | ||
| * @param query | ||
| * @returns {string} | ||
| */ | ||
| function formatQuery(query: MediaQuery): string { | ||
|
bonchevskyi marked this conversation as resolved.
|
||
| return typeof query === 'string' ? query : toQuery(query); | ||
| } | ||
|
|
||
| /** | ||
| * Executes media query | ||
| * @param query | ||
| * @param onQueryChange | ||
| * @returns {boolean} | ||
| */ | ||
| function useQuery(query: MediaQuery, onQueryChange?: (_: boolean) => void): boolean { | ||
| return _useMediaQuery({ query: formatQuery(query) }, null, onQueryChange); | ||
| } | ||
|
|
||
| /** | ||
| * Determines device capabilities for hover and pointer features | ||
| * @returns {{anyPointer: *, hover: (string), pointer: *, anyHover: (string)}} | ||
| */ | ||
| function useDeviceCapabilities() { | ||
| const isHover: boolean = useQuery(HOVER); | ||
| const isAnyHover: boolean = useQuery(ANY_HOVER); | ||
|
|
||
| const anyHover = isAnyHover ? HOVER_TYPE.hover : HOVER_TYPE.none; | ||
| const hover = isHover ? HOVER_TYPE.hover : HOVER_TYPE.none; | ||
| const pointer = getPointerCapabilities(useQuery(POINTER_FINE), useQuery(POINTER_COARSE)); | ||
| const anyPointer = getPointerCapabilities(useQuery(ANY_POINTER_FINE), useQuery(ANY_POINTER_COARSE)); | ||
|
|
||
| const isTouchDevice = hover === HOVER_TYPE.none && pointer === POINTER_TYPE.coarse; | ||
|
|
||
| return { | ||
| anyHover, | ||
| hover, | ||
| anyPointer, | ||
| pointer, | ||
| isTouchDevice, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Determines device size using media queries | ||
| * @returns {string} | ||
| */ | ||
| function useDeviceSize() { | ||
| const isSmall: boolean = useQuery(SIZE_SMALL); | ||
| const isMedium: boolean = useQuery(SIZE_MEDIUM); | ||
| const isLarge: boolean = useQuery(SIZE_LARGE); | ||
|
|
||
| if (isSmall) return VIEW_SIZE_TYPE.small; | ||
| if (isMedium) return VIEW_SIZE_TYPE.medium; | ||
| if (isLarge) return VIEW_SIZE_TYPE.large; | ||
|
|
||
| return VIEW_SIZE_TYPE.xlarge; | ||
| } | ||
|
|
||
| function useMediaQuery(): MediaShape { | ||
| const deviceCapabilities = useDeviceCapabilities(); | ||
| const deviceSize = useDeviceSize(); | ||
| const viewDimensions = getViewDimensions(); | ||
| return { | ||
| ...deviceCapabilities, | ||
| ...viewDimensions, | ||
| size: deviceSize, | ||
| }; | ||
| } | ||
|
|
||
| export default useMediaQuery; | ||
File renamed without changes.
This file contains hidden or 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,15 @@ | ||
| import * as React from 'react'; | ||
| import useMediaQuery from './useMediaQuery'; | ||
| import type { MediaShape } from './types'; | ||
|
|
||
| function withMediaQuery<Props extends object>( | ||
|
bonchevskyi marked this conversation as resolved.
|
||
| WrappedComponent: React.ComponentType<Props>, | ||
| ): React.ComponentType<Record<string, unknown>> { | ||
| return (props: Record<string, unknown>) => { | ||
| const mediaProps: MediaShape = useMediaQuery(); | ||
|
|
||
| return <WrappedComponent {...({ ...props, ...mediaProps } as Props)} />; | ||
| }; | ||
|
bonchevskyi marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export default withMediaQuery; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.