File size: 28,667 Bytes
1250018 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | {
"id": "sidebar-menu",
"name": "Sidebar Menu",
"description": "An elegant sidebar menu for complex Gradio apps like dashboards and AI tools, offering dynamic routing, collapsible groups, and full theme support built entirely with gr.HTML",
"author": "elismasilva",
"tags": [
"menu",
"sidebar",
"dashboard"
],
"html_template": "\n <div class=\"sidebar ${position} ${open ? 'open' : ''}\" style=\"width: ${width}px; ${position}: -${width}px;\">\n <button class=\"toggle-button\" aria-label=\"Toggle Sidebar\">\n <i data-lucide=\"chevron-right\" class=\"toggle-icon\"></i>\n </button>\n \n <div class=\"sidebar-content\">\n <div class=\"sidebar-wrapper\">\n ${menu_data.map(item => {\n if (item.type === 'group') {\n return `\n <div class=\"menu-group\">\n <div class=\"group-header\">\n <div class=\"header-left\">\n <i data-lucide=\"${item.icon}\" class=\"icon\" style=\"color: ${item.color || '#a9b1d6'};\"></i> \n <span class=\"label\">${item.label}</span>\n </div>\n <i data-lucide=\"chevron-down\" class=\"arrow icon\"></i>\n </div>\n <div class=\"group-children\">\n ${item.children.map(child => `\n <div class=\"menu-item ${value === child.id ? 'active' : ''}\" data-id=\"${child.id}\">\n <i data-lucide=\"${child.icon}\" class=\"icon\" style=\"color: ${child.color || '#a9b1d6'};\"></i>\n <span class=\"label\">${child.label}</span>\n </div>\n `).join('')}\n </div>\n </div>`;\n } else {\n return `\n <div class=\"menu-item ${value === item.id ? 'active' : ''}\" data-id=\"${item.id}\">\n <i data-lucide=\"${item.icon}\" class=\"icon\" style=\"color: ${item.color || '#a9b1d6'};\"></i>\n <span class=\"label\">${item.label}</span>\n </div>`;\n }\n }).join('')}\n </div>\n </div>\n </div>\n ",
"css_template": "\n .sidebar {\n display: flex;\n flex-direction: column;\n position: fixed;\n top: 0;\n height: 100%;\n background-color: var(--background-fill-secondary);\n transform: translateX(0%);\n z-index: 1000;\n transition: transform 0.3s ease-in-out;\n border-right: 1px solid var(--border-color-primary); \n color: var(--body-text-color);\n font-family: var(--font-sans-serif); \n }\n\n .sidebar.open.left {\n transform: translateX(100%);\n }\n\n .sidebar.open.right {\n transform: translateX(-100%);\n }\n\n .toggle-button {\n position: absolute;\n top: 20px;\n background: var(--background-fill-secondary); \n border: 1px solid var(--border-color-primary); \n cursor: pointer;\n padding: 8px;\n display: flex;\n align-items: center;\n justify-content: center;\n width: 28px;\n height: 32px;\n z-index: 1001;\n transition: all 0.3s ease;\n border-radius: 0; \n }\n .toggle-button * {\n pointer-events: none;\n }\n .toggle-button:hover {\n background: var(--background-fill-primary); \n border-color: var(--color-accent); \n }\n\n .toggle-icon {\n width: 20px;\n height: 20px;\n color: var(--body-text-color-subdued);\n transition: transform 0.3s ease-in-out;\n stroke-width: 2.5; \n }\n\n .sidebar.open .toggle-icon {\n transform: rotate(180deg);\n }\n\n .sidebar.left .toggle-button {\n left: 100%;\n border-radius: 0 8px 8px 0;\n }\n\n .sidebar.right .toggle-button {\n right: 100%;\n border-radius: 8px 0 0 8px;\n }\n\n .sidebar-content {\n height: 100%;\n overflow-y: auto;\n overflow-x: hidden;\n }\n\n .sidebar-content::-webkit-scrollbar {\n width: 6px;\n }\n\n .sidebar-content::-webkit-scrollbar-thumb {\n background: var(--border-color-primary);\n border-radius: 10px;\n }\n\n .sidebar-wrapper {\n font-family: 'Inter', sans-serif;\n padding: 20px 15px;\n }\n\n .menu-item {\n display: flex;\n align-items: center;\n padding: 12px 15px;\n margin-bottom: 4px;\n border-radius: 8px;\n cursor: pointer;\n font-size: 14px;\n font-weight: 500;\n transition: all 0.2s ease;\n }\n\n .menu-item:hover {\n background-color: var(--background-fill-primary);\n color: var(--body-text-color)\n }\n\n .menu-item.active {\n background-color: var(--color-accent) !important;\n color: white !important;\n }\n\n .group-header {\n display: flex;\n justify-content: space-between;\n align-items: center;\n padding: 12px 15px;\n margin-bottom: 4px;\n border-radius: 8px;\n cursor: pointer;\n font-size: 14px;\n font-weight: 600;\n color: var(--body-text-color);\n ;\n transition: background-color 0.2s;\n }\n\n .group-header:hover {\n background-color: var(--background-fill-primary);\n }\n\n .header-left {\n display: flex;\n align-items: center;\n }\n\n .group-children {\n display: none;\n padding-left: 15px;\n margin-top: 4px;\n }\n\n .icon {\n margin-right: 12px;\n width: 18px;\n height: 18px;\n stroke: currentColor;\n stroke-width: 2.2;\n color: inherit;\n }\n\n .toggle-icon {\n width: 20px;\n height: 20px;\n color: var(--body-text-color-subdued);\n transition: transform 0.3s ease-in-out;\n stroke-width: 2.5;\n }\n\n .arrow {\n width: 14px;\n height: 14px;\n transition: transform 0.3s ease;\n color: var(--body-text-color-subdued);\n margin-right: 0;\n }\n\n @media (max-width: 768px) {\n .sidebar {\n width: 100vw !important;\n }\n\n .sidebar.left {\n left: -100vw !important;\n }\n\n .sidebar.right {\n right: -100vw !important;\n }\n }\n ",
"js_on_load": "\n let openFolders = new Set();\n\n function initLucide(retries = 8, delay = 200) {\n if (window.lucide) {\n window.lucide.createIcons();\n element.querySelectorAll('i[data-lucide]').forEach(i => {\n const svg = i.querySelector('svg');\n if (svg) {\n let desiredColor = i.style.color || getComputedStyle(i).color;\n if (desiredColor && \n desiredColor !== 'rgb(0,0,0)' && desiredColor !== '#000000' &&\n desiredColor !== 'rgb(255,255,255)' && desiredColor !== '#ffffff') {\n svg.style.color = desiredColor;\n const shapes = svg.querySelectorAll('path, line, polyline, circle, rect, polygon');\n shapes.forEach(shape => {\n shape.setAttribute('stroke', desiredColor);\n shape.style.stroke = desiredColor + ' !important';\n });\n if (svg.hasAttribute('fill') && svg.getAttribute('fill') === 'currentColor') {\n svg.setAttribute('fill', desiredColor);\n }\n svg.querySelectorAll('[fill=\"currentColor\"]').forEach(el => {\n el.setAttribute('fill', desiredColor);\n });\n }\n }\n });\n } else if (retries > 0) {\n setTimeout(() => initLucide(retries - 1, delay), delay);\n }\n }\n\n function applyFolderStates() {\n element.querySelectorAll('.group-header').forEach(header => {\n const label = header.querySelector('.label')?.innerText.trim();\n if (!label) return;\n const children = header.nextElementSibling;\n const arrow = header.querySelector('.arrow');\n if (children && arrow) {\n const isOpen = openFolders.has(label);\n children.style.display = isOpen ? 'block' : 'none';\n arrow.style.transform = isOpen ? 'rotate(180deg)' : 'rotate(0deg)';\n }\n });\n }\n \n initLucide();\n applyFolderStates();\n\n const observer = new MutationObserver(() => {\n setTimeout(() => {\n applyFolderStates();\n initLucide();\n }, 50);\n });\n\n observer.observe(element, {\n childList: true,\n subtree: true,\n attributes: true,\n characterData: true\n });\n\n element.addEventListener('click', (e) => {\n if (e.target.closest('.toggle-button')) {\n const sidebar = element.querySelector('.sidebar');\n sidebar.classList.toggle('open');\n trigger(sidebar.classList.contains('open') ? 'expand' : 'collapse');\n return;\n }\n\n const header = e.target.closest('.group-header');\n if (header) {\n const label = header.querySelector('.label')?.innerText.trim();\n if (label) {\n if (openFolders.has(label)) openFolders.delete(label);\n else openFolders.add(label);\n }\n applyFolderStates();\n return;\n }\n\n const item = e.target.closest('.menu-item');\n if (item) {\n props.value = item.dataset.id;\n trigger('change');\n\n if (window.innerWidth <= 768) {\n element.querySelector('.sidebar')?.classList.remove('open');\n }\n\n setTimeout(() => {\n applyFolderStates();\n initLucide();\n }, 0);\n }\n });\n ",
"default_props": {
"value": "dash",
"menu_data": [
{
"type": "item",
"id": "dash",
"label": "Dashboard",
"icon": "layout-dashboard",
"color": "#10b981"
},
{
"type": "item",
"id": "gallery",
"label": "Community Gallery",
"icon": "image",
"color": "#8b5cf6"
},
{
"type": "item",
"id": "my_images",
"label": "My Images",
"icon": "images",
"color": "#f59e0b"
},
{
"type": "item",
"id": "my_models",
"label": "My Custom Models",
"icon": "palette",
"color": "#10b981"
},
{
"type": "group",
"label": "Create",
"icon": "folder",
"color": "#f43f5e",
"children": [
{
"id": "create_new",
"label": "New Project",
"icon": "plus-circle",
"color": "#8f95b2"
}
]
},
{
"type": "group",
"label": "Enhance",
"icon": "folder",
"color": "#f43f5e",
"children": [
{
"id": "upscale",
"label": "Upscale Image",
"icon": "zoom-in",
"color": "#8f95b2"
}
]
},
{
"type": "group",
"label": "Analyze",
"icon": "folder",
"color": "#f43f5e",
"children": [
{
"id": "data_analysis",
"label": "Data Analysis",
"icon": "bar-chart",
"color": "#8f95b2"
}
]
},
{
"type": "group",
"label": "Train",
"icon": "folder",
"color": "#f43f5e",
"children": [
{
"id": "flux_train",
"label": "FLUX Model Trainer",
"icon": "dumbbell",
"color": "#3b82f6"
},
{
"id": "flux_creator",
"label": "Flux Image Creator",
"icon": "pen-tool",
"color": "#10b981"
},
{
"id": "sdxl_train",
"label": "SDXL Model Trainer",
"icon": "dumbbell",
"color": "#6366f1"
},
{
"id": "sdxl_creator",
"label": "SDXL Image Creator",
"icon": "brush",
"color": "#a855f7"
}
]
},
{
"type": "group",
"label": "Experiment",
"icon": "folder",
"color": "#f43f5e",
"children": [
{
"id": "labs",
"label": "Labs",
"icon": "flask-conical",
"color": "#8f95b2"
}
]
}
],
"open": true,
"position": "left",
"width": 300
},
"python_code": "class SidebarMenu(gr.HTML): \n def __init__(\n self, menu_data, value=None, open=True, position=\"right\", width=300, **kwargs\n ):\n\n html_template = \"\"\"\n <div class=\"sidebar ${position} ${open ? 'open' : ''}\" style=\"width: ${width}px; ${position}: -${width}px;\">\n <button class=\"toggle-button\" aria-label=\"Toggle Sidebar\">\n <i data-lucide=\"chevron-right\" class=\"toggle-icon\"></i>\n </button>\n\n <div class=\"sidebar-content\">\n <div class=\"sidebar-wrapper\">\n ${menu_data.map(item => {\n if (item.type === 'group') {\n return `\n <div class=\"menu-group\">\n <div class=\"group-header\">\n <div class=\"header-left\">\n <i data-lucide=\"${item.icon}\" class=\"icon\" style=\"color: ${item.color || '#a9b1d6'};\"></i> \n <span class=\"label\">${item.label}</span>\n </div>\n <i data-lucide=\"chevron-down\" class=\"arrow icon\"></i>\n </div>\n <div class=\"group-children\">\n ${item.children.map(child => `\n <div class=\"menu-item ${value === child.id ? 'active' : ''}\" data-id=\"${child.id}\">\n <i data-lucide=\"${child.icon}\" class=\"icon\" style=\"color: ${child.color || '#a9b1d6'};\"></i>\n <span class=\"label\">${child.label}</span>\n </div>\n `).join('')}\n </div>\n </div>`;\n } else {\n return `\n <div class=\"menu-item ${value === item.id ? 'active' : ''}\" data-id=\"${item.id}\">\n <i data-lucide=\"${item.icon}\" class=\"icon\" style=\"color: ${item.color || '#a9b1d6'};\"></i>\n <span class=\"label\">${item.label}</span>\n </div>`;\n }\n }).join('')}\n </div>\n </div>\n </div>\n \"\"\"\n\n css_template = \"\"\"\n .sidebar {\n display: flex;\n flex-direction: column;\n position: fixed;\n top: 0;\n height: 100%;\n background-color: var(--background-fill-secondary);\n transform: translateX(0%);\n z-index: 1000;\n transition: transform 0.3s ease-in-out;\n border-right: 1px solid var(--border-color-primary); \n color: var(--body-text-color);\n font-family: var(--font-sans-serif); \n }\n\n .sidebar.open.left {\n transform: translateX(100%);\n }\n\n .sidebar.open.right {\n transform: translateX(-100%);\n }\n\n .toggle-button {\n position: absolute;\n top: 20px;\n background: var(--background-fill-secondary); \n border: 1px solid var(--border-color-primary); \n cursor: pointer;\n padding: 8px;\n display: flex;\n align-items: center;\n justify-content: center;\n width: 28px;\n height: 32px;\n z-index: 1001;\n transition: all 0.3s ease;\n border-radius: 0; \n }\n .toggle-button * {\n pointer-events: none;\n }\n .toggle-button:hover {\n background: var(--background-fill-primary); \n border-color: var(--color-accent); \n }\n\n .toggle-icon {\n width: 20px;\n height: 20px;\n color: var(--body-text-color-subdued);\n transition: transform 0.3s ease-in-out;\n stroke-width: 2.5; \n }\n\n .sidebar.open .toggle-icon {\n transform: rotate(180deg);\n }\n\n .sidebar.left .toggle-button {\n left: 100%;\n border-radius: 0 8px 8px 0;\n }\n\n .sidebar.right .toggle-button {\n right: 100%;\n border-radius: 8px 0 0 8px;\n }\n\n .sidebar-content {\n height: 100%;\n overflow-y: auto;\n overflow-x: hidden;\n }\n\n .sidebar-content::-webkit-scrollbar {\n width: 6px;\n }\n\n .sidebar-content::-webkit-scrollbar-thumb {\n background: var(--border-color-primary);\n border-radius: 10px;\n }\n\n .sidebar-wrapper {\n font-family: 'Inter', sans-serif;\n padding: 20px 15px;\n }\n\n .menu-item {\n display: flex;\n align-items: center;\n padding: 12px 15px;\n margin-bottom: 4px;\n border-radius: 8px;\n cursor: pointer;\n font-size: 14px;\n font-weight: 500;\n transition: all 0.2s ease;\n }\n\n .menu-item:hover {\n background-color: var(--background-fill-primary);\n color: var(--body-text-color)\n }\n\n .menu-item.active {\n background-color: var(--color-accent) !important;\n color: white !important;\n }\n\n .group-header {\n display: flex;\n justify-content: space-between;\n align-items: center;\n padding: 12px 15px;\n margin-bottom: 4px;\n border-radius: 8px;\n cursor: pointer;\n font-size: 14px;\n font-weight: 600;\n color: var(--body-text-color);\n ;\n transition: background-color 0.2s;\n }\n\n .group-header:hover {\n background-color: var(--background-fill-primary);\n }\n\n .header-left {\n display: flex;\n align-items: center;\n }\n\n .group-children {\n display: none;\n padding-left: 15px;\n margin-top: 4px;\n }\n\n .icon {\n margin-right: 12px;\n width: 18px;\n height: 18px;\n stroke: currentColor;\n stroke-width: 2.2;\n color: inherit;\n }\n\n .toggle-icon {\n width: 20px;\n height: 20px;\n color: var(--body-text-color-subdued);\n transition: transform 0.3s ease-in-out;\n stroke-width: 2.5;\n }\n\n .arrow {\n width: 14px;\n height: 14px;\n transition: transform 0.3s ease;\n color: var(--body-text-color-subdued);\n margin-right: 0;\n }\n\n @media (max-width: 768px) {\n .sidebar {\n width: 100vw !important;\n }\n\n .sidebar.left {\n left: -100vw !important;\n }\n\n .sidebar.right {\n right: -100vw !important;\n }\n }\n \"\"\"\n\n js_on_load = \"\"\"\n let openFolders = new Set();\n\n function initLucide(retries = 8, delay = 200) {\n if (window.lucide) {\n window.lucide.createIcons();\n element.querySelectorAll('i[data-lucide]').forEach(i => {\n const svg = i.querySelector('svg');\n if (svg) {\n let desiredColor = i.style.color || getComputedStyle(i).color;\n if (desiredColor && \n desiredColor !== 'rgb(0,0,0)' && desiredColor !== '#000000' &&\n desiredColor !== 'rgb(255,255,255)' && desiredColor !== '#ffffff') {\n svg.style.color = desiredColor;\n const shapes = svg.querySelectorAll('path, line, polyline, circle, rect, polygon');\n shapes.forEach(shape => {\n shape.setAttribute('stroke', desiredColor);\n shape.style.stroke = desiredColor + ' !important';\n });\n if (svg.hasAttribute('fill') && svg.getAttribute('fill') === 'currentColor') {\n svg.setAttribute('fill', desiredColor);\n }\n svg.querySelectorAll('[fill=\"currentColor\"]').forEach(el => {\n el.setAttribute('fill', desiredColor);\n });\n }\n }\n });\n } else if (retries > 0) {\n setTimeout(() => initLucide(retries - 1, delay), delay);\n }\n }\n\n function applyFolderStates() {\n element.querySelectorAll('.group-header').forEach(header => {\n const label = header.querySelector('.label')?.innerText.trim();\n if (!label) return;\n const children = header.nextElementSibling;\n const arrow = header.querySelector('.arrow');\n if (children && arrow) {\n const isOpen = openFolders.has(label);\n children.style.display = isOpen ? 'block' : 'none';\n arrow.style.transform = isOpen ? 'rotate(180deg)' : 'rotate(0deg)';\n }\n });\n }\n\n initLucide();\n applyFolderStates();\n\n const observer = new MutationObserver(() => {\n setTimeout(() => {\n applyFolderStates();\n initLucide();\n }, 50);\n });\n\n observer.observe(element, {\n childList: true,\n subtree: true,\n attributes: true,\n characterData: true\n });\n\n element.addEventListener('click', (e) => {\n if (e.target.closest('.toggle-button')) {\n const sidebar = element.querySelector('.sidebar');\n sidebar.classList.toggle('open');\n trigger(sidebar.classList.contains('open') ? 'expand' : 'collapse');\n return;\n }\n\n const header = e.target.closest('.group-header');\n if (header) {\n const label = header.querySelector('.label')?.innerText.trim();\n if (label) {\n if (openFolders.has(label)) openFolders.delete(label);\n else openFolders.add(label);\n }\n applyFolderStates();\n return;\n }\n\n const item = e.target.closest('.menu-item');\n if (item) {\n props.value = item.dataset.id;\n trigger('change');\n\n if (window.innerWidth <= 768) {\n element.querySelector('.sidebar')?.classList.remove('open');\n }\n\n setTimeout(() => {\n applyFolderStates();\n initLucide();\n }, 0);\n }\n });\n \"\"\"\n\n super().__init__(\n value=value,\n menu_data=menu_data,\n open=open,\n position=position,\n width=width,\n html_template=html_template,\n css_template=css_template,\n apply_default_css=False,\n js_on_load=js_on_load,\n **kwargs\n )\n\n def api_info(self):\n return {\"type\": \"string\"}\n",
"head": "<script src=\"https://unpkg.com/lucide@latest\"></script>",
"repo_url": "https://huggingface.co/spaces/elismasilva/gradio_sidebar_menu"
} |