Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): make instructions elements ids unique #123

Merged
merged 1 commit into from
Nov 4, 2021
Merged
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
20 changes: 15 additions & 5 deletions sandpack-react/src/components/CodeEditor/CodeMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import * as React from "react";
import { useSandpack } from "../../hooks/useSandpack";
import { useSandpackTheme } from "../../hooks/useSandpackTheme";
import type { EditorState as SandpackEditorState } from "../../types";
import { getFileName } from "../../utils/stringUtils";
import { getFileName, generateRandomId } from "../../utils/stringUtils";

import { highlightDecorators } from "./highlightDecorators";
import { highlightInlineError } from "./highlightInlineError";
Expand Down Expand Up @@ -88,6 +88,7 @@ export const CodeMirror = React.forwardRef<HTMLElement, CodeMirrorProps>(
const [internalCode, setInternalCode] = React.useState<string>(code);
const c = useClasser("sp");
const { listen } = useSandpack();
const ariaId = React.useRef<string>(generateRandomId());

React.useEffect(() => {
if (!wrapper.current) {
Expand Down Expand Up @@ -195,7 +196,10 @@ export const CodeMirror = React.forwardRef<HTMLElement, CodeMirrorProps>(

if (!readOnly) {
view.contentDOM.setAttribute("tabIndex", "-1");
view.contentDOM.setAttribute("aria-describedby", "exit-instructions");
view.contentDOM.setAttribute(
"aria-describedby",
`exit-instructions-${ariaId.current}`
);
}

cmView.current = view;
Expand Down Expand Up @@ -299,7 +303,7 @@ export const CodeMirror = React.forwardRef<HTMLElement, CodeMirrorProps>(
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
<div
ref={combinedRef}
aria-describedby="enter-instructions"
aria-describedby={`enter-instructions-${ariaId.current}`}
aria-label={
filePath ? `Code Editor for ${getFileName(filePath)}` : `Code Editor`
}
Expand All @@ -319,11 +323,17 @@ export const CodeMirror = React.forwardRef<HTMLElement, CodeMirrorProps>(
</pre>

<>
<p id="enter-instructions" style={{ display: "none" }}>
<p
id={`enter-instructions-${ariaId.current}`}
style={{ display: "none" }}
>
To enter the code editing mode, press Enter. To exit the edit mode,
press Escape
</p>
<p id="exit-instructions" style={{ display: "none" }}>
<p
id={`exit-instructions-${ariaId.current}`}
style={{ display: "none" }}
>
You are editing the code. To exit the edit mode, press Escape
</p>
</>
Expand Down