Skip to content

Commit 824a30b

Browse files
committed
fix(CodeEditor): Set default height
The docs state that the default height is 100%. However, the examples just break if you don't pass in a height. This should help.
1 parent c7a4e5f commit 824a30b

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,16 @@ export interface CodeEditorProps extends Omit<React.HTMLProps<HTMLDivElement>, '
149149
emptyStateTitle?: React.ReactNode;
150150
/** Editor header main content title. */
151151
headerMainContent?: string;
152-
/** Height of code editor. Defaults to 100%. 'sizeToFit' will automatically change the height
152+
/** Height of code editor. 'sizeToFit' will automatically change the height
153153
* to the height of the content.
154154
*/
155155
height?: string | 'sizeToFit';
156156
/** Flag to add copy button to code editor actions. */
157157
isCopyEnabled?: boolean;
158158
/** Flag indicating the editor is styled using monaco's dark theme. */
159159
isDarkTheme?: boolean;
160+
/** Flag that enables component to consume the available height of its container */
161+
isFullHeight?: boolean;
160162
/** Flag indicating the editor has a plain header. */
161163
isHeaderPlain?: boolean;
162164
/** Flag to add download button to code editor actions. */
@@ -250,7 +252,6 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
250252
onEditorDidMount: () => {},
251253
language: Language.plaintext,
252254
isDarkTheme: false,
253-
height: '',
254255
width: '',
255256
isLineNumbersVisible: true,
256257
isReadOnly: false,
@@ -521,6 +522,7 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
521522
},
522523
...optionsProp
523524
};
525+
const isFullHeight = this.props.height === '100%' ? true : this.props.isFullHeight;
524526

525527
return (
526528
<Dropzone multiple={false} onDropAccepted={this.onDropAccepted} onDropRejected={this.onDropRejected}>
@@ -643,7 +645,7 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
643645
const editor = (
644646
<div className={css(styles.codeEditorCode)} ref={this.wrapperRef} tabIndex={0} dir="ltr">
645647
<Editor
646-
height={height}
648+
height={height === '100%' ? undefined : height}
647649
width={width}
648650
language={language}
649651
value={value}
@@ -658,13 +660,21 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
658660
);
659661

660662
return (
661-
<div className={css(styles.codeEditor, isReadOnly && styles.modifiers.readOnly, className)} ref={this.ref}>
663+
<div
664+
className={css(
665+
styles.codeEditor,
666+
isReadOnly && styles.modifiers.readOnly,
667+
isFullHeight && styles.modifiers.fullHeight,
668+
className
669+
)}
670+
ref={this.ref}
671+
>
662672
{isUploadEnabled || providedEmptyState ? (
663673
<div
664674
{...getRootProps({
665675
onClick: (event) => event.stopPropagation() // Prevents clicking TextArea from opening file dialog
666676
})}
667-
className={css(isLoading && fileUploadStyles.modifiers.loading)}
677+
className={css(styles.codeEditorContainer, isLoading && fileUploadStyles.modifiers.loading)}
668678
>
669679
{editorHeader}
670680
<div className={css(styles.codeEditorMain, isDragActive && styles.modifiers.dragHover)}>

packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ These examples below are the shortcuts that we recommend describing in the popov
4343
```ts file="CodeEditorCustomControl.tsx"
4444

4545
```
46+
47+
### With 100% height
48+
49+
```ts file="CodeEditorModal.tsx"
50+
51+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
import { CodeEditor, Language } from '@patternfly/react-code-editor';
3+
import { Button, Modal, ModalBody, ModalFooter, ModalHeader } from '@patternfly/react-core';
4+
5+
export const CodeEditorFullHeight: React.FunctionComponent = () => {
6+
const [isModalOpen, setIsModalOpen] = React.useState(false);
7+
8+
const onEditorDidMount = (editor, monaco) => {
9+
editor.layout();
10+
editor.focus();
11+
monaco.editor.getModels()[0].updateOptions({ tabSize: 5 });
12+
};
13+
14+
const handleModalToggle = (_event: KeyboardEvent | React.MouseEvent) => {
15+
setIsModalOpen(!isModalOpen);
16+
};
17+
18+
const onChange = (value) => {
19+
// eslint-disable-next-line no-console
20+
console.log(value);
21+
};
22+
23+
return (
24+
<React.Fragment>
25+
<Button variant="primary" onClick={handleModalToggle} ouiaId="ShowBasicModal">
26+
Show basic modal with code editor inside
27+
</Button>
28+
<Modal
29+
isOpen={isModalOpen}
30+
onClose={handleModalToggle}
31+
ouiaId="BasicModal"
32+
aria-labelledby="basic-modal-title"
33+
aria-describedby="modal-box-body-basic"
34+
style={{ height: '800px' }}
35+
>
36+
<ModalHeader title="Basic modal" labelId="basic-modal-title" />
37+
<ModalBody id="modal-box-body-basic">
38+
<CodeEditor
39+
isLanguageLabelVisible
40+
code="Some example content"
41+
onChange={onChange}
42+
language={Language.javascript}
43+
onEditorDidMount={onEditorDidMount}
44+
isFullHeight
45+
/>
46+
</ModalBody>
47+
<ModalFooter>
48+
<Button key="confirm" variant="primary" onClick={handleModalToggle}>
49+
Confirm
50+
</Button>
51+
<Button key="cancel" variant="link" onClick={handleModalToggle}>
52+
Cancel
53+
</Button>
54+
</ModalFooter>
55+
</Modal>
56+
</React.Fragment>
57+
);
58+
};

0 commit comments

Comments
 (0)