Skip to content
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
38 changes: 38 additions & 0 deletions packages/ui/src/base/badge/badge.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,44 @@ export const IconOnly: Story = {
},
};

export const IconOpticallyAlignedIconOnly: Story = {
argTypes: {
children: {
control: false,
},
icon: {
control: false,
},
},
args: {
children: undefined,
},
render: (args) => (
<div style={collectionStyle}>
<GlBadge {...args} variant="success" icon="issue-open-m" aria-label="Open" />
<GlBadge {...args} variant="info" icon="issue-close" aria-label="Closed" />
<GlBadge {...args} variant="danger" icon="status_failed" iconOpticallyAligned aria-label="Failed" />
<GlBadge {...args} variant="success" icon="status_success" iconOpticallyAligned aria-label="Passed" />
<GlBadge {...args} variant="warning" icon="status_pending" iconOpticallyAligned aria-label="Pending" />
</div>
),
play: async ({ canvas }) => {
for(const [icon, label] of [
["issue-open-m", "Open"],
["issue-close", "Closed"],
["status_failed", "Failed"],
["status_success", "Passed"],
["status_pending", "Pending"],
]) {
const badge = canvas.getByRole("img", { name: label });

await expect(canvas.getByTestId(`${icon}-icon`)).not.toHaveClass("-gl-ml-2");
await expect(badge).toHaveClass("!gl-px-2");
await expect(badge.querySelector(".gl-badge-content")).toBeNull();
}
},
};

export const Truncated: Story = {
argTypes: {
children: {
Expand Down
20 changes: 19 additions & 1 deletion packages/ui/src/base/badge/badge.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe("GlBadge", () => {
it.each([
[{ icon: "issue-open-m" }, true],
[{ icon: "issue-close" }, true],
[{ icon: "status_failed", iconOpticallyAligned: true }, true],
[{ icon: "license", iconOpticallyAligned: true }, true],
[{ icon: "license" }, false],
] as const)("aligns circular icons for %s", (props, expected) => {
Expand All @@ -81,6 +82,22 @@ describe("GlBadge", () => {
expect(markup.includes("-gl-ml-2")).toBe(expected);
});

it.each([
["issue-open-m", false],
["issue-close", false],
["status_failed", true],
] as const)("keeps the icon-only %s badge centered", (icon, iconOpticallyAligned) => {
const markup = renderToStaticMarkup(
<GlBadge aria-label="Status" icon={icon} iconOpticallyAligned={iconOpticallyAligned} />,
);

expect(markup).not.toContain("-gl-ml-2");
expect(markup).toContain("role=\"img\"");
expect(markup).toContain("aria-label=\"Status\"");
expect(markup).toContain("!gl-px-2");
expect(markup).not.toContain("gl-badge-content");
});

it("warns when an icon-only badge has no aria-label", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});

Expand Down Expand Up @@ -132,11 +149,12 @@ describe("GlBadge", () => {

it("keeps the img role for icon-only link badges", () => {
const markup = renderBadge(
{ "aria-label": "Scheduled", href: "https://www.gitlab.com", icon: "calendar" },
{ "aria-label": "Closed", href: "https://www.gitlab.com", icon: "issue-close" },
null,
);

expect(markup).toContain("role=\"img\"");
expect(markup).not.toContain("-gl-ml-2");
});
});
});
4 changes: 2 additions & 2 deletions packages/ui/src/base/badge/badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type GlBadgeProps = BaseBadgeProps & {
href?: string;
/** Name of an icon from `@gitlab/svgs` shown before the text. */
icon?: string;
/** Optically aligns circular icons with the badge. */
/** Optically aligns circular icons when the badge also has content. */
iconOpticallyAligned?: boolean;
/** Icon size: `sm` (12px) or `md` (16px). */
iconSize?: GlBadgeIconSize;
Expand Down Expand Up @@ -147,7 +147,7 @@ const GlBadge = forwardRef<HTMLElement, GlBadgeProps>(function GlBadge({
{icon ? (
<GlIcon
className={badgeIconVariants({
circular: iconOpticallyAligned || circularIconNames.has(icon),
circular: hasContent && (iconOpticallyAligned || circularIconNames.has(icon)),
})}
name={icon}
size={badgeIconSizes[iconSize]} />
Expand Down
Loading