Skip to content

Commit

Permalink
Deprecate getBreakpoint
Browse files Browse the repository at this point in the history
- in favor of memoizing its behavior in `CurrentEuiBreakpoint` logic

+ update testenv mock slightly - it's not super DRY, but it's fine
  • Loading branch information
cee-chen committed Aug 9, 2022
1 parent 4b0ead2 commit d13a5a1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 112 deletions.
58 changes: 0 additions & 58 deletions src/services/breakpoint/breakpoint.test.ts

This file was deleted.

39 changes: 0 additions & 39 deletions src/services/breakpoint/breakpoint.ts

This file was deleted.

35 changes: 26 additions & 9 deletions src/services/breakpoint/current_breakpoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import React, {
createContext,
useState,
useEffect,
useMemo,
useCallback,
FunctionComponent,
} from 'react';

import { _EuiThemeBreakpoint } from '../../global_styling/variables/breakpoint';
import { keysOf } from '../../components/common';
import {
_EuiThemeBreakpoint,
_EuiThemeBreakpoints,
} from '../../global_styling/variables/breakpoint';
import { useEuiTheme } from '../theme';
import { throttle } from '../throttle';

import { getBreakpoint } from './breakpoint';
import { sortMapByLargeToSmallValues } from './_sorting';

type CurrentEuiBreakpoint = _EuiThemeBreakpoint | undefined;

Expand All @@ -32,27 +37,39 @@ export const CurrentEuiBreakpointContext = createContext<CurrentEuiBreakpoint>(
export const CurrentEuiBreakpointProvider: FunctionComponent = ({
children,
}) => {
// Obtain the breakpoints map from the EUI theme
const {
euiTheme: { breakpoint: breakpoints }, // Obtain the breakpoints map from the EUI theme
euiTheme: { breakpoint: breakpoints },
} = useEuiTheme();

// Ensure the breakpoints map is sorted from largest value to smallest
const sortedBreakpoints: _EuiThemeBreakpoints = useMemo(
() => sortMapByLargeToSmallValues(breakpoints),
[breakpoints]
);

// Find the breakpoint (key) whose value is <= windowWidth starting with largest first
const getBreakpoint = useCallback(
(width: number) =>
keysOf(sortedBreakpoints).find((key) => sortedBreakpoints[key] <= width),
[sortedBreakpoints]
);

const [currentBreakpoint, setCurrentBreakpoint] = useState<
CurrentEuiBreakpoint
>(
typeof window !== 'undefined'
? getBreakpoint(window.innerWidth, breakpoints)
: undefined
typeof window !== 'undefined' ? getBreakpoint(window.innerWidth) : undefined
);

useEffect(() => {
const onWindowResize = throttle(() => {
setCurrentBreakpoint(getBreakpoint(window.innerWidth, breakpoints));
setCurrentBreakpoint(getBreakpoint(window.innerWidth));
}, 50);

window.addEventListener('resize', onWindowResize);

return () => window.removeEventListener('resize', onWindowResize);
}, [breakpoints]);
}, [getBreakpoint]);

return (
<CurrentEuiBreakpointContext.Provider value={currentBreakpoint}>
Expand Down
12 changes: 8 additions & 4 deletions src/services/breakpoint/current_breakpoint_hook.testenv.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import { useContext } from 'react';

import { getBreakpoint } from './breakpoint';
import { keysOf } from '../../components/common';
import { breakpoint as breakpoints } from '../../themes/amsterdam/global_styling/variables/_breakpoint';
import { CurrentEuiBreakpointContext } from './current_breakpoint';

/**
Expand All @@ -19,7 +20,10 @@ export const useCurrentEuiBreakpoint = () => {
const context = useContext(CurrentEuiBreakpointContext);
if (context !== undefined) return context; // Component has been wrapped, everything is fine.

return typeof window !== 'undefined'
? getBreakpoint(window.innerWidth)
: undefined;
if (typeof window === 'undefined') return undefined; // SSR catch

// Use the default Amsterdam breakpoints (which are already ordered by largest first)
return keysOf(breakpoints).find(
(key) => breakpoints[key] <= window.innerWidth
);
};
1 change: 0 additions & 1 deletion src/services/breakpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

export type { _EuiThemeBreakpoint as EuiBreakpointSize } from '../../global_styling/variables/breakpoint';
export * from './breakpoint';
export * from './current_breakpoint';
export * from './current_breakpoint_hook';
export * from './is_within_hooks';
1 change: 0 additions & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export { LEFT_ALIGNMENT, RIGHT_ALIGNMENT, CENTER_ALIGNMENT } from './alignment';

export type { EuiBreakpointSize } from './breakpoint';
export {
getBreakpoint,
useIsWithinBreakpoints,
useIsWithinMaxBreakpoint,
useIsWithinMinBreakpoint,
Expand Down
1 change: 1 addition & 0 deletions upcoming_changelogs/7000.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

**Breaking changes**

- Removed `getBreakpoint`. Use `useCurrentEuiBreakpoint` instead
- Removed `BREAKPOINTS` and `BREAKPOINT_KEYS`. Use `euiTheme.breakpoint` instead
- Removed `isWithinBreakpoints`. Use `useIsWithinBreakpoints` instead
- Removed `isWithinMaxBreakpoint`. Use `useIsWithinMaxBreakpoint` instead
Expand Down

0 comments on commit d13a5a1

Please sign in to comment.