@uiw/react-codemirror 4.9.5 → 4.9.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +3 -8
- package/cjs/index.js.map +2 -4
- package/cjs/useCodeMirror.d.ts +2 -2
- package/cjs/useCodeMirror.js +6 -2
- package/cjs/useCodeMirror.js.map +2 -2
- package/dist/mdeditor.js +39 -21
- package/dist/mdeditor.min.js +1 -1
- package/esm/index.js +6 -9
- package/esm/index.js.map +3 -4
- package/esm/useCodeMirror.d.ts +2 -2
- package/esm/useCodeMirror.js +4 -2
- package/esm/useCodeMirror.js.map +2 -2
- package/package.json +1 -1
- package/src/index.tsx +9 -7
- package/src/useCodeMirror.ts +4 -2
package/esm/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import _extends from "@babel/runtime/helpers/extends";
|
|
2
2
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
|
|
3
3
|
var _excluded = ["className", "value", "selection", "extensions", "onChange", "onUpdate", "autoFocus", "theme", "height", "minHeight", "maxHeight", "width", "minWidth", "maxWidth", "basicSetup", "placeholder", "indentWithTab", "editable", "readOnly", "root"];
|
|
4
|
-
import React, {
|
|
4
|
+
import React, { useRef, forwardRef, useImperativeHandle } from 'react';
|
|
5
5
|
import { useCodeMirror } from './useCodeMirror';
|
|
6
6
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
7
7
|
export * from './basicSetup';
|
|
8
8
|
export * from './useCodeMirror';
|
|
9
|
-
var ReactCodeMirror = /*#__PURE__*/
|
|
9
|
+
var ReactCodeMirror = /*#__PURE__*/forwardRef((props, ref) => {
|
|
10
10
|
var {
|
|
11
11
|
className,
|
|
12
12
|
value = '',
|
|
@@ -60,13 +60,10 @@ var ReactCodeMirror = /*#__PURE__*/React.forwardRef((props, ref) => {
|
|
|
60
60
|
extensions
|
|
61
61
|
});
|
|
62
62
|
useImperativeHandle(ref, () => ({
|
|
63
|
-
editor:
|
|
64
|
-
state,
|
|
65
|
-
view
|
|
66
|
-
}), [container, state, view]);
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
setContainer(editor.current); // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
69
|
-
}, []); // check type of value
|
|
63
|
+
editor: editor.current,
|
|
64
|
+
state: state,
|
|
65
|
+
view: view
|
|
66
|
+
}), [editor, container, state, view]); // check type of value
|
|
70
67
|
|
|
71
68
|
if (typeof value !== 'string') {
|
|
72
69
|
throw new Error("value must be typeof string but got " + typeof value);
|
package/esm/index.js.map
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"names": [
|
|
4
4
|
"React",
|
|
5
|
-
"useEffect",
|
|
6
5
|
"useRef",
|
|
6
|
+
"forwardRef",
|
|
7
7
|
"useImperativeHandle",
|
|
8
8
|
"useCodeMirror",
|
|
9
9
|
"ReactCodeMirror",
|
|
10
|
-
"forwardRef",
|
|
11
10
|
"props",
|
|
12
11
|
"ref",
|
|
13
12
|
"className",
|
|
@@ -45,7 +44,7 @@
|
|
|
45
44
|
"../src/index.tsx"
|
|
46
45
|
],
|
|
47
46
|
"sourcesContent": [
|
|
48
|
-
"import React, {
|
|
47
|
+
"import React, { useRef, forwardRef, useImperativeHandle } from 'react';\nimport { EditorState, EditorStateConfig, Extension } from '@codemirror/state';\nimport { EditorView, ViewUpdate } from '@codemirror/view';\nimport { useCodeMirror } from './useCodeMirror';\nimport { BasicSetupOptions } from './basicSetup';\n\nexport * from './basicSetup';\nexport * from './useCodeMirror';\n\nexport interface ReactCodeMirrorProps\n extends Omit<EditorStateConfig, 'doc' | 'extensions'>,\n Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'placeholder'> {\n /** value of the auto created model in the editor. */\n value?: string;\n height?: string;\n minHeight?: string;\n maxHeight?: string;\n width?: string;\n minWidth?: string;\n maxWidth?: string;\n /** focus on the editor. */\n autoFocus?: boolean;\n /** Enables a placeholder—a piece of example content to show when the editor is empty. */\n placeholder?: string | HTMLElement;\n /**\n * `light` / `dark` / `Extension` Defaults to `light`.\n * @default light\n */\n theme?: 'light' | 'dark' | Extension;\n /**\n * Whether to optional basicSetup by default\n * @default true\n */\n basicSetup?: boolean | BasicSetupOptions;\n /**\n * This disables editing of the editor content by the user.\n * @default true\n */\n editable?: boolean;\n /**\n * This disables editing of the editor content by the user.\n * @default false\n */\n readOnly?: boolean;\n /**\n * Whether to optional basicSetup by default\n * @default true\n */\n indentWithTab?: boolean;\n /** Fired whenever a change occurs to the document. */\n onChange?(value: string, viewUpdate: ViewUpdate): void;\n /** Fired whenever any state change occurs within the editor, including non-document changes like lint results. */\n onUpdate?(viewUpdate: ViewUpdate): void;\n /**\n * Extension values can be [provided](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions) when creating a state to attach various kinds of configuration and behavior information.\n * They can either be built-in extension-providing objects,\n * such as [state fields](https://codemirror.net/6/docs/ref/#state.StateField) or [facet providers](https://codemirror.net/6/docs/ref/#state.Facet.of),\n * or objects with an extension in its `extension` property. Extensions can be nested in arrays arbitrarily deep—they will be flattened when processed.\n */\n extensions?: Extension[];\n /**\n * If the view is going to be mounted in a shadow root or document other than the one held by the global variable document (the default), you should pass it here.\n * Originally from the [config of EditorView](https://codemirror.net/6/docs/ref/#view.EditorView.constructor%5Econfig.root)\n */\n root?: ShadowRoot | Document;\n}\n\nexport interface ReactCodeMirrorRef {\n editor?: HTMLDivElement | null;\n state?: EditorState;\n view?: EditorView;\n}\n\nconst ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props, ref) => {\n const {\n className,\n value = '',\n selection,\n extensions = [],\n onChange,\n onUpdate,\n autoFocus,\n theme = 'light',\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n basicSetup,\n placeholder,\n indentWithTab,\n editable,\n readOnly,\n root,\n ...other\n } = props;\n const editor = useRef<HTMLDivElement>(null);\n const { state, view, container, setContainer } = useCodeMirror({\n container: editor.current,\n root,\n value,\n autoFocus,\n theme,\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n basicSetup,\n placeholder,\n indentWithTab,\n editable,\n readOnly,\n selection,\n onChange,\n onUpdate,\n extensions,\n });\n\n useImperativeHandle(ref, () => ({ editor: editor.current, state: state, view: view }), [\n editor,\n container,\n state,\n view,\n ]);\n\n // check type of value\n if (typeof value !== 'string') {\n throw new Error(`value must be typeof string but got ${typeof value}`);\n }\n\n const defaultClassNames = typeof theme === 'string' ? `cm-theme-${theme}` : 'cm-theme';\n return <div ref={editor} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...other}></div>;\n});\n\nReactCodeMirror.displayName = 'CodeMirror';\n\nexport default ReactCodeMirror;\n"
|
|
49
48
|
],
|
|
50
|
-
"mappings": ";;;AAAA,OAAOA,KAAP,IAAgBC,
|
|
49
|
+
"mappings": ";;;AAAA,OAAOA,KAAP,IAAgBC,MAAhB,EAAwBC,UAAxB,EAAoCC,mBAApC,QAA+D,OAA/D;AAGA,SAASC,aAAT,QAA8B,iBAA9B;;AAGA,cAAc,cAAd;AACA,cAAc,iBAAd;AAkEA,IAAMC,eAAe,gBAAGH,UAAU,CAA2C,CAACI,KAAD,EAAQC,GAAR,KAAgB;EAC3F,IAAM;IACJC,SADI;IAEJC,KAAK,GAAG,EAFJ;IAGJC,SAHI;IAIJC,UAAU,GAAG,EAJT;IAKJC,QALI;IAMJC,QANI;IAOJC,SAPI;IAQJC,KAAK,GAAG,OARJ;IASJC,MATI;IAUJC,SAVI;IAWJC,SAXI;IAYJC,KAZI;IAaJC,QAbI;IAcJC,QAdI;IAeJC,UAfI;IAgBJC,WAhBI;IAiBJC,aAjBI;IAkBJC,QAlBI;IAmBJC,QAnBI;IAoBJC;EApBI,IAsBFrB,KAtBJ;EAAA,IAqBKsB,KArBL,iCAsBItB,KAtBJ;;EAuBA,IAAMuB,MAAM,GAAG5B,MAAM,CAAiB,IAAjB,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
|
|
|
9
9
|
setState: import("react").Dispatch<import("react").SetStateAction<EditorState | undefined>>;
|
|
10
10
|
view: EditorView | undefined;
|
|
11
11
|
setView: import("react").Dispatch<import("react").SetStateAction<EditorView | undefined>>;
|
|
12
|
-
container: HTMLDivElement |
|
|
13
|
-
setContainer: import("react").Dispatch<import("react").SetStateAction<HTMLDivElement |
|
|
12
|
+
container: HTMLDivElement | undefined;
|
|
13
|
+
setContainer: import("react").Dispatch<import("react").SetStateAction<HTMLDivElement | undefined>>;
|
|
14
14
|
};
|
package/esm/useCodeMirror.js
CHANGED
|
@@ -26,7 +26,7 @@ export function useCodeMirror(props) {
|
|
|
26
26
|
basicSetup: defaultBasicSetup = true,
|
|
27
27
|
root
|
|
28
28
|
} = props;
|
|
29
|
-
var [container, setContainer] = useState(
|
|
29
|
+
var [container, setContainer] = useState();
|
|
30
30
|
var [view, setView] = useState();
|
|
31
31
|
var [state, setState] = useState();
|
|
32
32
|
var defaultLightThemeOption = EditorView.theme({
|
|
@@ -121,10 +121,12 @@ export function useCodeMirror(props) {
|
|
|
121
121
|
|
|
122
122
|
return () => {
|
|
123
123
|
if (view) {
|
|
124
|
+
setState(undefined);
|
|
124
125
|
setView(undefined);
|
|
125
126
|
}
|
|
126
|
-
};
|
|
127
|
+
};
|
|
127
128
|
}, [container, state]);
|
|
129
|
+
useEffect(() => setContainer(props.container), [props.container]);
|
|
128
130
|
useEffect(() => () => {
|
|
129
131
|
if (view) {
|
|
130
132
|
view.destroy();
|
package/esm/useCodeMirror.js.map
CHANGED
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
"../src/useCodeMirror.ts"
|
|
74
74
|
],
|
|
75
75
|
"sourcesContent": [
|
|
76
|
-
"import { useEffect, useState } from 'react';\nimport { EditorState, StateEffect } from '@codemirror/state';\nimport { indentWithTab } from '@codemirror/commands';\nimport { EditorView, keymap, ViewUpdate, placeholder } from '@codemirror/view';\nimport { oneDark } from '@codemirror/theme-one-dark';\nimport { basicSetup } from './basicSetup';\nimport { ReactCodeMirrorProps } from '.';\n\nexport interface UseCodeMirror extends ReactCodeMirrorProps {\n container?: HTMLDivElement | null;\n}\n\nexport function useCodeMirror(props: UseCodeMirror) {\n const {\n value,\n selection,\n onChange,\n onUpdate,\n extensions = [],\n autoFocus,\n theme = 'light',\n height = '',\n minHeight = '',\n maxHeight = '',\n placeholder: placeholderStr = '',\n width = '',\n minWidth = '',\n maxWidth = '',\n editable = true,\n readOnly = false,\n indentWithTab: defaultIndentWithTab = true,\n basicSetup: defaultBasicSetup = true,\n root,\n } = props;\n const [container, setContainer] = useState(
|
|
76
|
+
"import { useEffect, useState } from 'react';\nimport { EditorState, StateEffect } from '@codemirror/state';\nimport { indentWithTab } from '@codemirror/commands';\nimport { EditorView, keymap, ViewUpdate, placeholder } from '@codemirror/view';\nimport { oneDark } from '@codemirror/theme-one-dark';\nimport { basicSetup } from './basicSetup';\nimport { ReactCodeMirrorProps } from '.';\n\nexport interface UseCodeMirror extends ReactCodeMirrorProps {\n container?: HTMLDivElement | null;\n}\n\nexport function useCodeMirror(props: UseCodeMirror) {\n const {\n value,\n selection,\n onChange,\n onUpdate,\n extensions = [],\n autoFocus,\n theme = 'light',\n height = '',\n minHeight = '',\n maxHeight = '',\n placeholder: placeholderStr = '',\n width = '',\n minWidth = '',\n maxWidth = '',\n editable = true,\n readOnly = false,\n indentWithTab: defaultIndentWithTab = true,\n basicSetup: defaultBasicSetup = true,\n root,\n } = props;\n const [container, setContainer] = useState<HTMLDivElement>();\n const [view, setView] = useState<EditorView>();\n const [state, setState] = useState<EditorState>();\n const defaultLightThemeOption = EditorView.theme(\n {\n '&': {\n backgroundColor: '#fff',\n },\n },\n {\n dark: false,\n },\n );\n const defaultThemeOption = EditorView.theme({\n '&': {\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n },\n });\n const updateListener = EditorView.updateListener.of((vu: ViewUpdate) => {\n if (vu.docChanged && typeof onChange === 'function') {\n const doc = vu.state.doc;\n const value = doc.toString();\n onChange(value, vu);\n }\n });\n\n let getExtensions = [updateListener, defaultThemeOption];\n if (defaultIndentWithTab) {\n getExtensions.unshift(keymap.of([indentWithTab]));\n }\n if (defaultBasicSetup) {\n if (typeof defaultBasicSetup === 'boolean') {\n getExtensions.unshift(basicSetup());\n } else {\n getExtensions.unshift(basicSetup(defaultBasicSetup));\n }\n }\n\n if (placeholderStr) {\n getExtensions.unshift(placeholder(placeholderStr));\n }\n\n switch (theme) {\n case 'light':\n getExtensions.push(defaultLightThemeOption);\n break;\n case 'dark':\n getExtensions.push(oneDark);\n break;\n default:\n getExtensions.push(theme);\n break;\n }\n\n if (editable === false) {\n getExtensions.push(EditorView.editable.of(false));\n }\n if (readOnly) {\n getExtensions.push(EditorState.readOnly.of(true));\n }\n\n if (onUpdate && typeof onUpdate === 'function') {\n getExtensions.push(EditorView.updateListener.of(onUpdate));\n }\n getExtensions = getExtensions.concat(extensions);\n\n useEffect(() => {\n if (container && !state) {\n const stateCurrent = EditorState.create({\n doc: value,\n selection,\n extensions: getExtensions,\n });\n setState(stateCurrent);\n if (!view) {\n const viewCurrent = new EditorView({\n state: stateCurrent,\n parent: container,\n root,\n });\n setView(viewCurrent);\n }\n }\n return () => {\n if (view) {\n setState(undefined);\n setView(undefined);\n }\n };\n }, [container, state]);\n\n useEffect(() => setContainer(props.container!), [props.container]);\n\n useEffect(\n () => () => {\n if (view) {\n view.destroy();\n setView(undefined);\n }\n },\n [view],\n );\n\n useEffect(() => {\n if (autoFocus && view) {\n view.focus();\n }\n }, [autoFocus, view]);\n\n useEffect(() => {\n if (view) {\n view.dispatch({ effects: StateEffect.reconfigure.of(getExtensions) });\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [\n theme,\n extensions,\n height,\n minHeight,\n maxHeight,\n width,\n minWidth,\n maxWidth,\n placeholderStr,\n editable,\n readOnly,\n defaultIndentWithTab,\n defaultBasicSetup,\n onChange,\n onUpdate,\n ]);\n\n useEffect(() => {\n const currentValue = view ? view.state.doc.toString() : '';\n if (view && value !== currentValue) {\n view.dispatch({\n changes: { from: 0, to: currentValue.length, insert: value || '' },\n });\n }\n }, [value, view]);\n\n return { state, setState, view, setView, container, setContainer };\n}\n"
|
|
77
77
|
],
|
|
78
|
-
"mappings": "AAAA,SAASA,SAAT,EAAoBC,QAApB,QAAoC,OAApC;AACA,SAASC,WAAT,EAAsBC,WAAtB,QAAyC,mBAAzC;AACA,SAASC,aAAT,QAA8B,sBAA9B;AACA,SAASC,UAAT,EAAqBC,MAArB,EAAyCC,WAAzC,QAA4D,kBAA5D;AACA,SAASC,OAAT,QAAwB,4BAAxB;AACA,SAASC,UAAT,QAA2B,cAA3B;AAOA,OAAO,SAASC,aAAT,CAAuBC,KAAvB,EAA6C;EAClD,IAAM;IACJC,KADI;IAEJC,SAFI;IAGJC,QAHI;IAIJC,QAJI;IAKJC,UAAU,GAAG,EALT;IAMJC,SANI;IAOJC,KAAK,GAAG,OAPJ;IAQJC,MAAM,GAAG,EARL;IASJC,SAAS,GAAG,EATR;IAUJC,SAAS,GAAG,EAVR;IAWJd,WAAW,EAAEe,cAAc,GAAG,EAX1B;IAYJC,KAAK,GAAG,EAZJ;IAaJC,QAAQ,GAAG,EAbP;IAcJC,QAAQ,GAAG,EAdP;IAeJC,QAAQ,GAAG,IAfP;IAgBJC,QAAQ,GAAG,KAhBP;IAiBJvB,aAAa,EAAEwB,oBAAoB,GAAG,IAjBlC;IAkBJnB,UAAU,EAAEoB,iBAAiB,GAAG,IAlB5B;IAmBJC;EAnBI,IAoBFnB,KApBJ;EAqBA,IAAM,CAACoB,SAAD,EAAYC,YAAZ,IAA4B/B,QAAQ,
|
|
78
|
+
"mappings": "AAAA,SAASA,SAAT,EAAoBC,QAApB,QAAoC,OAApC;AACA,SAASC,WAAT,EAAsBC,WAAtB,QAAyC,mBAAzC;AACA,SAASC,aAAT,QAA8B,sBAA9B;AACA,SAASC,UAAT,EAAqBC,MAArB,EAAyCC,WAAzC,QAA4D,kBAA5D;AACA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AAZ,CAJM,CAAT;EAMAjC,SAAS,CAAC,MAAM;IACd,IAAIiC,IAAJ,EAAU;MACRA,IAAI,CAACyB,QAAL,CAAc;QAAEC,OAAO,EAAExD,WAAW,CAACyD,WAAZ,CAAwBlB,EAAxB,CAA2BK,aAA3B;MAAX,CAAd;IACD,CAHa,CAId;;EACD,CALQ,EAKN,CACD7B,KADC,EAEDF,UAFC,EAGDG,MAHC,EAIDC,SAJC,EAKDC,SALC,EAMDE,KANC,EAODC,QAPC,EAQDC,QARC,EASDH,cATC,EAUDI,QAVC,EAWDC,QAXC,EAYDC,oBAZC,EAaDC,iBAbC,EAcDf,QAdC,EAeDC,QAfC,CALM,CAAT;EAuBAf,SAAS,CAAC,MAAM;IACd,IAAM6D,YAAY,GAAG5B,IAAI,GAAGA,IAAI,CAACE,KAAL,CAAWU,GAAX,CAAeC,QAAf,EAAH,GAA+B,EAAxD;;IACA,IAAIb,IAAI,IAAIrB,KAAK,KAAKiD,YAAtB,EAAoC;MAClC5B,IAAI,CAACyB,QAAL,CAAc;QACZI,OAAO,EAAE;UAAEC,IAAI,EAAE,CAAR;UAAWC,EAAE,EAAEH,YAAY,CAACI,MAA5B;UAAoCC,MAAM,EAAEtD,KAAK,IAAI;QAArD;MADG,CAAd;IAGD;EACF,CAPQ,EAON,CAACA,KAAD,EAAQqB,IAAR,CAPM,CAAT;EASA,OAAO;IAAEE,KAAF;IAASC,QAAT;IAAmBH,IAAnB;IAAyBC,OAAzB;IAAkCH,SAAlC;IAA6CC;EAA7C,CAAP;AACD"
|
|
79
79
|
}
|
package/package.json
CHANGED
package/src/index.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, { useRef, forwardRef, useImperativeHandle } from 'react';
|
|
2
2
|
import { EditorState, EditorStateConfig, Extension } from '@codemirror/state';
|
|
3
3
|
import { EditorView, ViewUpdate } from '@codemirror/view';
|
|
4
4
|
import { useCodeMirror } from './useCodeMirror';
|
|
@@ -71,7 +71,7 @@ export interface ReactCodeMirrorRef {
|
|
|
71
71
|
view?: EditorView;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
const ReactCodeMirror =
|
|
74
|
+
const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props, ref) => {
|
|
75
75
|
const {
|
|
76
76
|
className,
|
|
77
77
|
value = '',
|
|
@@ -118,11 +118,13 @@ const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProp
|
|
|
118
118
|
onUpdate,
|
|
119
119
|
extensions,
|
|
120
120
|
});
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
121
|
+
|
|
122
|
+
useImperativeHandle(ref, () => ({ editor: editor.current, state: state, view: view }), [
|
|
123
|
+
editor,
|
|
124
|
+
container,
|
|
125
|
+
state,
|
|
126
|
+
view,
|
|
127
|
+
]);
|
|
126
128
|
|
|
127
129
|
// check type of value
|
|
128
130
|
if (typeof value !== 'string') {
|
package/src/useCodeMirror.ts
CHANGED
|
@@ -32,7 +32,7 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
32
32
|
basicSetup: defaultBasicSetup = true,
|
|
33
33
|
root,
|
|
34
34
|
} = props;
|
|
35
|
-
const [container, setContainer] = useState(
|
|
35
|
+
const [container, setContainer] = useState<HTMLDivElement>();
|
|
36
36
|
const [view, setView] = useState<EditorView>();
|
|
37
37
|
const [state, setState] = useState<EditorState>();
|
|
38
38
|
const defaultLightThemeOption = EditorView.theme(
|
|
@@ -122,12 +122,14 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
122
122
|
}
|
|
123
123
|
return () => {
|
|
124
124
|
if (view) {
|
|
125
|
+
setState(undefined);
|
|
125
126
|
setView(undefined);
|
|
126
127
|
}
|
|
127
128
|
};
|
|
128
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
129
129
|
}, [container, state]);
|
|
130
130
|
|
|
131
|
+
useEffect(() => setContainer(props.container!), [props.container]);
|
|
132
|
+
|
|
131
133
|
useEffect(
|
|
132
134
|
() => () => {
|
|
133
135
|
if (view) {
|