Skip to content

Commit

Permalink
Switch from modal to chatbot layout
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccaalpert committed Dec 17, 2024
1 parent bfe5c87 commit 0466ab9
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,44 @@
import React from 'react';

import Settings from '@patternfly/chatbot/dist/dynamic/Settings';
import SettingsForm from '@patternfly/chatbot/dist/dynamic/Settings';
import {
Button,
Divider,
Dropdown,
DropdownGroup,
DropdownItem,
DropdownList,
FormGroup,
MenuToggle,
MenuToggleElement,
Radio,
Switch
Switch,
Title
} from '@patternfly/react-core';
import { ChatbotDisplayMode } from '@patternfly/chatbot/dist/dynamic/Chatbot';
import Chatbot, { ChatbotDisplayMode } from '@patternfly/chatbot/dist/dynamic/Chatbot';
import ChatbotHeader, {
ChatbotHeaderActions,
ChatbotHeaderCloseButton,
ChatbotHeaderMain,
ChatbotHeaderOptionsDropdown,
ChatbotHeaderTitle
} from '@patternfly/chatbot/dist/dynamic/ChatbotHeader';
import { CogIcon, ExpandIcon, OpenDrawerRightIcon, OutlinedWindowRestoreIcon } from '@patternfly/react-icons';

export const SettingsDemo: React.FunctionComponent = () => {
const [isChecked, setIsChecked] = React.useState<boolean>(true);
const [isThemeOpen, setIsThemeOpen] = React.useState(false);
const [isLanguageOpen, setIsLanguageOpen] = React.useState(false);
const [isVoiceOpen, setIsVoiceOpen] = React.useState(false);
const [displayMode, setDisplayMode] = React.useState(ChatbotDisplayMode.default);
const [isModalOpen, setIsModalOpen] = React.useState(true);
const [areSettingsOpen, setAreSettingsOpen] = React.useState(true);
const chatbotVisible = true;

const onFocus = (id: string) => {
const element = document.getElementById(id);
(element as HTMLElement).focus();
};

const onEscapePress = (event: KeyboardEvent) => {
const target = event.target as Element;
if (target?.id === 'voice') {
setIsVoiceOpen(!isVoiceOpen);
onFocus('voice');
return;
}
if (target?.id === 'language') {
setIsLanguageOpen(!isLanguageOpen);
onFocus('language');
return;
}
if (target?.id === 'theme') {
setIsThemeOpen(!isThemeOpen);
onFocus('theme');
return;
}
setIsModalOpen(!isModalOpen);
};

const onThemeToggleClick = () => {
setIsThemeOpen(!isThemeOpen);
};
Expand Down Expand Up @@ -219,14 +211,70 @@ export const SettingsDemo: React.FunctionComponent = () => {
}
];

const onSelectDropdownItem = (
_event: React.MouseEvent<Element, MouseEvent> | undefined,
value: string | number | undefined
) => {
if (value === 'Settings') {
setAreSettingsOpen(true);
} else {
setDisplayMode(value as ChatbotDisplayMode);
}
};

const regularChatbot = (
<ChatbotHeader>
<ChatbotHeaderActions>
<ChatbotHeaderOptionsDropdown onSelect={onSelectDropdownItem}>
<DropdownGroup label="Display mode">
<DropdownList>
<DropdownItem
value={ChatbotDisplayMode.default}
key="switchDisplayOverlay"
icon={<OutlinedWindowRestoreIcon aria-hidden />}
isSelected={displayMode === ChatbotDisplayMode.default}
>
<span>Overlay</span>
</DropdownItem>
<DropdownItem
value={ChatbotDisplayMode.docked}
key="switchDisplayDock"
icon={<OpenDrawerRightIcon aria-hidden />}
isSelected={displayMode === ChatbotDisplayMode.docked}
>
<span>Dock to window</span>
</DropdownItem>
<DropdownItem
value={ChatbotDisplayMode.fullscreen}
key="switchDisplayFullscreen"
icon={<ExpandIcon aria-hidden />}
isSelected={displayMode === ChatbotDisplayMode.fullscreen}
>
<span>Fullscreen</span>
</DropdownItem>
</DropdownList>
</DropdownGroup>
<Divider />
<DropdownList>
<DropdownItem value="Settings" key="switchSettings" icon={<CogIcon aria-hidden />}>
<span>Settings</span>
</DropdownItem>
</DropdownList>
</ChatbotHeaderOptionsDropdown>
</ChatbotHeaderActions>
</ChatbotHeader>
);

