Breakpoints
Defined in: utilities/breakpoints/breakpoints.ts:96
A custom resize event based on the “breakpoints”, or media queries, in the CSS of the page.
It reads the min-width media queries from the page’s stylesheets and fires a switch
event on window when the width of the window moves from one breakpoint to another.
The event gives the name and the width in pixels of the new breakpoint, as a Breakpoint.
- Only
min-widthmedia queries are read, and the number in them is taken as pixels. - Each breakpoint is named
bp-{width}.bp-0always exists: it is the width below the smallest query. switchfires once when it starts, so you learn the current breakpoint without resizing.- The CSS is read once the DOM is ready, so styles in the
<head>and in the<body>are both seen. - The
<script>build starts one for you when it loads, so there is nothing to call. Start a second one and every event fires twice. With modules, create one and call Breakpoints.init.
| Event | Fired on | Fired when | event.detail |
|---|---|---|---|
switch |
window |
the page starts, and each time the breakpoint changes | Breakpoint |
debounce.resize |
window |
the window was resized, at most once per frame | {} |
Examples
Section titled “Examples”Using the custom event
window.addEventListener('switch', (event) => { const breakpoint = (event as CustomEvent<Breakpoint>).detail;
console.log(breakpoint.name); // name of the breakpoint console.log(breakpoint.size); // width in px of the breakpoint});Starting it with modules
const breakpoints = new Breakpoints();breakpoints.init();Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Breakpoints():
Breakpoints
Returns
Section titled “Returns”Breakpoints
Methods
Section titled “Methods”destroy()
Section titled “destroy()”destroy():
void
Defined in: utilities/breakpoints/breakpoints.ts:135
Stops watching: removes the listeners and cancels anything that is waiting. Calling Breakpoints.init afterwards starts again and reads the CSS again.
Returns
Section titled “Returns”void
init()
Section titled “init()”init():
void
Defined in: utilities/breakpoints/breakpoints.ts:111
Starts watching the window. Calling it again does nothing.
If the page is still loading, it waits until the DOM is ready, so the stylesheets and the
listeners added by the page’s own scripts are in place. Then it reads the CSS and fires
the first switch.
Returns
Section titled “Returns”void