Skip to content

Commit 67ddedb

Browse files
committed
feat(config): add screenBreakpoints config option (#31502)
Adds a shared breakpoints utility that resolves the screen breakpoints from config, validating each value and falling back to the default per key. The `xs` screen breakpoint defaults to `0` and cannot be changed. --------- Co-authored-by: Brandy Smith <6577830+brandyscarney@users.noreply.github.com>
1 parent 95a000a commit 67ddedb

8 files changed

Lines changed: 1184 additions & 44 deletions

File tree

‎core/src/components/col/col.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ComponentInterface } from '@stencil/core';
22
import { Component, Element, Host, Listen, Prop, forceUpdate, h } from '@stencil/core';
3+
import { matchBreakpoint } from '@utils/breakpoints';
34
import { printIonWarning } from '@utils/logging';
4-
import { matchBreakpoint } from '@utils/media';
55

66
import type { IonColProperty, IonColStyle } from './col.interface';
77
import { ION_COL_BREAKPOINTS } from './col.interface';

‎core/src/components/grid/test/fixed/grid.e2e.ts‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
import { expect } from '@playwright/test';
2+
import type { ScreenBreakpoint } from '@utils/breakpoints';
3+
import { DEFAULT_SCREEN_BREAKPOINTS, SCREEN_BREAKPOINT_NAMES } from '@utils/breakpoints';
24
import { configs, test } from '@utils/test/playwright';
35

46
import { defaultTheme as mdTheme } from '../../../../themes/md/default.tokens';
5-
import { SIZE_TO_MEDIA } from '../../../../utils/media';
6-
import { ION_GRID_BREAKPOINTS } from '../../grid.interface';
77

88
const ionGridBreakpoints = mdTheme.components!.IonGrid!.breakpoint!;
99

10-
/**
11-
* Parse the min-width (in px) out of each `SIZE_TO_MEDIA` entry. These are
12-
* the same activation thresholds `matchBreakpoint` evaluates at runtime and
13-
* that `$screen-breakpoints` defines on the SCSS side.
14-
*/
15-
const minWidthFor = (bp: (typeof ION_GRID_BREAKPOINTS)[number]): number => {
16-
const match = (SIZE_TO_MEDIA[bp] as string).match(/\(min-width:\s*(\d+)px\)/);
17-
return match ? parseInt(match[1], 10) : 0;
18-
};
19-
2010
/**
2111
* Viewport width that activates each breakpoint. `max(400, minWidth)` lands
22-
* exactly on the threshold for every breakpoint above xs (firing only that
23-
* breakpoint's rule and nothing above it) while giving xs a renderable non-zero
24-
* viewport.
12+
* exactly on the threshold for sm and up (firing only that breakpoint's rule
13+
* and nothing above it) while giving xs a renderable non-zero viewport.
14+
*
15+
* The default screen breakpoints are used rather than the resolved ones,
16+
* since the test page does not override them and `$screen-breakpoints`
17+
* defines the same values on the SCSS side.
2518
*/
2619
const VIEWPORT_AT_BREAKPOINT = Object.fromEntries(
27-
ION_GRID_BREAKPOINTS.map((bp) => [bp, Math.max(400, minWidthFor(bp))])
28-
) as Record<(typeof ION_GRID_BREAKPOINTS)[number], number>;
20+
SCREEN_BREAKPOINT_NAMES.map((bp) => [bp, Math.max(400, DEFAULT_SCREEN_BREAKPOINTS[bp])])
21+
) as Record<ScreenBreakpoint, number>;
2922