return (
<>
<div
style={{
position: 'fixed',
padding: 'var(--pf-t--global--spacer--lg)',
zIndex: '601',
boxShadow: 'var(--pf-t--global--box-shadow--lg)'
boxShadow: 'var(--pf-t--global--box-shadow--lg)',
left: 0,
bottom: 120
}}
>
<FormGroup role="radiogroup" isInline fieldId="basic-form-radio-group" label="Display mode">
Expand Down Expand Up @@ -259,15 +307,27 @@ export const SettingsDemo: React.FunctionComponent = () => {
id="embedded"
/>
</FormGroup>
<Button onClick={() => setIsModalOpen(!isModalOpen)}>Launch modal</Button>
<Button onClick={() => setAreSettingsOpen(!areSettingsOpen)}>Launch settings</Button>
</div>
<Settings
onEscapePress={onEscapePress}
displayMode={displayMode}
isModalOpen={isModalOpen}
handleModalToggle={() => setIsModalOpen(!isModalOpen)}
fields={children}
></Settings>
<Chatbot isVisible={chatbotVisible} displayMode={displayMode}>
{areSettingsOpen ? (
<>
<ChatbotHeader>
<ChatbotHeaderMain>
<ChatbotHeaderTitle>
<Title headingLevel="h1" size="2xl">
Settings
</Title>
</ChatbotHeaderTitle>
</ChatbotHeaderMain>
<ChatbotHeaderCloseButton onClick={() => setAreSettingsOpen(false)} />
</ChatbotHeader>
<SettingsForm fields={children} />
</>
) : (
<>{regularChatbot}</>
)}
</Chatbot>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { PreviewAttachment } from '@patternfly/chatbot/dist/dynamic/PreviewAttac
import ChatbotAlert from '@patternfly/chatbot/dist/dynamic/ChatbotAlert';
import {
ChatbotHeader,
ChatbotHeaderCloseButton,
ChatbotHeaderMain,
ChatbotHeaderMenu,
ChatbotHeaderActions,
Expand All @@ -65,7 +66,7 @@ import { ChatbotFooter, ChatbotFootnote } from '@patternfly/chatbot/dist/dynamic
import { MessageBar } from '@patternfly/chatbot/dist/dynamic/MessageBar';
import SourceDetailsMenuItem from '@patternfly/chatbot/dist/dynamic/SourceDetailsMenuItem';
import { ChatbotModal } from '@patternfly/chatbot/dist/dynamic/ChatbotModal';
import Settings from '@patternfly/chatbot/dist/dynamic/Settings';
import SettingsForm from '@patternfly/chatbot/dist/dynamic/Settings';
import { BellIcon, CalendarAltIcon, ClipboardIcon, CodeIcon, UploadIcon } from '@patternfly/react-icons';
import { useDropzone } from 'react-dropzone';

Expand All @@ -75,10 +76,12 @@ import { DropdownItem, DropdownList, Checkbox } from '@patternfly/react-core';
import OutlinedWindowRestoreIcon from '@patternfly/react-icons/dist/esm/icons/outlined-window-restore-icon';
import ExpandIcon from '@patternfly/react-icons/dist/esm/icons/expand-icon';
import OpenDrawerRightIcon from '@patternfly/react-icons/dist/esm/icons/open-drawer-right-icon';
import CogIcon from '@patternfly/react-icons/dist/esm/icons/cog-icon';
import PFHorizontalLogoColor from './PF-HorizontalLogo-Color.svg';
import PFHorizontalLogoReverse from './PF-HorizontalLogo-Reverse.svg';
import userAvatar from '../Messages/user_avatar.svg';
import patternflyAvatar from '../Messages/patternfly_avatar.jpg';
import { CloseIcon } from '@patternfly/react-icons';

## Structure

Expand Down Expand Up @@ -346,6 +349,8 @@ Based on the [PatternFly modal](/components/modal), this modal adapts to the Cha

### Settings

A settings layout can be substituted for other components within the chatbot. It should accept any number of buttons, dropdowns, toggles, etc. and labels, and render them appropriately within all four display modes.

```js file="./Settings.tsx" isFullscreen

```
51 changes: 51 additions & 0 deletions packages/module/src/ChatbotHeader/ChatbotHeaderCloseButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';

import { Button, Icon, Tooltip, TooltipProps } from '@patternfly/react-core';
import { CloseIcon } from '@patternfly/react-icons';

export interface ChatbotHeaderCloseButtonProps {
/** Callback function for when button is clicked */
onClick: () => void;
/** Custom classname for the header component */
className?: string;
/** Props spread to the PF Tooltip component wrapping the display mode dropdown */
tooltipProps?: TooltipProps;
/** Aria label for menu */
menuAriaLabel?: string;
/** Ref applied to menu */
innerRef?: React.Ref<HTMLButtonElement>;
/** Content used in tooltip */
tooltipContent?: string;
}

const ChatbotHeaderCloseButtonBase: React.FunctionComponent<ChatbotHeaderCloseButtonProps> = ({
className,
onClick,
tooltipProps,
menuAriaLabel = 'Close',
innerRef,
tooltipContent = 'Close'
}: ChatbotHeaderCloseButtonProps) => (
<div className={`pf-chatbot__menu ${className}`}>
<Tooltip content={tooltipContent} position="bottom" {...tooltipProps}>
<Button
className="pf-chatbot__button--toggle-menu"
variant="plain"
onClick={onClick}
aria-label={menuAriaLabel}
ref={innerRef}
icon={
<Icon size="xl" isInline>
<CloseIcon />
</Icon>
}
/>
</Tooltip>
</div>
);

export const ChatbotHeaderCloseButton = React.forwardRef(
(props: ChatbotHeaderCloseButtonProps, ref: React.Ref<HTMLButtonElement>) => (
<ChatbotHeaderCloseButtonBase innerRef={ref} {...props} />
)
);
7 changes: 5 additions & 2 deletions packages/module/src/ChatbotHeader/ChatbotHeaderMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ export interface ChatbotHeaderMenuProps {
menuAriaLabel?: string;
/** Ref applied to menu */
innerRef?: React.Ref<HTMLButtonElement>;
/** Content used in tooltip */
tooltipContent?: string;
}

const ChatbotHeaderMenuBase: React.FunctionComponent<ChatbotHeaderMenuProps> = ({
className,
onMenuToggle,
tooltipProps,
menuAriaLabel = 'Toggle menu',
innerRef
innerRef,
tooltipContent = 'Menu'
}: ChatbotHeaderMenuProps) => (
<div className={`pf-chatbot__menu ${className}`}>
<Tooltip content="Menu" position="bottom" {...tooltipProps}>
<Tooltip content={tooltipContent} position="bottom" {...tooltipProps}>
<Button
className="pf-chatbot__button--toggle-menu"
variant="plain"
Expand Down
1 change: 1 addition & 0 deletions packages/module/src/ChatbotHeader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './ChatbotHeaderMenu';
export * from './ChatbotHeaderTitle';
export * from './ChatbotHeaderOptionsDropdown';
export * from './ChatbotHeaderSelectorDropdown';
export * from './ChatbotHeaderCloseButton';
9 changes: 9 additions & 0 deletions packages/module/src/Settings/Settings.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
.pf-chatbot__settings-form-container {
width: 100%;
display: flex;
justify-content: center;
overflow: auto;
}

.pf-chatbot__settings-form {
display: flex;
flex-direction: column;
max-width: 60rem;
flex: 1;
}

.pf-chatbot__settings-form-row {
Expand Down
60 changes: 0 additions & 60 deletions packages/module/src/Settings/Settings.tsx

This file was deleted.

25 changes: 25 additions & 0 deletions packages/module/src/Settings/SettingsForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';

export interface SettingsFormProps {
/** Class applied to form container */
className?: string;
/** Array of fields to display in the settings layout */
fields?: { id: string; label: string; field: React.ReactElement }[];
}

export const SettingsForm: React.FunctionComponent<SettingsFormProps> = ({ className, fields = [], ...props }) => (
<div className={`pf-chatbot__settings-form-container ${className}`} {...props}>
<form className="pf-chatbot__settings-form">
{fields.map((field) => (
<div className="pf-chatbot__settings-form-row" key={field.label}>
<label className="pf-chatbot__settings-label" htmlFor={field.id}>
{field.label}
</label>
{field.field}
</div>
))}
</form>
</div>
);

export default SettingsForm;
Loading

0 comments on commit 0466ab9

Please sign in to comment.