Skip to content

Commit 33411cc

Browse files
author
Alice Koreman
committed
revert highlight function type to return ReactNode
1 parent 713e49a commit 33411cc

File tree

5 files changed

+8
-8
lines changed

5 files changed

+8
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
"size-limit": [
147147
{
148148
"path": "lib/components/index.js",
149-
"limit": "6.53kb"
149+
"limit": "6.56kb"
150150
},
151151
{
152152
"path": "lib/components/code-view/highlight/javascript.js",

src/__tests__/__snapshots__/documenter.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ exports[`definition for code-view matches the snapshot 1`] = `
2828
"description": "A function to perform custom syntax highlighting.",
2929
"name": "highlight",
3030
"optional": true,
31-
"type": "(code: string) => ReactElement",
31+
"type": "(code: string) => React.ReactNode",
3232
},
3333
{
3434
"description": "Controls the display of line numbers.

src/code-view/highlight/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { tokenize } from "ace-code/src/ext/simple_tokenizer";
77
import "ace-code/styles/theme/cloud_editor.css";
88
import "ace-code/styles/theme/cloud_editor_dark.css";
99

10-
type CreateHighlightType = (code: string) => React.ReactElement;
10+
type CreateHighlightType = (code: string) => React.ReactNode;
1111

1212
export function createHighlight(rules: Ace.HighlightRules): CreateHighlightType {
1313
return (code: string) => {

src/code-view/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ export interface CodeViewProps {
4141
* A function to perform custom syntax highlighting.
4242
*
4343
*/
44-
highlight?: (code: string) => React.ReactElement;
44+
highlight?: (code: string) => React.ReactNode;
4545
}

src/code-view/internal.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { useRef } from "react";
4-
import { Children } from "react";
3+
import { Children, createElement, Fragment, isValidElement, ReactElement, useRef } from "react";
54
import clsx from "clsx";
65

76
import { useCurrentMode } from "@cloudscape-design/component-toolkit/internal";
@@ -51,6 +50,7 @@ export function InternalCodeView({
5150

5251
// Create tokenized elements of the content.
5352
const code = highlight ? highlight(content) : textHighlight(content);
53+
const codeElement: ReactElement = isValidElement(code) ? code : createElement(Fragment, null, code);
5454

5555
return (
5656
<div
@@ -76,7 +76,7 @@ export function InternalCodeView({
7676
<col style={{ width: "auto" }} />
7777
</colgroup>
7878
<tbody>
79-
{Children.map(code.props.children, (child, index) => {
79+
{Children.map(codeElement.props.children, (child, index) => {
8080
return (
8181
<tr key={index}>
8282
{lineNumbers && (
@@ -90,7 +90,7 @@ export function InternalCodeView({
9090
<Box variant="code" fontSize="body-m">
9191
<span
9292
className={clsx(
93-
code.props.className,
93+
codeElement.props.className,
9494
lineWrapping ? styles["code-line-wrap"] : styles["code-line-nowrap"],
9595
)}
9696
>

0 commit comments

Comments
 (0)