metaquery icon indicating copy to clipboard operation
metaquery copied to clipboard

Make currently-active breakpoints accessible on `metaQuery`

Open bensmithett opened this issue 10 years ago • 3 comments

... so you don't need to harvest them out of an onBreakpointChange handler - you can just query metaQuery.activeBreakpoints at any time to see what the current state of the world is.

My use case is checking for specific breakpoints in React lifecycle methods some time after an onBreakpointChange handler is added, but before the window actually changes size again.

bensmithett avatar May 14 '15 01:05 bensmithett

I like dis, but I'm not clear on the use case you mentioned. Got an example of a React component before/after this change?

geelen avatar May 14 '15 02:05 geelen

ping @bensmithett

benschwarz avatar May 15 '15 18:05 benschwarz

So I missed that calling onBreakpointChange sneaks in a synchronous call to mqChange.

Kinda tricky to see unless you look at the internals - event handlers don't usually get called unless the event really happened.

// Didn't expect this to work, the event handler *looks* async
const activeBreakpoints = "dontKnowYet";

metaQuery.onBreakpointChange((activeBreakpointsFromEvent) => {
  activeBreakpoints = activeBreakpointsFromEvent;
});

console.log(activeBreakpoints); // ["widescreen"]

Maybe it's clearer if the event handler isn't magically triggered the first time?

const activeBreakpoints = metaQuery.activeBreakpoints;

metaQuery.onBreakpointChange((activeBreakpointsFromEvent) => {
  activeBreakpoints = activeBreakpointsFromEvent;
});

I don't have an issue any more though, so feel free to close :smile:

bensmithett avatar May 15 '15 23:05 bensmithett