Skip to content

Instantly share code, notes, and snippets.

@klickreflex
Created June 13, 2016 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klickreflex/35013f1282fa998eb2c6103104ec5f2e to your computer and use it in GitHub Desktop.
Save klickreflex/35013f1282fa998eb2c6103104ec5f2e to your computer and use it in GitHub Desktop.
// Get Breakpoints from CSS and store in `breakpoint` object
var breakpoint = {};
breakpoint.refreshValue = function () {
this.value = window.getComputedStyle(document.querySelector('head'), ':before').getPropertyValue('content').replace(/\"/g, '');
};
// Put it inside a function
var currentBreakFn = function() {
breakpoint.refreshValue();
};
// Get the currently active breakpoint intially
currentBreakFn();
// Get the updated values on resize but use lodash's debounce for better performance
window.addEventListener('resize', _.debounce(currentBreakFn, 250));
// Example Breakpoint Checking
/*
if (breakpoint.value == 'break-1') {
console.log('breakpoint break-1');
}
else if (breakpoint.value == 'break-2') {
console.log('breakpoint break-2');
}
else if (breakpoint.value == 'break-3') {
console.log('breakpoint break-3');
}
else if (breakpoint.value == 'break-4') {
console.log('breakpoint break-4');
}
else {
console.log('Some other breakpoint');
}
*/
// using <head> to inject content on the pseudo elelemnt
// because the body's ::before is already occupied with some other styling
head {
&::before {
content: 'break-0';
display: none;
@include breakpoint(break-1) {
content: 'break-1';
}
@include breakpoint(break-2) {
content: 'break-2';
}
@include breakpoint(break-3) {
content: 'break-3';
}
@include breakpoint(break-4) {
content: 'break-4';
}
}
}
@klickreflex
Copy link
Author

Makes Breakpoints that are defined in CSS/SCSS available in JavaScript without the need to re-declare them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment