Skip to content

Commit

Permalink
fix(link): ensure Icon in component has inline display property to re…
Browse files Browse the repository at this point in the history
…spect dimensions of the parent

all children of anchor elements in `Link` have the inline-block display property applied.
However due to our new `Icon` font sizes this now causes `Icon`s to not respect the dimensions
of parent containers. To mitigate this we've set display to inline instead when `Link`s are
rendered with no children and an `Icon`, ensuring the parents dimensions are properly respected

fix #6295
  • Loading branch information
tomdavies73 committed Oct 16, 2023
1 parent 41fe318 commit be0a33c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
15 changes: 14 additions & 1 deletion cypress/components/link/link.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable jest/valid-expect, jest/no-disabled-tests, no-unused-expressions */
import React from "react";
import { LinkProps } from "components/link";
import Link, { LinkProps } from "../../../src/components/link";
import { LinkComponent } from "../../../src/components/link/link-test.stories";
import { skipLink } from "../../locators/link/index";
import { keyCode } from "../../support/helper";
import { CHARACTERS } from "../../support/component-helper/constants";
import CypressMountWithProviders from "../../support/component-helper/cypress-mount";
import Box from "../../../src/components/box";
import { ICON } from "../../locators/locators";
import {
icon,
tooltipPreview,
Expand Down Expand Up @@ -72,6 +73,18 @@ context("Test for Link component", () => {
tooltipPreview().should("have.text", testCypress);
});

it("when no children are passed, there should be no text decoration on the anchor element", () => {
CypressMountWithProviders(<Link icon="bin" href="www.sage.com" />);

link().find("a").should("have.css", "text-decoration-line", "none");
});

it("when no children are passed, link should have the inline display property", () => {
CypressMountWithProviders(<Link icon="bin" href="www.sage.com" />);

link().find(ICON).should("have.css", "display", "inline");
});

it.each([
"top",
"bottom",
Expand Down
33 changes: 32 additions & 1 deletion src/components/link/link-test.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { action } from "@storybook/addon-actions";
import { ICONS } from "../icon/icon-config";
import { LINK_ALIGNMENTS, LINK_POSITIONS, LINK_VARIANTS } from "./link.config";
import Link, { LinkProps } from "./link.component";
import Box from "../box";

export default {
title: "Link/Test",
includeStories: ["DefaultStory"],
includeStories: ["DefaultStory", "FlexContainer"],
parameters: {
info: { disable: true },
chromatic: {
Expand Down Expand Up @@ -94,6 +95,36 @@ DefaultStory.args = {
isDarkBackground: false,
};

export const FlexContainer = () => {
const link = (
<Box
display="flex"
flexDirection="row"
justifyContent="flex-end"
alignItems="center"
width="60px"
height="40px"
bg="red"
mx={5}
>
<Link icon="close" variant="neutral" />
</Box>
);
return (
<div
style={{
margin: "64px",
width: "fit-content",
padding: "8px",
}}
>
{link}
</div>
);
};

FlexContainer.parameters = { chromatic: { disableSnapshot: false } };

export const LinkComponent = (props: LinkProps) => {
return (
<div
Expand Down
26 changes: 26 additions & 0 deletions src/components/link/link.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,32 @@ describe("Link", () => {
});
});

describe("when a link is rendered with an icon and no children", () => {
beforeEach(() => {
wrapper = mount(<Link icon="home" href="www.sage.com" />);
});

it("there should be no text decoration on the anchor element", () => {
assertStyleMatch(
{
textDecoration: "none",
},
wrapper.find(StyledLink),
{ modifier: "a" }
);
});

it("link should have the inline display property", () => {
assertStyleMatch(
{
display: "inline",
},
wrapper.find(StyledLink),
{ modifier: `a > ${StyledIcon}` }
);
});
});

describe("when the `onKeyDown` event is triggered", () => {
let onClickFn: () => jest.Mock;
let onKeyDownFn: () => jest.Mock;
Expand Down
4 changes: 2 additions & 2 deletions src/components/link/link.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ const StyledLink = styled.span<StyledLinkProps & PrivateStyledLinkProps>`
a,
button {
text-decoration: underline;
text-decoration: ${hasContent ? "underline" : "none"};
${isMenuItem && "display: inline-block;"}
> ${StyledIcon} {
display: inline-block;
display: ${hasContent ? "inline-block" : "inline"};
position: relative;
vertical-align: middle;
${iconAlign === "left" &&
Expand Down

0 comments on commit be0a33c

Please sign in to comment.