-
Notifications
You must be signed in to change notification settings - Fork 0
getCurrentMediaQuery
Mike Byrne edited this page Aug 24, 2022
·
6 revisions
Returns the current media query in use by reading a CSS :root
variable.
Useful for running JS functions at certain breakpoints without holding breakpoint size information in CSS and JS.
- specific CSS
Set up :root
variables changing a --breakpoint
value at each of your breakpoints, with the name of your breakpoint. If using A17 Tailwind Plugins or A17 SCSS Utilities then this set up is likely done for you.
:root {
--breakpoint: "xs";
}
@media (min-width: 544px) {
:root {
--breakpoint: "sm";
}
}
@media (min-width: 650px) {
:root {
--breakpoint: "md";
}
}
@media (min-width: 990px) {
:root {
--breakpoint: "lg";
}
}
@media (min-width: 1300px) {
:root {
--breakpoint: "xl";
}
}
@media (min-width: 1520px) {
:root {
--breakpoint: "xxl";
}
}
- none
- media query string as a string
var mq = getCurrentMediaQuery(); // returns 'lg' (if viewport at lg size)
If you are using isBreakpoint
within a behavior JS file with A17 Behaviors - then you can use this.isBreakpoint('medium')
without importing/including this helper. Assuming you follow the A17 Behaviors Setup, especially passing in breakpoint info then isBreakpoint
within behaviors will know the application breakpoints without being specified.