Skip to content

Instantly share code, notes, and snippets.

@esedic
Created April 11, 2025 09:21
Show Gist options
  • Save esedic/13629af9efd7004e2fd12c93ea999365 to your computer and use it in GitHub Desktop.
Save esedic/13629af9efd7004e2fd12c93ea999365 to your computer and use it in GitHub Desktop.
Uikit Custom Component Example
(function (UIkit) {
if (!UIkit) {
throw new Error('UIkit not found. Make sure UIkit is loaded before this plugin.');
}
UIkit.component('floating-label', {
connected() {
this.initFloatingLabel();
},
methods: {
initFloatingLabel() {
const input = this.$el.querySelector("input, textarea");
if (!input) return;
const label = document.createElement("label");
label.classList.add("uk-form-label", "floating-label");
label.textContent = input.getAttribute("placeholder") || "Label";
input.removeAttribute("placeholder");
this.$el.prepend(label);
if (input.value) {
label.classList.add("active");
}
input.addEventListener("focus", () => label.classList.add("active"));
input.addEventListener("blur", () => {
if (!input.value) label.classList.remove("active");
});
}
}
});
})(window.UIkit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment