Skip to content

Commit

Permalink
[APM] Update useBreakpoints hook with EUI breakpoint deprecations
Browse files Browse the repository at this point in the history
- since getBreakpoint is no longer an usable util

- EUI's new breakpoint hooks already use/debounce window resize events, so we now get to skip all that custom logic by simply making custom EUI breakpoint overrides and using EUI breakpoint hooks

- remove returned `breakpoint` and `width` keys - they weren't actually being used anywhere in APM that I could see and were causing type errors.
- If someone wants to access them, they can use `useCurrentEuiBreakpoint` and `useWindowSize` individually instead
  • Loading branch information
cee-chen committed Sep 9, 2022
1 parent 652bcf2 commit b24bc95
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 186 deletions.
10 changes: 9 additions & 1 deletion x-pack/plugins/apm/public/application/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ export const renderApp = ({
element.classList.add(APP_WRAPPER_CLASS);

ReactDOM.render(
<KibanaThemeProvider theme$={theme$}>
<KibanaThemeProvider
theme$={theme$}
modify={{
breakpoint: {
xxl: 1600,
xxxl: 2000,
},
}}
>
<ApmAppRoot
apmPluginContextValue={apmPluginContextValue}
pluginsStart={pluginsStart}
Expand Down
153 changes: 0 additions & 153 deletions x-pack/plugins/apm/public/hooks/use_breakpoints.test.ts

This file was deleted.

125 changes: 125 additions & 0 deletions x-pack/plugins/apm/public/hooks/use_breakpoints.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { FC } from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { EuiProvider } from '@elastic/eui';
import { useBreakpoints } from './use_breakpoints';

const wrapper: FC = ({ children }) => (
<EuiProvider
modify={{
// set in apm/public/application/index.tsx
breakpoint: {
xxl: 1600,
xxxl: 2000,
},
}}
>
{children}
</EuiProvider>
);

describe('useBreakpoints', () => {
test('xs breakpoint', () => {
window.innerWidth = 0;
const { result } = renderHook(() => useBreakpoints(), { wrapper });
expect(result.current).toEqual({
isXSmall: true,
isSmall: true,
isMedium: true,
isLarge: true,
isXl: true,
isXXL: true,
isXXXL: false,
});
});

test('s breakpoint', () => {
window.innerWidth = 575;
const { result } = renderHook(() => useBreakpoints(), { wrapper });
expect(result.current).toEqual({
isXSmall: false,
isSmall: true,
isMedium: true,
isLarge: true,
isXl: true,
isXXL: true,
isXXXL: false,
});
});

test('m breakpoint', () => {
window.innerWidth = 768;
const { result } = renderHook(() => useBreakpoints(), { wrapper });
expect(result.current).toEqual({
isXSmall: false,
isSmall: false,
isMedium: true,
isLarge: true,
isXl: true,
isXXL: true,
isXXXL: false,
});
});

test('l breakpoint', () => {
window.innerWidth = 992;
const { result } = renderHook(() => useBreakpoints(), { wrapper });
expect(result.current).toEqual({
isXSmall: false,
isSmall: false,
isMedium: false,
isLarge: true,
isXl: true,
isXXL: true,
isXXXL: false,
});
});

test('xl breakpoint', () => {
window.innerWidth = 1200;
const { result } = renderHook(() => useBreakpoints(), { wrapper });
expect(result.current).toEqual({
isXSmall: false,
isSmall: false,
isMedium: false,
isLarge: false,
isXl: true,
isXXL: true,
isXXXL: false,
});
});

test('xxl breakpoint', () => {
window.innerWidth = 1600;
const { result } = renderHook(() => useBreakpoints(), { wrapper });
expect(result.current).toEqual({
isXSmall: false,
isSmall: false,
isMedium: false,
isLarge: false,
isXl: false,
isXXL: true,
isXXXL: false,
});
});

test('xxxl breakpoint', () => {
window.innerWidth = 2000;
const { result } = renderHook(() => useBreakpoints(), { wrapper });
expect(result.current).toEqual({
isXSmall: false,
isSmall: false,
isMedium: false,
isLarge: false,
isXl: false,
isXXL: false,
isXXXL: true,
});
});
});
45 changes: 13 additions & 32 deletions x-pack/plugins/apm/public/hooks/use_breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,23 @@
* 2.0.
*/

import { useState } from 'react';
import useWindowSize from 'react-use/lib/useWindowSize';
import useDebounce from 'react-use/lib/useDebounce';
import {
getBreakpoint,
isWithinMaxBreakpoint,
isWithinMinBreakpoint,
useIsWithinMaxBreakpoint,
useIsWithinMinBreakpoint,
} from '@elastic/eui';

export type Breakpoints = ReturnType<typeof getScreenSizes>;

export function getScreenSizes(windowWidth: number) {
return {
isXSmall: isWithinMaxBreakpoint(windowWidth, 'xs'),
isSmall: isWithinMaxBreakpoint(windowWidth, 's'),
isMedium: isWithinMaxBreakpoint(windowWidth, 'm'),
isLarge: isWithinMaxBreakpoint(windowWidth, 'l'),
isXl: isWithinMaxBreakpoint(windowWidth, 1599),
isXXL: isWithinMaxBreakpoint(windowWidth, 1999),
isXXXL: isWithinMinBreakpoint(windowWidth, 2000),
};
}
export type Breakpoints = Record<string, boolean>;

export function useBreakpoints() {
const { width } = useWindowSize();
const [breakpoint, setBreakpoint] = useState(getBreakpoint(width));
const [screenSizes, setScreenSizes] = useState(getScreenSizes(width));

useDebounce(
() => {
setBreakpoint(getBreakpoint(width));
setScreenSizes(getScreenSizes(width));
},
50,
[width]
);
const screenSizes = {
isXSmall: useIsWithinMaxBreakpoint('xs'),
isSmall: useIsWithinMaxBreakpoint('s'),
isMedium: useIsWithinMaxBreakpoint('m'),
isLarge: useIsWithinMaxBreakpoint('l'),
isXl: useIsWithinMaxBreakpoint('xl'),
isXXL: useIsWithinMaxBreakpoint('xxl'),
isXXXL: useIsWithinMinBreakpoint('xxxl'),
};

return { ...screenSizes, breakpoint, width };
return screenSizes;
}

0 comments on commit b24bc95

Please sign in to comment.