The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
GDGT-3329: Make CopyButton an accessible button (#83845)
Will Fry1fcb427a05a2
diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsSidebar/ErrorsSection/ErrorsSection.module.css b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsSidebar/ErrorsSection/ErrorsSection.module.cssindex 6109c93a3d0..85acfecffa1 100644--- a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsSidebar/ErrorsSection/ErrorsSection.module.css+++ b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsSidebar/ErrorsSection/ErrorsSection.module.css@@ -9,3 +9,13 @@border-bottom: 1px solid var(--mb-color-border-neutral);}}++/* opacity rather than visibility, so the button stays reachable by keyboard */+.copyIcon {+ opacity: 0;+}++.item:hover .copyIcon,+.copyIcon:focus-visible {+ opacity: 1;+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsSidebar/ErrorsSection/ErrorsSection.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsSidebar/ErrorsSection/ErrorsSection.tsxindex 4d69cefd3a3..78501a3ac41 100644--- a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsSidebar/ErrorsSection/ErrorsSection.tsx+++ b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsSidebar/ErrorsSection/ErrorsSection.tsx@@ -2,8 +2,8 @@ import { useClipboard } from "@mantine/hooks";import cx from "classnames";import { t } from "ttag";-import { CopyButton } from "metabase/common/components/CopyButton";import CS from "metabase/css/core/index.css";+import visuallyHidden from "metabase/css/core/visually-hidden.module.css";import {ActionIcon,Badge,@@ -102,7 +102,7 @@ function ErrorListItem({ item }: ErrorListItemProps) {return (<Tooltip opened={clipboard.copied} label={t`Copied!`}><Group- className={cx(S.item, CS.hoverParent, CS.hoverVisibility)}+ className={S.item}p="lg"justify="space-between"wrap="nowrap"@@ -111,15 +111,13 @@ function ErrorListItem({ item }: ErrorListItemProps) {<Box className={cx(CS.textWrap, CS.textMonospace)} fz="sm" lh="1rem">{item}</Box>- <CopyButton- className={CS.hoverChild}- value={item}- target={- <ActionIcon aria-label={t`Copy`}>- <FixedSizeIcon name="copy" />- </ActionIcon>- }- />+ {/* The click bubbles up to the row, which does the copying */}+ <ActionIcon className={S.copyIcon} aria-label={t`Copy`}>+ <FixedSizeIcon name="copy" />+ </ActionIcon>+ <span role="status" className={visuallyHidden.visuallyHidden}>+ {clipboard.copied ? t`Copied!` : ""}+ </span></Group></Tooltip>);diff --git a/frontend/src/metabase/admin/ai/MetabotSlackSetupAlerts.tsx b/frontend/src/metabase/admin/ai/MetabotSlackSetupAlerts.tsxindex 97ce61bf1a8..e3cec6f3de1 100644--- a/frontend/src/metabase/admin/ai/MetabotSlackSetupAlerts.tsx+++ b/frontend/src/metabase/admin/ai/MetabotSlackSetupAlerts.tsx@@ -30,12 +30,13 @@ export function MissingScopesAlert({{t`Your Slack app is missing OAuth scopes required for Metabot. Copy the updated manifest and paste it in your Slack app settings. You will need to re-install your application to your workspace after.`}<Group gap="lg"><CopyButton+ className={S.copyButton}value={JSON.stringify(manifest, null, 2)}target={- <button type="button" className={S.copyButton}>+ <><span>{t`Copy manifest`}</span>- <Icon name="copy" size={16} ml="sm" />- </button>+ <Icon name="copy" size={16} ml="sm" aria-hidden />+ </>}/><ButtonLink href={slackUrl}>diff --git a/frontend/src/metabase/common/components/CopyButton/CopyButton.module.css b/frontend/src/metabase/common/components/CopyButton/CopyButton.module.cssindex 775d28bf35d..f19113a9963 100644--- a/frontend/src/metabase/common/components/CopyButton/CopyButton.module.css+++ b/frontend/src/metabase/common/components/CopyButton/CopyButton.module.css@@ -1,6 +1,3 @@.CopyButton {- &:focus {- outline: 2px solid var(--mb-color-input-focus);- border-radius: var(--mantine-radius-xxs);- }+ border-radius: var(--mantine-radius-xxs);}diff --git a/frontend/src/metabase/common/components/CopyButton/CopyButton.tsx b/frontend/src/metabase/common/components/CopyButton/CopyButton.tsxindex 595c8a4859b..3dd44f30c19 100644--- a/frontend/src/metabase/common/components/CopyButton/CopyButton.tsx+++ b/frontend/src/metabase/common/components/CopyButton/CopyButton.tsx@@ -1,64 +1,60 @@import { useClipboard } from "@mantine/hooks";import cx from "classnames";-import { useCallback } from "react";+import type { CSSProperties, ReactNode } from "react";import { t } from "ttag";import Styles from "metabase/css/core/index.css";-import { Icon, Text, Tooltip } from "metabase/ui";-import { isPlainKey } from "metabase/utils/keyboard";+import visuallyHidden from "metabase/css/core/visually-hidden.module.css";+import { Icon, Text, Tooltip, UnstyledButton } from "metabase/ui";-import CopyButtonStyles from "./CopyButton.module.css";+import S from "./CopyButton.module.css";-export const COPY_BUTTON_ICON = (- <Icon className={CopyButtonStyles.CopyButton} tabIndex={0} name="copy" />-);+export const COPY_BUTTON_ICON = <Icon name="copy" aria-hidden />;type CopyButtonProps = {value: string;onCopy?: () => void;className?: string;- style?: object;+ style?: CSSProperties;"aria-label"?: string;- target?: React.ReactNode;+ target?: ReactNode;};export const CopyButton = ({value,onCopy,- className = cx(Styles.textBrandHover, Styles.cursorPointer),+ className = Styles.textBrandHover,style,- target = COPY_BUTTON_ICON,+ "aria-label": ariaLabel,+ target,}: CopyButtonProps) => {const clipboard = useClipboard({ timeout: 2000 });+ const accessibleName = ariaLabel ?? (target ? undefined : t`Copy`);- const onCopyValue = useCallback(() => {+ const handleClick = () => {clipboard.copy(value);onCopy?.();- }, [clipboard, value, onCopy]);-- const copyOnEnter = useCallback(- (e: React.KeyboardEvent<HTMLElement>) => {- if (isPlainKey(e, "Enter")) {- onCopyValue();- }- },- [onCopyValue],- );+ };return (- <div- className={className}- data-testid="copy-button"- onClick={onCopyValue}- onKeyDown={copyOnEnter}- style={style}- >+ <><Tooltiplabel={<Text fw={700} c="inherit">{t`Copied!`}</Text>}opened={clipboard.copied}>- <span>{target}</span>+ <UnstyledButton+ className={cx(S.CopyButton, className)}+ data-testid="copy-button"+ aria-label={accessibleName}+ onClick={handleClick}+ style={style}+ >+ {target ?? COPY_BUTTON_ICON}+ </UnstyledButton></Tooltip>- </div>+ <span role="status" className={visuallyHidden.visuallyHidden}>+ {clipboard.copied ? t`Copied!` : ""}+ </span>+ </>);};diff --git a/frontend/src/metabase/common/components/CopyButton/CopyButton.unit.spec.tsx b/frontend/src/metabase/common/components/CopyButton/CopyButton.unit.spec.tsxnew file mode 100644index 00000000000..b4134488b0c--- /dev/null+++ b/frontend/src/metabase/common/components/CopyButton/CopyButton.unit.spec.tsx@@ -0,0 +1,90 @@+import { useClipboard } from "@mantine/hooks";+import userEvent from "@testing-library/user-event";+import type { ReactNode } from "react";++import { renderWithProviders, screen } from "__support__/ui";++import { CopyButton } from "./CopyButton";++jest.mock("@mantine/hooks", () => ({+ ...jest.requireActual("@mantine/hooks"),+ useClipboard: jest.fn(),+}));++const mockUseClipboard = jest.mocked(useClipboard);++interface SetupOpts {+ copied?: boolean;+ ariaLabel?: string;+ target?: ReactNode;+}++const setup = ({ copied = false, ariaLabel, target }: SetupOpts = {}) => {+ const copy = jest.fn();+ const onCopy = jest.fn();+ mockUseClipboard.mockReturnValue({+ copy,+ copied,+ reset: jest.fn(),+ error: null,+ });++ renderWithProviders(+ <CopyButton+ value="some value"+ onCopy={onCopy}+ aria-label={ariaLabel}+ target={target}+ />,+ );++ return { copy, onCopy };+};++describe("CopyButton", () => {+ it("renders a button named 'Copy' by default", () => {+ setup();++ expect(screen.getByRole("button", { name: "Copy" })).toHaveAttribute(+ "type",+ "button",+ );+ });++ it("copies the value on click", async () => {+ const { copy, onCopy } = setup();++ await userEvent.click(screen.getByRole("button", { name: "Copy" }));++ expect(copy).toHaveBeenCalledWith("some value");+ expect(onCopy).toHaveBeenCalledTimes(1);+ });++ it("uses a custom aria-label", () => {+ setup({ ariaLabel: "Copy link" });++ expect(+ screen.getByRole("button", { name: "Copy link" }),+ ).toBeInTheDocument();+ });++ it("is named by a custom target's content", () => {+ setup({ target: <span>Copy manifest</span> });++ expect(+ screen.getByRole("button", { name: "Copy manifest" }),+ ).toBeInTheDocument();+ });++ it("announces a successful copy", () => {+ setup({ copied: true });++ expect(screen.getByRole("status")).toHaveTextContent("Copied!");+ });++ it("announces nothing before copying", () => {+ setup();++ expect(screen.getByRole("status")).toBeEmptyDOMElement();+ });+});diff --git a/frontend/src/metabase/common/components/EntityIdCard/EntityIdCard.module.css b/frontend/src/metabase/common/components/EntityIdCard/EntityIdCard.module.cssindex 16c2da90f7e..f96f42498ca 100644--- a/frontend/src/metabase/common/components/EntityIdCard/EntityIdCard.module.css+++ b/frontend/src/metabase/common/components/EntityIdCard/EntityIdCard.module.css@@ -8,6 +8,15 @@}}+.CopyIcon {+ visibility: hidden;+}++.CopyButton:hover .CopyIcon,+.CopyButton:focus-visible .CopyIcon {+ visibility: visible;+}+.InfoIcon {color: var(--mb-color-text-disabled);diff --git a/frontend/src/metabase/common/components/EntityIdCard/EntityIdCard.tsx b/frontend/src/metabase/common/components/EntityIdCard/EntityIdCard.tsxindex d80ba5aeb09..a8eb588436d 100644--- a/frontend/src/metabase/common/components/EntityIdCard/EntityIdCard.tsx+++ b/frontend/src/metabase/common/components/EntityIdCard/EntityIdCard.tsx@@ -1,4 +1,3 @@-import cx from "classnames";import { t } from "ttag";import {@@ -12,7 +11,6 @@ import {SidesheetCardTitle,} from "metabase/common/components/Sidesheet";import { useDocsUrl, useHasTokenFeature } from "metabase/common/hooks";-import CS from "metabase/css/core/index.css";import {Flex,Group,@@ -71,15 +69,18 @@ export const EntityInfoIcon = () => {export const EntityCopyButton = ({ entityId }: { entityId: string }) => (<CopyButton- className={cx(Styles.CopyButton, CS.hoverParent, CS.hoverVisibility)}+ className={Styles.CopyButton}value={entityId}+ aria-label={t`Copy entity ID ${entityId}`}style={{height: "1rem",}}target={- <Flex gap="sm" wrap="nowrap" align="center">- <div className={cx(CS.hoverChild)}>{COPY_BUTTON_ICON}</div>- <Text lh="1rem">{entityId}</Text>+ <Flex component="span" gap="sm" wrap="nowrap" align="center">+ <span className={Styles.CopyIcon}>{COPY_BUTTON_ICON}</span>+ <Text component="span" lh="1rem">+ {entityId}+ </Text></Flex>}/>diff --git a/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.module.css b/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.module.cssindex 4f473445207..c0e108b6daa 100644--- a/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.module.css+++ b/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.module.css@@ -7,8 +7,7 @@align-items: center;padding: var(--mantine-spacing-sm);border-left: 1px solid var(--mb-color-border-neutral);- border-top-right-radius: var(--mantine-radius-xxs);- border-bottom-right-radius: var(--mantine-radius-xxs);+ border-radius: 0 var(--mantine-radius-xxs) var(--mantine-radius-xxs) 0;color: var(--mb-color-core-brand);outline: none;diff --git a/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.unit.spec.tsx b/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.unit.spec.tsxindex ae4a2b306a5..b130c205a75 100644--- a/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.unit.spec.tsx+++ b/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.unit.spec.tsx@@ -2,7 +2,7 @@ import userEvent from "@testing-library/user-event";import type { AnySchema } from "yup";import * as Yup from "yup";-import { getIcon, queryIcon, render, screen, waitFor } from "__support__/ui";+import { render, screen, waitFor } from "__support__/ui";import {Form,FormProvider,@@ -66,7 +66,7 @@ describe("FormTextInput", () => {});expect(screen.getByDisplayValue("Test")).toBeInTheDocument();- expect(getIcon("copy")).toBeInTheDocument();+ expect(screen.getByRole("button", { name: "Copy" })).toBeInTheDocument();});it("should not show copy button when disabled", () => {@@ -76,7 +76,9 @@ describe("FormTextInput", () => {});expect(screen.getByDisplayValue("Test")).toBeInTheDocument();- expect(queryIcon("copy")).not.toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: "Copy" }),+ ).not.toBeInTheDocument();});it("should submit a non-empty value", async () => {diff --git a/frontend/src/metabase/monitor/tools/components/TaskDetailsPage/TaskDetailsPage.unit.spec.tsx b/frontend/src/metabase/monitor/tools/components/TaskDetailsPage/TaskDetailsPage.unit.spec.tsxindex 7daf4d21ad3..f77d741f1db 100644--- a/frontend/src/metabase/monitor/tools/components/TaskDetailsPage/TaskDetailsPage.unit.spec.tsx+++ b/frontend/src/metabase/monitor/tools/components/TaskDetailsPage/TaskDetailsPage.unit.spec.tsx@@ -9,7 +9,6 @@ import {renderWithProviders,screen,waitForLoaderToBeRemoved,- within,} from "__support__/ui";import { Route } from "metabase/router";import * as Urls from "metabase/urls";@@ -125,18 +124,12 @@ describe("TaskDetailsPage", () => {await waitForLoaderToBeRemoved();- const copyButtons = screen.getAllByTestId("copy-button");+ const copyButtons = screen.getAllByRole("button", { name: "Copy" });- const startedAtCopyIcon = within(copyButtons[0]).getByRole("img", {- name: "copy icon",- });- await userEvent.click(startedAtCopyIcon);+ await userEvent.click(copyButtons[0]);expect(copyMock).toHaveBeenCalledWith(startedAt);- const endedAtCopyIcon = within(copyButtons[1]).getByRole("img", {- name: "copy icon",- });- await userEvent.click(endedAtCopyIcon);+ await userEvent.click(copyButtons[1]);expect(copyMock).toHaveBeenCalledWith(endedAt);});diff --git a/frontend/src/metabase/monitor/tools/components/TaskRunDetailsPage/TaskRunDetailsPage.unit.spec.tsx b/frontend/src/metabase/monitor/tools/components/TaskRunDetailsPage/TaskRunDetailsPage.unit.spec.tsxindex 0d03534f2f4..83a4efeea73 100644--- a/frontend/src/metabase/monitor/tools/components/TaskRunDetailsPage/TaskRunDetailsPage.unit.spec.tsx+++ b/frontend/src/metabase/monitor/tools/components/TaskRunDetailsPage/TaskRunDetailsPage.unit.spec.tsx@@ -6,7 +6,6 @@ import {renderWithProviders,screen,waitForLoaderToBeRemoved,- within,} from "__support__/ui";import { Route } from "metabase/router";import * as Urls from "metabase/urls";@@ -115,18 +114,12 @@ describe("TaskRunDetailsPage", () => {await waitForLoaderToBeRemoved();- const copyButtons = screen.getAllByTestId("copy-button");+ const copyButtons = screen.getAllByRole("button", { name: "Copy" });- const startedAtCopyIcon = within(copyButtons[0]).getByRole("img", {- name: "copy icon",- });- await userEvent.click(startedAtCopyIcon);+ await userEvent.click(copyButtons[0]);expect(copyMock).toHaveBeenCalledWith(startedAt);- const endedAtCopyIcon = within(copyButtons[1]).getByRole("img", {- name: "copy icon",- });- await userEvent.click(endedAtCopyIcon);+ await userEvent.click(copyButtons[1]);expect(copyMock).toHaveBeenCalledWith(endedAt);});});