vega-frontend-monorepo/libs/ui-toolkit/src/components/button/button.tsx

219 lines
6.0 KiB
TypeScript
Raw Normal View History

import type { AnchorHTMLAttributes, ButtonHTMLAttributes } from 'react';
import { forwardRef } from 'react';
2022-03-01 09:26:26 +00:00
import classNames from 'classnames';
import type { IconName } from '../icon';
import { Icon } from '../icon';
import {
includesLeftPadding,
includesRightPadding,
includesBorderWidth,
} from '../../utils/class-names';
interface CommonProps {
2022-03-01 09:26:26 +00:00
children?: React.ReactNode;
variant?: 'primary' | 'secondary' | 'accent' | 'inline' | 'inline-link';
2022-03-01 09:26:26 +00:00
className?: string;
2022-03-02 09:56:05 +00:00
prependIconName?: IconName;
appendIconName?: IconName;
2022-03-01 09:26:26 +00:00
}
export interface ButtonProps
extends ButtonHTMLAttributes<HTMLButtonElement>,
CommonProps {}
export interface AnchorButtonProps
extends AnchorHTMLAttributes<HTMLAnchorElement>,
CommonProps {}
2022-03-01 09:26:26 +00:00
const getClasses = (
variant: CommonProps['variant'],
paddingLeftProvided: boolean,
paddingRightProvided: boolean,
borderWidthProvided: boolean
) => {
// Add classes into variables if there are multiple classes shared in multiple button styles
const sharedClasses =
'inline-flex items-center justify-center box-border h-28 transition-all disabled:no-underline';
const underlineOnHover = 'no-underline hover:underline';
const commonHoverAndActiveBorder =
'hover:border-black dark:hover:border-white active:border-black dark:active:border-white';
const commonDisabled =
'disabled:bg-black-10 dark:disabled:bg-white-10 disabled:text-black-60 dark:disabled:text-white-60 disabled:border-black-25 dark:disabled:border-white-25';
const inlineTextColour =
'text-black-95 dark:text-white-95 hover:text-black hover:dark:text-white active:text-black dark:active:text-vega-yellow';
const standardButtonPaddingLeft = `${
paddingLeftProvided ? paddingLeftProvided : 'pl-28'
}`;
const standardButtonPaddingRight = `${
paddingRightProvided ? paddingRightProvided : 'pr-28'
}`;
const inlineButtonPaddingLeft = `${
paddingLeftProvided ? paddingLeftProvided : 'pl-4'
}`;
const inlineButtonPaddingRight = `${
paddingRightProvided ? paddingRightProvided : 'pr-4'
}`;
const standardButtonBorderWidth = `${
borderWidthProvided ? borderWidthProvided : 'border'
}`;
2022-03-03 11:11:36 +00:00
const primaryClasses = [
sharedClasses,
commonHoverAndActiveBorder,
underlineOnHover,
commonDisabled,
standardButtonPaddingLeft,
standardButtonPaddingRight,
standardButtonBorderWidth,
'bg-black dark:bg-white hover:bg-black-80 dark:hover:bg-white-80 active:bg-white dark:active:bg-black',
'text-ui text-white dark:text-black active:text-black dark:active:text-white',
];
2022-03-01 09:26:26 +00:00
const secondaryClasses = [
sharedClasses,
commonHoverAndActiveBorder,
underlineOnHover,
commonDisabled,
standardButtonPaddingLeft,
standardButtonPaddingRight,
standardButtonBorderWidth,
'bg-white dark:bg-black hover:bg-black-25 dark:hover:bg-white-25 active:bg-black dark:active:bg-white',
'text-ui text-black dark:text-white active:text-white dark:active:text-black',
'border-black-60 dark:border-white-60 hover:border-black',
];
2022-03-03 14:48:40 +00:00
const accentClasses = [
sharedClasses,
commonHoverAndActiveBorder,
underlineOnHover,
commonDisabled,
standardButtonPaddingLeft,
standardButtonPaddingRight,
standardButtonBorderWidth,
'bg-vega-yellow dark:bg-vega-yellow hover:bg-vega-yellow-dark dark:hover:bg-vega-yellow/30 active:bg-white dark:active:bg-black',
'text-ui uppercase text-black dark:text-black hover:text-white dark:hover:text-white active:text-black dark:active:text-white',
'border-transparent dark:border-transparent',
];
2022-03-01 09:26:26 +00:00
const inlineClasses = [
sharedClasses,
inlineButtonPaddingLeft,
inlineButtonPaddingRight,
inlineTextColour,
'border-none',
'text-ui',
];
2022-03-01 09:26:26 +00:00
const inlineLinkClasses = [
sharedClasses,
inlineButtonPaddingLeft,
inlineButtonPaddingRight,
inlineTextColour,
'underline hover:underline',
'border-none',
];
2022-03-03 14:48:40 +00:00
switch (variant) {
case 'primary':
return primaryClasses;
case 'secondary':
return secondaryClasses;
case 'accent':
return accentClasses;
case 'inline':
return inlineClasses;
case 'inline-link':
return inlineLinkClasses;
default:
return '';
}
};
const classes = (
className: CommonProps['className'],
variant: CommonProps['variant']
) => {
const paddingLeftProvided = includesLeftPadding(className);
const paddingRightProvided = includesRightPadding(className);
const borderWidthProvided = includesBorderWidth(className);
return classNames(
getClasses(
variant,
paddingLeftProvided,
paddingRightProvided,
borderWidthProvided
),
2022-03-01 09:26:26 +00:00
className
);
};
const getContent = (
children: React.ReactNode,
prependIconName?: IconName,
appendIconName?: IconName
) => {
2022-03-02 09:56:05 +00:00
const iconName = prependIconName || appendIconName;
if (iconName === undefined) {
return children;
2022-03-02 09:56:05 +00:00
}
const iconClassName = classNames(['fill-current'], {
'mr-8': prependIconName,
'ml-8': appendIconName,
});
const icon = <Icon name={iconName} className={iconClassName} size={16} />;
2022-03-01 09:26:26 +00:00
return (
<>
2022-03-02 09:56:05 +00:00
{prependIconName && icon}
2022-03-01 09:26:26 +00:00
{children}
2022-03-02 09:56:05 +00:00
{appendIconName && icon}
</>
2022-03-01 09:26:26 +00:00
);
};
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
(
{
variant = 'primary',
Feat/63 Deal ticket (#82) * scaffold dealticket package, remove trading views from react-helpers * add deal ticket component, add intent utils, expand dialog and form group styles * add splash component, show market not found message if market doesnt exist * tidy up error handling * add handleError method for vega tx hook * add better testname for provider test, flesh out tests a bit more for deal ticket * Add unit tests for useVegaTransaction and useOrderSubmit hooks * add wrapper component for order dialog styles * add vega styled loader to ui toolkit and use in order dialog * add title prop to order dialog * split limit and market tickets into own files * add button radio component * revert dialog styles * move splash component to ui-toolkit, add story * convert intent to enum * Make button always type=button unless type prop is passed * inline filter logic for tif selector * add date-fns, add datetime to helpers * add order types to wallet package, make price undefined if order type is market * use enums in deal ticket logic * tidy up order state by moving submit and transaction hooks out of deal ticket * add comment for dialog styles * remove decimal from price input * add types package, delete old generated types from trading project * rename types package to graphql * update generate command to point to correct locations * fix use order submit test * use intent shadow helper * remove date-fns and format manually, update submit button error to use input-error * remove stray console.log
2022-03-17 19:35:46 +00:00
type = 'button',
children,
className,
prependIconName,
appendIconName,
...props
},
ref
) => {
return (
Feat/63 Deal ticket (#82) * scaffold dealticket package, remove trading views from react-helpers * add deal ticket component, add intent utils, expand dialog and form group styles * add splash component, show market not found message if market doesnt exist * tidy up error handling * add handleError method for vega tx hook * add better testname for provider test, flesh out tests a bit more for deal ticket * Add unit tests for useVegaTransaction and useOrderSubmit hooks * add wrapper component for order dialog styles * add vega styled loader to ui toolkit and use in order dialog * add title prop to order dialog * split limit and market tickets into own files * add button radio component * revert dialog styles * move splash component to ui-toolkit, add story * convert intent to enum * Make button always type=button unless type prop is passed * inline filter logic for tif selector * add date-fns, add datetime to helpers * add order types to wallet package, make price undefined if order type is market * use enums in deal ticket logic * tidy up order state by moving submit and transaction hooks out of deal ticket * add comment for dialog styles * remove decimal from price input * add types package, delete old generated types from trading project * rename types package to graphql * update generate command to point to correct locations * fix use order submit test * use intent shadow helper * remove date-fns and format manually, update submit button error to use input-error * remove stray console.log
2022-03-17 19:35:46 +00:00
<button
ref={ref}
className={classes(className, variant)}
Feat/63 Deal ticket (#82) * scaffold dealticket package, remove trading views from react-helpers * add deal ticket component, add intent utils, expand dialog and form group styles * add splash component, show market not found message if market doesnt exist * tidy up error handling * add handleError method for vega tx hook * add better testname for provider test, flesh out tests a bit more for deal ticket * Add unit tests for useVegaTransaction and useOrderSubmit hooks * add wrapper component for order dialog styles * add vega styled loader to ui toolkit and use in order dialog * add title prop to order dialog * split limit and market tickets into own files * add button radio component * revert dialog styles * move splash component to ui-toolkit, add story * convert intent to enum * Make button always type=button unless type prop is passed * inline filter logic for tif selector * add date-fns, add datetime to helpers * add order types to wallet package, make price undefined if order type is market * use enums in deal ticket logic * tidy up order state by moving submit and transaction hooks out of deal ticket * add comment for dialog styles * remove decimal from price input * add types package, delete old generated types from trading project * rename types package to graphql * update generate command to point to correct locations * fix use order submit test * use intent shadow helper * remove date-fns and format manually, update submit button error to use input-error * remove stray console.log
2022-03-17 19:35:46 +00:00
type={type}
{...props}
>
{getContent(children, prependIconName, appendIconName)}
</button>
);
}
);
export const AnchorButton = forwardRef<HTMLAnchorElement, AnchorButtonProps>(
(
{
variant = 'primary',
children,
className,
prependIconName,
appendIconName,
...prosp
},
ref
) => {
return (
<a ref={ref} className={classes(className, variant)} {...prosp}>
{getContent(children, prependIconName, appendIconName)}
</a>
);
}
);