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(toast): toast vertical align prop #6391

Merged
merged 2 commits into from
Nov 8, 2023
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
23 changes: 22 additions & 1 deletion cypress/components/toast/toast.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from "react";
import { ToastProps } from "components/toast";
import { ToastComponent } from "../../../src/components/toast/toast-test.stories";
import {
ToastComponent,
AllAlign,
} from "../../../src/components/toast/toast-test.stories";
import { TOAST_COLORS } from "../../../src/components/toast/toast.config";
import CypressMountWithProviders from "../../support/component-helper/cypress-mount";
import toastComponent from "../../locators/toast";
Expand Down Expand Up @@ -204,6 +207,19 @@ context("Testing Toast component", () => {
.should("have.css", "justify-content", align);
}
);
it.each(["top", "bottom"] as ToastProps["alignY"][])(
"should render Toast component alignY prop set to %s",
(alignY) => {
CypressMountWithProviders(<ToastComponent alignY={alignY} />);

toastComponent()
.parent()
.parent()
.parent()
.parent()
Comment on lines +215 to +219
Copy link
Contributor

Choose a reason for hiding this comment

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

thought: it might be worth when we migrate these tests to Playwright to amend how we are locating the portal element here.

It's not essential now, but if we can directly locate the portal element instead of traversing the DOM, this should make the test less fragile if we ever change the component's markup.

.should("have.css", alignY, "0px");
}
);
});

describe("check events for Toast component", () => {
Expand Down Expand Up @@ -248,6 +264,11 @@ context("Testing Toast component", () => {

cy.checkAccessibility();
});

it("should render Toast components with all align combinations and check accessibility", () => {
CypressMountWithProviders(<AllAlign />);
cy.checkAccessibility();
});
});

