Skip to content

Commit

Permalink
Merge pull request #1 from evanshunt/js-breakpoints
Browse files Browse the repository at this point in the history
Js breakpoints
  • Loading branch information
davejtoews committed Aug 5, 2020
2 parents 1f70c3f + f44804d commit 61034db
Show file tree
Hide file tree
Showing 12 changed files with 3,904 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"presets": [
[
"@babel/preset-env"
]
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
**/.DS_Store
7 changes: 7 additions & 0 deletions breakpoints/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ $desktop-extra-large: 1920px !default;
$breakpointList: (
'large-phone': $large-phone,
'tablet': $tablet,
'desktop': $desktop,
'desktop-large': $desktop-large,
'desktop-extra-large': $desktop-extra-large
) !default;

:export {
@each $name, $width in $breakpointList {
#{$name}: #{$width};
}
}
1 change: 1 addition & 0 deletions dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion index.js

This file was deleted.

18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"name": "@evanshunt/derekstrap",
"version": "0.0.2",
"description": "An SCSS base layout and styles library by Evans Hunt",
"main": "index.js",
"version": "0.1.0",
"description": "A base layout and styles library by Evans Hunt",
"main": "dist/main.js",
"keywords": [
"scss",
"derek",
"evans",
"hunt"
],
"scripts": {
"build": "webpack --mode production"
},
"author": "Evans Hunt Tech Team (https://www.evanshunt.com)",
"license": "ISC",
"dependencies": {
Expand All @@ -26,5 +29,12 @@
"bugs": {
"url": "https://github.com/evanshunt/derekstrap/issues"
},
"homepage": "https://github.com/evanshunt/derekstrap#readme"
"homepage": "https://github.com/evanshunt/derekstrap#readme",
"devDependencies": {
"@babel/core": "^7.11.1",
"@babel/preset-env": "^7.11.0",
"babel-loader": "^8.1.0",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.12"
}
}
48 changes: 48 additions & 0 deletions src/Breakpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import debounce from "./debounce";

const Breakpoints = {
breakpoints: {},
noUnitBreakpoints: {},
currentBreakpoint: '',
init: function (breakpoints) {
this.breakpoints = breakpoints;
this.noUnitBreakpoints = Object.fromEntries(
Object.entries(this.breakpoints).map(
([key, value]) => [key, Number(value.replace(/\D/g, ''))]
)
);

this.currentBreakpoint = this.getCurrent();
window.addEventListener('resize', this.eventEmitter);
},
getCurrent: function () {
const width = window.innerWidth;
const breakpoint = Object.entries(this.noUnitBreakpoints).reduce((accumulator, entry) => {
if (accumulator && width < accumulator[1]) {
accumulator = null;
}

if (width >= entry[1] && accumulator && accumulator[1] < entry[1]) {
return entry;
}

return accumulator;
});

return breakpoint ? breakpoint[0] : null;
},
eventEmitter: debounce(() => {
const newBreakpoint = Breakpoints.getCurrent();
if (newBreakpoint !== Breakpoints.currentBreakpoint) {
window.dispatchEvent(new CustomEvent('breakpointChange', {
detail: {
breakpoint: newBreakpoint,
lastBreakpoint: Breakpoints.currentBreakpoint
}
}));
Breakpoints.currentBreakpoint = newBreakpoint;
}
}, 50),
};

export default Breakpoints;
22 changes: 22 additions & 0 deletions src/debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// https://davidwalsh.name/javascript-debounce-function

// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}

export default debounce;
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import debounce from "./debounce";
import setUserAgent from "./setUserAgent";
import Breakpoints from "./Breakpoints";

const Derekstrap = {
init: function () {
setUserAgent();
}
}

export {debounce, Derekstrap, Breakpoints};
5 changes: 5 additions & 0 deletions src/setUserAgent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function setUserAgent() {
document.documentElement.setAttribute('data-useragent', navigator.userAgent);
}

export default setUserAgent;
22 changes: 22 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('path');

module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js',
library: 'Derekstrap',
libraryTarget: 'umd'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
},
};
Loading

0 comments on commit 61034db

Please sign in to comment.