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

add close button to alert #2079

Merged
merged 14 commits into from
Jun 29, 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
5 changes: 5 additions & 0 deletions .changeset/six-ducks-eat-duplicate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-css": patch
---

- Button: Fikset outline-bug i tertiary-variant ved `:active`-state
6 changes: 6 additions & 0 deletions .changeset/six-ducks-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@navikt/ds-css": minor
"@navikt/ds-react": minor
---

- Alert: La til `closeButton`-prop
9 changes: 9 additions & 0 deletions @navikt/core/css/alert.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
font-size: 1.5rem;
align-self: flex-start;
height: var(--a-font-line-height-xlarge);
margin: var(--a-spacing-1);
background: radial-gradient(circle, var(--a-surface-default) 50%, 0, transparent);
}

Expand Down Expand Up @@ -73,3 +74,11 @@
border: none;
padding: 0;
}

.navds-alert__button-wrapper {
align-self: flex-start;
flex: 1;
display: flex;
flex-flow: row nowrap;
justify-content: flex-end;
}
2 changes: 0 additions & 2 deletions @navikt/core/css/button.css
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@
.navds-button--tertiary:active {
color: var(--ac-button-tertiary-active-text, var(--a-text-on-action));
background-color: var(--ac-button-tertiary-active-bg, var(--a-surface-action-active));
box-shadow: inset 0 0 0 1px var(--a-surface-default);
}

.navds-button--tertiary:active:hover {
Expand Down Expand Up @@ -346,7 +345,6 @@
.navds-button--tertiary-neutral:active {
color: var(--ac-button-tertiary-neutral-active-text, var(--a-text-on-neutral));
background-color: var(--ac-button-tertiary-neutral-active-bg, var(--a-surface-neutral-active));
box-shadow: inset 0 0 0 1px var(--a-surface-default);
}

.navds-button--tertiary-neutral:active:hover {
Expand Down
67 changes: 49 additions & 18 deletions @navikt/core/react/src/alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {
CheckmarkCircleFillIcon,
ExclamationmarkTriangleFillIcon,
XMarkOctagonFillIcon,
XMarkIcon,
} from "@navikt/aksel-icons";
import cl from "clsx";
import React, { forwardRef } from "react";
import { BodyLong } from "../typography/BodyLong";
import { Button } from "../button";

export interface AlertProps extends React.HTMLAttributes<HTMLDivElement> {
/**
Expand All @@ -32,6 +34,17 @@ export interface AlertProps extends React.HTMLAttributes<HTMLDivElement> {
* @default false
*/
inline?: boolean;
/**
* Removes close-button(X) when false
* Requires onClose to be set
* @default true
*/
closeButton?: boolean;
/**
* Callback for alert wanting to close
* requires closeButton to be true
*/
onClose?: () => void;
}

const Icon = ({ variant, ...props }) => {
Expand Down Expand Up @@ -71,27 +84,45 @@ export const Alert = forwardRef<HTMLDivElement, AlertProps>(
size = "medium",
fullWidth = false,
inline = false,
closeButton = false,
onClose,
...rest
},
ref
) => (
<div
{...rest}
ref={ref}
className={cl(
className,
"navds-alert",
`navds-alert--${variant}`,
`navds-alert--${size}`,
{ "navds-alert--full-width": fullWidth, "navds-alert--inline": inline }
)}
>
<Icon variant={variant} className="navds-alert__icon" />
<BodyLong as="div" size={size} className="navds-alert__wrapper">
{children}
</BodyLong>
</div>
)
) => {
return (
<div
{...rest}
ref={ref}
className={cl(
className,
"navds-alert",
`navds-alert--${variant}`,
`navds-alert--${size}`,
{
"navds-alert--full-width": fullWidth,
"navds-alert--inline": inline,
}
)}
>
<Icon variant={variant} className="navds-alert__icon" />
<BodyLong as="div" size={size} className="navds-alert__wrapper">
{children}
</BodyLong>
{closeButton && !inline && (
<div className="navds-alert__button-wrapper">
<Button
JulianNymark marked this conversation as resolved.
Show resolved Hide resolved
className="navds-alert__button"
size="small"
variant="tertiary-neutral"
onClick={onClose}
icon={<XMarkIcon title="Lukk Alert" />}
/>
</div>
)}
</div>
);
}
);

