Skip to content
Merged
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
7 changes: 7 additions & 0 deletions .changelog/6036-fix-css-modules-send-box.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Changed

- Converted the send box layout and text box to CSS Modules with `webchat__` class names and their style options to custom properties, in PR [#6036](https://github.com/microsoft/BotFramework-WebChat/pull/6036), by [@OEvgeny](https://github.com/OEvgeny)

### Removed

- 💥 Deprecated the `autoResizeTextArea`, `dictationInterims`, `sendBox` and `sendBoxTextBox` style set entries, which no longer affect rendering, in PR [#6036](https://github.com/microsoft/BotFramework-WebChat/pull/6036), by [@OEvgeny](https://github.com/OEvgeny)
28 changes: 11 additions & 17 deletions __tests__/html2/sendBox/focusSendBackToSendBox.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<html lang="en-US">
<head>
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
<style>
/* Shows where the focus is. */
#webchat .webchat__send-box-text-box__input:focus {
background-color: Yellow;
}
</style>
</head>
<body>
<main id="webchat"></main>
Expand All @@ -18,7 +24,7 @@
import '/test-harness.mjs';
import '/test-page-object.mjs';

import { createStoreWithOptions, createStyleSet, renderWebChat, testIds } from 'botframework-webchat';
import { createStoreWithOptions, renderWebChat, testIds } from 'botframework-webchat';

const {
testHelpers: { createDirectLineEmulator }
Expand All @@ -30,22 +36,7 @@
run(async function () {
const { directLine, store } = createDirectLineEmulator();

const styleSet = createStyleSet({});

renderWebChat(
{
directLine,
store,
styleSet: Object.assign({}, styleSet, {
sendBoxTextBox: Object.assign({}, styleSet.sendBoxTextBox, {
'& .webchat__send-box-text-box__input:focus': {
backgroundColor: 'Yellow'
}
})
})
},
document.getElementById('webchat')
);
renderWebChat({ directLine, store }, document.getElementById('webchat'));

await pageConditions.uiConnected();

Expand All @@ -59,6 +50,9 @@

await pageConditions.minNumActivitiesShown(2);

// THEN: The focus is back on the send box, which is yellow.
expect(document.activeElement).toBe(pageElements.sendBoxTextBox());

await host.snapshot('local');
});
</script>
Expand Down
56 changes: 56 additions & 0 deletions __tests__/html2/styleOptions/sendBox.disabled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!doctype html>
<html lang="en-US">
<head>
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<main id="webchat"></main>
<script type="importmap">
{
"imports": {
"botframework-webchat": "/__dist__/packages/bundle/static/botframework-webchat.js",
"react": "/__dist__/packages/bundle/static/react.js",
"react-dom": "/__dist__/packages/bundle/static/react-dom.js"
}
}
</script>
<script type="module">
import '/test-harness.mjs';
import '/test-page-object.mjs';

import { createStoreWithOptions, renderWebChat, testIds } from 'botframework-webchat';
import { waitFor } from 'https://esm.sh/@testduet/wait-for';

// TODO: Should find ways to eliminate this line.
window.WebChat = { createStoreWithOptions, testIds };

run(async function () {
const { directLine, store } = testHelpers.createDirectLineEmulator();

// GIVEN: A disabled Web Chat with a disabled text color.
renderWebChat(
{
directLine,
disabled: true,
store,
styleOptions: {
sendBoxDisabledTextColor: 'DarkOrange',
sendBoxTextColor: 'DarkGreen' // Not used while disabled.
}
},
document.getElementById('webchat')
);

await pageConditions.uiConnected();

// WHEN: The send box has text.
store.dispatch({ type: 'WEB_CHAT/SET_SEND_BOX', payload: { text: 'Hello, World!' } });

await waitFor(() => expect(pageElements.sendBoxTextBox().value).toBe('Hello, World!'));

// THEN: The text is dark orange.
await host.snapshot('local');
});
</script>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions __tests__/html2/styleOptions/sendBox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!doctype html>
<html lang="en-US">
<head>
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<main id="webchat"></main>
<script type="importmap">
{
"imports": {
"botframework-webchat": "/__dist__/packages/bundle/static/botframework-webchat.js",
"react": "/__dist__/packages/bundle/static/react.js",
"react-dom": "/__dist__/packages/bundle/static/react-dom.js"
}
}
</script>
<script type="module">
import '/test-harness.mjs';
import '/test-page-object.mjs';

import { createStoreWithOptions, renderWebChat, testIds } from 'botframework-webchat';
import { waitFor } from 'https://esm.sh/@testduet/wait-for';

// TODO: Should find ways to eliminate this line.
window.WebChat = { createStoreWithOptions, testIds };

run(async function () {
const { directLine, store } = testHelpers.createDirectLineEmulator();

// GIVEN: Every send box style option of the layout and the text box set to a value other than its default.
renderWebChat(
{
directLine,
store,
styleOptions: {
sendBoxBackground: 'LightYellow',
sendBoxBorderBottom: 'dotted 4px Blue',
sendBoxBorderLeft: 'solid 4px Red',
sendBoxBorderRight: 'dashed 4px Green',
sendBoxBorderTop: 'solid 4px Purple',
sendBoxHeight: 80, // Numbers are in pixels.
sendBoxMaxHeight: '60px',
sendBoxPlaceholderColor: 'Crimson',
sendBoxTextColor: 'DarkGreen',
sendBoxTextWrap: true // The maximum height applies to the multi-line text box.
}
},
document.getElementById('webchat')
);

await pageConditions.uiConnected();

// THEN: An 80 pixels tall, light yellow send box with a purple top, green dashed right, blue dotted bottom and red left border, and a crimson placeholder.
await host.snapshot('local');

// WHEN: The send box has 6 lines of text.
store.dispatch({ type: 'WEB_CHAT/SET_SEND_BOX', payload: { text: 'One\nTwo\nThree\nFour\nFive\nSix' } });

await waitFor(() => expect(pageElements.sendBoxTextBox().value).toBe('One\nTwo\nThree\nFour\nFive\nSix'));

// THEN: The text is dark green and the text box stops growing at 60 pixels, showing a scroll bar.
await host.snapshot('local');
});
</script>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 7 additions & 6 deletions packages/component/src/SendBox/AutoResizeTextArea.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import classNames from 'classnames';
import { useStyles, webChatPrefixedClassName } from '@msinternal/botframework-webchat-styles/react';
import cx from 'classnames';
import React, {
ChangeEventHandler,
FocusEventHandler,
Expand All @@ -10,7 +11,7 @@ import React, {

import AccessibleTextArea from '../Utils/AccessibleTextArea';
import useEnterKeyHint from '../hooks/internal/useEnterKeyHint';
import useStyleSet from '../hooks/useStyleSet';
import styles from './SendBox.module.css';

type AutoResizeTextAreaProps = Readonly<{
'aria-errormessage'?: string | undefined;
Expand Down Expand Up @@ -61,20 +62,20 @@ const AutoResizeTextArea = forwardRef<HTMLTextAreaElement, AutoResizeTextAreaPro
},
ref
) => {
const [{ autoResizeTextArea: autoResizeTextAreaStyleSet }] = useStyleSet();
const classNames = useStyles(styles, webChatPrefixedClassName);

useEnterKeyHint(ref, enterKeyHint);

return (
<div className={classNames('webchat__auto-resize-textarea', autoResizeTextAreaStyleSet + '', className)}>
<div className={cx(classNames['auto-resize-textarea'], className)}>
{/* We need to add a space here, so blank lines will be counted in the doppelganger. */}
<div aria-hidden={true} className="webchat__auto-resize-textarea__doppelganger">
<div aria-hidden={true} className={classNames['auto-resize-textarea__doppelganger']}>
{value}&nbsp;
</div>
<AccessibleTextArea
aria-errormessage={ariaErrorMessage}
aria-label={ariaLabel}
className={classNames('webchat__auto-resize-textarea__textarea', textAreaClassName)}
className={cx(classNames['auto-resize-textarea__textarea'], textAreaClassName)}
data-id={dataId}
data-testid={dataTestId}
disabled={disabled}
Expand Down
62 changes: 16 additions & 46 deletions packages/component/src/SendBox/BasicSendBox.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { SendBoxToolbarMiddlewareProxy, hooks } from 'botframework-webchat-api';
import { validateProps } from '@msinternal/botframework-webchat-react-valibot';
import { useStyles, webChatPrefixedClassName } from '@msinternal/botframework-webchat-styles/react';
import { Constants } from 'botframework-webchat-core';
import classNames from 'classnames';
import cx from 'classnames';
import React, { memo, useCallback, type ClipboardEventHandler } from 'react';
import { useRefFrom } from 'use-ref-from';
import { object, optional, pipe, readonly, string, type InferInput } from 'valibot';

import { useStyleToEmotionObject } from '../hooks/internal/styleToEmotionObject';
import useMakeThumbnail from '../hooks/useMakeThumbnail';
import useStyleSet from '../hooks/useStyleSet';
import useWebSpeechPonyfill from '../hooks/useWebSpeechPonyfill';
import useErrorMessageId from '../providers/internal/SendBox/useErrorMessageId';
import useSubmit from '../providers/internal/SendBox/useSubmit';
import { AttachmentBar } from './AttachmentBar/index';
import DictationInterims from './DictationInterims';
import DropZone from './DropZone';
import MicrophoneButton from './MicrophoneButton';
import styles from './SendBox.module.css';
import SendButton from './SendButton';
import SuggestedActions from './SuggestedActions';
import TextBox from './TextBox';
Expand All @@ -26,34 +26,6 @@ const {

const { useDirection, useDictateState, useSendBoxAttachments, useStyleOptions, useUIState } = hooks;

const ROOT_STYLE = {
'&.webchat__send-box': {
display: 'grid',
gridTemplateAreas: '"suggested-actions" "content"',
gridTemplateRows: 'auto 1fr',

'& .webchat__suggested-actions': {
gridArea: 'suggested-actions',
minWidth: '0'
},

'& .webchat__send-box__main': {
gridArea: 'content',
minWidth: '0'
},

'& .webchat__drop-zone': {
gridArea: 'content',
minWidth: '0'
},

'& .webchat__send-box__button': { flexShrink: 0 },
'& .webchat__send-box__dictation-interims': { flex: 10000 },
'& .webchat__send-box__microphone-button': { flex: 1 },
'& .webchat__send-box__text-box': { flex: 10000 }
}
};

function useSendBoxSpeechInterimsVisible(): [boolean] {
const [dictateState] = useDictateState();

Expand All @@ -73,19 +45,16 @@ function BasicSendBox(props: BasicSendBoxProps) {
const { className } = validateProps(basicSendBoxPropsSchema, props);

const [{ disableFileUpload, sendAttachmentOn, sendBoxButtonAlignment }] = useStyleOptions();
const [{ sendBox: sendBoxStyleSet }] = useStyleSet();
const classNames = useStyles(styles, webChatPrefixedClassName);
const [{ SpeechRecognition = undefined } = {}] = useWebSpeechPonyfill();
const [direction] = useDirection();
const [errorMessageId] = useErrorMessageId();
const [sendBoxAttachments, setSendBoxAttachments] = useSendBoxAttachments();
const [speechInterimsVisible] = useSendBoxSpeechInterimsVisible();
const [uiState] = useUIState();
const makeThumbnail = useMakeThumbnail();
const styleToEmotionObject = useStyleToEmotionObject();
const submit = useSubmit();

const rootClassName = styleToEmotionObject(ROOT_STYLE) + '';

const disabled = uiState === 'disabled';
const sendAttachmentOnRef = useRefFrom(sendAttachmentOn);
const sendBoxAttachmentsRef = useRefFrom(sendBoxAttachments);
Expand Down Expand Up @@ -127,35 +96,36 @@ function BasicSendBox(props: BasicSendBoxProps) {
[disabled, disableFileUpload, handleAddFiles]
);

const buttonClassName = classNames('webchat__send-box__button', {
'webchat__send-box__button--align-bottom': sendBoxButtonAlignment === 'bottom',
'webchat__send-box__button--align-stretch': sendBoxButtonAlignment !== 'bottom' && sendBoxButtonAlignment !== 'top',
'webchat__send-box__button--align-top': sendBoxButtonAlignment === 'top'
const buttonClassName = cx(classNames['send-box__button'], {
[classNames['send-box__button--align-bottom']]: sendBoxButtonAlignment === 'bottom',
[classNames['send-box__button--align-stretch']]:
sendBoxButtonAlignment !== 'bottom' && sendBoxButtonAlignment !== 'top',
[classNames['send-box__button--align-top']]: sendBoxButtonAlignment === 'top'
});

return (
<div
aria-describedby={errorMessageId}
aria-errormessage={errorMessageId}
aria-invalid={!!errorMessageId || undefined}
className={classNames('webchat__send-box', sendBoxStyleSet + '', rootClassName + '', (className || '') + '')}
className={cx(classNames['send-box'], className)}
dir={direction}
onPaste={handlePaste}
role="form"
>
<SuggestedActions />
<div className="webchat__send-box__main">
<AttachmentBar className="webchat__send-box__attachment-bar" />
<div className={classNames['send-box__main']}>
<AttachmentBar className={classNames['send-box__attachment-bar']} />
<SendBoxToolbarMiddlewareProxy className={buttonClassName} request={undefined} />
<div className="webchat__send-box__editable">
<div className={classNames['send-box__editable']}>
{speechInterimsVisible ? (
<DictationInterims className="webchat__send-box__dictation-interims" />
<DictationInterims className={classNames['send-box__dictation-interims']} />
) : (
<TextBox className="webchat__send-box__text-box" />
<TextBox className={classNames['send-box__text-box']} />
)}
</div>
{supportSpeechRecognition ? (
<MicrophoneButton className={classNames(buttonClassName, 'webchat__send-box__microphone-button')} />
<MicrophoneButton className={cx(buttonClassName, classNames['send-box__microphone-button'])} />
) : (
<SendButton className={buttonClassName} />
)}
Expand Down
Loading
Loading