Created
April 11, 2025 09:21
-
-
Save esedic/13629af9efd7004e2fd12c93ea999365 to your computer and use it in GitHub Desktop.
Uikit Custom Component Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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