Last active
August 27, 2024 17:06
-
-
Save wallw-teal/aee4bc49ee3d0901ec8163e6fcdc5758 to your computer and use it in GitHub Desktop.
Simple WebComponent for using Lucide icons with HTMX
This file contains 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
import { LogIn } from "lucide"; | |
import replaceElement from "lucide/dist/esm/replaceElement.js"; | |
// this is the same as the typical lucide config ... | |
const config = { | |
icons: { | |
LogIn, | |
// any other icons you want to use here | |
}, | |
// ... except that this is required as we are skipping the function which | |
// sets its default value | |
nameAttr: "data-lucide", | |
}; | |
class LucideIcon extends HTMLElement { | |
connectedCallback() { | |
replaceElement(this, config); | |
} | |
} | |
customElements.define("lucide-icon", LucideIcon); | |
// use as <lucide-icon data-lucide="log-in" {...anyAttrs}></lucide-icon> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problems with base
lucide
package with a library like HTMX:createIcons()
after the DOM is available but before render (mine were kinda flashing in after initial render because I think I had it running on load, which prompted this setup instead)createIcons()
again when it swaps in content.Reasons I like it:
Potential Cons: