Navigation: Fix broken layout at 544px (#63793)

* ensure media queries transition properly

* fix unit tests
This commit is contained in:
Ashley Harrison
2023-03-01 15:31:32 +00:00
committed by GitHub
parent 78b39bb282
commit 8e9ccfc66e
8 changed files with 22 additions and 14 deletions

View File

@ -16,8 +16,8 @@ export interface ThemeBreakpoints {
values: ThemeBreakpointValues; values: ThemeBreakpointValues;
keys: string[]; keys: string[];
unit: string; unit: string;
up: (key: ThemeBreakpointsKey) => string; up: (key: ThemeBreakpointsKey | number) => string;
down: (key: ThemeBreakpointsKey) => string; down: (key: ThemeBreakpointsKey | number) => string;
} }
/** @internal */ /** @internal */

View File

@ -29,6 +29,14 @@ const renderWithProvider = ({ initialState }: { initialState?: Partial<appTypes.
}; };
describe('OrganisationSwitcher', () => { describe('OrganisationSwitcher', () => {
beforeEach(() => {
(window.matchMedia as jest.Mock).mockImplementation(() => ({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
matches: true,
}));
});
it('should only render if more than one organisations', () => { it('should only render if more than one organisations', () => {
renderWithProvider({ renderWithProvider({
initialState: { initialState: {
@ -75,7 +83,7 @@ describe('OrganisationSwitcher', () => {
(window.matchMedia as jest.Mock).mockImplementation(() => ({ (window.matchMedia as jest.Mock).mockImplementation(() => ({
addEventListener: jest.fn(), addEventListener: jest.fn(),
removeEventListener: jest.fn(), removeEventListener: jest.fn(),
matches: () => true, matches: false,
})); }));
renderWithProvider({ renderWithProvider({
initialState: { initialState: {

View File

@ -31,12 +31,12 @@ export function OrganizationSwitcher() {
const breakpoint = theme.breakpoints.values.sm; const breakpoint = theme.breakpoints.values.sm;
const [isSmallScreen, setIsSmallScreen] = useState(window.matchMedia(`(max-width: ${breakpoint}px)`).matches); const [isSmallScreen, setIsSmallScreen] = useState(!window.matchMedia(`(min-width: ${breakpoint}px)`).matches);
useMediaQueryChange({ useMediaQueryChange({
breakpoint, breakpoint,
onChange: (e) => { onChange: (e) => {
setIsSmallScreen(e.matches); setIsSmallScreen(!e.matches);
}, },
}); });

View File

@ -19,13 +19,13 @@ export const QuickAdd = ({}: Props) => {
const navBarTree = useSelector((state) => state.navBarTree); const navBarTree = useSelector((state) => state.navBarTree);
const breakpoint = theme.breakpoints.values.sm; const breakpoint = theme.breakpoints.values.sm;
const [isSmallScreen, setIsSmallScreen] = useState(window.matchMedia(`(max-width: ${breakpoint}px)`).matches); const [isSmallScreen, setIsSmallScreen] = useState(!window.matchMedia(`(min-width: ${breakpoint}px)`).matches);
const createActions = useMemo(() => findCreateActions(navBarTree), [navBarTree]); const createActions = useMemo(() => findCreateActions(navBarTree), [navBarTree]);
useMediaQueryChange({ useMediaQueryChange({
breakpoint, breakpoint,
onChange: (e) => { onChange: (e) => {
setIsSmallScreen(e.matches); setIsSmallScreen(!e.matches);
}, },
}); });

View File

@ -17,7 +17,7 @@ describe('TopSearchBarSection', () => {
(window.matchMedia as jest.Mock).mockImplementation(() => ({ (window.matchMedia as jest.Mock).mockImplementation(() => ({
addEventListener: jest.fn(), addEventListener: jest.fn(),
removeEventListener: jest.fn(), removeEventListener: jest.fn(),
matches: () => false, matches: true,
})); }));
const { container } = renderComponent(); const { container } = renderComponent();
@ -30,7 +30,7 @@ describe('TopSearchBarSection', () => {
(window.matchMedia as jest.Mock).mockImplementation(() => ({ (window.matchMedia as jest.Mock).mockImplementation(() => ({
addEventListener: jest.fn(), addEventListener: jest.fn(),
removeEventListener: jest.fn(), removeEventListener: jest.fn(),
matches: () => true, matches: false,
})); }));
const { container } = renderComponent(); const { container } = renderComponent();

View File

@ -15,12 +15,12 @@ export function TopSearchBarSection({ children, align = 'left' }: TopSearchBarSe
const theme = useTheme2(); const theme = useTheme2();
const breakpoint = theme.breakpoints.values.sm; const breakpoint = theme.breakpoints.values.sm;
const [isSmallScreen, setIsSmallScreen] = useState(window.matchMedia(`(max-width: ${breakpoint}px)`).matches); const [isSmallScreen, setIsSmallScreen] = useState(!window.matchMedia(`(min-width: ${breakpoint}px)`).matches);
useMediaQueryChange({ useMediaQueryChange({
breakpoint, breakpoint,
onChange: (e: MediaQueryListEvent) => { onChange: (e: MediaQueryListEvent) => {
setIsSmallScreen(e.matches); setIsSmallScreen(!e.matches);
}, },
}); });

View File

@ -18,12 +18,12 @@ export function TopSearchBarCommandPaletteTrigger() {
const breakpoint = theme.breakpoints.values.sm; const breakpoint = theme.breakpoints.values.sm;
const [isSmallScreen, setIsSmallScreen] = useState(window.matchMedia(`(max-width: ${breakpoint}px)`).matches); const [isSmallScreen, setIsSmallScreen] = useState(!window.matchMedia(`(min-width: ${breakpoint}px)`).matches);
useMediaQueryChange({ useMediaQueryChange({
breakpoint, breakpoint,
onChange: (e) => { onChange: (e) => {
setIsSmallScreen(e.matches); setIsSmallScreen(!e.matches);
}, },
}); });

View File

@ -8,7 +8,7 @@ export function useMediaQueryChange({
onChange: (e: MediaQueryListEvent) => void; onChange: (e: MediaQueryListEvent) => void;
}) { }) {
useEffect(() => { useEffect(() => {
const mediaQuery = window.matchMedia(`(max-width: ${breakpoint}px)`); const mediaQuery = window.matchMedia(`(min-width: ${breakpoint}px)`);
const onMediaQueryChange = (e: MediaQueryListEvent) => onChange(e); const onMediaQueryChange = (e: MediaQueryListEvent) => onChange(e);
mediaQuery.addEventListener('change', onMediaQueryChange); mediaQuery.addEventListener('change', onMediaQueryChange);