-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from evanshunt/js-breakpoints
Js breakpoints
- Loading branch information
Showing
12 changed files
with
3,904 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/preset-env" | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules/ | ||
**/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
] | ||
}, | ||
}; |
Oops, something went wrong.