2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2019-06-28 09:27:55 +02:00
|
|
|
import type { Node } from 'react';
|
2019-11-15 22:53:31 +01:00
|
|
|
import React, { forwardRef, useRef } from 'react';
|
2018-03-26 23:32:43 +02:00
|
|
|
import Icon from 'component/common/icon';
|
|
|
|
import classnames from 'classnames';
|
2019-04-04 23:05:23 +02:00
|
|
|
import { NavLink } from 'react-router-dom';
|
2019-03-28 17:53:13 +01:00
|
|
|
import { formatLbryUriForWeb } from 'util/uri';
|
2019-04-01 16:30:19 +02:00
|
|
|
import { OutboundLink } from 'react-ga';
|
2019-10-15 20:53:55 +02:00
|
|
|
import * as PAGES from 'constants/pages';
|
2019-11-15 22:53:31 +01:00
|
|
|
import useCombinedRefs from 'effects/use-combined-refs';
|
2018-03-26 23:32:43 +02:00
|
|
|
|
|
|
|
type Props = {
|
2019-06-12 04:22:21 +02:00
|
|
|
id: ?string,
|
2018-03-26 23:32:43 +02:00
|
|
|
href: ?string,
|
|
|
|
title: ?string,
|
|
|
|
label: ?string,
|
|
|
|
icon: ?string,
|
|
|
|
iconRight: ?string,
|
|
|
|
disabled: ?boolean,
|
2019-06-28 09:27:55 +02:00
|
|
|
children: ?Node,
|
2018-03-26 23:32:43 +02:00
|
|
|
navigate: ?string,
|
|
|
|
className: ?string,
|
|
|
|
description: ?string,
|
|
|
|
type: string,
|
|
|
|
button: ?string, // primary, secondary, alt, link
|
2018-12-19 06:44:53 +01:00
|
|
|
iconSize?: number,
|
2019-06-28 09:27:55 +02:00
|
|
|
iconColor?: string,
|
2018-12-19 06:44:53 +01:00
|
|
|
constrict: ?boolean, // to shorten the button and ellipsis, only use for links
|
2019-03-28 17:53:13 +01:00
|
|
|
activeClass?: string,
|
2019-06-05 08:07:53 +02:00
|
|
|
innerRef: ?any,
|
|
|
|
// Events
|
|
|
|
onClick: ?(any) => any,
|
|
|
|
onMouseEnter: ?(any) => any,
|
|
|
|
onMouseLeave: ?(any) => any,
|
2019-10-15 20:53:55 +02:00
|
|
|
pathname: string,
|
|
|
|
emailVerified: boolean,
|
|
|
|
requiresAuth: ?boolean,
|
|
|
|
myref: any,
|
|
|
|
dispatch: any,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
2019-06-28 09:27:55 +02:00
|
|
|
// use forwardRef to allow consumers to pass refs to the button content if they want to
|
|
|
|
// flow requires forwardRef have default type arguments passed to it
|
|
|
|
const Button = forwardRef<any, {}>((props: Props, ref: any) => {
|
|
|
|
const {
|
|
|
|
type = 'button',
|
|
|
|
onClick,
|
|
|
|
href,
|
|
|
|
title,
|
|
|
|
label,
|
|
|
|
icon,
|
|
|
|
// This should rarely be used. Regular buttons should just use `icon`
|
|
|
|
// `iconRight` is used for the header (home) button with the LBRY icon and external links that are displayed inline
|
|
|
|
iconRight,
|
|
|
|
disabled,
|
|
|
|
children,
|
|
|
|
navigate,
|
|
|
|
className,
|
|
|
|
description,
|
|
|
|
button,
|
|
|
|
iconSize,
|
|
|
|
iconColor,
|
|
|
|
constrict,
|
|
|
|
activeClass,
|
2019-10-15 20:53:55 +02:00
|
|
|
emailVerified,
|
|
|
|
requiresAuth,
|
|
|
|
myref,
|
|
|
|
dispatch, // <button> doesn't know what to do with dispatch
|
|
|
|
pathname,
|
2019-06-28 09:27:55 +02:00
|
|
|
...otherProps
|
|
|
|
} = props;
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2019-06-28 09:27:55 +02:00
|
|
|
const combinedClassName = classnames(
|
|
|
|
'button',
|
|
|
|
button
|
|
|
|
? {
|
|
|
|
'button--primary': button === 'primary',
|
|
|
|
'button--secondary': button === 'secondary',
|
|
|
|
'button--alt': button === 'alt',
|
|
|
|
'button--inverse': button === 'inverse',
|
|
|
|
'button--close': button === 'close',
|
|
|
|
'button--disabled': disabled,
|
|
|
|
'button--link': button === 'link',
|
|
|
|
'button--constrict': constrict,
|
|
|
|
}
|
|
|
|
: 'button--no-style',
|
|
|
|
className
|
|
|
|
);
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2019-11-15 22:53:31 +01:00
|
|
|
const innerRef = useRef(null);
|
|
|
|
const combinedRef = useCombinedRefs(ref, innerRef, myref);
|
|
|
|
|
2019-06-28 09:27:55 +02:00
|
|
|
const content = (
|
|
|
|
<span className="button__content">
|
|
|
|
{icon && <Icon icon={icon} iconColor={iconColor} size={iconSize} />}
|
|
|
|
{label && <span className="button__label">{label}</span>}
|
|
|
|
{children && children}
|
|
|
|
{iconRight && <Icon icon={iconRight} iconColor={iconColor} size={iconSize} />}
|
|
|
|
</span>
|
|
|
|
);
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2019-06-28 09:27:55 +02:00
|
|
|
if (href) {
|
|
|
|
return (
|
|
|
|
<OutboundLink eventLabel="outboundClick" to={href} target="_blank" className={combinedClassName}>
|
|
|
|
{content}
|
|
|
|
</OutboundLink>
|
2018-03-26 23:32:43 +02:00
|
|
|
);
|
2019-06-28 09:27:55 +02:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2019-06-28 09:27:55 +02:00
|
|
|
// Handle lbry:// uris passed in, or already formatted web urls
|
|
|
|
let path = navigate;
|
|
|
|
if (path) {
|
|
|
|
if (path.startsWith('lbry://')) {
|
|
|
|
path = formatLbryUriForWeb(path);
|
|
|
|
} else if (!path.startsWith('/')) {
|
|
|
|
// Force a leading slash so new paths aren't appended on to the current path
|
|
|
|
path = `/${path}`;
|
2019-03-28 17:53:13 +01:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
2019-06-28 09:27:55 +02:00
|
|
|
|
2019-10-15 20:53:55 +02:00
|
|
|
if (requiresAuth && !emailVerified) {
|
|
|
|
return (
|
|
|
|
<NavLink
|
|
|
|
exact
|
2019-10-23 20:59:33 +02:00
|
|
|
onClick={e => {
|
|
|
|
e.stopPropagation();
|
|
|
|
}}
|
2019-10-16 07:01:18 +02:00
|
|
|
to={`/$/${PAGES.AUTH}?redirect=${pathname}`}
|
2019-10-15 20:53:55 +02:00
|
|
|
title={title}
|
|
|
|
disabled={disabled}
|
|
|
|
className={combinedClassName}
|
|
|
|
activeClassName={activeClass}
|
|
|
|
>
|
|
|
|
{content}
|
|
|
|
</NavLink>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:27:55 +02:00
|
|
|
return path ? (
|
|
|
|
<NavLink
|
|
|
|
exact
|
|
|
|
to={path}
|
|
|
|
title={title}
|
|
|
|
disabled={disabled}
|
|
|
|
onClick={e => {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (onClick) {
|
|
|
|
onClick();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
className={combinedClassName}
|
|
|
|
activeClassName={activeClass}
|
|
|
|
>
|
|
|
|
{content}
|
|
|
|
</NavLink>
|
|
|
|
) : (
|
|
|
|
<button
|
2019-11-15 22:53:31 +01:00
|
|
|
ref={combinedRef}
|
2019-06-28 09:27:55 +02:00
|
|
|
title={title}
|
|
|
|
aria-label={description || label || title}
|
|
|
|
className={combinedClassName}
|
2019-08-13 07:35:13 +02:00
|
|
|
onClick={e => {
|
|
|
|
if (onClick) {
|
|
|
|
e.stopPropagation();
|
|
|
|
onClick(e);
|
|
|
|
}
|
|
|
|
}}
|
2019-06-28 09:27:55 +02:00
|
|
|
disabled={disabled}
|
|
|
|
type={type}
|
|
|
|
{...otherProps}
|
|
|
|
>
|
|
|
|
{content}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
});
|
2018-03-26 23:32:43 +02:00
|
|
|
|
|
|
|
export default Button;
|