it("render with the expected border radius", () => {
Expand Down
160 changes: 159 additions & 1 deletion src/components/toast/toast-test.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TOAST_COLORS } from "./toast.config";

export default {
title: "Toast/Test",
includeStories: ["Default", "Visual"],
includeStories: ["Default", "Visual", "AllAlign", "TopAndBottom"],
parameters: {
info: { disable: true },
chromatic: {
Expand Down Expand Up @@ -234,6 +234,164 @@ Visual.parameters = {
themeProvider: { chromatic: { theme: "sage" } },
};

export const AllAlign = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: nice idea to use chromatic to check all the align permutations 👍🏼

const [isOpen, setIsOpen] = useState(true);
const onDismissClick = () => {
setIsOpen(!isOpen);
};
return (
<div>
<Toast
align="left"
alignY="top"
isCenter={false}
variant="warning"
id="left-top"
targetPortalId="left-top"
open={isOpen}
onDismiss={onDismissClick}
>
My text
</Toast>

<Toast
align="center"
alignY="top"
isCenter={false}
variant="warning"
id="center-top"
targetPortalId="center-top"
open={isOpen}
onDismiss={onDismissClick}
>
My text
</Toast>

<Toast
align="right"
alignY="top"
isCenter={false}
variant="warning"
id="right-top"
targetPortalId="right-top"
open={isOpen}
onDismiss={onDismissClick}
>
My text
</Toast>

<Toast
align="left"
alignY="center"
isCenter={false}
variant="warning"
id="left-center"
targetPortalId="left-center"
open={isOpen}
onDismiss={onDismissClick}
>
My text
</Toast>

<Toast
align="center"
alignY="center"
isCenter={false}
variant="warning"
id="center-center"
targetPortalId="center-center"
open={isOpen}
onDismiss={onDismissClick}
>
My text
</Toast>

<Toast
align="right"
alignY="center"
isCenter={false}
variant="warning"
id="right-center"
targetPortalId="right-center"
open={isOpen}
onDismiss={onDismissClick}
>
My text
</Toast>

<Toast
align="left"
alignY="bottom"
isCenter={false}
variant="warning"
id="left-bottom"
targetPortalId="left-bottom"
open={isOpen}
onDismiss={onDismissClick}
>
My text
</Toast>

<Toast
align="center"
alignY="bottom"
isCenter={false}
variant="warning"
id="center-bottom"
targetPortalId="center-bottom"
open={isOpen}
onDismiss={onDismissClick}
>
My text
</Toast>

<Toast
align="right"
alignY="bottom"
isCenter={false}
variant="warning"
id="right-bottom"
targetPortalId="right-bottom"
open={isOpen}
onDismiss={onDismissClick}
>
My text
</Toast>
</div>
);
};

AllAlign.storyName = "all align";

export const TopAndBottom = () => {
const [isOpen, setIsOpen] = useState(true);
const handleOpen = () => {
setIsOpen(!isOpen);
action("open")(!isOpen);
};
return (
<>
<Button id="button" key="button" onClick={handleOpen}>
Open Toasts
</Button>
<Toast id="toast-a" variant="success" open={isOpen} isCenter alignY="top">
My Toast A
</Toast>
<Toast
id="toast-b"
variant="warning"
open={isOpen}
isCenter
alignY="bottom"
>
My Toast B
</Toast>
</>
);
};

TopAndBottom.storyName = "top and bottom";

export const ToastComponent = ({
children = "Toast",
...props
Expand Down
8 changes: 7 additions & 1 deletion src/components/toast/toast.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ToastVariants =
| "notification";

type AlignOptions = "left" | "center" | "right";
type AlignYOptions = "top" | "center" | "bottom";

interface IconTypes {
notification: "alert";
Expand All @@ -38,8 +39,10 @@ interface IconTypes {
}

export interface ToastProps {
/** Sets the alignment of the component. */
/** Sets the horizontal alignment of the component. */
align?: AlignOptions;
/** Sets the vertical alignment of the component */
alignY?: AlignYOptions;
/** The rendered children of the component. */
children: React.ReactNode;
/** Customizes the appearance in the DLS theme */
Expand Down Expand Up @@ -77,6 +80,7 @@ export const Toast = React.forwardRef<HTMLDivElement, ToastProps>(
(
{
align,
alignY,
children,
className,
id,
Expand Down Expand Up @@ -208,6 +212,7 @@ export const Toast = React.forwardRef<HTMLDivElement, ToastProps>(
>
<ToastStyle
align={align}
alignY={alignY}
isNotice={isNotice}
isNotification={isNotification}
className={className}
Expand Down Expand Up @@ -240,6 +245,7 @@ export const Toast = React.forwardRef<HTMLDivElement, ToastProps>(
<StyledPortal
id={targetPortalId}
align={align}
alignY={alignY}
isCenter={isCenter}
isNotice={isNotice}
>
Expand Down
49 changes: 48 additions & 1 deletion src/components/toast/toast.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ describe("TestContentStyle", () => {
});
});

describe("Align", () => {
describe("Align horizontal", () => {
let wrapper: ReactWrapper;

afterEach(() => {
Expand All @@ -593,6 +593,53 @@ describe("Align", () => {
);
});

describe("Align vertical", () => {
edleeks87 marked this conversation as resolved.
Show resolved Hide resolved
let wrapper: ReactWrapper;

afterEach(() => {
wrapper.unmount();
});

it.each(["top", "center", "bottom"] as const)(
"when align prop is %s, Portal is correctly positioned",
(alignYValue) => {
wrapper = mount(
<Toast alignY={alignYValue} open>
FooBar
</Toast>
);
expect(wrapper.find(StyledPortal).props().alignY).toBe(alignYValue);
}
);

it("when isNotice is set and alignY is set to top, should render with the correct style", () => {
wrapper = mount(
<Toast variant="notice" alignY="top" open>
Foo
</Toast>
);
assertStyleMatch({ marginTop: "0" }, wrapper.find(ToastStyle));
});
});

describe("Align vertical and horizontal", () => {
let wrapper: ReactWrapper;

afterEach(() => {
wrapper.unmount();
});

it("should pass align set to left and alignY set to center to StyledPortal", () => {
wrapper = mount(
<Toast align="left" alignY="center" open>
FooBar
</Toast>
);
expect(wrapper.find(StyledPortal).props().align).toBe("left");
expect(wrapper.find(StyledPortal).props().alignY).toBe("center");
});
});

describe("Notification variant", () => {
let wrapper: ReactWrapper;

Expand Down
33 changes: 26 additions & 7 deletions src/components/toast/toast.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,40 @@ Toast variant is `success` by default
<Story name="default" story={stories.Default} />
</Canvas>

### Align - Left
### Horizontal Align - Left

<Canvas>
<Story name="align left aligned" story={stories.AlignedLeft} />
<Story name="horizontal left aligned" story={stories.AlignedLeft} />
</Canvas>

### Align - Center
### Horizontal Align - Center

<Canvas>
<Story name="align center aligned" story={stories.AlignedCenter} />
<Story name="horizontal center aligned" story={stories.AlignedCenter} />
</Canvas>

### Align - Right
### Horizontal Align - Right

<Canvas>
<Story name="align right aligned" story={stories.AlignedRight} />
<Story name="horizontal right aligned" story={stories.AlignedRight} />
</Canvas>

### Vertical Align - Top

<Canvas>
<Story name="vertical top aligned" story={stories.AlignedYTop} />
</Canvas>

### Vertical Align - Center

<Canvas>
<Story name="vertical center aligned" story={stories.AlignedYCenter} />
</Canvas>

### Vertical Align - Bottom

<Canvas>
<Story name="vertical bottom aligned" story={stories.AlignedYBottom} />
</Canvas>

### Info
Expand Down Expand Up @@ -121,7 +139,8 @@ Toast variant is `success` by default
### Notice

When the "notice" variant is set, the Toast component is rendered at the bottom of the screen with alternative styling and animation.
The "isCenter" and "maxWidth" props will be ignored in this variant.
The "isCenter" and "maxWidth" props will be ignored in this variant.
To render it at the top of the screen use the "alignY" prop set to "top".

<Canvas>
<Story name="notice" story={stories.Notice} />
Expand Down
Loading
Loading