|
20 | 20 | // Component |
21 | 21 | <button |
22 | 22 | type="button" |
23 | | - onclick={() => updatingDropdown.toggle()} // Open / close the dropdown. |
| 23 | + onclick={(event) => updatingDropdown.toggle(event)} // Open / close the dropdown. |
24 | 24 | > |
25 | 25 |
|
26 | 26 | <UpdatingDropdown bind:this={updatingDropdown} {taskResult} {isLoggedIn} {onupdate} /> |
|
56 | 56 | let dropdownStatus = $state(false); |
57 | 57 | let closeDropdown = dropdown.close; |
58 | 58 |
|
| 59 | + let dropdownX = $state(0); |
| 60 | + let dropdownY = $state(0); |
| 61 | + let isLowerHalfInScreen = $state(false); |
| 62 | +
|
59 | 63 | $effect(() => { |
60 | 64 | activeUrl = $page.url.pathname; |
61 | 65 | dropdownStatus = dropdown.isOpen; |
| 66 | +
|
| 67 | + if (dropdownStatus) { |
| 68 | + document.documentElement.style.setProperty('--dropdown-x', `${dropdownX}px`); |
| 69 | + document.documentElement.style.setProperty('--dropdown-y', `${dropdownY}px`); |
| 70 | + } |
62 | 71 | }); |
63 | 72 |
|
64 | | - export function toggle(): void { |
| 73 | + export function toggle(event?: MouseEvent): void { |
| 74 | + if (event) { |
| 75 | + getDropdownPosition(event); |
| 76 | + } |
| 77 | +
|
65 | 78 | dropdown.toggle(); |
66 | 79 | } |
67 | 80 |
|
| 81 | + function getDropdownPosition(event: MouseEvent): void { |
| 82 | + const rect = (event.currentTarget as HTMLElement).getBoundingClientRect(); |
| 83 | +
|
| 84 | + dropdownX = rect.right; |
| 85 | + dropdownY = rect.bottom; |
| 86 | +
|
| 87 | + isLowerHalfInScreen = rect.top > window.innerHeight / 2; |
| 88 | + } |
| 89 | +
|
| 90 | + function getDropdownClasses(isLower: boolean): string { |
| 91 | + let classes = |
| 92 | + 'absolute w-32 z-[999] shadow-lg pointer-events-auto left-[var(--dropdown-x)] transform -translate-x-full '; |
| 93 | +
|
| 94 | + if (isLower) { |
| 95 | + classes += 'bottom-[calc(100vh-var(--dropdown-y))] mb-5'; |
| 96 | + } else { |
| 97 | + classes += 'top-[var(--dropdown-y)] mt-1'; |
| 98 | + } |
| 99 | + return classes; |
| 100 | + } |
| 101 | +
|
68 | 102 | let selectedSubmissionStatus = $state<SubmissionStatus>(); |
69 | 103 | let showForm = $state(false); |
70 | 104 |
|
|
176 | 210 | }); |
177 | 211 | </script> |
178 | 212 |
|
179 | | -<div class="relative"> |
| 213 | +<div class="fixed inset-0 pointer-events-none z-50 w-full h-full"> |
180 | 214 | <Dropdown |
181 | 215 | {activeUrl} |
182 | 216 | {dropdownStatus} |
183 | 217 | {closeDropdown} |
184 | | - class="absolute w-32 z-20 left-auto right-0 mt-5" |
| 218 | + class={getDropdownClasses(isLowerHalfInScreen)} |
185 | 219 | > |
186 | 220 | <DropdownUl class="border rounded-lg shadow"> |
187 | 221 | {#if isLoggedIn} |
|
0 commit comments