-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
react-utils.ts
251 lines (225 loc) Β· 7.74 KB
/
react-utils.ts
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { css as emotionCss, jsx } from '@emotion/core';
import { bool, isArray, isFunction, isObject, isString, uniqueArray } from '@remirror/core-helpers';
import { AnyFunction, PlainObject } from '@remirror/core-types';
import {
Children,
ComponentClass,
ComponentType,
FC,
Fragment,
isValidElement as reactIsValidElement,
LegacyRef,
PropsWithChildren,
ReactElement,
ReactNode,
} from 'react';
export interface RemirrorComponentStaticProperties {
/**
* Identifies this as a remirror specific component
*/
$$remirrorType: RemirrorType;
}
export type RemirrorFC<GProps extends object = {}> = FC<GProps> & RemirrorComponentStaticProperties;
export type RemirrorComponentClass<GProps extends object = {}> = ComponentClass<GProps> &
RemirrorComponentStaticProperties;
export type RemirrorComponentType<GProps extends object = {}> = ComponentType<GProps> &
RemirrorComponentStaticProperties;
export type RemirrorElement<GOptions extends object = any> = ReactElement & {
type: RemirrorComponentType<GOptions>;
};
/**
* These are the constants used to determine whether an element is a remirror constant.
*/
export enum RemirrorType {
Extension = 'extension',
SSR = 'ssr',
EditorProvider = 'editor-provider',
ManagedEditorProvider = 'managed-editor-provider',
Editor = 'editor',
Manager = 'manager',
ManagerProvider = 'manager-provider',
/**
* Used to identify the ContextProviderWrapper
*/
ContextProvider = 'context-provider',
}
/**
* A drop in replacement for built in React.isValidElement which accepts a test value of any type
*
* @param value - the value to check
*/
export const isValidElement = <GProps extends object = any>(value: unknown): value is ReactElement<GProps> =>
isObject(value) && reactIsValidElement(value);
/**
* Check whether a react node is a built in dom element (i.e. `div`, `span`)
*
* @param value - the value to check
*/
export const isReactDOMElement = <GProps extends object = any>(
value: unknown,
): value is ReactElement<GProps> & { type: string } => {
return isValidElement(value) && isString(value.type);
};
/**
* Checks whether the element is a react fragment
*
* @param value - the value to check
*/
export const isReactFragment = <GProps extends object = any>(
value: unknown,
): value is ReactElement<GProps> & { type: typeof Fragment } =>
isObject(value) && isValidElement(value) && value.type === Fragment;
/**
* Retrieve the element props for JSX Element
*
* @param element
*/
export const getElementProps = (element: JSX.Element): PlainObject & { children: JSX.Element } => {
return isValidElement(element) ? element.props : {};
};
/**
* Utility for generating a unique class name
*
* @param uid
* @param className
*/
export const uniqueClass = (uid: string, className: string) => `${className}-${uid}`;
/**
* Utility for properly typechecking static defaultProps for a class component in react.
*
* ```ts
* static defaultProps = asDefaultProps<RemirrorProps>()({
* initialContent: EMPTY_PARAGRAPH_NODE,
* });
* ```
*/
export const asDefaultProps = <GProps extends object>() => <GDefaultProps extends Partial<GProps>>(
props: GDefaultProps,
): GDefaultProps => props;
/**
* Checks if this element has a type of any RemirrorComponent
*
* @param value - the value to check
*/
export const isRemirrorElement = <GOptions extends object = any>(
value: unknown,
): value is RemirrorElement<GOptions> => {
return bool(
isObject(value) &&
isValidElement(value) &&
(value.type as RemirrorComponentType<GOptions>).$$remirrorType,
);
};
const isRemirrorElementOfType = (type: RemirrorType) => <GOptions extends object = any>(
value: unknown,
): value is RemirrorElement<GOptions> => isRemirrorElement(value) && value.type.$$remirrorType === type;
/**
* Checks to see if this is the wrapper we've created around the RemirrorContent.Provider component.
*
* This is used to help determine how the Remirror component will be rendered. `getRootProps` is the main reason
* for this, and I'm not even sure the effort is worth it.
*
* @param value - the value to check
*/
export const isRemirrorContextProvider = isRemirrorElementOfType(RemirrorType.ContextProvider);
/**
* Checks if this is a RemirrorExtension type. These are used to configure the extensions that determine
* the underlying behaviour of the editor.
*
* @param value - the value to check
*/
export const isRemirrorExtension = isRemirrorElementOfType(RemirrorType.Extension);
/**
* Finds if this is a RemirrorProvider (which provides the RemirrorInjectedProps into the context);
*
* @param value - the value to check
*/
export const isRemirrorProvider = isRemirrorElementOfType(RemirrorType.EditorProvider);
/**
* Checks if this is a ManagedRemirrorProvider which pulls in the manager from the context and places it's children
* inside the RemirrorProvider
*
* @param value - the value to check
*/
export const isManagedRemirrorProvider = isRemirrorElementOfType(RemirrorType.ManagedEditorProvider);
/**
* Clones an element while also enabling the css prop on jsx elements at the same time.
* This is used for emotion which needs to inject the css property which React.cloneElement doesn't support.
*
* @param element - the element to clone
* @param props - the props to pass through to the cloned element
* @param rest - the children of the cloned element
*
* @returns a cloned react element with builtin support for the emotion `css` props
*/
export const cloneElement = <GProps extends PropsWithChildren<{ ref?: LegacyRef<any> }> = any>(
element: ReactElement<GProps>,
props: GProps,
...rest: ReactNode[]
) => {
const children = uniqueArray([
...(isArray(props.children) ? props.children : props.children ? [props.children] : []),
...rest,
]);
return jsx(
element.type,
{
key: element.key,
ref: element.props.ref,
...element.props,
...props,
},
...children,
);
};
/**
* Will throw an error if the child provided is not a function.
*
* @remarks
* This is currently used in the remirror component to throw an error when the element children
* are not a render prop. It should be called outside of render for class Components.
*
* @param prop - the prop to test
*/
export const propIsFunction = (prop: unknown): prop is AnyFunction => {
if (!isFunction(prop)) {
throw new Error('The child argument to the Remirror component must be a function.');
}
return true;
};
/**
* A noop function that mimics the css emotion call but renders no output.
*
* @remarks
* This is useful for enabling the library user to switch off emotion for core react elements.
*/
export const cssNoOp: typeof emotionCss = () => undefined as any;
/**
* A css function that just returns the string.
* This is typically used for css syntax highlighting of plain strings in editors.
*/
export const css = bool(String.raw) ? String.raw : (template: TemplateStringsArray) => template[0];
/**
* A drop in replacement for React.Children.only which provides more readable errors
* when the child is not a react element or undefined.
*/
export const oneChildOnly = <GProps extends object = any>(value: unknown): ReactElement<GProps> => {
if (!value) {
throw new Error('This component requires ONE child component - Nothing was provided');
}
if (!isValidElement(value)) {
throw new Error('This component requires ONE child component - An invalid element was provided');
}
return Children.only(value);
};
/**
* Add the specified key to an element when it is a valid react element.
*
* This is useful when returning an array of components because a fragment isn't sufficient.
*/
export const addKeyToElement = (element: ReactNode, key: string | number) => {
if (!isValidElement(element)) {
return element;
}
return cloneElement(element, { ...element.props, key });
};