|
| 1 | +<!-- |
| 2 | + - Open Bank Project - API Explorer II |
| 3 | + - Copyright (C) 2023-2024, TESOBE GmbH |
| 4 | + - |
| 5 | + - This program is free software: you can redistribute it and/or modify |
| 6 | + - it under the terms of the GNU Affero General Public License as published by |
| 7 | + - the Free Software Foundation, either version 3 of the License, or |
| 8 | + - (at your option) any later version. |
| 9 | + - |
| 10 | + - This program is distributed in the hope that it will be useful, |
| 11 | + - but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + - GNU Affero General Public License for more details. |
| 14 | + - |
| 15 | + - You should have received a copy of the GNU Affero General Public License |
| 16 | + - along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + - |
| 18 | + - Email: contact@tesobe.com |
| 19 | + - TESOBE GmbH |
| 20 | + - Osloerstrasse 16/17 |
| 21 | + - Berlin 13359, Germany |
| 22 | + - |
| 23 | + - This product includes software developed at |
| 24 | + - TESOBE (http://www.tesobe.com/) |
| 25 | + - |
| 26 | + --> |
| 27 | + |
| 28 | +<script lang="ts"> |
| 29 | + interface Props { |
| 30 | + label?: string |
| 31 | + items?: string[] |
| 32 | + hoverColor?: string |
| 33 | + backgroundColor?: string |
| 34 | + } |
| 35 | +
|
| 36 | + let { |
| 37 | + label = 'Dropdown', |
| 38 | + items = [], |
| 39 | + hoverColor = '#32b9ce', |
| 40 | + backgroundColor = '#e8f4f8' |
| 41 | + }: Props = $props() |
| 42 | +
|
| 43 | + let isOpen = $state(false) |
| 44 | + let dropdownRef = $state<HTMLDivElement>() |
| 45 | + let timeoutId: number | null = null |
| 46 | +
|
| 47 | + function handleMouseEnter() { |
| 48 | + if (timeoutId) { |
| 49 | + clearTimeout(timeoutId) |
| 50 | + timeoutId = null |
| 51 | + } |
| 52 | + isOpen = true |
| 53 | + } |
| 54 | +
|
| 55 | + function handleMouseLeave() { |
| 56 | + timeoutId = window.setTimeout(() => { |
| 57 | + isOpen = false |
| 58 | + }, 1000) |
| 59 | + } |
| 60 | +
|
| 61 | + function handleSelect(item: string) { |
| 62 | + const event = new CustomEvent('select', { |
| 63 | + detail: item, |
| 64 | + bubbles: true, |
| 65 | + composed: true |
| 66 | + }) |
| 67 | + dropdownRef?.dispatchEvent(event) |
| 68 | + isOpen = false |
| 69 | + } |
| 70 | +
|
| 71 | + function handleClickOutside(event: MouseEvent) { |
| 72 | + if (dropdownRef && !dropdownRef.contains(event.target as Node)) { |
| 73 | + isOpen = false |
| 74 | + } |
| 75 | + } |
| 76 | +
|
| 77 | + function handleEscape(event: KeyboardEvent) { |
| 78 | + if (event.key === 'Escape') { |
| 79 | + isOpen = false |
| 80 | + } |
| 81 | + } |
| 82 | +
|
| 83 | + $effect(() => { |
| 84 | + if (isOpen) { |
| 85 | + document.addEventListener('click', handleClickOutside) |
| 86 | + document.addEventListener('keydown', handleEscape) |
| 87 | + return () => { |
| 88 | + document.removeEventListener('click', handleClickOutside) |
| 89 | + document.removeEventListener('keydown', handleEscape) |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | +</script> |
| 94 | + |
| 95 | +<div |
| 96 | + bind:this={dropdownRef} |
| 97 | + class="dropdown-container" |
| 98 | + style:--hover-bg={backgroundColor} |
| 99 | + style:--hover-color={hoverColor} |
| 100 | + onmouseenter={handleMouseEnter} |
| 101 | + onmouseleave={handleMouseLeave} |
| 102 | + role="navigation" |
| 103 | +> |
| 104 | + <button |
| 105 | + class="dropdown-trigger" |
| 106 | + onclick={() => isOpen = !isOpen} |
| 107 | + aria-expanded={isOpen} |
| 108 | + aria-haspopup="true" |
| 109 | + > |
| 110 | + {label} |
| 111 | + <svg |
| 112 | + class="arrow-icon" |
| 113 | + class:rotated={isOpen} |
| 114 | + viewBox="0 0 1024 1024" |
| 115 | + xmlns="http://www.w3.org/2000/svg" |
| 116 | + > |
| 117 | + <path fill="currentColor" d="M831.872 340.864 512 652.672 192.128 340.864a30.592 30.592 0 0 0-42.752 0 29.12 29.12 0 0 0 0 41.6L489.664 714.24a32 32 0 0 0 44.672 0l340.288-331.712a29.12 29.12 0 0 0 0-41.728 30.592 30.592 0 0 0-42.752 0z"></path> |
| 118 | + </svg> |
| 119 | + </button> |
| 120 | + |
| 121 | + {#if isOpen} |
| 122 | + <div class="dropdown-menu"> |
| 123 | + <div class="dropdown-content"> |
| 124 | + {#each items as item} |
| 125 | + <button |
| 126 | + class="dropdown-item" |
| 127 | + onclick={() => handleSelect(item)} |
| 128 | + > |
| 129 | + {item} |
| 130 | + </button> |
| 131 | + {/each} |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + {/if} |
| 135 | +</div> |
| 136 | + |
| 137 | +<style> |
| 138 | + .dropdown-container { |
| 139 | + position: relative; |
| 140 | + display: inline-block; |
| 141 | + } |
| 142 | +
|
| 143 | + .dropdown-trigger { |
| 144 | + padding: 9px; |
| 145 | + margin: 3px; |
| 146 | + color: #39455f; |
| 147 | + font-family: 'Roboto', sans-serif; |
| 148 | + font-size: 14px; |
| 149 | + text-decoration: none; |
| 150 | + border-radius: 8px; |
| 151 | + background: transparent; |
| 152 | + border: none; |
| 153 | + cursor: pointer; |
| 154 | + display: inline-flex; |
| 155 | + align-items: center; |
| 156 | + gap: 4px; |
| 157 | + transition: all 0.2s ease; |
| 158 | + } |
| 159 | +
|
| 160 | + .dropdown-trigger:hover { |
| 161 | + background-color: var(--hover-bg) !important; |
| 162 | + color: var(--hover-color) !important; |
| 163 | + } |
| 164 | +
|
| 165 | + .arrow-icon { |
| 166 | + width: 14px; |
| 167 | + height: 14px; |
| 168 | + transition: transform 0.3s ease; |
| 169 | + } |
| 170 | +
|
| 171 | + .arrow-icon.rotated { |
| 172 | + transform: rotate(180deg); |
| 173 | + } |
| 174 | +
|
| 175 | + .dropdown-menu { |
| 176 | + position: absolute; |
| 177 | + top: 100%; |
| 178 | + right: 0; |
| 179 | + margin-top: 4px; |
| 180 | + background: white; |
| 181 | + border: 1px solid #e4e7ed; |
| 182 | + border-radius: 8px; |
| 183 | + box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); |
| 184 | + z-index: 2000; |
| 185 | + min-width: 180px; |
| 186 | + max-width: 280px; |
| 187 | + animation: fadeIn 0.2s ease; |
| 188 | + } |
| 189 | +
|
| 190 | + @keyframes fadeIn { |
| 191 | + from { |
| 192 | + opacity: 0; |
| 193 | + transform: translateY(-8px); |
| 194 | + } |
| 195 | + to { |
| 196 | + opacity: 1; |
| 197 | + transform: translateY(0); |
| 198 | + } |
| 199 | + } |
| 200 | +
|
| 201 | + .dropdown-content { |
| 202 | + max-height: 400px; |
| 203 | + overflow-y: auto; |
| 204 | + padding: 6px 0; |
| 205 | + } |
| 206 | +
|
| 207 | + .dropdown-item { |
| 208 | + width: 100%; |
| 209 | + padding: 10px 20px; |
| 210 | + margin: 0; |
| 211 | + color: #606266; |
| 212 | + font-family: 'Roboto', sans-serif; |
| 213 | + font-size: 14px; |
| 214 | + text-align: left; |
| 215 | + background: transparent; |
| 216 | + border: none; |
| 217 | + cursor: pointer; |
| 218 | + transition: all 0.2s ease; |
| 219 | + white-space: nowrap; |
| 220 | + overflow: hidden; |
| 221 | + text-overflow: ellipsis; |
| 222 | + } |
| 223 | +
|
| 224 | + .dropdown-item:hover { |
| 225 | + background-color: var(--hover-bg); |
| 226 | + color: var(--hover-color); |
| 227 | + } |
| 228 | +
|
| 229 | + .dropdown-item:active { |
| 230 | + background-color: var(--hover-bg); |
| 231 | + opacity: 0.8; |
| 232 | + } |
| 233 | +
|
| 234 | + /* Custom scrollbar styling */ |
| 235 | + .dropdown-content::-webkit-scrollbar { |
| 236 | + width: 6px; |
| 237 | + } |
| 238 | +
|
| 239 | + .dropdown-content::-webkit-scrollbar-track { |
| 240 | + background: #f5f5f5; |
| 241 | + border-radius: 3px; |
| 242 | + } |
| 243 | +
|
| 244 | + .dropdown-content::-webkit-scrollbar-thumb { |
| 245 | + background: #c1c1c1; |
| 246 | + border-radius: 3px; |
| 247 | + } |
| 248 | +
|
| 249 | + .dropdown-content::-webkit-scrollbar-thumb:hover { |
| 250 | + background: #a8a8a8; |
| 251 | + } |
| 252 | +
|
| 253 | + /* Firefox scrollbar */ |
| 254 | + .dropdown-content { |
| 255 | + scrollbar-width: thin; |
| 256 | + scrollbar-color: #c1c1c1 #f5f5f5; |
| 257 | + } |
| 258 | +</style> |
0 commit comments