docbrown/app/javascript/crayons/Buttons/Button.jsx
ludwiczakpawel 245e70d4a8
Crayons: Buttons vs CTAs (#15311)
* init

* buttons

* buttons, ctas, icons

* ctas fix

* links vs ctas

* alias

* fixes

* add tests for Buttons component

* add tooltip to button story controls

* add link and CTA tests

* add prop types

* docs

* designs

* docs

* focus visible moved to global place

* Updated some component snapshot tests.

* Mocked react-inlinesvg module because of https://github.com/gilbarbara/react-inlinesvg/issues/145\#issuecomment-623453339

* tests

* Copy updates

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* missed spots

* Apply suggestions from code review

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* danger accent colors

* node

* colors

* adding code comment

* better prop name: style -> variant

* button new

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
Co-authored-by: Nick Taylor <nick@dev.to>
2021-11-30 20:40:32 +01:00

84 lines
2 KiB
JavaScript

import { h } from 'preact';
import PropTypes from 'prop-types';
import { useState } from 'preact/hooks';
import classNames from 'classnames/bind';
import { defaultChildrenPropTypes } from '../../common-prop-types/default-children-prop-types';
import { Icon } from '@crayons';
export const ButtonNew = (props) => {
const {
children,
primary,
icon,
rounded,
destructive,
type = 'button',
className,
tooltip,
onKeyUp,
...otherProps
} = props;
const [suppressTooltip, setSuppressTooltip] = useState(false);
const handleKeyUp = (event) => {
onKeyUp?.(event);
if (!tooltip) {
return;
}
setSuppressTooltip(event.key === 'Escape');
};
const classes = classNames('c-btn', {
'c-btn--primary': primary,
'c-btn--destructive': destructive,
'c-btn--icon-left': icon && children,
'c-btn--icon-alone': icon && !children,
'crayons-tooltip__activator': tooltip,
'radius-full': rounded,
[className]: className,
});
return (
<button
type={type}
className={classes}
onKeyUp={handleKeyUp}
{...otherProps}
>
{icon && (
<Icon
aria-hidden="true"
focusable="false"
src={icon}
className={classNames('c-btn__icon')}
/>
)}
{children}
{tooltip ? (
<span
data-testid="tooltip"
className={classNames('crayons-tooltip__content', {
'crayons-tooltip__suppressed': suppressTooltip,
})}
>
{tooltip}
</span>
) : null}
</button>
);
};
ButtonNew.displayName = 'ButtonNew';
ButtonNew.propTypes = {
children: defaultChildrenPropTypes,
primary: PropTypes.bool,
rounded: PropTypes.bool,
destructive: PropTypes.bool,
type: PropTypes.oneOf(['button', 'submit']),
className: PropTypes.string,
tooltip: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
onKeyUp: PropTypes.func,
icon: PropTypes.ReactNode,
};