Skip to content

Commit

Permalink
Merge pull request #186 from kiwicom/add-new-buttonlink
Browse files Browse the repository at this point in the history
Add new ButtonLink component
  • Loading branch information
tomashapl committed Jun 14, 2018
2 parents 128838f + 92f36fb commit 119935d
Show file tree
Hide file tree
Showing 13 changed files with 5,872 additions and 782 deletions.
163 changes: 163 additions & 0 deletions src/Alert/Alert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// @flow
import * as React from "react";
import styled from "styled-components";
import defaultTokens from "@kiwicom/orbit-design-tokens";

import { InformationCircle, Check, Alert as AlertTriangle, AlertCircle, Close } from "../icons";
import ButtonLink from "../ButtonLink/ButtonLink";

export const TYPE_OPTIONS = {
INFO: "info",
SUCCESS: "success",
WARNING: "warning",
CRITICAL: "critical",
};

type IconProps = {
icon: React.Node,
type: string,
};

const Icon = ({ icon, type }: IconProps) => {
// Icon should be boolean and TRUE
if (typeof icon === "boolean" && icon) {
if (type === TYPE_OPTIONS.INFO) {
return <InformationCircle />;
}
if (type === TYPE_OPTIONS.SUCCESS) {
return <Check />;
}
if (type === TYPE_OPTIONS.WARNING) {
return <AlertTriangle />;
}
if (type === TYPE_OPTIONS.CRITICAL) {
return <AlertCircle />;
}
}
return icon;
};

type Props = {
type: $Values<typeof TYPE_OPTIONS>,
title?: string,
icon?: React.Element<any> | boolean,
closable: boolean,
onClose: () => void,
children: React.Node,
theme: typeof defaultTokens,
};

const StyledDiv = ({ className, children }: { className: string, children: React.Node }) => (
<div className={className}>{children}</div>
);

const StyledAlert = styled(StyledDiv)`
position: relative;
display: flex;
width: 100%;
padding: ${({ theme, icon }) =>
icon ? `${theme.paddingAlert} ${theme.paddingAlertWithIcon}` : theme.paddingAlert};
padding-right: ${({ theme, closable }) => closable && theme.spaceXXLarge};
border-radius: ${({ theme }) => theme.borderRadiusNormal};
background: ${({ tokens, type }) => tokens.backgroundAlert[type]};
color: ${({ tokens, type }) => tokens.colorTextAlert[type]};
font-family: ${({ theme }) => theme.fontFamily};
font-size: ${({ theme }) => theme.fontSizeTextNormal};
box-sizing: border-box;
`;

const IconContainer = styled(StyledDiv)`
flex-shrink: 0;
margin-right: ${({ theme }) => theme.spaceSmall};
color: ${({ tokens, type }) => tokens.colorIconAlert[type]};
`;

const Title = styled(StyledDiv)`
display: flex;
align-items: center;
margin-bottom: ${({ theme, hasChildren }) => hasChildren && theme.spaceXSmall};
font-weight: ${({ theme }) => theme.fontWeightBold};
line-height: ${({ theme }) => theme.lineHeightHeading};
min-height: ${({ theme }) => theme.heightIconMedium};
`;

const Content = styled(StyledDiv)`
display: flex;
align-items: center;
min-height: ${({ theme, title }) => !title && theme.heightIconMedium};
margin-bottom: ${({ theme, title }) => title && theme.spaceXXSmall};
`;

const CloseContainer = styled(StyledDiv)`
position: absolute;
top: ${({ hasChildren }) => (hasChildren ? 0 : "50%")};
margin-top: ${({ hasChildren, theme }) => !hasChildren && `-${theme.widthIconSmall}`};
right: 0;
margin-right: ${({ hasChildren, theme }) => !hasChildren && theme.spaceXSmall};
`;

