PromucFlow_constructor/app/client/src/components/designSystems/blueprint/ButtonComponent.tsx

136 lines
3.7 KiB
TypeScript
Raw Normal View History

2019-10-21 15:12:45 +00:00
import React from "react";
import { AnchorButton, IButtonProps, MaybeElement } from "@blueprintjs/core";
2019-10-21 15:12:45 +00:00
import styled, { css } from "styled-components";
2019-10-31 05:28:11 +00:00
import { TextComponentProps } from "./TextComponent";
2019-11-25 05:07:27 +00:00
import { ButtonStyle } from "widgets/ButtonWidget";
import { Theme } from "constants/DefaultTheme";
2019-11-28 07:08:39 +00:00
import _ from "lodash";
2019-10-21 15:12:45 +00:00
2019-11-13 07:34:59 +00:00
const getButtonColorStyles = (props: { theme: Theme } & ButtonStyleProps) => {
if (props.filled) return props.theme.colors.textOnDarkBG;
2019-11-28 07:08:39 +00:00
if (props.accent) {
if (props.accent === "secondary") {
2019-11-13 07:34:59 +00:00
return props.theme.colors.OXFORD_BLUE;
2019-10-21 15:12:45 +00:00
}
2019-11-28 07:08:39 +00:00
return props.theme.colors[props.accent];
2019-11-13 07:34:59 +00:00
}
};
const ButtonColorStyles = css<ButtonStyleProps>`
color: ${getButtonColorStyles};
svg {
fill: ${getButtonColorStyles};
}
2019-10-21 15:12:45 +00:00
`;
2019-11-28 07:08:39 +00:00
const ButtonWrapper = styled((props: ButtonStyleProps & IButtonProps) => (
<AnchorButton {..._.omit(props, ["accent", "filled"])} />
))<ButtonStyleProps>`
2019-10-21 15:12:45 +00:00
&& {
${ButtonColorStyles};
width: 100%;
height: 100%;
transition: background-color 0.2s;
background-color: ${props =>
2019-11-28 07:08:39 +00:00
props.filled && props.accent && props.theme.colors[props.accent]};
2019-10-21 15:12:45 +00:00
border: 1px solid
${props =>
2019-11-28 07:08:39 +00:00
props.accent
? props.theme.colors[props.accent]
2019-10-21 15:12:45 +00:00
: props.theme.colors.secondary};
border-radius: 4px;
2019-11-14 09:01:23 +00:00
font-weight: ${props => props.theme.fontWeights[2]};
font-family: "DM Sans";
2019-10-21 15:12:45 +00:00
outline: none;
&&:hover,
&&:focus {
${ButtonColorStyles};
background-color: ${props => {
if (!props.filled) return props.theme.colors.secondaryDarker;
2019-11-28 07:08:39 +00:00
if (props.accent !== "secondary") {
return props.theme.colors[`${props.accent}Darker`];
2019-10-21 15:12:45 +00:00
}
}};
border-color: ${props => {
if (!props.filled) return;
2019-11-28 07:08:39 +00:00
if (props.accent !== "secondary") {
return props.theme.colors[`${props.accent}Darker`];
2019-10-21 15:12:45 +00:00
}
}};
}
&&:active {
${ButtonColorStyles};
background-color: ${props => {
if (!props.filled) return props.theme.colors.secondaryDarkest;
2019-11-28 07:08:39 +00:00
if (props.accent !== "secondary") {
return props.theme.colors[`${props.accent}Darkest`];
2019-10-21 15:12:45 +00:00
}
}};
border-color: ${props => {
if (!props.filled) return;
2019-11-28 07:08:39 +00:00
if (props.accent !== "secondary") {
return props.theme.colors[`${props.accent}Darkest`];
2019-10-21 15:12:45 +00:00
}
}};
}
2019-11-25 09:15:11 +00:00
&&.bp3-disabled {
background-color: #d0d7dd;
border: none;
}
2019-10-21 15:12:45 +00:00
}
`;
export type ButtonStyleName = "primary" | "secondary" | "error";
2019-10-21 15:12:45 +00:00
type ButtonStyleProps = {
2019-11-28 07:08:39 +00:00
accent?: ButtonStyleName;
2019-10-21 15:12:45 +00:00
filled?: boolean;
};
// To be used in any other part of the app
export const BaseButton = (props: IButtonProps & ButtonStyleProps) => {
return <ButtonWrapper {...props} />;
};
BaseButton.defaultProps = {
2019-11-28 07:08:39 +00:00
accent: "secondary",
disabled: false,
2019-10-21 15:12:45 +00:00
text: "Button Text",
minimal: true,
};
interface ButtonContainerProps extends TextComponentProps {
icon?: MaybeElement;
onClick?: (event: React.MouseEvent<HTMLElement>) => void;
disabled?: boolean;
2019-10-31 05:28:11 +00:00
buttonStyle?: ButtonStyle;
2019-10-21 15:12:45 +00:00
}
2019-10-31 05:28:11 +00:00
const mapButtonStyleToStyleName = (buttonStyle?: ButtonStyle) => {
switch (buttonStyle) {
case "PRIMARY_BUTTON":
return "primary";
case "SECONDARY_BUTTON":
return "secondary";
case "DANGER_BUTTON":
return "error";
default:
return undefined;
}
};
2019-10-21 15:12:45 +00:00
// To be used with the canvas
const ButtonContainer = (props: ButtonContainerProps & ButtonStyleProps) => {
2019-10-21 15:12:45 +00:00
return (
<BaseButton
icon={props.icon}
text={props.text}
filled={props.buttonStyle === "PRIMARY_BUTTON"}
2019-11-28 07:08:39 +00:00
accent={mapButtonStyleToStyleName(props.buttonStyle)}
onClick={props.onClick}
disabled={props.disabled}
/>
2019-10-21 15:12:45 +00:00
);
};
export default ButtonContainer;