Skip to content

Commit a7fc5a8

Browse files
authored
LG-5706: Fix stuck CodeEditor loading state (#3297)
* refactor(CodeEditor): normalize props handling and clean up unused styles in SearchPanel - Introduced a `normalizedProps` object in CodeEditor to ensure all default values are included when spreading props. - Updated the usage of props in various hooks to utilize `normalizedProps`. - Removed unused `findInputStyles` from SearchPanel styles and component to streamline the code. * changeset * copilot feedback * Add stories to assert correctness
1 parent d8c4e35 commit a7fc5a8

File tree

5 files changed

+66
-14
lines changed

5 files changed

+66
-14
lines changed

.changeset/tidy-bushes-fly.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@leafygreen-ui/code-editor': patch
3+
---
4+
5+
- Fixes stuck loading state
6+
- Includes minor small visual improvements

packages/code-editor/src/CodeEditor.stories.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ const meta: StoryMetaType<typeof CodeEditor> = {
4343
controls: {
4444
exclude: [...storybookExcludedControlParams, 'extensions'],
4545
},
46+
generate: {
47+
args: {
48+
width: '100%',
49+
},
50+
combineArgs: {
51+
darkMode: [false, true],
52+
baseFontSize: Object.values(BaseFontSize),
53+
copyButtonAppearance: Object.values(CopyButtonAppearance),
54+
enableLineNumbers: [true, false],
55+
},
56+
},
4657
},
4758
decorators: [
4859
(Story: StoryFn, context) => (
@@ -251,6 +262,35 @@ LiveExample.args = {
251262
),
252263
};
253264

265+
/** This asserts that the editor loads correctly when no defaults are provided. */
266+
export const NoDefaults = Template.bind({});
267+
NoDefaults.args = {
268+
copyButtonAppearance: undefined,
269+
customContextMenuItems: undefined,
270+
enableClickableUrls: undefined,
271+
enableCodeFolding: undefined,
272+
enableLineNumbers: undefined,
273+
enableLineWrapping: undefined,
274+
enableSearchPanel: undefined,
275+
baseFontSize: undefined,
276+
forceParsing: undefined,
277+
placeholder: undefined,
278+
readOnly: undefined,
279+
indentSize: undefined,
280+
indentUnit: undefined,
281+
isLoading: undefined,
282+
defaultValue: undefined,
283+
tooltips: undefined,
284+
darkMode: undefined,
285+
height: undefined,
286+
maxHeight: '',
287+
maxWidth: undefined,
288+
minHeight: undefined,
289+
minWidth: undefined,
290+
preLoadedModules: undefined,
291+
width: undefined,
292+
};
293+
254294
export const Minimal = Template.bind({});
255295
Minimal.args = {
256296
enableLineNumbers: false,
@@ -416,3 +456,5 @@ export const WithPreLoadedModules: StoryObj<typeof CodeEditor> = {
416456
/>
417457
),
418458
};
459+
460+
export const Generated = () => {};

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,22 @@ const BaseCodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
9090
const [undoDepth, setUndoDepth] = useState(0);
9191
const [redoDepth, setRedoDepth] = useState(0);
9292

93-
const { modules, isLoading } = useModules(props);
93+
// Create normalized props object with all defaults applied
94+
// This ensures that when we spread props, all default values are included
95+
// Note: We spread props first, then override with destructured values (which have defaults applied)
96+
// This ensures that if a prop is undefined in props, the default value is used
97+
const normalizedProps: CodeEditorProps = {
98+
...props,
99+
copyButtonAppearance,
100+
enableSearchPanel,
101+
extensions: consumerExtensions,
102+
forceParsing: forceParsingProp,
103+
isLoading: isLoadingProp,
104+
};
105+
106+
const { modules, isLoading } = useModules({
107+
...normalizedProps,
108+
});
94109

95110
// Get formatting functionality
96111
const { formatCode, isFormattingAvailable } = useCodeFormatter({
@@ -102,11 +117,8 @@ const BaseCodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
102117
const customExtensions = useExtensions({
103118
editorViewInstance: editorViewRef.current,
104119
props: {
105-
...props,
106-
forceParsing: forceParsingProp,
120+
...normalizedProps,
107121
onChange: onChangeProp,
108-
isLoading: isLoadingProp,
109-
extensions: consumerExtensions,
110122
baseFontSize,
111123
/**
112124
* CodeEditorTooltip in particular renders outside of the LeafyGreenProvider
@@ -283,7 +295,7 @@ const BaseCodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
283295

284296
const searchPanelExtension = useSearchPanelExtension({
285297
props: {
286-
...props,
298+
...normalizedProps,
287299
darkMode,
288300
baseFontSize,
289301
},
@@ -335,7 +347,6 @@ const BaseCodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
335347
!EditorView ||
336348
!Prec ||
337349
!commands ||
338-
!searchModule ||
339350
!StateEffect
340351
) {
341352
return;

packages/code-editor/src/SearchPanel/SearchPanel.styles.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ export const findSectionStyles = css`
8888
display: flex;
8989
align-items: center;
9090
padding: ${spacing[200]}px;
91-
height: 100%;
9291
`;
9392

9493
export const replaceSectionStyles = css`
@@ -162,7 +161,3 @@ export const replaceInputContainerStyles = css`
162161
export const replaceButtonStyles = css`
163162
margin-left: ${spacing[100]}px;
164163
`;
165-
166-
export const findInputStyles = css`
167-
width: 100%;
168-
`;

packages/code-editor/src/SearchPanel/SearchPanel.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import { Body, useUpdatedBaseFontSize } from '@leafygreen-ui/typography';
3232
import {
3333
closeButtonStyles,
3434
findInputContainerStyles,
35-
findInputStyles,
3635
findOptionsContainerStyles,
3736
findSectionStyles,
3837
getContainerStyles,
@@ -294,7 +293,6 @@ export function SearchPanel({
294293
aria-labelledby="find"
295294
onChange={handleSearchQueryChange}
296295
onKeyDown={handleFindInputKeyDown}
297-
className={findInputStyles}
298296
value={searchString}
299297
// CodeMirror looks for this attribute to refocus when CMD+F is pressed and the panel is already open
300298
main-field="true"

0 commit comments

Comments
 (0)