Skip to content

Commit

Permalink
Merge pull request #54 from buildo/3175916-add_action_to_banner
Browse files Browse the repository at this point in the history
  • Loading branch information
gabro committed Mar 4, 2022
2 parents 0886864 + 42a5a62 commit ea184be
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 39 deletions.
101 changes: 63 additions & 38 deletions src/Banner/createBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
IconWarning,
IconNegative,
IconIdea,
ButtonProps,
} from "..";
import { Columns, Column, Box, Stack, BentoSprinkles } from "../internal";
import { Columns, Column, Box, Stack, BentoSprinkles, Inline } from "../internal";
import { bannerRecipe } from "./Banner.css";
import { ComponentProps, FunctionComponent } from "react";
import { IconProps } from "../Icons/IconProps";
Expand All @@ -31,6 +32,7 @@ type Kind = "informative" | "positive" | "warning" | "negative" | "tip";

type Props = {
kind: Kind;
action?: ActionProps;
} & DismissProps &
(
| {
Expand All @@ -43,6 +45,11 @@ type Props = {
}
);

type ActionProps = {
label: LocalizedString;
onPress: ButtonProps["onPress"];
};

type KindConfig<T> = {
[k in Kind]: T;
};
Expand All @@ -55,21 +62,24 @@ type BannerConfig = {
kindIcons?: KindConfig<FunctionComponent<IconProps>>;
};

export function createBanner({
padding = 16,
titleSize = "small",
descriptionSize = "small",
radius = 8,
closeIcon = IconClose,
kindIcons = {
informative: IconInformative,
positive: IconCheckCircleSolid,
warning: IconWarning,
negative: IconNegative,
tip: IconIdea,
},
}: BannerConfig) {
return function Banner({ title, description, kind, ...dismissProps }: Props) {
export function createBanner(
Button: FunctionComponent<ButtonProps>,
{
padding = 16,
titleSize = "small",
descriptionSize = "small",
radius = 8,
closeIcon = IconClose,
kindIcons = {
informative: IconInformative,
positive: IconCheckCircleSolid,
warning: IconWarning,
negative: IconNegative,
tip: IconIdea,
},
}: BannerConfig
) {
return function Banner({ title, description, kind, action, ...dismissProps }: Props) {
const isWithoutTitle = title === undefined;
const iconSize = isWithoutTitle ? 16 : 24;
const iconProps = { size: iconSize, color: kind === "tip" ? "secondary" : kind } as const;
Expand All @@ -79,31 +89,46 @@ export function createBanner({

return (
<Box padding={padding} borderRadius={radius} className={bannerRecipe({ kind })}>
<Columns space={16} align="left" alignY={title && description ? "top" : "center"}>
<Column width="content">
<Box>
<Icon {...iconProps} />
</Box>
</Column>
<Stack align="left" space={4}>
{title && (
<Title size={titleSize} color={kind === "tip" ? "secondary" : kind}>
{title}
</Title>
)}
{description && <Body size={descriptionSize}>{description}</Body>}
</Stack>
{dismissProps.onDismiss && (
<Stack space={4}>
<Columns space={16} align="left" alignY={title && description ? "top" : "center"}>
<Column width="content">
<IconButton
label={dismissProps.dismissButtonLabel ?? defaultMessages.Banner.dismissButtonLabel}
onPress={dismissProps.onDismiss}
size={12}
icon={closeIcon}
/>
<Box>
<Icon {...iconProps} />
</Box>
</Column>
<Stack align="left" space={4}>
{title && (
<Title size={titleSize} color={kind === "tip" ? "secondary" : kind}>
{title}
</Title>
)}
{description && <Body size={descriptionSize}>{description}</Body>}
</Stack>
{dismissProps.onDismiss && (
<Column width="content">
<IconButton
label={
dismissProps.dismissButtonLabel ?? defaultMessages.Banner.dismissButtonLabel
}
onPress={dismissProps.onDismiss}
size={12}
icon={closeIcon}
/>
</Column>
)}
</Columns>
{action && (
<Inline space={0} align="right">
<Button
onPress={action.onPress}
label={action.label}
kind="transparent"
hierarchy="secondary"
size="medium"
/>
</Inline>
)}
</Columns>
</Stack>
</Box>
);
};
Expand Down
11 changes: 11 additions & 0 deletions stories/Components/Banner.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createComponentStories, formatMessage, textArgType } from "../util";
import { Banner } from "../";
import { action } from "@storybook/addon-actions";

const { defaultExport, createStory } = createComponentStories({
component: Banner,
Expand Down Expand Up @@ -27,6 +28,16 @@ export const Dismissable = createStory({
description: shortDescription,
});

export const DismissableWithAction = createStory({
kind: "informative",
title,
description: shortDescription,
action: {
label: formatMessage("Close"),
onPress: action("onAction"),
},
});

export const NonDismissable = createStory(
{
kind: "informative",
Expand Down
2 changes: 1 addition & 1 deletion stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const {
export const { Button, ButtonLink } = createButtons();
export const Actions = createActions(Button);
export const { Form, FormSection, FormRow } = createFormLayoutComponents(Actions);
export const Banner = createBanner({});
export const Banner = createBanner(Button, {});
export const { Toast, ToastProvider } = createToast(Button, {});
export const Card = createCard<24 | 32 | 40>({});
export const Link = createLink();
Expand Down

0 comments on commit ea184be

Please sign in to comment.