Skip to content

Instantly share code, notes, and snippets.

@akhoury
Created November 17, 2019 09:02
Show Gist options
  • Save akhoury/6192a4fdf32a46c511d906d8a507b47a to your computer and use it in GitHub Desktop.
Save akhoury/6192a4fdf32a46c511d906d8a507b47a to your computer and use it in GitHub Desktop.
/*
// from bootstrap/scss/_variables.scss
$grid-breakpoints: (
xs: 0,
sm: 576px,
md: 768px,
lg: 992px,
xl: 1200px
) !default;
*/
export const breakpoints = {
xs: 0,
sm: 576,
md: 768,
lg: 992,
xl: 1200,
}
export const orderedKeys = ['xs', 'sm', 'md', 'lg', 'xl']
export const getActiveBreakpoint = () => {
let breakpoint = ''
let classList = []
orderedKeys.some((k, i) => {
const n = orderedKeys[i + 1]
let query = ''
query += `(min-width: ${breakpoints[k]}px)`
if (n) {
query += `and (max-width: ${breakpoints[n] - 1}px)`
}
if (window.matchMedia(query).matches) {
breakpoint = k
classList = [...orderedKeys.slice(0, i).map(b => `gt-${b}`), k, ...orderedKeys.slice(i + 1, orderedKeys.length).map(b => `lt-${b}`)]
return true
}
return false
})
return { breakpoint, classList, className: classList.join(' ') }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment