diff --git a/public/translations/en.json b/public/translations/en.json index 9ee80fc55..aaebded83 100644 --- a/public/translations/en.json +++ b/public/translations/en.json @@ -215,11 +215,14 @@ "errors": { "interrupted": "Execution interrupted" }, + "errorMessage": { + "title": "Error running your code" + }, "aiErrorMessages": { "loadingText": "Generating hint", - "buttonText": "Need help? Generate an AI hint", + "buttonText": "Generate an AI hint", "retryButtonText": "Try again", - "aiGeneratedHint": "AI generated hint", + "aiGeneratedHint": "AI-generated hint", "errorText": "We were not able to generate a hint, try again later.", "notice": "AI hints can be wrong. Learn more and share feedback", "turnOff": "Turn off AI hints" @@ -360,7 +363,7 @@ "charity": "Raspberry Pi Foundation - UK registered charity 1129409", "settingsMenu": { "heading": "Settings", - "aiHint": "AI generated hints", + "aiHint": "AI-generated hints", "aiHintDescription": "When your code has an error, you can generate a hint using AI. Hints do not give you the answer and can be wrong. Learn more about how AI hints work or share feedback.", "textSize": "Text size", "theme": "Theme", diff --git a/src/assets/icons/cancel_FILL.svg b/src/assets/icons/cancel_FILL.svg index 5ef0d2fec..23f1bdc80 100644 --- a/src/assets/icons/cancel_FILL.svg +++ b/src/assets/icons/cancel_FILL.svg @@ -1,3 +1,3 @@ - - - + + + \ No newline at end of file diff --git a/src/assets/icons/chevron_down.svg b/src/assets/icons/chevron_down.svg index 77708f4a0..c4da7a249 100644 --- a/src/assets/icons/chevron_down.svg +++ b/src/assets/icons/chevron_down.svg @@ -1,9 +1,9 @@ - + diff --git a/src/assets/stylesheets/ErrorMessage.scss b/src/assets/stylesheets/ErrorMessage.scss index 409b7b947..74d5d2959 100644 --- a/src/assets/stylesheets/ErrorMessage.scss +++ b/src/assets/stylesheets/ErrorMessage.scss @@ -4,29 +4,96 @@ .error-message { color: var(--rpf-red-900); background-color: var(--rpf-red-100); - border-block: 1px solid var(--editor-color-outline); - padding: var(--space-1); + border-block-start: 1px solid var(--rpf-red-200); + display: flex; + flex-direction: column; + min-block-size: 0; overflow-y: auto; + &__summary { + position: sticky; + inset-block-start: 0; + background-color: var(--rpf-red-100); + z-index: 1; + display: flex; + align-items: center; + justify-content: space-between; + gap: var(--space-1); + padding: var(--space-1); + list-style: none; + cursor: pointer; + + &::-webkit-details-marker { + display: none; + } + + &:focus-visible { + outline: 3px solid var(--editor-focus-color); + outline-offset: -3px; + } + } + + @supports selector(::details-content) { + overflow-y: visible; + details { + display: flex; + flex-direction: column; + min-block-size: 0; + + &::details-content { + min-block-size: 0; + overflow-y: auto; + } + } + + .error-message__summary { + position: static; + } + } + + details &__summary { + border-block-end: 1px solid var(--rpf-red-200); + } + + &__title { + display: flex; + align-items: center; + gap: var(--space-1); + margin: 0; + @include typography.style-1; + font-weight: bold; + + svg { + flex-shrink: 0; + } + } + + &__chevron { + flex-shrink: 0; + fill: var(--rpf-text-primary); + transition: transform 0.2s ease-in-out; + } + + details[open] &__chevron { + transform: rotate(180deg); + } + + &__content { + padding-block: var(--space-1); + padding-inline: var(--space-1-5); + } + &__error { padding: 0; margin: 0; + font-family: inherit; white-space: pre-wrap; overflow-wrap: break-word; - font-weight: bold; + @include typography.style-1; } &__python { display: flex; gap: var(--space-1); - @include typography.style-1; - - &--medium { - @include typography.style-1; - } - - &--large { - @include typography.style-2; - } } } diff --git a/src/assets/stylesheets/SettingsPanel.scss b/src/assets/stylesheets/SettingsPanel.scss index f25d19873..c6e51e2e4 100644 --- a/src/assets/stylesheets/SettingsPanel.scss +++ b/src/assets/stylesheets/SettingsPanel.scss @@ -23,6 +23,10 @@ p { color: var(--rpf-text-secondary); margin-block: var(--space-0-5) 0; + + a { + font-weight: var(--font-weight-bold); + } } } } diff --git a/src/components/Editor/AiErrorMessage/AiErrorMessage.jsx b/src/components/Editor/AiErrorMessage/AiErrorMessage.jsx index e937fe259..97dbc35ed 100644 --- a/src/components/Editor/AiErrorMessage/AiErrorMessage.jsx +++ b/src/components/Editor/AiErrorMessage/AiErrorMessage.jsx @@ -46,6 +46,14 @@ const AiErrorMessage = () => { }; }, [errorDetails]); + // Showing the hint removes the button that had focus, so move focus to the result. + const resultRef = useRef(null); + const showResult = Boolean(hint || failed); + + useEffect(() => { + if (showResult) resultRef.current?.focus(); + }, [showResult]); + const handleButtonClick = async () => { const code = projectCode?.find((c) => c.name === "main" && c.extension === "py") @@ -106,11 +114,11 @@ const AiErrorMessage = () => { }; return ( -
- {(hint || failed) && ( -
+
+ {showResult && ( +

- +

diff --git a/src/components/Editor/AiErrorMessage/AiErrorMessage.test.jsx b/src/components/Editor/AiErrorMessage/AiErrorMessage.test.jsx index 1d391698b..1f54f5f8f 100644 --- a/src/components/Editor/AiErrorMessage/AiErrorMessage.test.jsx +++ b/src/components/Editor/AiErrorMessage/AiErrorMessage.test.jsx @@ -140,6 +140,30 @@ describe("AiErrorMessage", () => { ).not.toBeInTheDocument(); }); + test("Moves focus to the hint when it replaces the button", async () => { + getAiErrorHint.mockResolvedValue({ data: { fem: "A friendly hint" } }); + renderAiErrorMessage(); + hintButton().focus(); + await clickButtonAndWait(); + + expect(document.querySelector(".ai-error-message__hint")).toHaveFocus(); + }); + + test("Moves focus to the error text when the request fails", async () => { + getAiErrorHint.mockRejectedValue(new Error("Bad gateway")); + renderAiErrorMessage(); + hintButton().focus(); + await clickButtonAndWait(); + + expect(document.querySelector(".ai-error-message__hint")).toHaveFocus(); + }); + + test("Does not move focus before a hint is requested", () => { + renderAiErrorMessage(); + + expect(document.body).toHaveFocus(); + }); + test("Keeps loading for the minimum time when the response is fast", async () => { getAiErrorHint.mockResolvedValue({ data: { fem: "A friendly hint" } }); renderAiErrorMessage(); diff --git a/src/components/Editor/ErrorMessage/ErrorMessage.jsx b/src/components/Editor/ErrorMessage/ErrorMessage.jsx index 419fcc724..dee576386 100644 --- a/src/components/Editor/ErrorMessage/ErrorMessage.jsx +++ b/src/components/Editor/ErrorMessage/ErrorMessage.jsx @@ -1,33 +1,52 @@ -import React, { useContext } from "react"; import "../../../assets/stylesheets/ErrorMessage.scss?inline"; import { useSelector } from "react-redux"; import DOMPurify from "dompurify"; -import { SettingsContext } from "../../../utils/settings"; +import { useTranslation } from "react-i18next"; import CancelFillIcon from "../../../assets/icons/cancel_FILL.svg"; +import ChevronDownIcon from "../../../assets/icons/chevron_down.svg"; import FriendlyErrorMessage from "../FriendlyErrorMessage/FriendlyErrorMessage"; import AiErrorMessage from "../AiErrorMessage/AiErrorMessage"; import useAiHintOffered from "../../../hooks/useAiHintOffered"; const ErrorMessage = () => { const error = useSelector((state) => state.editor.error); - const settings = useContext(SettingsContext); + const { t } = useTranslation(); const aiHintOffered = useAiHintOffered(); - return error ? ( -

-
- -
+  return (
+    <>
+      {/* Screen readers are already watching it when an error appears.
+        Announces the title, without moving focus away from the Run button. */}
+      
+ {error ? t("output.errorMessage.title") : ""}
- - {aiHintOffered && } -
- ) : null; + {error ? ( +
+
+ + + + {t("output.errorMessage.title")} + + + +
+
+
+              
+ + {aiHintOffered && } +
+
+
+ ) : null} + + ); }; export default ErrorMessage; diff --git a/src/components/Editor/ErrorMessage/ErrorMessage.test.jsx b/src/components/Editor/ErrorMessage/ErrorMessage.test.jsx index e13d1c950..51f623b2d 100644 --- a/src/components/Editor/ErrorMessage/ErrorMessage.test.jsx +++ b/src/components/Editor/ErrorMessage/ErrorMessage.test.jsx @@ -2,7 +2,7 @@ import configureStore from "redux-mock-store"; import { Provider } from "react-redux"; import { SettingsContext } from "../../../utils/settings"; import ErrorMessage from "./ErrorMessage"; -import { render, screen } from "@testing-library/react"; +import { fireEvent, render, screen } from "@testing-library/react"; import useIsOnline from "../../../hooks/useIsOnline"; const mockI18n = vi.hoisted(() => ({ language: "en" })); @@ -44,9 +44,27 @@ describe("When error is set", () => { expect(screen.queryByText("Oops")).toBeInTheDocument(); }); - test("Font size class is set correctly", () => { - const errorMessage = screen.queryByText("Oops").parentElement; - expect(errorMessage).toHaveClass("error-message__python--myFontSize"); + test("Displays the error title in an expanded details section", () => { + const title = document.querySelector(".error-message__title"); + expect(title.closest("summary")).toBeInTheDocument(); + expect(title.closest("details")).toHaveAttribute("open"); + }); + + test("Clicking the title collapses and expands the error details", () => { + const title = document.querySelector(".error-message__title"); + const details = title.closest("details"); + + fireEvent.click(title); + expect(details).not.toHaveAttribute("open"); + + fireEvent.click(title); + expect(details).toHaveAttribute("open"); + }); + + test("Announces the error title in a status region", () => { + expect(screen.getByRole("status")).toHaveTextContent( + "output.errorMessage.title", + ); }); test("Does not display friendly error elements", () => { @@ -146,6 +164,20 @@ describe("When error is set", () => { }); }); +describe("When error is not set", () => { + test("Keeps an empty status region rendered for announcements", () => { + const store = configureStore([])({ editor: { error: "" } }); + render( + + + , + ); + + expect(screen.getByRole("status")).toBeEmptyDOMElement(); + expect(document.querySelector(".error-message")).not.toBeInTheDocument(); + }); +}); + describe("AI hint", () => { const mockStore = configureStore([]);