@wordpress/compose
Version:
WordPress higher-order components (HOCs).
45 lines (44 loc) • 1.34 kB
JavaScript
// packages/compose/src/hooks/use-viewport-match/index.js
import { createContext, useContext } from "@wordpress/element";
import useMediaQuery from "../use-media-query";
var BREAKPOINTS = {
xhuge: 1920,
huge: 1440,
wide: 1280,
xlarge: 1080,
large: 960,
medium: 782,
small: 600,
mobile: 480
};
var CONDITIONS = {
">=": "min-width",
"<": "max-width"
};
var OPERATOR_EVALUATORS = {
">=": (breakpointValue, width) => width >= breakpointValue,
"<": (breakpointValue, width) => width < breakpointValue
};
var ViewportMatchWidthContext = createContext(
/** @type {null | number} */
null
);
ViewportMatchWidthContext.displayName = "ViewportMatchWidthContext";
var useViewportMatch = (breakpoint, operator = ">=") => {
const simulatedWidth = useContext(ViewportMatchWidthContext);
const mediaQuery = !simulatedWidth && `(${CONDITIONS[operator]}: ${BREAKPOINTS[breakpoint]}px)`;
const mediaQueryResult = useMediaQuery(mediaQuery || void 0);
if (simulatedWidth) {
return OPERATOR_EVALUATORS[operator](
BREAKPOINTS[breakpoint],
simulatedWidth
);
}
return mediaQueryResult;
};
useViewportMatch.__experimentalWidthProvider = ViewportMatchWidthContext.Provider;
var use_viewport_match_default = useViewportMatch;
export {
use_viewport_match_default as default
};
//# sourceMappingURL=index.js.map