mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
💭 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:
parent
fc05ea3946
commit
da2bc05cdc
4 changed files with 189 additions and 16 deletions
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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)) {
|
||||
|
|
|
|||
129
e2e/specs/mock/scenarios/thoughts-floating-bar.spec.ts
Normal file
129
e2e/specs/mock/scenarios/thoughts-floating-bar.spec.ts
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
import { expect, test } from '@playwright/test';
|
||||
import type { Locator, Page } from '@playwright/test';
|
||||
import {
|
||||
MOCK_ENDPOINTS,
|
||||
NEW_CHAT_PATH,
|
||||
messagesView,
|
||||
selectMockEndpoint,
|
||||
sendMessageAndWaitForCompletion,
|
||||
} from '../helpers';
|
||||
|
||||
/**
|
||||
* The floating Thoughts bar reveals on pointer hover or focus within the block, and only while
|
||||
* the header is off screen. Hover does not exist on touch, so every project is pinned to a
|
||||
* desktop pointer here; the viewport height is set per test.
|
||||
*/
|
||||
test.use({ isMobile: false, hasTouch: false });
|
||||
|
||||
const unique = (prefix: string) => `${prefix}-${Date.now()}-${Math.floor(Math.random() * 1e6)}`;
|
||||
|
||||
async function openLongThoughts(page: Page, label: string) {
|
||||
await page.addInitScript(() => {
|
||||
window.localStorage.setItem('showThinking', 'true');
|
||||
});
|
||||
await page.goto(NEW_CHAT_PATH, { timeout: 10_000 });
|
||||
await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);
|
||||
await sendMessageAndWaitForCompletion(page, `E2E_LONG_THINK_REPLY:${label}`);
|
||||
|
||||
const message = messagesView(page).locator('.message-render').last();
|
||||
const header = message.getByRole('button', { name: /Thoughts/ }).first();
|
||||
await expect(header).toHaveAttribute('aria-expanded', 'true', { timeout: 15_000 });
|
||||
const body = message.locator('p', { hasText: `E2E long reasoning ${label}` });
|
||||
await expect(body).toBeVisible();
|
||||
const collapse = message.getByRole('button', { name: 'Collapse Thoughts' });
|
||||
/** The bar wrapper is the absolutely positioned div that holds the collapse control. */
|
||||
const bar = collapse.locator('xpath=ancestor::div[contains(@class,"bottom-3")][1]');
|
||||
return { message, header, body, collapse, bar };
|
||||
}
|
||||
|
||||
/** Nearest ancestor that scrolls vertically, as the messages view does. */
|
||||
async function scrollHeaderOffScreen(header: Locator) {
|
||||
await header.evaluate((node) => {
|
||||
let scroller: HTMLElement | null = node.parentElement;
|
||||
while (scroller != null) {
|
||||
const { overflowY } = getComputedStyle(scroller);
|
||||
if (
|
||||
(overflowY === 'auto' || overflowY === 'scroll') &&
|
||||
scroller.scrollHeight > scroller.clientHeight
|
||||
) {
|
||||
break;
|
||||
}
|
||||
scroller = scroller.parentElement;
|
||||
}
|
||||
if (scroller == null) {
|
||||
throw new Error('no scrolling ancestor for the Thoughts header');
|
||||
}
|
||||
const top = scroller.getBoundingClientRect().top;
|
||||
scroller.scrollTop += node.getBoundingClientRect().bottom - top + 8;
|
||||
});
|
||||
}
|
||||
|
||||
const isHeaderInViewport = (header: Locator) =>
|
||||
header.evaluate((node) => {
|
||||
const rect = node.getBoundingClientRect();
|
||||
return rect.bottom > 0 && rect.top < window.innerHeight;
|
||||
});
|
||||
|
||||
/** Center of the part of `body` that is inside the viewport, or null when none is. */
|
||||
const visiblePoint = (body: Locator) =>
|
||||
body.evaluate((node) => {
|
||||
const rect = node.getBoundingClientRect();
|
||||
const top = Math.max(rect.top, 0);
|
||||
const bottom = Math.min(rect.bottom, window.innerHeight);
|
||||
if (bottom - top < 20) {
|
||||
return null;
|
||||
}
|
||||
return { x: rect.left + rect.width / 2, y: (top + bottom) / 2 };
|
||||
});
|
||||
|
||||
test.describe('thoughts floating bar', () => {
|
||||
test('keeps the bar hidden while the header is in view @scenario:thoughts-bar-hidden-while-header-visible', async ({
|
||||
page,
|
||||
}) => {
|
||||
test.setTimeout(90_000);
|
||||
await page.setViewportSize({ width: 1280, height: 1100 });
|
||||
const { header, body, collapse, bar } = await openLongThoughts(page, unique('bar-hidden'));
|
||||
|
||||
await header.scrollIntoViewIfNeeded();
|
||||
expect(await isHeaderInViewport(header)).toBe(true);
|
||||
const point = await visiblePoint(body);
|
||||
expect(point, 'thoughts body should be on screen with the header').not.toBeNull();
|
||||
|
||||
await page.mouse.move(0, 0);
|
||||
await page.mouse.move(point!.x, point!.y, { steps: 4 });
|
||||
/** Longer than the 150ms opacity transition, so a late reveal would show. */
|
||||
await page.waitForTimeout(500);
|
||||
await expect(bar).toHaveCSS('opacity', '0');
|
||||
await expect(collapse).toHaveAttribute('tabindex', '-1');
|
||||
|
||||
/** Focus inside the block is the other reveal path; it stays gated by the header too. */
|
||||
await header.focus();
|
||||
await page.waitForTimeout(500);
|
||||
await expect(bar).toHaveCSS('opacity', '0');
|
||||
expect(await isHeaderInViewport(header)).toBe(true);
|
||||
});
|
||||
|
||||
test('shows the bar once the header scrolls away and it collapses the block @scenario:thoughts-bar-appears-after-header-scrolls-away', async ({
|
||||
page,
|
||||
}) => {
|
||||
test.setTimeout(90_000);
|
||||
/** A short viewport guarantees the messages view overflows, so the header can leave it. */
|
||||
await page.setViewportSize({ width: 1280, height: 520 });
|
||||
const { header, body, collapse, bar } = await openLongThoughts(page, unique('bar-shown'));
|
||||
|
||||
await scrollHeaderOffScreen(header);
|
||||
await expect.poll(() => isHeaderInViewport(header)).toBe(false);
|
||||
const point = await visiblePoint(body);
|
||||
expect(point, 'thoughts body should still be on screen').not.toBeNull();
|
||||
|
||||
/** Enter from outside the block so mouseenter fires after the header is already hidden. */
|
||||
await page.mouse.move(0, 0);
|
||||
await page.mouse.move(point!.x, point!.y, { steps: 4 });
|
||||
await expect(bar).toHaveCSS('opacity', '1');
|
||||
await expect(collapse).toHaveAttribute('tabindex', '0');
|
||||
|
||||
await collapse.click();
|
||||
await expect(header).toHaveAttribute('aria-expanded', 'false');
|
||||
await expect(body).toBeHidden();
|
||||
});
|
||||
});
|
||||
|
|
@ -559,7 +559,7 @@
|
|||
"count": 4
|
||||
},
|
||||
"shadcn/no-restyle": {
|
||||
"count": 6
|
||||
"count": 2
|
||||
}
|
||||
},
|
||||
"client/src/components/Chat/Messages/Content/Parts/SkillCall.tsx": {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue