Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Make switch to data button keyboard accessible/interactive (#64321)

Sébastien20d149c48d91
diff --git a/e2e/test/scenarios/organization/timelines-question.cy.spec.js b/e2e/test/scenarios/organization/timelines-question.cy.spec.js
index 7b3769635cb..f90526dd923 100644
--- a/e2e/test/scenarios/organization/timelines-question.cy.spec.js
+++ b/e2e/test/scenarios/organization/timelines-question.cy.spec.js
@@ -90,7 +90,7 @@ describe("scenarios > organization > timelines > question", () => {
// eslint-disable-next-line no-unscoped-text-selectors -- deprecated usage
cy.findByText("v3").should("be.visible");
- cy.findByLabelText("table2 icon").click();
+ cy.findByLabelText("Switch to data").click();
// eslint-disable-next-line no-unscoped-text-selectors -- deprecated usage
cy.findByText("v1").should("be.visible");
// eslint-disable-next-line no-unscoped-text-selectors -- deprecated usage
diff --git a/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.module.css b/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.module.css
index fb49e284b6f..c304fa3789e 100644
--- a/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.module.css
+++ b/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.module.css
@@ -5,22 +5,36 @@
border-radius: 99px;
background-color: var(--mb-color-bg-medium);
transition: background 300ms linear;
+ cursor: pointer;
&:hover {
background-color: color-mix(in srgb, var(--mb-color-bg-medium), black 5%);
}
+
+ &:focus-visible {
+ outline: 2px solid var(--mb-color-focus);
+ outline-offset: 1px;
+ }
}
.ToggleIcon {
display: flex;
padding: 4px 8px;
- cursor: pointer;
background-color: transparent;
- color: inherit;
border-radius: 99px;
+ outline: none;
+ cursor: pointer;
&.active {
background-color: var(--mb-color-brand);
+ }
+}
+
+.InnerLabel {
+ color: var(--mb-color-text-medium);
+ transition: 300ms;
+
+ &.activeLabel {
color: var(--mb-color-text-white);
}
}
diff --git a/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.tsx b/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.tsx
index 0b0b3b664ea..4b342a35ffe 100644
--- a/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.tsx
+++ b/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.tsx
@@ -2,7 +2,7 @@ import cx from "classnames";
import { t } from "ttag";
import { useRegisterShortcut } from "metabase/palette/hooks/useRegisterShortcut";
-import { Flex, Icon } from "metabase/ui";
+import { Icon, SegmentedControl } from "metabase/ui";
import QuestionDisplayToggleS from "./QuestionDisplayToggle.module.css";
@@ -28,27 +28,58 @@ const QuestionDisplayToggle = ({
);
return (
- <Flex
- className={cx(QuestionDisplayToggleS.Well, className)}
- onClick={() => onToggleRawTable(!isShowingRawTable)}
- >
- <Flex
- className={cx(QuestionDisplayToggleS.ToggleIcon, {
- [QuestionDisplayToggleS.active]: isShowingRawTable,
- })}
- aria-label={t`Switch to data`}
- >
- <Icon name="table2" />
- </Flex>
- <Flex
- className={cx(QuestionDisplayToggleS.ToggleIcon, {
- [QuestionDisplayToggleS.active]: !isShowingRawTable,
- })}
- aria-label={t`Switch to visualization`}
- >
- <Icon name="lineandbar" />
- </Flex>
- </Flex>
+ <SegmentedControl
+ classNames={{
+ root: cx(QuestionDisplayToggleS.Well, className),
+ label: QuestionDisplayToggleS.ToggleIcon,
+ indicator: cx(
+ QuestionDisplayToggleS.ToggleIcon,
+ QuestionDisplayToggleS.active,
+ ),
+ }}
+ onClick={(e) => {
+ e.preventDefault();
+ onToggleRawTable(!isShowingRawTable);
+ }}
+ onKeyDown={(e) => {
+ if (e.key === "Enter" || e.key === " ") {
+ e.preventDefault();
+ onToggleRawTable(!isShowingRawTable);
+ }
+ }}
+ tabIndex={0}
+ value={isShowingRawTable ? "data" : "visualization"}
+ data={[
+ {
+ disabled: true,
+ value: "data",
+ label: (
+ <Icon
+ size={16}
+ name="table2"
+ className={cx(QuestionDisplayToggleS.InnerLabel, {
+ [QuestionDisplayToggleS.activeLabel]: isShowingRawTable,
+ })}
+ aria-label={t`Switch to data`}
+ />
+ ),
+ },
+ {
+ disabled: true,
+ value: "visualization",
+ label: (
+ <Icon
+ size={16}
+ name="lineandbar"
+ className={cx(QuestionDisplayToggleS.InnerLabel, {
+ [QuestionDisplayToggleS.activeLabel]: !isShowingRawTable,
+ })}
+ aria-label={t`Switch to visualization`}
+ />
+ ),
+ },
+ ]}
+ />
);
};
diff --git a/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.unit.spec.tsx b/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.unit.spec.tsx
new file mode 100644
index 00000000000..6a76833c82e
--- /dev/null
+++ b/frontend/src/metabase/query_builder/components/view/QuestionDisplayToggle/QuestionDisplayToggle.unit.spec.tsx
@@ -0,0 +1,76 @@
+import userEvent from "@testing-library/user-event";
+
+import { renderWithProviders, screen } from "__support__/ui";
+
+import { QuestionDisplayToggle } from "./QuestionDisplayToggle";
+
+describe("QuestionDisplayToggle", () => {
+ it("renders correctly", () => {
+ renderWithProviders(
+ <QuestionDisplayToggle
+ isShowingRawTable={false}
+ onToggleRawTable={jest.fn()}
+ />,
+ );
+ expect(screen.getByLabelText("Switch to data")).toBeInTheDocument();
+ expect(
+ screen.getByLabelText("Switch to visualization"),
+ ).toBeInTheDocument();
+ });
+
+ it("toggles to data on click", async () => {
+ const onToggleRawTable = jest.fn();
+ renderWithProviders(
+ <QuestionDisplayToggle
+ isShowingRawTable={false}
+ onToggleRawTable={onToggleRawTable}
+ />,
+ );
+
+ const dataButton = screen.getByLabelText("Switch to data");
+ await userEvent.click(dataButton);
+ expect(onToggleRawTable).toHaveBeenCalledWith(true);
+ });
+
+ it("toggles to visualization on click", async () => {
+ const onToggleRawTable = jest.fn();
+ renderWithProviders(
+ <QuestionDisplayToggle
+ isShowingRawTable={true}
+ onToggleRawTable={onToggleRawTable}
+ />,
+ );
+
+ const vizButton = screen.getByLabelText("Switch to visualization");
+ await userEvent.click(vizButton);
+ expect(onToggleRawTable).toHaveBeenCalledWith(false);
+ });
+
+ it("toggles state on Enter key press", async () => {
+ const onToggleRawTable = jest.fn();
+ renderWithProviders(
+ <QuestionDisplayToggle
+ isShowingRawTable={false}
+ onToggleRawTable={onToggleRawTable}
+ />,
+ );
+
+ const button = screen.getByLabelText("Switch to data");
+ await userEvent.type(button, "{enter}");
+ expect(onToggleRawTable).toHaveBeenCalledWith(true);
+ });
+
+ it("toggles state on Space key press", async () => {
+ const onToggleRawTable = jest.fn();
+ renderWithProviders(
+ <QuestionDisplayToggle
+ isShowingRawTable={true}
+ onToggleRawTable={onToggleRawTable}
+ />,
+ );
+
+ const button = screen.getByLabelText("Switch to visualization");
+ await userEvent.type(button, " ");
+ expect(onToggleRawTable).toHaveBeenCalledWith(false);
+ });
+});