rss_feed

A Small Script for Responsiveness

2 June 2020

I recently wrote a small script using TypeScript that can be used to determine the size of the view port using the breakpoints as defined by Bootstrap. The script was written with the intention of using it with React in order to hide or show elements without using CSS so that the element never even shows up in the DOM.

// View port widths from Bootstrap
export const xs = 0;
export const sm = 576;
export const md = 768;
export const lg = 992;
export const xl = 1200;
export const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);

export const isSmDown = (): boolean => vw <= sm;
export const isMdDown = (): boolean => vw <= md;
export const isLgDown = (): boolean => vw <= lg;

export const isSmUp = (): boolean => vw >= sm;
export const isMdUp = (): boolean => vw >= md;
export const isLgUp = (): boolean => vw >= lg;

export const isXs = (): boolean => vw <= xs;
export const isSm = (): boolean => vw >= sm && vw < md;
export const isMd = (): boolean => vw >= md && vw < lg;
export const isLg = (): boolean => vw >= lg && vw < xl;
export const isXl = (): boolean => vw >= xl;

I don’t know why WordPress decided to escape the “&&” on lines 18-20 when I specifically chose a code block, but whatever.

The script is also available as a GitHub Gist.

How would you rate this post?

About the Author

Alex Seifert
Alex is a developer, a drummer and an amateur historian. He enjoys being on the stage in front of a large crowd, but also sitting in a room alone, programming something or writing about history.

Related Posts

Post a Comment

Your email is kept private. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>