Skip to content

Commit 5cea9fa

Browse files
refactor: minor cleanups (#111)
- Hoist regionProps objects to module-level constants to avoid recreation on every render - Remove redundant optional chaining in highlight/index.tsx where token.className is already checked in the ternary condition
1 parent a4cabe9 commit 5cea9fa

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

src/code-view/highlight/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function createHighlight(rules: Ace.HighlightRules): CreateHighlightType
1818
<Fragment key={lineIndex}>
1919
{lineTokens.map((token, tokenIndex) => {
2020
return token.className ? (
21-
<span className={token?.className} key={tokenIndex}>
21+
<span className={token.className} key={tokenIndex}>
2222
{token.value}
2323
</span>
2424
) : (

src/code-view/internal.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import styles from "./styles.css.js";
1313

1414
const ACE_CLASSES = { light: "ace-cloud_editor", dark: "ace-cloud_editor_dark" };
1515

16+
// Static props objects to avoid recreation on every render.
17+
const REGION_PROPS = { role: "region" } as const;
18+
const NO_REGION_PROPS = {} as const;
19+
1620
type InternalCodeViewProps = CodeViewProps & InternalBaseComponentProps;
1721

1822
// Breaks down the input code for non-highlighted code-view into React
@@ -47,7 +51,7 @@ export function InternalCodeView({
4751
const containerRef = useRef<HTMLDivElement>(null);
4852
const darkMode = useCurrentMode(containerRef) === "dark";
4953

50-
const regionProps = ariaLabel || ariaLabelledby ? { role: "region" } : {};
54+
const regionProps = ariaLabel || ariaLabelledby ? REGION_PROPS : NO_REGION_PROPS;
5155
const accessibleLineNumbers = lineNumbers && i18nStrings?.lineNumberLabel && i18nStrings?.codeLabel;
5256

5357
// Create tokenized React nodes of the content.

0 commit comments

Comments
 (0)