|
| 1 | +(function () { |
| 2 | + const lucide = window.lucide; |
| 3 | + |
| 4 | + if (!lucide || !lucide.icons) return; |
| 5 | + |
| 6 | + const iconEntries = Object.keys(lucide.icons) |
| 7 | + .map(key => ({ |
| 8 | + key, |
| 9 | + value: key |
| 10 | + .replace(/([a-z0-9])([A-Z])/g, "$1-$2") |
| 11 | + .replace(/([A-Z])([A-Z][a-z])/g, "$1-$2") |
| 12 | + .toLowerCase(), |
| 13 | + })) |
| 14 | + .sort((left, right) => left.value.localeCompare(right.value)); |
| 15 | + |
| 16 | + const renderIcons = () => lucide.createIcons({ |
| 17 | + attrs: { |
| 18 | + width: 18, |
| 19 | + height: 18, |
| 20 | + "stroke-width": 1.75, |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
| 24 | + const getHiddenInput = root => |
| 25 | + root.closest("[id$='_FieldWrapper']")?.querySelector("[data-lucide-value]") ?? |
| 26 | + root.parentElement?.querySelector("[data-lucide-value]") ?? |
| 27 | + root.querySelector("[data-lucide-value]"); |
| 28 | + |
| 29 | + const updateSelection = (root, value) => { |
| 30 | + const hiddenInput = getHiddenInput(root); |
| 31 | + const preview = root.querySelector("[data-lucide-preview]"); |
| 32 | + |
| 33 | + if (hiddenInput) hiddenInput.value = value || ""; |
| 34 | + preview.innerHTML = value |
| 35 | + ? `<i data-lucide="${value}"></i>` |
| 36 | + : ""; |
| 37 | + |
| 38 | + root.querySelectorAll("[data-lucide-icon]").forEach(option => { |
| 39 | + const isActive = option.dataset.lucideIcon === value; |
| 40 | + option.classList.toggle("active", isActive); |
| 41 | + option.setAttribute("aria-selected", isActive ? "true" : "false"); |
| 42 | + }); |
| 43 | + |
| 44 | + renderIcons(); |
| 45 | + }; |
| 46 | + |
| 47 | + const filterOptions = root => { |
| 48 | + const search = root.querySelector("[data-lucide-search]"); |
| 49 | + const empty = root.querySelector("[data-lucide-empty]"); |
| 50 | + const query = search.value.trim().toLowerCase(); |
| 51 | + |
| 52 | + let visibleOptionCount = 0; |
| 53 | + |
| 54 | + root.querySelectorAll("[data-lucide-icon]").forEach(option => { |
| 55 | + const matches = !query || option.dataset.lucideIcon.includes(query); |
| 56 | + option.hidden = !matches; |
| 57 | + |
| 58 | + if (matches) visibleOptionCount++; |
| 59 | + }); |
| 60 | + |
| 61 | + empty.classList.toggle("d-none", visibleOptionCount > 0); |
| 62 | + }; |
| 63 | + |
| 64 | + const initializePicker = root => { |
| 65 | + if (root.dataset.lucideIconPickerInitialized === "true") return; |
| 66 | + root.dataset.lucideIconPickerInitialized = "true"; |
| 67 | + |
| 68 | + const grid = root.querySelector("[data-lucide-grid]"); |
| 69 | + const search = root.querySelector("[data-lucide-search]"); |
| 70 | + const clear = root.querySelector("[data-lucide-clear]"); |
| 71 | + const hiddenInput = getHiddenInput(root); |
| 72 | + |
| 73 | + const optionsMarkup = iconEntries.map(({ value }) => |
| 74 | + `<button type="button" class="lucide-icon-picker__option" role="option" ` + |
| 75 | + `data-lucide-icon="${value}" aria-label="${value}" aria-selected="false" title="${value}">` + |
| 76 | + `<span class="lucide-icon-picker__icon" aria-hidden="true"><i data-lucide="${value}"></i></span>` + |
| 77 | + `<span class="lucide-icon-picker__label">${value}</span>` + |
| 78 | + `</button>`).join(""); |
| 79 | + |
| 80 | + grid.innerHTML = optionsMarkup; |
| 81 | + |
| 82 | + grid.addEventListener("click", event => { |
| 83 | + const option = event.target.closest("[data-lucide-icon]"); |
| 84 | + if (!option) return; |
| 85 | + |
| 86 | + updateSelection(root, option.dataset.lucideIcon); |
| 87 | + }); |
| 88 | + |
| 89 | + search.addEventListener("input", () => filterOptions(root)); |
| 90 | + clear.addEventListener("click", () => { |
| 91 | + search.value = ""; |
| 92 | + updateSelection(root, ""); |
| 93 | + filterOptions(root); |
| 94 | + }); |
| 95 | + |
| 96 | + updateSelection(root, hiddenInput?.value); |
| 97 | + filterOptions(root); |
| 98 | + }; |
| 99 | + |
| 100 | + const initialize = () => { |
| 101 | + document.querySelectorAll("[data-lucide-icon-picker]").forEach(initializePicker); |
| 102 | + }; |
| 103 | + |
| 104 | + if (document.readyState === "loading") { |
| 105 | + document.addEventListener("DOMContentLoaded", initialize, { once: true }); |
| 106 | + } |
| 107 | + else { |
| 108 | + initialize(); |
| 109 | + } |
| 110 | +})(); |
0 commit comments