Skip to content
Closed
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
46 changes: 33 additions & 13 deletions packages/instantsearch-ui-components/src/components/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ export type ChatClassNames = {
suggestions?: ChatPromptSuggestionsOwnProps['classNames'];
};

export type ChatMode = 'overlay' | 'side-panel' | 'inline';

export type ChatProps = Omit<ComponentProps<'div'>, 'onError' | 'title'> & {
/*
* The display mode of the chat widget.
* - 'overlay' (default): Fixed bottom-right overlay
* - 'side-panel': Fixed right-edge full-height panel
* - 'inline': Flows within the page layout
*/
mode?: ChatMode;
/*
* Whether the chat is open or closed.
*/
Expand Down Expand Up @@ -99,6 +108,7 @@ export function createChatComponent({ createElement, Fragment }: Renderer) {

return function Chat(userProps: ChatProps) {
const {
mode = 'overlay',
open,
maximized = false,
headerProps,
Expand All @@ -114,11 +124,18 @@ export function createChatComponent({ createElement, Fragment }: Renderer) {
className,
...props
} = userProps;

// In inline mode, the chat is always open and has no toggle button
const effectiveOpen = mode === 'inline' ? true : open;
const showToggleButton = mode !== 'inline';

return (
<div
{...props}
className={cx(
'ais-Chat',
mode !== 'overlay' && `ais-Chat--${mode}`,
effectiveOpen && 'ais-Chat--open',
maximized && 'ais-Chat--maximized',
classNames.root,
className
Expand All @@ -127,7 +144,7 @@ export function createChatComponent({ createElement, Fragment }: Renderer) {
<div
className={cx(
'ais-Chat-container',
open && 'ais-Chat-container--open',
effectiveOpen && 'ais-Chat-container--open',
maximized && 'ais-Chat-container--maximized',
classNames.container
)}
Expand All @@ -141,6 +158,7 @@ export function createChatComponent({ createElement, Fragment }: Renderer) {
{...messagesProps}
classNames={classNames.messages}
messageClassNames={classNames.message}
{...(mode === 'inline' && { visibleMessageCount: 2 })}
suggestionsElement={createElement(
SuggestionsComponent || ChatPromptSuggestions,
{
Expand All @@ -155,18 +173,20 @@ export function createChatComponent({ createElement, Fragment }: Renderer) {
})}
</div>

<div className="ais-Chat-toggleButtonWrapper">
{createElement(ToggleButtonComponent || ChatToggleButton, {
...toggleButtonProps,
classNames: classNames.toggleButton,
onClick: () => {
toggleButtonProps.onClick?.();
if (!open) {
promptProps.promptRef?.current?.focus();
}
},
})}
</div>
{showToggleButton && (
<div className="ais-Chat-toggleButtonWrapper">
{createElement(ToggleButtonComponent || ChatToggleButton, {
...toggleButtonProps,
classNames: classNames.toggleButton,
onClick: () => {
toggleButtonProps.onClick?.();
if (!open) {
promptProps.promptRef?.current?.focus();
}
},
})}
</div>
)}
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ export type ChatMessagesProps<
* Suggestions element to display below a message
*/
suggestionsElement?: VNode;
/**
* When set, only the last N messages are rendered.
*/
visibleMessageCount?: number;
};

const copyToClipboard = (message: ChatMessageBase) => {
Expand Down Expand Up @@ -299,6 +303,7 @@ export function createChatMessagesComponent({
contentRef,
onScrollToBottom,
suggestionsElement,
visibleMessageCount,
...props
} = userProps;

Expand Down Expand Up @@ -339,6 +344,10 @@ export function createChatMessagesComponent({
const DefaultLoader = LoaderComponent || DefaultLoaderComponent;
const DefaultError = ErrorComponent || DefaultErrorComponent;

const visibleMessages = visibleMessageCount
? messages.slice(-visibleMessageCount)
: messages;

return (
<div
{...props}
Expand All @@ -363,7 +372,7 @@ export function createChatMessagesComponent({
}
}}
>
{messages.map((message, index) => (
{visibleMessages.map((message, index) => (
<DefaultMessage
key={message.id}
message={message}
Expand All @@ -382,7 +391,7 @@ export function createChatMessagesComponent({
suggestionsElement={
status === 'ready' &&
message.role === 'assistant' &&
index === messages.length - 1
index === visibleMessages.length - 1
? suggestionsElement
: undefined
}
Expand Down
88 changes: 76 additions & 12 deletions packages/instantsearch.css/src/components/chat/_chat.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,75 @@
height: var(--ais-chat-maximized-height);
}

// Side panel mode: full-height panel anchored to the right edge
&--side-panel {
right: 0;
bottom: 0;
top: 0;
width: 0;
height: 100%;
max-height: 100%;
max-width: 100%;
gap: 0;

&.ais-Chat--open {
width: var(--ais-chat-side-panel-width, 24rem);
}

.ais-Chat-container {
border-radius: 0;
transform-origin: right center;
transform: translateX(100%);

&--open {
transform: translateX(0);
}
}

.ais-Chat-toggleButtonWrapper {
position: absolute;
left: 0;
bottom: var(--ais-chat-margin);
transform: translateX(calc(-100% - var(--ais-chat-margin)));
}
}

// Inline mode: flows with the document, single-message view
&--inline {
position: relative;
right: auto;
bottom: auto;
width: 100%;
height: auto;
max-width: none;
max-height: none;
pointer-events: auto;
gap: 0;

.ais-Chat-container {
pointer-events: auto;
opacity: 1;
transform: none;
max-height: var(--ais-chat-inline-max-height, 32rem);
}

.ais-ChatHeader-maximize,
.ais-ChatHeader-close {
display: none;
}
}

@media (prefers-reduced-motion: no-preference) {
transition: width var(--ais-transition-duration)
var(--ais-transition-timing-function),
height var(--ais-transition-duration)
var(--ais-transition-timing-function);
transition: width var(--ais-transition-duration) var(--ais-transition-timing-function),
height var(--ais-transition-duration) var(--ais-transition-timing-function);
}
}

.ais-Chat-container {
display: flex;
flex-direction: column;
background-color: rgba(
var(--ais-background-color-rgb),
var(--ais-background-color-alpha)
);
background-color: rgba(var(--ais-background-color-rgb),
var(--ais-background-color-alpha));
border-radius: var(--ais-border-radius-lg);
box-shadow: var(--ais-shadow-md);
width: 100%;
Expand All @@ -46,10 +100,8 @@
transform: scale(0.95) translateY(var(--ais-spacing));

@media (prefers-reduced-motion: no-preference) {
transition: opacity var(--ais-transition-duration)
var(--ais-transition-timing-function),
transform var(--ais-transition-duration)
var(--ais-transition-timing-function);
transition: opacity var(--ais-transition-duration) var(--ais-transition-timing-function),
transform var(--ais-transition-duration) var(--ais-transition-timing-function);
}

&--open {
Expand All @@ -63,6 +115,13 @@
pointer-events: auto;
}

// Smooth body margin transition for side-panel mode
body {
@media (prefers-reduced-motion: no-preference) {
transition: margin-right var(--ais-transition-duration, 200ms) var(--ais-transition-timing-function, ease);
}
}

@media (max-width: variables.$ais-chat-breakpoint) {
.ais-Chat {
inset: 0;
Expand All @@ -78,4 +137,9 @@
border-radius: 0;
transition: none;
}

// On mobile, side-panel falls back to full-screen like overlay
.ais-Chat--side-panel {
width: 100%;
}
}
38 changes: 37 additions & 1 deletion packages/instantsearch.js/src/widgets/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
createChatComponent,
} from 'instantsearch-ui-components';
import { Fragment, h, render } from 'preact';
import { useMemo } from 'preact/hooks';
import { useEffect, useMemo } from 'preact/hooks';

import TemplateComponent from '../../components/Template/Template';
import connectChat from '../../connectors/chat/connectChat';
Expand Down Expand Up @@ -54,6 +54,7 @@ import type {
ChatMessageLoaderProps,
ChatMessageProps,
ChatMessagesTranslations,
ChatMode,
ChatPromptProps,
ChatPromptTranslations,
ChatStatus,
Expand Down Expand Up @@ -270,6 +271,7 @@ function createDefaultTools<
}

type ChatWrapperProps = {
mode: ChatMode;
cssClasses: ChatCSSClasses;
chatOpen: boolean;
setChatOpen: (open: boolean) => void;
Expand Down Expand Up @@ -335,6 +337,7 @@ type ChatWrapperProps = {
};

function ChatWrapper({
mode,
cssClasses,
chatOpen,
setChatOpen,
Expand Down Expand Up @@ -368,8 +371,28 @@ function ChatWrapper({

const [maximized, setMaximized] = state.use(false);

// In side-panel mode, push page content by applying margin to <body>
useEffect(() => {
if (mode === 'side-panel') {
if (chatOpen) {
const width =
getComputedStyle(document.documentElement)
.getPropertyValue('--ais-chat-side-panel-width')
.trim() || '24rem';
document.body.style.marginRight = width;
} else {
document.body.style.marginRight = '';
}
}

return () => {
document.body.style.marginRight = '';
};
}, [mode, chatOpen]);

return (
<Chat
mode={mode}
classNames={cssClasses}
open={chatOpen}
maximized={maximized}
Expand Down Expand Up @@ -448,6 +471,7 @@ const createRenderer = <THit extends RecordWithObjectID = RecordWithObjectID>({
containerNode,
templates,
tools,
mode,
}: {
containerNode: HTMLElement;
cssClasses: ChatCSSClasses;
Expand All @@ -456,6 +480,7 @@ const createRenderer = <THit extends RecordWithObjectID = RecordWithObjectID>({
};
templates: ChatTemplates<THit>;
tools: UserClientSideToolsWithTemplate;
mode: ChatMode;
}): Renderer<ChatRenderState, Partial<ChatWidgetParams>> => {
const state = createLocalState();
const promptRef = { current: null as HTMLTextAreaElement | null };
Expand Down Expand Up @@ -819,6 +844,7 @@ const createRenderer = <THit extends RecordWithObjectID = RecordWithObjectID>({
function rerender() {
render(
<ChatWrapper
mode={mode}
cssClasses={cssClasses}
chatOpen={open}
setChatOpen={setOpen}
Expand Down Expand Up @@ -1120,6 +1146,14 @@ type ChatWidgetParams<THit extends RecordWithObjectID = RecordWithObjectID> = {
*/
container: string | HTMLElement;

/**
* The display mode of the chat widget.
* - 'overlay' (default): Fixed bottom-right overlay
* - 'side-panel': Fixed right-edge full-height panel
* - 'inline': Flows within the page layout
*/
mode?: ChatMode;

/**
* Return the URL of the main search page with the `nextUiState`.
* This is used to navigate to the main search page when the user clicks on "View all" in the search tool.
Expand Down Expand Up @@ -1161,6 +1195,7 @@ export default (function chat<
>(widgetParams: ChatWidgetParams<THit> & ChatConnectorParams) {
const {
container,
mode = 'overlay',
templates: userTemplates = {},
cssClasses = {},
resume = false,
Expand Down Expand Up @@ -1190,6 +1225,7 @@ export default (function chat<
renderState: {},
templates,
tools,
mode,
});

const makeWidget = connectChat(specializedRenderer, () =>
Expand Down
Loading