Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add update functions for currentBreakpoint and defaultBreakpoint #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions dist/vue-mq.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ function _defineProperty(obj, key, value) {
}

function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
}

function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];

return arr2;
} else {
return Array.from(arr);
}
}

function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}

function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function convertBreakpointsToMediaQueries(breakpoints) {
var keys = Object.keys(breakpoints);
var values = keys.map(function (key) {
Expand Down Expand Up @@ -83,8 +93,6 @@ function isArray(arg) {
}

// USAGE
// mq-layout(mq="lg")
// p I’m lg
var component = {
props: {
mq: {
Expand Down Expand Up @@ -126,8 +134,20 @@ var install = function install(Vue) {
var reactorComponent = new Vue({
data: function data() {
return {
defaultBreakpoint: defaultBreakpoint,
currentBreakpoint: defaultBreakpoint
};
},
methods: {
update: function update(breakpoint) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'currentBreakpoint';
var isTypeValid = Object.keys(this.$data).includes(type);
var isBreakPointValid = Object.keys(breakpoints).includes(breakpoint);

if (isTypeValid && isBreakPointValid) {
this.$data[type] = breakpoint;
}
}
}
});
Vue.filter('mq', function (currentBreakpoint, values) {
Expand All @@ -140,7 +160,7 @@ var install = function install(Vue) {
}
},
created: function created() {
if (this.$isServer) reactorComponent.currentBreakpoint = defaultBreakpoint;
if (this.$isServer) reactorComponent.currentBreakpoint = reactorComponent.defaultBreakpoint;
},
mounted: function mounted() {
if (!hasSetupListeners) {
Expand All @@ -165,6 +185,15 @@ var install = function install(Vue) {
}
});
Vue.prototype.$mqAvailableBreakpoints = breakpoints;

Vue.prototype.$mqUpdateCurrentBreakpoint = function (breakpoint) {
return reactorComponent.update(breakpoint, 'currentBreakpoint');
};

Vue.prototype.$mqUpdateDefaultBreakpoint = function (breakpoint) {
return reactorComponent.update(breakpoint, 'defaultBreakpoint');
};

Vue.component('MqLayout', component);
};

Expand Down
39 changes: 34 additions & 5 deletions dist/vue-mq.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@ function _defineProperty(obj, key, value) {
}

function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
}

function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];

return arr2;
} else {
return Array.from(arr);
}
}

function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}

function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function convertBreakpointsToMediaQueries(breakpoints) {
var keys = Object.keys(breakpoints);
var values = keys.map(function (key) {
Expand Down Expand Up @@ -79,8 +89,6 @@ function isArray(arg) {
}

// USAGE
// mq-layout(mq="lg")
// p I’m lg
var component = {
props: {
mq: {
Expand Down Expand Up @@ -122,8 +130,20 @@ var install = function install(Vue) {
var reactorComponent = new Vue({
data: function data() {
return {
defaultBreakpoint: defaultBreakpoint,
currentBreakpoint: defaultBreakpoint
};
},
methods: {
update: function update(breakpoint) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'currentBreakpoint';
var isTypeValid = Object.keys(this.$data).includes(type);
var isBreakPointValid = Object.keys(breakpoints).includes(breakpoint);

if (isTypeValid && isBreakPointValid) {
this.$data[type] = breakpoint;
}
}
}
});
Vue.filter('mq', function (currentBreakpoint, values) {
Expand All @@ -136,7 +156,7 @@ var install = function install(Vue) {
}
},
created: function created() {
if (this.$isServer) reactorComponent.currentBreakpoint = defaultBreakpoint;
if (this.$isServer) reactorComponent.currentBreakpoint = reactorComponent.defaultBreakpoint;
},
mounted: function mounted() {
if (!hasSetupListeners) {
Expand All @@ -161,6 +181,15 @@ var install = function install(Vue) {
}
});
Vue.prototype.$mqAvailableBreakpoints = breakpoints;

Vue.prototype.$mqUpdateCurrentBreakpoint = function (breakpoint) {
return reactorComponent.update(breakpoint, 'currentBreakpoint');
};

Vue.prototype.$mqUpdateDefaultBreakpoint = function (breakpoint) {
return reactorComponent.update(breakpoint, 'defaultBreakpoint');
};

Vue.component('MqLayout', component);
};

Expand Down
Loading