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(Buttons): Add destructive styles #196

Merged
merged 3 commits into from
Feb 2, 2021
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
113 changes: 98 additions & 15 deletions packages/react/src/components/buttons/abstract-button.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import { AriaAttributes, ButtonHTMLAttributes, DetailedHTMLProps, ReactNode } from 'react';
import styled from 'styled-components';
import styled, { css, FlattenInterpolation, ThemeProps } from 'styled-components';
import { Theme } from '../../themes/theme';
import { focus } from '../../utils/css-state';

type PartialButtonProps =
Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, 'type'>
& AriaAttributes;

export interface AbstractButtonProps extends PartialButtonProps {
label?: string;
children?: ReactNode;
className?: string;
disabled?: boolean;

onClick?(): void;
}

export const AbstractButton = styled.button<{ isMobile: boolean }>`
align-items: center;
appearance: none;
Expand Down Expand Up @@ -47,3 +34,99 @@ export const AbstractButton = styled.button<{ isMobile: boolean }>`
color: inherit;
}
`;

type ButtonType = 'primary' | 'secondary' | 'tertiary' | 'destructive';

interface ButtonTypeStyles {
buttonType: ButtonType;
inverted?: boolean;
theme: Theme;
}

const getPrimaryButtonStyles: (props: ButtonTypeStyles) => FlattenInterpolation<ThemeProps<Theme>> = ({ theme }) => css`
background-color: ${theme.main['primary-1.1']};
border-color: ${theme.main['primary-1.1']};
color: ${theme.greys.white};

&:hover {
background-color: ${theme.main['primary-1.3']};
border-color: ${theme.main['primary-1.3']};
}

&:disabled {
background-color: ${theme.main['primary-1.2']};
border-color: ${theme.main['primary-1.2']};
}
`;

const getSecondaryButtonStyles: (props: ButtonTypeStyles) => FlattenInterpolation<ThemeProps<Theme>> = ({ theme }) => css`
background-color: transparent;
border-color: ${theme.main['primary-1.1']};
color: ${theme.main['primary-1.1']};

&:hover {
border-color: ${theme.main['primary-1.3']};
color: ${theme.main['primary-1.3']};
}

&:disabled {
border-color: ${theme.main['primary-1.2']};
color: ${theme.main['primary-1.2']};
}
`;

const getTertiaryButtonStyles: (props: ButtonTypeStyles) => FlattenInterpolation<ThemeProps<Theme>> = ({ theme }) => css`
background-color: transparent;
border-color: transparent;
color: ${theme.greys['dark-grey']};

&:hover {
background-color: ${theme.greys.grey};
color: ${theme.greys.black};
}

&:disabled {
background-color: transparent;
color: ${theme.greys['mid-grey']};
}
`;

const getDestructiveButtonStyles: (props: ButtonTypeStyles) => FlattenInterpolation<ThemeProps<Theme>> = ({ inverted, theme }) => css`
background-color: ${inverted ? theme.greys.white : theme.notifications['error-2.1']};
border-color: ${inverted ? theme.greys.white : theme.notifications['error-2.1']};
color: ${inverted ? theme.notifications['error-2.1'] : theme.greys.white};

&:hover {
/* TODO change colors when updating thematization */
background-color: ${inverted ? theme.greys.white : '#62071b'};
border-color: ${inverted ? theme.greys.white : '#62071b'};
color: ${inverted ? '#62071b' : theme.greys.white};
}

&:disabled {
&,
&:focus,
&:hover {
/* TODO change colors when updating thematization */
background-color: ${inverted ? theme.greys.white : '#ea8da3'};
border-color: ${inverted ? theme.greys.white : '#ea8da3'};
color: ${inverted ? '#ea8da3' : theme.greys.white};
}
}
`;

export const getButtonTypeStyles: (props: ButtonTypeStyles) => FlattenInterpolation<ThemeProps<Theme>> = (props) => css`
${focus(props, true)};
${() => {
switch (props.buttonType) {
case 'primary':
return getPrimaryButtonStyles(props);
case 'secondary':
return getSecondaryButtonStyles(props);
case 'tertiary':
return getTertiaryButtonStyles(props);
case 'destructive':
return getDestructiveButtonStyles(props);
}
}}
`;
25 changes: 7 additions & 18 deletions packages/react/src/components/buttons/add-button.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 15 additions & 12 deletions packages/react/src/components/buttons/add-button.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import React, { ButtonHTMLAttributes, DetailedHTMLProps, ReactElement } from 'react';
import React, { ReactElement } from 'react';
import styled from 'styled-components';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
import { Icon } from '../icon/icon';
import { AbstractButtonProps } from './abstract-button';
import { Button } from './button';

type ButtonType = 'primary' | 'secondary' | 'tertiary';

type Type = DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>['type'];

const PlusIcon = styled(Icon)`
margin-right: var(--spacing-1x);
`;

interface ButtonProps extends Omit<AbstractButtonProps, 'children'> {
type ButtonType = 'primary' | 'secondary' | 'tertiary' | 'destructive';

type Type = 'submit' | 'button' | 'reset';

interface ButtonProps {
/**
* Visual style
* @default primary
* */
buttonType: ButtonType;
/**
* Sets button type
* @default submit
*/
buttonType: ButtonType;
className?: string;
disabled?: boolean;
inverted?: boolean;
label?: string;
type?: Type;

onClick?(): void;
}

export function AddButton({
className,
type = 'submit',
buttonType,
disabled,
inverted,
label,
onClick,
}: ButtonProps): ReactElement {
Expand All @@ -43,6 +45,7 @@ export function AddButton({
buttonType={buttonType}
onClick={onClick}
disabled={disabled}
inverted={inverted}
label={label}
>
<PlusIcon name="plusSign" size={isMobile ? '24' : '16'} />
Expand Down
16 changes: 16 additions & 0 deletions packages/react/src/components/buttons/button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ describe('Button', () => {
expect(tree).toMatchSnapshot();
});

test('has destructive styles', () => {
const tree = renderWithProviders(
<Button onClick={doNothing} buttonType="destructive" label="Destructive Button" />,
);

expect(tree).toMatchSnapshot();
});

test('has destructive styles (inverted)', () => {
const tree = renderWithProviders(
<Button onClick={doNothing} buttonType="destructive" label="Destructive Button" inverted />,
);

expect(tree).toMatchSnapshot();
});

test('has mobile styles', () => {
const tree = renderWithProviders(
<Button onClick={doNothing} buttonType="primary" label="Primary Button" />,
Expand Down
Loading