Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential compatibility with shadow DOM #2083

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/lucide/src/lucide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ import * as iconAndAliases from './iconsAndAliases';
* Replaces all elements with matching nameAttr with the defined icons
* @param {{ icons?: object, nameAttr?: string, attrs?: object }} options
*/
const createIcons = ({ icons = {}, nameAttr = 'data-lucide', attrs = {} } = {}) => {
const createIcons = ({ icons = {}, nameAttr = 'data-lucide', attrs = {}, rootElement = document } = {}) => {
if (!Object.values(icons).length) {
throw new Error(
"Please provide an icons object.\nIf you want to use all the icons you can import it like:\n `import { createIcons, icons } from 'lucide';\nlucide.createIcons({icons});`",
);
}

if (typeof document === 'undefined') {
if (typeof rootElement === 'undefined') {
throw new Error('`createIcons()` only works in a browser environment.');
}

const elementsToReplace = document.querySelectorAll(`[${nameAttr}]`);
const elementsToReplace = rootElement.querySelectorAll(`[${nameAttr}]`);
Array.from(elementsToReplace).forEach((element) =>
replaceElement(element, { nameAttr, icons, attrs }),
);

/** @todo: remove this block in v1.0 */
if (nameAttr === 'data-lucide') {
const deprecatedElements = document.querySelectorAll('[icon-name]');
const deprecatedElements = rootElement.querySelectorAll('[icon-name]');
if (deprecatedElements.length > 0) {
console.warn(
'[Lucide] Some icons were found with the now deprecated icon-name attribute. These will still be replaced for backwards compatibility, but will no longer be supported in v1.0 and you should switch to data-lucide',
Expand Down
Loading