Skip to content

fix: correct markup for Abbr component #19317

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

Merged
merged 6 commits into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions site/src/components/Abbr/Abbr.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const meta: Meta<typeof Abbr> = {
component: Abbr,
decorators: [
(Story) => (
<>
<div className="max-w-prose text-base">
<p>Try the following text out in a screen reader!</p>
<Story />
</>
</div>
),
],
};
Expand All @@ -25,9 +25,9 @@ export const InlinedShorthand: Story = {
},
decorators: [
(Story) => (
<p className="max-w-2xl">
<p>
The physical pain of getting bonked on the head with a cartoon mallet
lasts precisely 593{" "}
lasts precisely 593
<span className="underline decoration-dotted">
<Story />
</span>
Expand Down
61 changes: 34 additions & 27 deletions site/src/components/Abbr/Abbr.test.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
import { render, screen } from "@testing-library/react";
import { Abbr, type Pronunciation } from "./Abbr";
import { Abbr } from "./Abbr";

type AbbreviationData = {
abbreviation: string;
title: string;
expectedLabel: string;
};

type AssertionInput = AbbreviationData & {
pronunciation: Pronunciation;
};

function assertAccessibleLabel({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got rid of the test helper because it felt like we were binding all our tests to a shallow abstraction

abbreviation,
title,
expectedLabel,
pronunciation,
}: AssertionInput) {
const { unmount } = render(
<Abbr title={title} pronunciation={pronunciation}>
{abbreviation}
</Abbr>,
);

screen.getByLabelText(expectedLabel, { selector: "abbr" });
unmount();
}

describe(Abbr.name, () => {
it("Has an aria-label that equals the title if the abbreviation is shorthand", () => {
it("Omits abbreviation from screen-reader output if it is shorthand", () => {
const sampleShorthands: AbbreviationData[] = [
{
abbreviation: "ms",
Expand All @@ -43,11 +23,22 @@ describe(Abbr.name, () => {
];

for (const shorthand of sampleShorthands) {
assertAccessibleLabel({ ...shorthand, pronunciation: "shorthand" });
const { unmount } = render(
<Abbr title={shorthand.title} pronunciation="shorthand">
{shorthand.abbreviation}
</Abbr>,
);

// The <abbr> element doesn't have any ARIA role semantics baked in,
// so we have to get a little bit more creative with making sure the
// expected content is on screen in an accessible way
const element = screen.getByTitle(shorthand.title);
expect(element).toHaveTextContent(shorthand.expectedLabel);
unmount();
}
});

it("Has an aria label with title and 'flattened' pronunciation if abbreviation is acronym", () => {
it("Adds title and 'flattened' pronunciation if abbreviation is acronym", () => {
const sampleAcronyms: AbbreviationData[] = [
{
abbreviation: "NASA",
Expand All @@ -67,11 +58,19 @@ describe(Abbr.name, () => {
];

for (const acronym of sampleAcronyms) {
assertAccessibleLabel({ ...acronym, pronunciation: "acronym" });
const { unmount } = render(
<Abbr title={acronym.title} pronunciation="acronym">
{acronym.abbreviation}
</Abbr>,
);

const element = screen.getByTitle(acronym.title);
expect(element).toHaveTextContent(acronym.expectedLabel);
unmount();
}
});

it("Has an aria label with title and initialized pronunciation if abbreviation is initialism", () => {
it("Adds title and initialized pronunciation if abbreviation is initialism", () => {
const sampleInitialisms: AbbreviationData[] = [
{
abbreviation: "FBI",
Expand All @@ -91,7 +90,15 @@ describe(Abbr.name, () => {
];

for (const initialism of sampleInitialisms) {
assertAccessibleLabel({ ...initialism, pronunciation: "initialism" });
const { unmount } = render(
<Abbr title={initialism.title} pronunciation="initialism">
{initialism.abbreviation}
</Abbr>,
);

const element = screen.getByTitle(initialism.title);
expect(element).toHaveTextContent(initialism.expectedLabel);
unmount();
}
});
});
20 changes: 12 additions & 8 deletions site/src/components/Abbr/Abbr.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { FC, HTMLAttributes } from "react";
import { cn } from "utils/cn";

export type Pronunciation = "shorthand" | "acronym" | "initialism";
type Pronunciation = "shorthand" | "acronym" | "initialism";

type AbbrProps = HTMLAttributes<HTMLElement> & {
children: string;
title: string;
pronunciation?: Pronunciation;
className?: string;
};

/**
Expand All @@ -22,23 +23,26 @@ export const Abbr: FC<AbbrProps> = ({
children,
title,
pronunciation = "shorthand",
className,
...delegatedProps
}) => {
return (
<abbr
// Title attributes usually aren't natively available to screen readers;
// always have to supplement with aria-label
// Adding title to make things a little easier for sighted users,
// but titles aren't always exposed via screen readers. Still have
// to inject the actual text content inside the abbr itself
title={title}
aria-label={getAccessibleLabel(children, title, pronunciation)}
className={cn(
"decoration-inherit",
children === children.toUpperCase()
? "tracking-wide"
: "tracking-normal",
"no-underline tracking-normal",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we understood where the underline is coming from. these kinds of conflicts are so annoying. can we at least add a TODO to clean this up one day?

Copy link
Member Author

@Parkreiner Parkreiner Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the funny thing is, abbr elements have an underline by default in normal HTML – it's MUI that somehow overrode it

I actually don't think we should do anything – once MUI is out, we should be much closer to "normal" CSS, and this behavior would be 100% expected

children === children.toUpperCase() && "tracking-wide",
className,
)}
{...delegatedProps}
>
<span aria-hidden>{children}</span>
<span className="sr-only">
{getAccessibleLabel(children, title, pronunciation)}
</span>
</abbr>
);
};
Expand Down
Loading