-
Notifications
You must be signed in to change notification settings - Fork 112
/
ChatComposer.tsx
147 lines (142 loc) · 4.23 KB
/
ChatComposer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Box } from "@twilio-paste/box";
import type { BoxProps, BoxStyleProps } from "@twilio-paste/box";
import {
// The component that renders the content editable div
ContentEditable,
/*
* ErrorBoundary catches errors in any of the children
* https://reactjs.org/docs/error-boundaries.html
*/
ErrorBoundary,
/**
* Plugin that adds history stack (allows for undo/redo)
* https://lexical.dev/docs/react/plugins#lexicalonchangeplugin
*/
HistoryPlugin,
// Create and manages the Lexical editor instance
LexicalComposer,
/**
* Plugin that calls the OnChange function when the state changes
* https://lexical.dev/docs/react/plugins#lexicalonchangeplugin
*/
OnChangePlugin,
/*
* Adds the ability to edit the text, also support for bold/italic/underline
* https://lexical.dev/docs/react/plugins#lexicalrichtextplugin
*/
RichTextPlugin,
} from "@twilio-paste/lexical-library";
import type { ContentEditableProps, LexicalComposerProps, OnChangeFunction } from "@twilio-paste/lexical-library";
import { StylingGlobals } from "@twilio-paste/styling-library";
import merge from "deepmerge";
import * as React from "react";
import { AutoLinkPlugin } from "./AutoLinkPlugin";
import { PlaceholderWrapper } from "./PlaceholderWrapper";
import { baseConfig, renderInitialText } from "./helpers";
import { chatComposerLexicalStyles } from "./styles";
export interface ChatComposerProps extends Omit<ContentEditableProps, "style" | "className" | "onChange"> {
children?: LexicalComposerProps["children"];
/**
* Utilize the Lexical API directly
*
* @default null
* @type {LexicalComposerProps["initialConfig"]}
* @memberof ChatComposerProps
*/
config: LexicalComposerProps["initialConfig"];
/**
* Overrides the default element name to apply unique styles with the Customization Provider
*
* @default "CHAT_COMPOSER"
* @type {BoxProps["element"]}
* @memberof ChatComposerProps
*/
element?: BoxProps["element"];
/**
*
* @default null
* @type {BoxStyleProps["maxHeight"]}
* @memberof ChatComposerProps
*/
maxHeight?: BoxStyleProps["maxHeight"];
/**
*
* @default null
* @type {string}
* @memberof ChatComposerProps
*/
initialValue?: string;
/**
*
* @default null
* @type {boolean}
* @memberof ChatComposerProps
*/
disabled?: boolean;
/**
*
* @default null
* @type {OnChangeFunction}
* @memberof ChatComposerProps
*/
onChange?: OnChangeFunction;
}
export const ChatComposer = React.forwardRef<HTMLDivElement, ChatComposerProps>(
(
{
children,
element = "CHAT_COMPOSER",
onChange,
placeholder = "",
initialValue,
config,
maxHeight,
disabled,
...props
},
ref,
) => {
const baseConfigWithEditorState = {
...baseConfig,
editable: disabled ? false : true,
editorState: initialValue ? () => renderInitialText(initialValue) : undefined,
};
return (
<Box
flexGrow={1}
ref={ref}
element={element}
position="relative"
transition="background-color 100ms ease-in,box-shadow 100ms ease-in,color 100ms ease-in"
paddingY="space30"
paddingX="space40"
borderRadius="borderRadius20"
_focusWithin={{ boxShadow: "shadowFocus" }}
overflowY="auto"
maxHeight={maxHeight}
disabled={disabled}
aria-disabled={disabled}
_disabled={{
color: "colorTextWeaker",
backgroundColor: "colorBackground",
}}
>
<StylingGlobals styles={chatComposerLexicalStyles} />
<LexicalComposer initialConfig={merge(baseConfigWithEditorState, config)}>
<>
<RichTextPlugin
contentEditable={<ContentEditable {...props} className="paste-chat-composer-content-editable" />}
placeholder={<PlaceholderWrapper element={`${element}_PLACEHOLDER_WRAPPER`} placeholder={placeholder} />}
ErrorBoundary={ErrorBoundary}
/>
{onChange && <OnChangePlugin onChange={onChange} />}
<HistoryPlugin />
<AutoLinkPlugin />
{children}
</>
</LexicalComposer>
</Box>
);
},
);
ChatComposer.displayName = "ChatComposer";