💭 fix: Show the Thoughts Floating Bar Only When Its Header Is Off Screen (#16642)

* fix: No Pressed Fill on the Thoughts Header

* fix: Even Thoughts Padding and Show the Floating Bar Only When the Header Is Off Screen

* fix: Re-measure the Thoughts Header on Every Visibility Notification

* test: Scenarios for the Thoughts Floating Bar

* test: Keep the Thoughts Scenarios to the Floating Bar
This commit is contained in:
Marco Beretta 2026-10-02 23:18:18 +02:00 • committed by GitHub
parent fc05ea3946
commit da2bc05cdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 189 additions and 16 deletions

View file

@ -162,7 +162,7 @@ const Reasoning = memo((props: ReasoningProps) => {
const isExpanded = expansionOverride ?? defaultExpanded;
const [isBarVisible, setIsBarVisible] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const { ref: headerRef, inViewport: headerInViewport } = useInViewport();
const { ref: headerRef, inViewport: headerInViewport, recheck: recheckHeader } = useInViewport();
const { style: expandStyle, ref: expandRef } = useExpandCollapse(isExpanded);
const { shouldRenderBody, mountBody, handleTransitionEnd } = useLazyCollapseBody(isExpanded);
const { isSubmitting, isLatestMessage, nextType } = useMessage();
@ -180,8 +180,9 @@ const Reasoning = memo((props: ReasoningProps) => {
);
const handleFocus = useCallback(() => {
recheckHeader();
setIsBarVisible(true);
}, []);
}, [recheckHeader]);
const handleBlur = useCallback((e: FocusEvent) => {
if (!containerRef.current?.contains(e.relatedTarget as Node)) {
@ -190,8 +191,9 @@ const Reasoning = memo((props: ReasoningProps) => {
}, []);
const handleMouseEnter = useCallback(() => {
recheckHeader();
setIsBarVisible(true);
}, []);
}, [recheckHeader]);
const handleMouseLeave = useCallback(() => {
if (!containerRef.current?.contains(document.activeElement)) {
@ -318,7 +320,11 @@ export const ReasoningCompact = memo(
const [isBarVisible, setIsBarVisible] = useState(false);
const [isCopied, setIsCopied] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const { ref: headerRef, inViewport: headerInViewport } = useInViewport();
const {
ref: headerRef,
inViewport: headerInViewport,
recheck: recheckHeader,
} = useInViewport();
const { style: expandStyle, ref: expandRef } = useExpandCollapse(isExpanded);
/** Collapsed is the default whenever thoughts are hidden, and a streaming
* THINK part re-renders on every delta. Keeping the full text mounted
@ -345,7 +351,10 @@ export const ReasoningCompact = memo(
setTimeout(() => setIsCopied(false), 2000);
}, [reasoningText]);
const revealBar = useCallback(() => setIsBarVisible(true), []);
const revealBar = useCallback(() => {
recheckHeader();
setIsBarVisible(true);
}, [recheckHeader]);
const hideBar = useCallback(() => {
if (!containerRef.current?.contains(document.activeElement)) {
setIsBarVisible(false);
@ -372,11 +381,11 @@ export const ReasoningCompact = memo(
>
<div ref={headerRef} className="relative flex h-5 shrink-0 items-center gap-1.5">
<Button
variant="ghost"
variant="disclosure"
onClick={handleToggle}
aria-expanded={isExpanded}
aria-controls={contentId}
className="group/disclosure text-text-secondary h-auto min-w-0 flex-1 justify-start gap-2 rounded-none p-0 font-normal hover:bg-transparent"
className="group/disclosure text-text-secondary min-w-0 flex-1 font-normal"
>
<span className={ROW_GLYPH_SLOT} aria-hidden="true">
<Lightbulb className="text-text-secondary size-4 shrink-0" />
@ -416,7 +425,7 @@ export const ReasoningCompact = memo(
>
<div className="overflow-hidden" ref={expandRef}>
{shouldRenderBody && (
<div className="border-border-light bg-surface-secondary text-text-secondary relative my-2 rounded-2xl border p-4 pb-9">
<div className="border-border-light bg-surface-secondary text-text-secondary relative my-2 rounded-2xl border p-4">
<p className={cn('leading-[26px] whitespace-pre-wrap', fontSize)}>
{reasoningText}
</p>

View file

@ -19,14 +19,38 @@ import { AnimatedText } from '../animate';
import { ROW_GLYPH_SLOT } from '../rows';
import { cn } from '~/utils';
/** Whether any part of `el` is inside the viewport and every clipping ancestor. */
function isRectVisible(el: HTMLElement): boolean {
const rect = el.getBoundingClientRect();
let top = Math.max(rect.top, 0);
let bottom = Math.min(rect.bottom, window.innerHeight);
for (let node = el.parentElement; node != null && top < bottom; node = node.parentElement) {
const { overflowY } = getComputedStyle(node);
if (overflowY === 'visible') {
continue;
}
const clip = node.getBoundingClientRect();
top = Math.max(top, clip.top);
bottom = Math.min(bottom, clip.bottom);
}
return top < bottom;
}
/**
* Tracks whether the referenced element is within the viewport. Mirrors the
* CodeBlock pattern: the header copy/collapse controls live at the top, and the
* floating bottom-right bar only takes over once the header scrolls out of view.
*
* The observer is only a trigger. Its entries can be stale: a header mounted
* inside a fold that is still opening is first reported hidden, and that report
* can land after the pointer has already revealed the bar. So every
* notification re-measures the header's real position instead of trusting
* `isIntersecting`, and `recheck` does the same for callers about to reveal.
*/
export function useInViewport(): {
ref: React.RefObject<HTMLDivElement>;
inViewport: boolean;
recheck: () => boolean;
} {
const ref = useRef<HTMLDivElement>(null);
const [inViewport, setInViewport] = useState(true);
@ -36,7 +60,7 @@ export function useInViewport(): {
if (!el) {
return;
}
const observer = new IntersectionObserver(([entry]) => setInViewport(entry.isIntersecting), {
const observer = new IntersectionObserver(() => setInViewport(isRectVisible(el)), {
root: null,
threshold: 0,
});
@ -44,7 +68,14 @@ export function useInViewport(): {
return () => observer.disconnect();
}, []);
return { ref, inViewport };
const recheck = useCallback(() => {
const el = ref.current;
const visible = el == null ? true : isRectVisible(el);
setInViewport(visible);
return visible;
}, []);
return { ref, inViewport, recheck };
}
/**
@ -61,7 +92,7 @@ export const ThinkingContent: FC<{
animate && typeof children === 'string' ? <AnimatedText text={children} /> : children;
return (
<div className="border-border-light bg-surface-secondary text-text-secondary relative rounded-lg border p-3 pb-8">
<div className="border-border-light bg-surface-secondary text-text-secondary relative rounded-lg border p-3">
<p className={cn('leading-[26px] whitespace-pre-wrap', fontSize)}>{content}</p>
</div>
);
@ -243,7 +274,9 @@ export const FloatingThinkingBar = memo(
return (
<div
className={cn(
'absolute right-3 bottom-3 flex items-center gap-2 transition-opacity duration-150',
/* Laid over the text rather than reserved below it, so the box keeps
even padding; the fill keeps the controls legible over a line. */
'bg-surface-secondary absolute right-3 bottom-3 flex items-center gap-2 rounded-lg transition-opacity duration-150',
isVisible ? 'opacity-100' : 'pointer-events-none opacity-0',
)}
>
@ -304,7 +337,7 @@ const Thinking: React.ElementType = memo(({ children }: { children: React.ReactN
const [isExpanded, setIsExpanded] = useState(showThinking);
const [isBarVisible, setIsBarVisible] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const { ref: headerRef, inViewport: headerInViewport } = useInViewport();
const { ref: headerRef, inViewport: headerInViewport, recheck: recheckHeader } = useInViewport();
const contentId = useId();
const { style: expandStyle, ref: expandRef } = useExpandCollapse(isExpanded);
@ -314,8 +347,9 @@ const Thinking: React.ElementType = memo(({ children }: { children: React.ReactN
}, []);
const handleFocus = useCallback(() => {
recheckHeader();
setIsBarVisible(true);
}, []);
}, [recheckHeader]);
const handleBlur = useCallback((e: FocusEvent) => {
if (!containerRef.current?.contains(e.relatedTarget as Node)) {
@ -324,8 +358,9 @@ const Thinking: React.ElementType = memo(({ children }: { children: React.ReactN
}, []);
const handleMouseEnter = useCallback(() => {
recheckHeader();
setIsBarVisible(true);
}, []);
}, [recheckHeader]);
const handleMouseLeave = useCallback(() => {
if (!containerRef.current?.contains(document.activeElement)) {