Skip to content
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

feat(hr): add height prop #7159

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions src/components/hr/hr.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ export interface HrProps extends MarginProps {
/** Breakpoint for adaptive left and right margins (below the breakpoint they go to 0).
* Enables the adaptive behaviour when set */
adaptiveMxBreakpoint?: number;
/** Set the height of the component. Accepts one of "small", "medium", or "large" */
height?: "small" | "medium" | "large";
}

export const Hr = ({
adaptiveMxBreakpoint,
ml,
mr,
"aria-hidden": ariaHidden,
height = "small",
...rest
}: HrProps): JSX.Element => {
const largeScreen = useIsAboveBreakpoint(adaptiveMxBreakpoint);
Expand All @@ -32,6 +35,7 @@ export const Hr = ({
aria-hidden={ariaHidden}
data-component="hr"
data-role="hr"
height={height}
ml={marginLeft}
mr={marginRight}
mt={rest.mt || 3}
Expand Down
6 changes: 6 additions & 0 deletions src/components/hr/hr.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ The `mb` and `mt` props set the vertical spacing. These props are mulitipliers a

<Canvas of={HrStories.DifferentSpacing} />

### With different heights

The `height` prop sets the height of the hr. Accepts `"small"`, `"medium"` and `"large"`.

<Canvas of={HrStories.DifferentHeights} />

### Inside a form

<Canvas of={HrStories.InsideForm} />
Expand Down
44 changes: 44 additions & 0 deletions src/components/hr/hr.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ test.describe("Hr component", () => {
await expect(hrComponent(page)).toHaveCSS("margin-bottom", "24px");
});

test("renders with a small height", async ({ mount, page }) => {
await mount(<HrComponent height="small" />);

await expect(hrComponent(page)).toHaveCSS("height", "1px");
});

test("renders with a medium height", async ({ mount, page }) => {
await mount(<HrComponent height="medium" />);

await expect(hrComponent(page)).toHaveCSS("height", "2px");
});

test("renders with a large height", async ({ mount, page }) => {
await mount(<HrComponent height="large" />);

await expect(hrComponent(page)).toHaveCSS("height", "3px");
});
[
[799, 80, 320],
[800, 80, 320],
Expand Down Expand Up @@ -72,6 +89,33 @@ test.describe("Accessibility tests for Hr component", () => {
await checkAccessibility(page);
});

test("should pass accessibility tests when rendered with a small height", async ({
mount,
page,
}) => {
await mount(<HrComponent height="small" />);

await checkAccessibility(page);
});

test("should pass accessibility tests when rendered with a medium height", async ({
mount,
page,
}) => {
await mount(<HrComponent height="medium" />);

await checkAccessibility(page);
});

test("should pass accessibility tests when rendered with a large height", async ({
mount,
page,
}) => {
await mount(<HrComponent height="large" />);

await checkAccessibility(page);
});

test("should pass accessibility tests for EnablingAdaptiveBehaviour example", async ({
mount,
page,
Expand Down
15 changes: 15 additions & 0 deletions src/components/hr/hr.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Meta, StoryObj } from "@storybook/react";
import generateStyledSystemProps from "../../../.storybook/utils/styled-system-props";

import Hr from ".";
import Box from "../box";
import Form from "../form";
import Textbox from "../textbox";
import Button from "../button";
Expand Down Expand Up @@ -37,6 +38,20 @@ export const DifferentSpacing: Story = () => {
};
DifferentSpacing.storyName = "Different Spacing";

export const DifferentHeights: Story = () => {
const heights = ["small", "medium", "large"] as const;
return (
<Box>
{heights.map((height) => (
<Box key={height} mb={3}>
<Hr height={height} />
</Box>
))}
</Box>
);
};
DifferentHeights.storyName = "Different Heights";

export const InsideForm: Story = () => {
return (
<Form
Expand Down
12 changes: 10 additions & 2 deletions src/components/hr/hr.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import styled from "styled-components";
import { margin, MarginProps } from "styled-system";
import baseTheme from "../../style/themes/base";

const StyledHr = styled.hr<MarginProps>`
const heightMap = {
small: 1,
medium: 2,
large: 3,
};

const StyledHr = styled.hr<
MarginProps & { height: "small" | "medium" | "large" }
>`
${margin}
width: inherit;
border: 0;
height: 1px;
height: ${({ height }) => heightMap[height]}px;
background: var(--colorsUtilityMajor100);
`;

Expand Down
21 changes: 21 additions & 0 deletions src/components/hr/hr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ testStyledSystemMargin(
},
);

test("should render with a small height", () => {
render(<Hr height="small" />);
const hr = screen.getByRole("separator");

expect(hr).toHaveStyle("height: 1px");
});

test("should render with a medium height", () => {
render(<Hr height="medium" />);
const hr = screen.getByRole("separator");

expect(hr).toHaveStyle("height: 2px");
});

test("should render with a large height", () => {
render(<Hr height="large" />);
const hr = screen.getByRole("separator");

expect(hr).toHaveStyle("height: 3px");
});

test("should apply the expected margin top", () => {
render(
<CarbonProvider validationRedesignOptIn>
Expand Down
Loading