Created
October 23, 2019 11:44
-
-
Save innocenzi/d44e15ff26feb6e2b67cfce3bb97ede6 to your computer and use it in GitHub Desktop.
Breakpoint helper in TypeScript
This file contains hidden or 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
export enum Breakpoint { | |
sm = 640, | |
md = 768, | |
lg = 1024, | |
xl = 1080, | |
} |
This file contains hidden or 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
import _ from 'lodash'; | |
import { Breakpoint } from './Breakpoint'; | |
export { Breakpoint }; | |
/** | |
* A helper for using breakpoints. | |
* | |
* @class Breakpoint | |
*/ | |
export default class BreakpointHelper { | |
/** | |
* Array of breakpoints. | |
* | |
* @private | |
* @static | |
* @returns {Array<Number>} | |
* @memberof BreakpointHelper | |
*/ | |
static get breakpoints(): Array<Number> { | |
return Object.values(Breakpoint) | |
.filter(value => isNaN(Number(value)) === false) | |
.map(key => Number(key)); | |
} | |
/** | |
* Gets the width of the document. | |
*/ | |
static getWidth(): Number { | |
return window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth; | |
} | |
/** | |
* Gets the breakpoint above the current width. | |
* | |
* @static | |
* @param {Number} [width] | |
* @returns {(Breakpoint | null)} | |
* @memberof BreakpointHelper | |
*/ | |
static up(width?: Number): Breakpoint | null { | |
width = width || this.getWidth(); | |
let above: Number | null = _.find(this.breakpoints, (match: Number) => { | |
let below = this.breakpoints[this.breakpoints.indexOf(match) - 1]; | |
return width <= match && (width > below || !below); | |
}); | |
return above ? Breakpoint[(<Number>above).toString()] : null; | |
} | |
/** | |
* Gets the breakpoint below the current width. | |
* | |
* @static | |
* @param {Number} [width] | |
* @returns {(Breakpoint | null)} | |
* @memberof BreakpointHelper | |
*/ | |
static down(width?: Number): Breakpoint | null { | |
width = width || this.getWidth(); | |
let below: Number | null = _.find(this.breakpoints, (match: Number) => { | |
let above = this.breakpoints[this.breakpoints.indexOf(match) + 1]; | |
return width >= match && (width < above || !above); | |
}); | |
return below ? Breakpoint[(<Number>below).toString()] : null; | |
} | |
/** | |
* Checks if the current width is above the given breakpoint. | |
* | |
* @static | |
* @param {Breakpoint} breakpoint | |
* @param {Number} [width] | |
* @returns | |
* @memberof BreakpointHelper | |
*/ | |
static isAbove(breakpoint: Breakpoint, width?: Number) { | |
return (width || this.getWidth()) > breakpoint; | |
} | |
/** | |
* Checks if the current width is below the given breakpoint. | |
* | |
* @static | |
* @param {Breakpoint} breakpoint | |
* @param {Number} [width] | |
* @returns | |
* @memberof BreakpointHelper | |
*/ | |
static isBelow(breakpoint: Breakpoint, width?: Number) { | |
return (width || this.getWidth()) < breakpoint;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment