Inline Toolbar in React Inline AI Assist

11 Aug 202624 minutes to read

The inline toolbar provides a customizable interface for users to interact with the Inline AI Assist component. You can render inline toolbar items by using the items property in the inlineToolbarSettings property.

Configure the toolbar and positioning

You can use the toolbarPosition property to customize footer toolbar position. It has two modes such as Inline, and Bottom. By default, the toolbarPosition is Inline.

By settings toolbarPosition as Bottom, footer items will be rendered at the bottom with a dedicated footer area.

Below sample demonstrates the usage of toolbar settings in Inline Assist component

import { InlineAIAssistComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    const assistRef = React.useRef(null);
    const editableRef = React.useRef(null);
    const [toolbarPosition, setToolbarPosition] = React.useState('Bottom');
    
    const promptsData = [
        {
            prompt: 'What is AI?',
            response: '<div>AI stands for Artificial Intelligence, enabling machines to mimic human intelligence for tasks such as learning, problem-solving, and decision-making.</div>'
        }
    ];

    const handleSummarizeBtnClick = () => {
        if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
            assistRef.current.showPopup();
        }
    };

    const handleToggleToolbarPosition = () => {
        const newPosition = toolbarPosition === 'Bottom' ? 'Inline' : 'Bottom';
        setToolbarPosition(newPosition);
    };

    const handlePromptRequest = (args) => {
        setTimeout(() => {
            const foundPrompt = promptsData.find((promptObj) => promptObj.prompt === args.prompt);
            const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
            if (assistRef.current) {
                assistRef.current.addResponse(foundPrompt ? foundPrompt.response : defaultResponse);
            }
        }, 1000);
    };

    const handleResponseItemSelect = (args) => {
        if (args.command.label === 'Accept') {
            if (editableRef.current && assistRef.current) {
                const lastResponse = assistRef.current.prompts && assistRef.current.prompts.length > 0
                    ? assistRef.current.prompts[assistRef.current.prompts.length - 1].response
                    : '';
                if (lastResponse) {
                    editableRef.current.innerHTML = '<p>' + lastResponse + '</p>';
                }
            }
            if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
                assistRef.current.hidePopup();
            }
        } else if (args.command.label === 'Discard') {
            if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
                assistRef.current.hidePopup();
            }
        }
    };

    const inlineToolbarSettings = {
        toolbarPosition: toolbarPosition,
        items: [
            { 
                text: 'Welcome User!', 
                align: 'Left' 
            },
            { 
                iconCss: 'e-icons e-assistview-icon', 
                align: 'Left'
            }
        ]
    };

    return (
        <div>
            <div style=>
                <button 
                    id="toolbarBtn" 
                    className="e-btn e-primary" 
                    onClick={handleToggleToolbarPosition}
                    style=
                >
                    Toggle Toolbar Position
                </button>
                <span style=>Current Position: {toolbarPosition}</span>
            </div>

            <button 
                id="summarizeBtn" 
                className="e-btn e-primary" 
                style=
                onClick={handleSummarizeBtnClick}
            >
                Content Summarize
            </button>
            <div 
                id="editableText" 
                contentEditable="true" 
                ref={editableRef}
            >
                <p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
                <p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
            </div>
            <InlineAIAssistComponent
                id="defaultInlineAssist"
                ref={assistRef}
                prompts={promptsData}
                relateTo="#summarizeBtn"
                inlineToolbarSettings={inlineToolbarSettings}
                promptRequest={handlePromptRequest}
                responseSettings=
            />
        </div>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { InlineAIAssistComponent, InlineToolbarSettingsModel, PromptResponseModel, ResponseItemSelectEventArgs } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';

const App: React.FC = () => {
    const assistRef = React.useRef<InlineAIAssistComponent>(null);
    const editableRef = React.useRef<HTMLDivElement>(null);
    const [toolbarPosition, setToolbarPosition] = React.useState<string>('Bottom');
    
    const promptsData: PromptResponseModel[] = [
        {
            prompt: 'What is AI?',
            response: '<div>AI stands for Artificial Intelligence, enabling machines to mimic human intelligence for tasks such as learning, problem-solving, and decision-making.</div>'
        }
    ];

    const handleSummarizeBtnClick = (): void => {
        if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
            assistRef.current.showPopup();
        }
    };

    const handleToggleToolbarPosition = (): void => {
        const newPosition = toolbarPosition === 'Bottom' ? 'Inline' : 'Bottom';
        setToolbarPosition(newPosition);
    };

    const handlePromptRequest = (args: any): void => {
        setTimeout(() => {
            const foundPrompt = promptsData.find((promptObj) => promptObj.prompt === args.prompt);
            const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
            if (assistRef.current) {
                assistRef.current.addResponse((foundPrompt ? foundPrompt.response : defaultResponse) as string);
            }
        }, 1000);
    };

    const handleResponseItemSelect = (args: ResponseItemSelectEventArgs): void => {
        if (args.command.label === 'Accept') {
            if (editableRef.current && assistRef.current) {
                const lastResponse = assistRef.current.prompts && assistRef.current.prompts.length > 0
                    ? assistRef.current.prompts[assistRef.current.prompts.length - 1].response
                    : '';
                if (lastResponse) {
                    editableRef.current.innerHTML = '<p>' + lastResponse + '</p>';
                }
            }
            if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
                assistRef.current.hidePopup();
            }
        } else if (args.command.label === 'Discard') {
            if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
                assistRef.current.hidePopup();
            }
        }
    };

    const inlineToolbarSettings: InlineToolbarSettingsModel = {
        toolbarPosition: toolbarPosition,
        items: [
            { 
                text: 'Welcome User!', 
                align: 'Left' 
            },
            { 
                iconCss: 'e-icons e-assistview-icon', 
                align: 'Left'
            }
        ]
    };

    return (
        <div>
            <div style=>
                <button 
                    id="toolbarBtn" 
                    className="e-btn e-primary" 
                    onClick={handleToggleToolbarPosition}
                    style=
                >
                    Toggle Toolbar Position
                </button>
                <span style=>Current Position: {toolbarPosition}</span>
            </div>

            <button 
                id="summarizeBtn" 
                className="e-btn e-primary" 
                style=
                onClick={handleSummarizeBtnClick}
            >
                Content Summarize
            </button>
            <div 
                id="editableText" 
                contentEditable="true" 
                ref={editableRef}
            >
                <p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
                <p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
            </div>
            <InlineAIAssistComponent
                id="defaultInlineAssist"
                ref={assistRef}
                prompts={promptsData}
                relateTo="#summarizeBtn"
                inlineToolbarSettings={inlineToolbarSettings}
                promptRequest={handlePromptRequest}
                responseSettings=
            />
        </div>
    );
};

ReactDOM.render(<App />, document.getElementById('container'));
#loader {
    color: #008cff;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
}

#container {
    margin: 20px auto;
}

#editableText {
    width: 100%;
    min-height: 120px;
    max-height: 300px;
    overflow-y: auto;
    font-size: 16px;
    padding: 12px;
    border-radius: 4px;
    border: 1px solid;
}

Built-in toolbar items

By default, the inline toolbar renders the send item which allows users to send the prompt text.

Adding custom toolbar items

You can use the inlineToolbarSettings property to add custom items for the inline toolbar in the Inline AI Assist. The custom items will be added along with the existing built-in items in the inline toolbar.

Each toolbar item object can include the following properties:

Property Type Default Description
iconCss string ’’ Specifies the CSS class for the item’s icon.
type string ‘Button’ Supports three types of items: Button, Separator, and Input.
text string ’’ Specifies the text label for the toolbar item.
template string ’’ Specifies a custom template for the toolbar item.
visible boolean true Specifies whether to show or hide the toolbar item.
id string ’’ Specifies a unique identifier for the toolbar item.
disabled boolean false Specifies whether the toolbar item is disabled and unselectable.
tooltip string ’’ Specifies the tooltip text displayed on hover.
cssClass string ’’ Specifies custom CSS classes for styling the toolbar item.
align string ‘Left’ Specifies the alignment of the toolbar item (Left, Center, Right).
tabIndex number -1 Specifies the tab order for keyboard navigation.

Below sample demonstrates the usage of custom toolbar items in Inline Assist component

import { InlineAIAssistComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    const assistRef = React.useRef(null);
    const editableRef = React.useRef(null);

    const handleSummarizeBtnClick = () => {
        if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
            assistRef.current.showPopup();
        }
    };

    const handlePromptRequest = () => {
        setTimeout(() => {
            const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
            if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
                assistRef.current.addResponse(defaultResponse);
            }
        }, 1000);
    };

    const handleResponseItemSelect = (args) => {
        if (args.command.label === 'Accept') {
            if (editableRef.current && assistRef.current) {
                const lastResponse = assistRef.current.prompts && assistRef.current.prompts.length > 0
                    ? assistRef.current.prompts[assistRef.current.prompts.length - 1].response
                    : '';
                if (lastResponse) {
                    editableRef.current.innerHTML = '<p>' + lastResponse + '</p>';
                }
            }
            if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
                assistRef.current.hidePopup();
            }
        } else if (args.command.label === 'Discard') {
            if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
                assistRef.current.hidePopup();
            }
        }
    };

    const inlineToolbarSettings = {
        items: [
            {
                type: 'Button',
                iconCss: 'e-icons e-refresh',
                align: 'Right',
                tooltip: 'Refresh content',
                disabled: false,
                visible: true,
                cssClass: 'custom-btn'
            },
            {
                type: 'Button',
                iconCss: 'e-icons e-user',
                align: 'Right',
                tooltip: 'User profile',
                disabled: false,
                visible: true,
                cssClass: 'custom-btn'
            },
            {
                type: 'Button',
                iconCss: 'e-icons e-settings',
                align: 'Right',
                disabled: true,
                visible: true
            }
        ]
    };

    return (
        <div>
            <button 
                id="summarizeBtn" 
                className="e-btn e-primary" 
                style=
                onClick={handleSummarizeBtnClick}
            >
                Content Summarize
            </button>
            <div 
                id="editableText" 
                contentEditable="true" 
                ref={editableRef}
            >
                <p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
                <p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
            </div>
            <InlineAIAssistComponent
                id="defaultInlineAssist"
                ref={assistRef}
                relateTo="#summarizeBtn"
                inlineToolbarSettings={inlineToolbarSettings}
                promptRequest={handlePromptRequest}
                responseSettings=
            />
        </div>
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { InlineAIAssistComponent, ResponseItemSelectEventArgs, InlineToolbarSettingsModel } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';

const App: React.FC = () => {
    const assistRef = React.useRef<InlineAIAssistComponent>(null);
    const editableRef = React.useRef<HTMLDivElement>(null);

    const handleSummarizeBtnClick = (): void => {
        if (assistRef.current && typeof assistRef.current.showPopup === 'function') {
            assistRef.current.showPopup();
        }
    };

    const handlePromptRequest = (): void => {
        setTimeout(() => {
            const defaultResponse = 'For real-time prompt processing, connect the Inline AI Assist component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
            if (assistRef.current && typeof assistRef.current.addResponse === 'function') {
                assistRef.current.addResponse(defaultResponse);
            }
        }, 1000);
    };

    const handleResponseItemSelect = (args: ResponseItemSelectEventArgs): void => {
        if (args.command.label === 'Accept') {
            if (editableRef.current && assistRef.current) {
                const lastResponse = assistRef.current.prompts && assistRef.current.prompts.length > 0
                    ? assistRef.current.prompts[assistRef.current.prompts.length - 1].response
                    : '';
                if (lastResponse) {
                    editableRef.current.innerHTML = '<p>' + lastResponse + '</p>';
                }
            }
            if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
                assistRef.current.hidePopup();
            }
        } else if (args.command.label === 'Discard') {
            if (assistRef.current && typeof assistRef.current.hidePopup === 'function') {
                assistRef.current.hidePopup();
            }
        }
    };

    const inlineToolbarSettings: InlineToolbarSettingsModel = {
        items: [
            {
                type: 'Button',
                iconCss: 'e-icons e-refresh',
                align: 'Right',
                tooltip: 'Refresh content',
                disabled: false,
                visible: true,
                cssClass: 'custom-btn'
            },
            {
                type: 'Button',
                iconCss: 'e-icons e-user',
                align: 'Right',
                tooltip: 'User profile',
                disabled: false,
                visible: true,
                cssClass: 'custom-btn'
            },
            {
                type: 'Button',
                iconCss: 'e-icons e-settings',
                align: 'Right',
                disabled: true,
                visible: true
            }
        ]
    };

    return (
        <div>
            <button 
                id="summarizeBtn" 
                className="e-btn e-primary" 
                style=
                onClick={handleSummarizeBtnClick}
            >
                Content Summarize
            </button>
            <div 
                id="editableText" 
                contentEditable="true" 
                ref={editableRef}
            >
                <p>Inline AI Assist component provides intelligent text processing capabilities that enhance user productivity. It leverages advanced natural language processing to understand context and deliver precise suggestions. Users can seamlessly integrate AI-powered features into their applications.</p>
                <p>With real-time response streaming and customizable prompts, developers can create interactive experiences. The component supports multiple response modes including inline editing and popup-based interactions.</p>
            </div>
            <InlineAIAssistComponent
                id="defaultInlineAssist"
                ref={assistRef}
                relateTo="#summarizeBtn"
                inlineToolbarSettings={inlineToolbarSettings}
                promptRequest={handlePromptRequest}
                responseSettings=
            />
        </div>
    );
};

ReactDOM.render(<App />, document.getElementById('container'));
#loader {
    color: #008cff;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
}

#container {
    margin: 20px auto;
}

#editableText {
    width: 100%;
    min-height: 120px;
    max-height: 300px;
    overflow-y: auto;
    font-size: 16px;
    padding: 12px;
    border-radius: 4px;
    border: 1px solid;
}

Toolbar itemClick event

The itemClick event is triggered when the inline toolbar item is clicked.

import { InlineAIAssistComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {

    const inlineToolbarSettings = {
        itemClick: (args) => {
            // Your actions here
        }
    };

    return (
        <InlineAIAssistComponent 
            inlineToolbarSettings={inlineToolbarSettings}
        />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { InlineAIAssistComponent, InlineToolbarSettingsModel, ToolbarItemClickEventArgs } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {

    const inlineToolbarSettings: InlineToolbarSettingsModel = {
        itemClick: (args: ToolbarItemClickEventArgs) => {
            // Your actions here
        }
    };

    return (
        <InlineAIAssistComponent 
            inlineToolbarSettings={inlineToolbarSettings}
        />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));