Open dotfrag opened 1 week ago
I had a quick look at the code. I am not experienced with JS, but I'll try to help:
It seems that lucide searches for elements using querySelectorAll
:
querySelectorAll
doesn't return <template>
elements because they are not real elements. However, you can explicitly query them and their content with the following:
// Test to see if the browser supports the HTML template element by checking
// for the presence of the template element's content attribute.
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template#examples
if ("content" in document.createElement("template")) {
document.querySelectorAll('template').forEach((template) =>
template.content.querySelectorAll('[data-lucide]').forEach((element) =>
console.log(element)
)
);
}
As I said, I'm not experienced enough to contribute with a PR. For anyone who encounters this, you might be able to solve it like it did:
Use pnpm patch and edit the code as below:
diff --git a/dist/esm/lucide.js b/dist/esm/lucide.js
index xxx..xxx 100644
--- a/dist/esm/lucide.js
+++ b/dist/esm/lucide.js
@@ -1562,6 +1562,15 @@ const createIcons = ({ icons = {}, nameAttr = "data-lucide", attrs = {} } = {})
Array.from(elementsToReplace).forEach(
(element) => replaceElement(element, { nameAttr, icons, attrs })
);
+ if ("content" in document.createElement("template")) {
+ const templateElements = document.querySelectorAll('template');
+ templateElements.forEach((template) => {
+ const templateContentElementsToReplace = template.content.querySelectorAll(`[${nameAttr}]`);
+ Array.from(templateContentElementsToReplace).forEach(
+ (element) => replaceElement(element, { nameAttr, icons, attrs })
+ );
+ })
+ }
if (nameAttr === "data-lucide") {
const deprecatedElements = document.querySelectorAll("[icon-name]");
if (deprecatedElements.length > 0) {
If you are using Node with npm, there's patch-package.
If you are using bun, there's bun patch, if you are using Deno I have no idea, sorry.
Package
Version
0.460.0
Can you reproduce this in the latest version?
Browser
Operating system
Description
I'm using the Content Template element (
<template>
) to render parts of an HTML form that I use as a template (duh) within JavaScript. Elements withdata-lucide
inside thetemplate
tag do not render.Steps to reproduce
Checklist