vuetify
Version:
Vue.js 2 Semantic Component Framework
153 lines (134 loc) • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* A modified version of https://gist.github.com/cb109/b074a65f7595cffc21cea59ce8d15f9b
*/
/**
* A Vue mixin to get the current width/height and the associated breakpoint.
*
* Useful to e.g. adapt the user interface from inside a Vue component
* as opposed to using CSS classes. The breakpoint pixel values and
* range names are taken from Vuetify (https://github.com/vuetifyjs).
*
* Use within a component:
*
* import breakpoint from './breakpoint.js'
*
* export default {
* name: 'my-component',
* mixins: [breakpoint],
* ...
*
* Then inside a template:
*
* <div v-if="$breakpoint.smAndDown">...</div>
*/
var breakpoint = {
data: function data() {
return {
clientWidth: clientDimensions.getWidth(),
clientHeight: clientDimensions.getHeight(),
resizeTimeout: null
};
},
computed: {
breakpoint: function breakpoint() {
var xs = this.clientWidth < 600;
var sm = this.clientWidth < 960 && !xs;
var md = this.clientWidth < 1280 - 16 && !(sm || xs);
var lg = this.clientWidth < 1920 - 16 && !(md || sm || xs);
var xl = this.clientWidth >= 1920 - 16 && !(lg || md || sm || xs);
var xsOnly = xs;
var smOnly = sm;
var smAndDown = (xs || sm) && !(md || lg || xl);
var smAndUp = !xs && (sm || md || lg || xl);
var mdOnly = md;
var mdAndDown = (xs || sm || md) && !(lg || xl);
var mdAndUp = !(xs || sm) && (md || lg || xl);
var lgOnly = lg;
var lgAndDown = (xs || sm || md || lg) && !xl;
var lgAndUp = !(xs || sm || md) && (lg || xl);
var xlOnly = xl;
var name = void 0;
switch (true) {
case xs:
name = 'xs';
break;
case sm:
name = 'sm';
break;
case md:
name = 'md';
break;
case lg:
name = 'lg';
break;
default:
name = 'xl';
break;
}
var result = {
// Definite breakpoint.
xs: xs,
sm: sm,
md: md,
lg: lg,
xl: xl,
// Useful e.g. to construct CSS class names dynamically.
name: name,
// Breakpoint ranges.
xsOnly: xsOnly,
smOnly: smOnly,
smAndDown: smAndDown,
smAndUp: smAndUp,
mdOnly: mdOnly,
mdAndDown: mdAndDown,
mdAndUp: mdAndUp,
lgOnly: lgOnly,
lgAndDown: lgAndDown,
lgAndUp: lgAndUp,
xlOnly: xlOnly,
// For custom breakpoint logic.
width: this.clientWidth,
height: this.clientHeight
};
return result;
}
},
watch: {
breakpoint: function breakpoint(val) {
this.$vuetify.breakpoint = val;
}
},
created: function created() {
this.$vuetify.breakpoint = this.breakpoint;
},
methods: {
onResize: function onResize() {
var _this = this;
clearTimeout(this.resizeTimeout);
// Added debounce to match what
// v-resize used to do but was
// removed due to a memory leak
// https://github.com/vuetifyjs/vuetify/pull/2997
this.resizeTimeout = setTimeout(function () {
_this.clientWidth = clientDimensions.getWidth();
_this.clientHeight = clientDimensions.getHeight();
}, 200);
}
}
// Cross-browser support as described in:
// https://stackoverflow.com/questions/1248081
};var clientDimensions = {
getWidth: function getWidth() {
if (typeof document === 'undefined') return 0; // SSR
return Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
},
getHeight: function getHeight() {
if (typeof document === 'undefined') return 0; // SSR
return Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
}
};
exports.default = breakpoint;