export default Alert;
97 changes: 75 additions & 22 deletions @navikt/core/react/src/alert/alert.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Meta, StoryObj } from "@storybook/react";
import React from "react";
import { Alert } from ".";
import { BodyLong, Heading as DsHeading, Link } from "..";
import { within, userEvent } from "@storybook/testing-library";
import { expect } from "@storybook/jest";

const meta: Meta<typeof Alert> = {
title: "ds-react/Alert",
Expand All @@ -20,22 +22,14 @@ const variants: Array<"error" | "warning" | "info" | "success"> = [
];

export const Default: Story = {
render: (props) => (
<Alert
variant={props.variant}
size={props.size}
fullWidth={props.fullWidth}
inline={props.inline}
>
{props.children}
</Alert>
),
render: (props) => <Alert {...props} />,

args: {
children: "Id elit esse enim reprehenderit enim nisi veniam nostrud.",
fullWidth: false,
variant: "info",
size: "medium",
closeButton: false,
},
argTypes: {
variant: {
Expand All @@ -50,21 +44,39 @@ export const Default: Story = {
},
options: ["medium", "small"],
},
closeButton: {
type: "boolean",
},
},
};

export const Small = () => {
return (
<div className="colgap">
{variants.map((variant, i) => (
<Alert key={variant} variant={variant} size="small">
{new Array(i + 1).fill(
"Id elit esse enim reprehenderit enim nisi veniam nostrud."
)}
</Alert>
))}
</div>
);
export const Small = {
render: (props) => {
return (
<div className="colgap">
{variants.map((variant, i) => (
<Alert
key={variant}
variant={variant}
size="small"
closeButton={props.closeButton}
>
{new Array(i + 1).fill(
"Id elit esse enim reprehenderit enim nisi veniam nostrud."
)}
</Alert>
))}
</div>
);
},
args: {
closeButton: false,
},
argtypes: {
closeButton: {
type: "boolean",
},
},
};

export const FullWidth = () => {
Expand Down Expand Up @@ -147,3 +159,44 @@ export const Links = () => {
</div>
);
};

const AlertWithCloseButton = ({ children }: { children?: React.ReactNode }) => {
let [show, setShow] = React.useState(true);

return (
show && (
<Alert variant="success" closeButton onClose={() => setShow(false)}>
{children || "Content"}
</Alert>
)
);
};

export const WithCloseButton: Story = {
render: () => {
return (
<div className="colgap">
<AlertWithCloseButton />
<AlertWithCloseButton>
<BodyLong>
Ullamco ullamco laborum et commodo sint culpa cupidatat culpa qui
laboris ex. Labore ex occaecat proident qui qui fugiat magna. Fugiat
sint commodo consequat eu aute.
</BodyLong>
<Link href="#">Id elit esse enim reprehenderit</Link>
</AlertWithCloseButton>
</div>
);
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
const buttons = canvas.getAllByTitle("Lukk Alert");

await step("click button", async () => {
await userEvent.click(buttons[0]);
});

const buttonsAfter = canvas.getAllByTitle("Lukk Alert");
expect(buttonsAfter.length).toBe(1);
},
};
53 changes: 53 additions & 0 deletions aksel.nav.no/website/pages/eksempler/alert/med-lukkeknapp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Alert, AlertProps } from "@navikt/ds-react";
import { withDsExample } from "components/website-modules/examples/withDsExample";
import React from "react";

const AlertWithCloseButton = ({
children,
variant,
}: {
children?: React.ReactNode;
variant: AlertProps["variant"];
}) => {
const [show, setShow] = React.useState(true);

return (
show && (
<Alert variant={variant} closeButton onClose={() => setShow(false)}>
{children || "Content"}
</Alert>
)
);
};

const Example = () => {
return (
<div className="grid gap-4">
<AlertWithCloseButton variant="info">
Hvis du er mellom 62 og 67 år når du søker, må du som hovedregel ha hatt
en pensjonsgivende inntekt som tilsvarer x G, året før du fikk nedsatt
arbeidsevnen.
</AlertWithCloseButton>
<AlertWithCloseButton variant="success">
Søknad er sendt inn!
</AlertWithCloseButton>
<AlertWithCloseButton variant="warning">
Du må være registrert hos NAV for å bruke aktivitetsplanen.
</AlertWithCloseButton>
<AlertWithCloseButton variant="error">
Noe gikk galt! Prøv igjen om noen minutter.
</AlertWithCloseButton>
</div>
);
};

export default withDsExample(Example);

/* Storybook story */
export const Demo = {
render: Example,
};

export const args = {
index: 1,
};