const Alert = (props: Props) => {
const { type, title, theme, closable, icon, onClose, children } = props;
const tokens = {
colorIconAlert: {
[TYPE_OPTIONS.INFO]: theme.colorAlertIconInfo,
[TYPE_OPTIONS.SUCCESS]: theme.colorAlertIconSuccess,
[TYPE_OPTIONS.WARNING]: theme.colorAlertIconWarning,
[TYPE_OPTIONS.CRITICAL]: theme.colorAlertIconCritical,
},
backgroundAlert: {
[TYPE_OPTIONS.INFO]: theme.backgroundAlertInfo,
[TYPE_OPTIONS.SUCCESS]: theme.backgroundAlertSuccess,
[TYPE_OPTIONS.WARNING]: theme.backgroundAlertWarning,
[TYPE_OPTIONS.CRITICAL]: theme.backgroundAlertCritical,
},
colorTextAlert: {
[TYPE_OPTIONS.INFO]: theme.colorTextAlertInfo,
[TYPE_OPTIONS.SUCCESS]: theme.colorTextAlertSuccess,
[TYPE_OPTIONS.WARNING]: theme.colorTextAlertWarning,
[TYPE_OPTIONS.CRITICAL]: theme.colorTextAlertCritical,
},
};
return (
<StyledAlert tokens={tokens} {...props}>
{icon && (
<IconContainer theme={theme} tokens={tokens} type={type}>
<Icon type={type} icon={icon} />
</IconContainer>
)}
<div>
{title && (
<Title theme={theme} hasChildren={children}>
{title}
</Title>
)}
{children && (
<Content theme={theme} title={title}>
{children}
</Content>
)}
</div>
{closable && (
<CloseContainer hasChildren={children} theme={theme}>
<ButtonLink
onClick={onClose}
size="small"
icon={<Close size="small" customColor={tokens.colorIconAlert[type]} />}
theme={theme}
transparent
/>
</CloseContainer>
)}
</StyledAlert>
);
};

Alert.displayName = "Alert";
Alert.defaultProps = {
type: "info",
closable: false,
onClose: () => {},
theme: defaultTokens,
};

export default Alert;
63 changes: 23 additions & 40 deletions src/Alert/Alert.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import styles from "@sambego/storybook-styles";
import chaptersAddon from "react-storybook-addon-chapters";
import { withKnobs, text, boolean, select } from "@storybook/addon-knobs/react";

import ThemeProvider from "../Theming/ThemeProvider";
import * as Icons from "../icons";

import Alert, { TYPE_OPTIONS } from "./index";
import Alert, { TYPE_OPTIONS } from "./Alert";

setAddon(chaptersAddon);

const getIcons = defaultIcon => select("Icon", [undefined, ...Object.keys(Icons)], defaultIcon);
const getIcon = source => Icons[source];
const renderTitle = title => (title === "" ? undefined : title);

const options = {
showSource: true,
Expand All @@ -40,11 +37,7 @@ storiesOf("Alert", module)
{
sections: [
{
sectionFn: () => (
<ThemeProvider>
<Alert>{message}</Alert>
</ThemeProvider>
),
sectionFn: () => <Alert>{message}</Alert>,
options,
},
],
Expand All @@ -63,11 +56,9 @@ storiesOf("Alert", module)
sections: [
{
sectionFn: () => (
<ThemeProvider>
<Alert title={renderTitle(title)} icon>
{message}
</Alert>
</ThemeProvider>
<Alert title={title} icon>
{message}
</Alert>
),
options,
},
Expand All @@ -88,11 +79,9 @@ storiesOf("Alert", module)
sections: [
{
sectionFn: () => (
<ThemeProvider>
<Alert type="success" title={renderTitle(title)} icon>
{message}
</Alert>
</ThemeProvider>
<Alert type="success" title={title} icon>
{message}
</Alert>
),
options,
},
Expand All @@ -112,11 +101,9 @@ storiesOf("Alert", module)
sections: [
{
sectionFn: () => (
<ThemeProvider>
<Alert type="warning" title={renderTitle(title)} icon>
{message}
</Alert>
</ThemeProvider>
<Alert type="warning" title={title} icon>
{message}
</Alert>
),
options,
},
Expand All @@ -136,11 +123,9 @@ storiesOf("Alert", module)
sections: [
{
sectionFn: () => (
<ThemeProvider>
<Alert type="critical" title={renderTitle(title)} icon>
{message}
</Alert>
</ThemeProvider>
<Alert type="critical" title={title} icon>
{message}
</Alert>
),
options,
},
Expand All @@ -163,17 +148,15 @@ storiesOf("Alert", module)
sections: [
{
sectionFn: () => (
<ThemeProvider>
<Alert
type={type}
icon={Icon && <Icon />}
title={renderTitle(title)}
closable={closable}
onClose={action("Close")}
>
{message}
</Alert>
</ThemeProvider>
<Alert
type={type}
icon={Icon && <Icon />}
title={title}
closable={closable}
onClose={action("Close")}
>
{message}
</Alert>
),
options,
},
Expand Down
Loading

0 comments on commit 119935d

Please sign in to comment.