docbrown/app/javascript/crayons/Buttons/Button.jsx
ludwiczakpawel 878c27f9b0
Crayons: buttons followup (#15720)
* init

* whoops

* components update

* components update

* classes

* variables

* focus

* test

* spec

* test

* adjust colors and variables

* buttons colors

* Update app/javascript/crayons/CTAs/CTA.jsx

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

* proptype

* remove duplicated tests now in Cypress, enhance Cypress nav menu tests

* premove unneeded viewport in test

* remove focus check for now

* classnames

* Update app/javascript/crayons/CTAs/__stories__/CTAs.mdx

Co-authored-by: Nick Taylor <nick@iamdeveloper.com>

* Update app/javascript/crayons/CTAs/__stories__/CTAs.mdx

Co-authored-by: Nick Taylor <nick@iamdeveloper.com>

* better contrast

* classname

* add secondary button

* variants + docs

* docs updates

* radius-full

* variants oneof

* the?

* default values + extra stories

* Apply suggestions from code review

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
Co-authored-by: Nick Taylor <nick@iamdeveloper.com>
Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>
2021-12-14 14:43:41 +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,
variant = 'default',
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--${variant}`]: variant && variant !== 'default',
'c-btn--destructive': destructive && variant !== 'secondary',
'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="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,
variant: PropTypes.oneOf(['default', 'primary', 'secondary']),
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.elementType,
};