Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/module/src/MessageBox/JumpButton.scss
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@
inset-block-end: var(--pf-t--global--spacer--md) !important;
}

&--hidden {
opacity: 0 !important;
pointer-events: none !important;
transition: none !important;

&:focus,
&:focus-visible {
opacity: 1 !important;
pointer-events: auto !important;
}
}

// allows for zoom conditions; try zooming to 200% to see
@media screen and (max-height: 518px) {
display: none;
Expand Down
13 changes: 11 additions & 2 deletions packages/module/src/MessageBox/JumpButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ describe('JumpButton', () => {
await userEvent.click(screen.getByRole('button', { name: /Back to bottom/i }));
expect(spy).toHaveBeenCalledTimes(1);
});
it('should be hidden if isHidden prop is used', async () => {
it('should remain in the DOM but visually hidden when isHidden is used', () => {
render(<JumpButton position="bottom" onClick={jest.fn()} isHidden />);
expect(screen.queryByRole('button', { name: /Back to bottom/i })).toBeFalsy();
const button = screen.getByRole('button', { name: /Back to bottom/i });
expect(button).toHaveClass('pf-chatbot__jump--hidden');
});

it('should become visible when focused while hidden', () => {
render(<JumpButton position="bottom" onClick={jest.fn()} isHidden />);
const button = screen.getByRole('button', { name: /Back to bottom/i });
button.focus();
expect(button).toHaveFocus();
expect(button).toHaveClass('pf-chatbot__jump--hidden');
});
});
39 changes: 19 additions & 20 deletions packages/module/src/MessageBox/JumpButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,25 @@ const JumpButton: FunctionComponent<JumpButtonProps> = ({
onClick,
jumpButtonProps,
jumpButtonTooltipProps
}: JumpButtonProps) =>
isHidden ? null : (
<Tooltip
id={`pf-chatbot__tooltip--jump-${position}`}
content={`Back to ${position}`}
position="top"
{...jumpButtonTooltipProps}
}: JumpButtonProps) => (
<Tooltip
id={`pf-chatbot__tooltip--jump-${position}`}
content={`Back to ${position}`}
position="top"
{...jumpButtonTooltipProps}
>
<Button
variant="plain"
className={`pf-chatbot__jump pf-chatbot__jump--${position}${isHidden ? ' pf-chatbot__jump--hidden' : ''}`}
aria-label={`Back to ${position}`}
onClick={onClick}
{...jumpButtonProps}
>
<Button
variant="plain"
className={`pf-chatbot__jump pf-chatbot__jump--${position}`}
aria-label={`Back to ${position}`}
onClick={onClick}
{...jumpButtonProps}
>
<Icon iconSize="lg" isInline>
{position === 'top' ? <ArrowUpIcon /> : <ArrowDownIcon />}
</Icon>
</Button>
</Tooltip>
);
<Icon iconSize="lg" isInline>
{position === 'top' ? <ArrowUpIcon /> : <ArrowDownIcon />}
</Icon>
</Button>
</Tooltip>
);

export default JumpButton;
91 changes: 91 additions & 0 deletions packages/module/src/MessageBox/MessageBox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createRef } from 'react';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import { MessageBox, MessageBoxHandle } from './MessageBox';
import userEvent from '@testing-library/user-event';

Expand Down Expand Up @@ -313,4 +314,94 @@ describe('MessageBox', () => {

expect(ref.current?.isSmartScrollActive()).toBe(true);
});

it('should keep auto-scroll active during programmatic scroll to bottom', async () => {
const ref = createRef<MessageBoxHandle>();
render(
<MessageBox ref={ref} enableSmartScroll>
<div>Test message content</div>
</MessageBox>
);

const element = ref.current!;
Object.defineProperty(element, 'scrollHeight', { configurable: true, value: 1000 });
Object.defineProperty(element, 'clientHeight', { configurable: true, value: 300 });
Object.defineProperty(element, 'scrollTop', { configurable: true, value: 0, writable: true });

act(() => {
ref.current?.scrollToBottom({ behavior: 'smooth' });
});

expect(ref.current?.isSmartScrollActive()).toBe(true);

act(() => {
element.scrollTop = 500;
element.dispatchEvent(new Event('scroll', { bubbles: true }));
});

expect(ref.current?.isSmartScrollActive()).toBe(true);
});

it('should not scroll to bottom on initial layout when the message box is at the top', () => {
render(
<MessageBox enableSmartScroll>
<div>Tall message content</div>
</MessageBox>
);

const region = screen.getByRole('region');
Object.defineProperty(region, 'scrollHeight', { configurable: true, value: 1000 });
Object.defineProperty(region, 'clientHeight', { configurable: true, value: 300 });
Object.defineProperty(region, 'scrollTop', { configurable: true, value: 0, writable: true });

act(() => {
region.dispatchEvent(new Event('scroll'));
});

expect(region.scrollTop).toBe(0);
});

it('should show the bottom jump button at the top on initial load when overflowing', async () => {
render(
<MessageBox enableSmartScroll>
<div>Tall message content</div>
</MessageBox>
);

const region = screen.getByRole('region');
Object.defineProperty(region, 'scrollHeight', { configurable: true, value: 1000 });
Object.defineProperty(region, 'clientHeight', { configurable: true, value: 300 });
Object.defineProperty(region, 'scrollTop', { configurable: true, value: 0, writable: true });

act(() => {
region.dispatchEvent(new Event('scroll'));
});

await waitFor(() => {
expect(screen.getByRole('button', { name: /Back to top/i })).toHaveClass('pf-chatbot__jump--hidden');
expect(screen.getByRole('button', { name: /Back to bottom/i })).not.toHaveClass('pf-chatbot__jump--hidden');
});
});

it('should hide the bottom jump button while smart scroll is actively following content', async () => {
render(
<MessageBox enableSmartScroll>
<div>Test message content</div>
</MessageBox>
);

const region = screen.getByRole('region');
Object.defineProperty(region, 'scrollHeight', { configurable: true, value: 1000 });
Object.defineProperty(region, 'clientHeight', { configurable: true, value: 300 });
Object.defineProperty(region, 'scrollTop', { configurable: true, value: 640, writable: true });

act(() => {
region.dispatchEvent(new Event('scroll'));
});

await waitFor(() => {
expect(screen.getByRole('button', { name: /Back to top/i })).not.toHaveClass('pf-chatbot__jump--hidden');
expect(screen.getByRole('button', { name: /Back to bottom/i })).toHaveClass('pf-chatbot__jump--hidden');
});
});
});
Loading
Loading