Breakpoints
A custom resize event based on the “breakpoints”, or media queries, in the CSS on the page.
- It reads the
min-widthmedia queries from your stylesheets and fires aswitchevent onwindowwhen the window moves from one breakpoint to another. event.detailis{ name, size }: the name of the breakpoint and its width in pixels.- Each breakpoint is named
bp-{width}, for examplebp-768.bp-0always exists: it is the width below the smallest query. switchis also fired once when the page loads, so you know the current breakpoint without resizing.- The
<script>build starts it for you, so there is nothing to call. With modules, create oneBreakpointsand callinit().
Resize the window. The current breakpoint is shown below and updates each time the window crosses one.
<p>Resize the window. The current breakpoint is shown below and updates each time the window crosses one.</p>
<div class="current-bp"></div>
<style> /* The breakpoints are the min-width media queries in your CSS: here 576, 768, 992 and 1200. */ .current-bp { border-left: 4px solid var(--sl-color-accent); }
@media (min-width: 576px) { .current-bp { border-left-width: 8px; } } @media (min-width: 768px) { .current-bp { border-left-width: 12px; } } @media (min-width: 992px) { .current-bp { border-left-width: 16px; } } @media (min-width: 1200px) { .current-bp { border-left-width: 20px; } }</style>// `switch` is fired on window once when the page loads, and again each time the window// crosses a breakpoint. event.detail is { name, size }.// The script build starts Breakpoints for you, so there is nothing else to set up.var currentBp = document.querySelector('.current-bp');
window.addEventListener('switch', function (e) { currentBp.textContent = JSON.stringify(e.detail);});The breakpoints here come from the <style> in the HTML above. The rest of this site’s CSS writes its media
queries as (width >= 50em), which is not read (see the notes).
Events
Section titled “Events”Both are fired on window.
| Event | Fired when | event.detail |
|---|---|---|
switch |
the page loads, and each time the breakpoint changes | { name, size } |
debounce.resize |
the window was resized, at most once per frame | {} |
window.addEventListener('switch', function (event) { console.log(event.detail.name); // name of the breakpoint console.log(event.detail.size); // width in px of the breakpoint});- Only
min-widthqueries are read, and the number in them is taken as pixels:min-width: 50embecomesbp-50. The range syntax,(width >= 768px), is not read at all. - Only the top-level
@mediarules of each stylesheet are looked at. A media query inside@layer,@supportsor another@mediais not seen. - The CSS is read once, when the DOM is ready, so styles in the
<head>and the<body>are both seen, as long as an external stylesheet has loaded by then. Styles added later are not seen. To read them, calldestroy()and theninit(). - A stylesheet from another origin that the browser does not let the script read is skipped.
- Run only one. The
<script>build starts one itself, and a second one makes every event fire twice. destroy()removes the listeners and stops the events.init()starts it again.
See the generated Breakpoints class reference for every method.
Enhancements
Section titled “Enhancements”A checklist. An unticked item is not built, and not promised: it says what is missing today. A ticked item has been built.
- Sizes in JavaScript. Giving the breakpoints to
Breakpointsinstead of reading them from the CSS, which a stylesheet from another origin can block. - More queries. Only top-level
min-widthqueries are read, as pixels. Not read:em, the range syntax ((width >= 768px)),max-width, and queries inside@layer,@supportsor another@media. -
matchMedia. Letting the browser say which query matches, instead of comparing the window width. - Stylesheets added later. The CSS is read once. Watching for new stylesheets would keep it up to date.
- One instance. Two instances fire every event twice, and loading
breakpoints.jstogether withml.jsstarts two. - Asking. A
currentbreakpoint to read, and a way to ask whether the window is at least a size.