@@ -80,7 +80,10 @@ function SettingsSearchTargetProvider({ |
80 | 80 | return <SettingsSearchTargetContext value={value}>{children}</SettingsSearchTargetContext>; |
81 | 81 | } |
82 | 82 |
|
| 83 | +let pendingTargetScroll: AbortController | undefined; |
| 84 | + |
83 | 85 | function scrollAndFocusSettingsTarget(target: HTMLElement, highlight = true): void { |
| 86 | + pendingTargetScroll?.abort(); |
84 | 87 | const prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches; |
85 | 88 | const markedScrollTarget = |
86 | 89 | typeof target.querySelector === "function" |
@@ -92,10 +95,60 @@ function scrollAndFocusSettingsTarget(target: HTMLElement, highlight = true): vo |
92 | 95 | ? (target.firstElementChild as HTMLElement) |
93 | 96 | : target); |
94 | 97 |
|
95 | | - scrollTarget.scrollIntoView({ |
96 | | - behavior: prefersReducedMotion ? "auto" : "smooth", |
97 | | - block: "center", |
98 | | - }); |
| 98 | + const scroll = () => |
| 99 | + scrollTarget.scrollIntoView({ |
| 100 | + behavior: prefersReducedMotion ? "auto" : "smooth", |
| 101 | + block: "center", |
| 102 | + }); |
| 103 | + scroll(); |
| 104 | + const panel = target.parentElement?.closest<HTMLElement>( |
| 105 | + '[data-slot="collapsible-panel"][data-open]', |
| 106 | + ); |
| 107 | + const heightTransition = () => |
| 108 | + panel |
| 109 | + ?.getAnimations() |
| 110 | + .find( |
| 111 | + (animation) => |
| 112 | + animation instanceof CSSTransition && animation.transitionProperty === "height", |
| 113 | + ); |
| 114 | + if (panel && (panel.hasAttribute("data-starting-style") || heightTransition())) { |
| 115 | + const controller = new AbortController(); |
| 116 | + pendingTargetScroll = controller; |
| 117 | + const { signal } = controller; |
| 118 | + const finishScroll = () => { |
| 119 | + if (signal.aborted) return; |
| 120 | + controller.abort(); |
| 121 | + if ( |
| 122 | + panel.hasAttribute("data-open") && |
| 123 | + target.isConnected && |
| 124 | + document.activeElement === target |
| 125 | + ) { |
| 126 | + scroll(); |
| 127 | + } |
| 128 | + }; |
| 129 | + const waitForHeight = () => { |
| 130 | + const transition = heightTransition(); |
| 131 | + if (transition) { |
| 132 | + void transition.finished.then(finishScroll, () => controller.abort()); |
| 133 | + } else { |
| 134 | + finishScroll(); |
| 135 | + } |
| 136 | + }; |
| 137 | + // Base UI clears the starting style before the height transition finishes. |
| 138 | + if (panel.hasAttribute("data-starting-style")) { |
| 139 | + const observer = new MutationObserver(() => { |
| 140 | + if (!panel.hasAttribute("data-starting-style")) { |
| 141 | + observer.disconnect(); |
| 142 | + waitForHeight(); |
| 143 | + } |
| 144 | + }); |
| 145 | + observer.observe(panel, { attributes: true, attributeFilter: ["data-starting-style"] }); |
| 146 | + signal.addEventListener("abort", () => observer.disconnect(), { once: true }); |
| 147 | + } else { |
| 148 | + waitForHeight(); |
| 149 | + } |
| 150 | + target.addEventListener("blur", () => controller.abort(), { once: true, signal }); |
| 151 | + } |
99 | 152 | target.focus({ preventScroll: true }); |
100 | 153 | target.classList.remove("settings-search-target-pulse"); |
101 | 154 | if (!highlight || prefersReducedMotion) return; |
|