3023
/**
3124
* This behavior does not vary across modes/directions.
@@ -36,7 +29,7 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
3629
await page.goto('/src/components/grid/test/fixed', config);
3730
});
3831

39-
for (const breakpoint of ION_GRID_BREAKPOINTS) {
32+
for (const breakpoint of SCREEN_BREAKPOINT_NAMES) {
4033
test(`fixed grid matches the ${breakpoint} width token`, async ({ page }) => {
4134
const viewportWidth = VIEWPORT_AT_BREAKPOINT[breakpoint];
4235
await page.setViewportSize({ width: viewportWidth, height: 800 });

‎core/src/global/ionic-global.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { applyComponentsTheme, applyGlobalTheme, getCustomTheme } from '@utils/t
55
import type { IonicConfig, Mode, Theme } from '../interface';
66
import { defaultTheme as baseTheme } from '../themes/base/default.tokens';
77
import type { DefaultTheme } from '../themes/themes.interfaces';
8+
import { refreshBreakpointListeners } from '../utils/breakpoints';
89
import { shouldUseCloseWatcher } from '../utils/hardware-back-button';
910
import { isPlatform, setupPlatforms } from '../utils/platform';
1011

@@ -120,6 +121,7 @@ export const initialize = (userConfig: IonicConfig = {}) => {
120121
};
121122

122123
config.reset(configObj);
124+
123125
if (config.getBoolean('persistConfig')) {
124126
saveConfig(win, configObj);
125127
}
@@ -199,6 +201,13 @@ export const initialize = (userConfig: IonicConfig = {}) => {
199201
config.set(configKey, value);
200202
});
201203

204+
/**
205+
* The breakpoint listeners are built from the configured widths, so they
206+
* have to be rebuilt once the configuration is settled. This runs after the
207+
* theme's config is merged, since a theme can set the widths itself.
208+
*/
209+
refreshBreakpointListeners();
210+
202211
if (config.getBoolean('_testing')) {
203212
config.set('animated', false);
204213
}

‎core/src/global/test/ionic-global.spec.ts‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ jest.mock('@stencil/core', () => {
1313
/**
1414
* The implementation needs to be mocked before the implementation is imported.
1515
*/
16-
// eslint-disable-next-line import/first
17-
import { getIonTheme, getIonMode } from '../ionic-global';
16+
import { onBreakpointChange, resetBreakpointListeners, resetScreenBreakpoints } from '@utils/breakpoints';
17+
18+
import { config } from '../config';
19+
import { getIonTheme, getIonMode, initialize } from '../ionic-global';
1820

1921
describe('Ionic Global', () => {
2022
describe('getIonMode', () => {
@@ -107,4 +109,69 @@ describe('Ionic Global', () => {
107109
expect(getIonTheme()).toBe('md');
108110
});
109111
});
112+
113+
describe('initialize', () => {
114+
const originalMatchMedia = (window as any).matchMedia;
115+
116+
/** Every media query the breakpoint listeners have asked for, in order. */
117+
let requested: string[];
118+
119+
beforeEach(() => {
120+
requested = [];
121+
122+
(window as any).matchMedia = (media: string) => {
123+
requested.push(media);
124+
125+
return { media, matches: false, addEventListener: () => {}, removeEventListener: () => {} };
126+
};
127+
128+
config.set('screenBreakpoints', undefined as any);
129+
resetScreenBreakpoints();
130+
resetBreakpointListeners();
131+
});
132+
133+
afterEach(() => {
134+
resetBreakpointListeners();
135+
resetScreenBreakpoints();
136+
config.set('screenBreakpoints', undefined as any);
137+
(window as any).matchMedia = originalMatchMedia;
138+
});
139+
140+
/**
141+
* A theme can set the screen breakpoints in its own config block, which is
142+
* merged in after the application config, so the listeners can only be
143+
* rebuilt once both have been applied.
144+
*/
145+
it('should rebuild the breakpoint listeners from a theme that sets the widths', () => {
146+
// Subscribing first is what creates the listeners there are to rebuild.
147+
const unsubscribe = onBreakpointChange(() => {});
148+
requested.length = 0;
149+
150+
initialize({ customTheme: { config: { screenBreakpoints: { lg: 700 } } } } as any);
151+
152+
expect(requested).toContain('(min-width: 700px)');
153+
expect(requested).not.toContain('(min-width: 992px)');
154+
155+
unsubscribe();
156+
});
157+
158+
/**
159+
* The theme's config is merged over the application's, so widths set in
160+
* either are kept and the listeners are built from the result.
161+
*/
162+
it('should keep widths set by the application alongside those from the theme', () => {
163+
const unsubscribe = onBreakpointChange(() => {});
164+
requested.length = 0;
165+
166+
initialize({
167+
screenBreakpoints: { sm: 300 },
168+
customTheme: { config: { screenBreakpoints: { lg: 700 } } },
169+
} as any);
170+
171+
expect(requested).toContain('(min-width: 300px)');
172+
expect(requested).toContain('(min-width: 700px)');
173+
174+
unsubscribe();
175+
});
176+
});
110177
});

0 commit comments

Comments
 (0)