Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/code-view/internal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { Children, createElement, Fragment, ReactElement, useRef } from "react";
import { Children, createElement, Fragment, ReactElement, useMemo, useRef } from "react";
import clsx from "clsx";

import { useCurrentMode } from "@cloudscape-design/component-toolkit/internal";
Expand Down Expand Up @@ -50,8 +50,8 @@ export function InternalCodeView({
const regionProps = ariaLabel || ariaLabelledby ? { role: "region" } : {};
const accessibleLineNumbers = lineNumbers && i18nStrings?.lineNumberLabel && i18nStrings?.codeLabel;

// Create tokenized React nodes of the content.
const code = highlight ? highlight(content) : textHighlight(content);
// Memoize tokenized React nodes to avoid re-running highlight on every render.
const code = useMemo(() => (highlight ? highlight(content) : textHighlight(content)), [content, highlight]);
// Create elements from the nodes.
const codeElementWrapper: ReactElement = createElement(Fragment, null, code);
const codeElement = Children.only(codeElementWrapper.props.children);
Expand Down