Skip to content

Instantly share code, notes, and snippets.

@jakerb
Last active February 12, 2018 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakerb/edef17a02698e88dd967fee747b2d513 to your computer and use it in GitHub Desktop.
Save jakerb/edef17a02698e88dd967fee747b2d513 to your computer and use it in GitHub Desktop.
A simple UIkit component for toggling classes at breakpoints.
/*
* UIkit Queries
* Requires jQuery.
* Built by @jakebown1
* -------------------------------
* UIkit.component('hello', __caller(<min-width>, <max-width>);
* -------------------------------
* <div uk-hello="my_class"></div>
*/
function __caller($minWidth, $maxWidth) {
return {
props: {
maxWidth: Number,
pageWidth: Number,
removeAttr: String
},
defaults: {
maxWidth: $maxWidth,
minWidth: $minWidth,
pageWidth: function() { return window.innerWidth; },
removeAttr: ['uk-xs', 'uk-sm', 'uk-md', 'uk-lg', 'uk-xl']
},
update: {
read: function read() {
this.classes = $(this.$el).attr(this.$name);
if(this.pageWidth() <= this.maxWidth && this.pageWidth() >= this.minWidth) {
for (var i = this.removeAttr.length - 1; i >= 0; i--) {
if(this.removeAttr[i] !== this.$name) {
$(this.$el).removeClass($(this.$el).attr(this.removeAttr[i]));
}
}
$(this.$el).addClass(this.classes);
}
},
events: ['load', 'resize']
}
};
};
/*
* Some Presets.
*/
UIkit.component('xs', __caller(0, 320));
UIkit.component('sm', __caller(321, 640));
UIkit.component('md', __caller(641, 768));
UIkit.component('lg', __caller(769, 960));
UIkit.component('xl', __caller(961, 10000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment