File size: 3,045 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import clsx from 'clsx';
import { forwardRef } from 'react';
import type {
	Ref,
	ForwardRefRenderFunction,
	AnchorHTMLAttributes,
	ButtonHTMLAttributes,
} from 'react';
import type { NonUndefined } from 'utility-types';

import './style.scss';
export interface OwnProps {
	className?: string;
	compact?: boolean;
	primary?: boolean;
	scary?: boolean;
	busy?: boolean;
	borderless?: boolean;
	plain?: boolean;
	transparent?: boolean;
}

type AnchorElementProps = AnchorHTMLAttributes< HTMLAnchorElement >;

type AnchorProps = OwnProps &
	AnchorElementProps & {
		href: NonUndefined< AnchorElementProps[ 'href' ] >;
	};

export type ButtonProps = OwnProps & Omit< ButtonHTMLAttributes< HTMLButtonElement >, 'href' >;

const isAnchor = ( props: AnchorProps | ButtonProps ): props is AnchorProps =>
	!! ( props as AnchorProps ).href;

const cleanAnchorProps = ( {
	type,
	borderless,
	busy,
	className,
	compact,
	primary,
	scary,
	plain,
	transparent,
	...anchorProps
}: ButtonProps | AnchorProps ): AnchorProps => anchorProps as AnchorProps;

const cleanButtonProps = ( {
	type = 'button',
	borderless,
	busy,
	className,
	compact,
	primary,
	scary,
	plain,
	// eslint-disable-next-line @typescript-eslint/ban-ts-comment
	// @ts-ignore Clean incorrect usage of the component
	rel,
	// eslint-disable-next-line @typescript-eslint/ban-ts-comment
	// @ts-ignore Clean incorrect usage of the component
	href,
	// eslint-disable-next-line @typescript-eslint/ban-ts-comment
	// @ts-ignore Clean incorrect usage of the component
	target,
	transparent,
	...buttonProps
}: ButtonProps | AnchorProps ): ButtonProps => ( { ...buttonProps, type } ) as ButtonProps;

const UnforwardedButton: ForwardRefRenderFunction<
	HTMLButtonElement | HTMLAnchorElement,
	ButtonProps | AnchorProps
> = ( props, ref ) => {
	const classes = props.plain
		? clsx( 'button-plain', props.className )
		: clsx( 'button', props.className, {
				'is-compact': props.compact,
				'is-primary': props.primary,
				'is-scary': props.scary,
				'is-busy': props.busy,
				'is-borderless': props.borderless,
				'is-transparent': props.transparent,
		  } );

	if ( isAnchor( props ) ) {
		const anchorProps = cleanAnchorProps( props );
		// block referrers when external link
		const rel: string | undefined = anchorProps.target
			? ( anchorProps.rel || '' ).replace( /noopener|noreferrer/g, '' ) + ' noopener noreferrer'
			: anchorProps.rel;

		return (
			<a
				{ ...anchorProps }
				rel={ rel }
				className={ classes }
				ref={ ref as Ref< HTMLAnchorElement > }
			/>
		);
	}

	const buttonProps = cleanButtonProps( props );
	return (
		<button { ...buttonProps } className={ classes } ref={ ref as Ref< HTMLButtonElement > } />
	);
};
/**
 * @deprecated This button has been deprecated in favor of the `Button` component from `@wordpress/components`.
 * Please use the `Button` component from `@wordpress/components` instead. This button has aggressive and generic CSS that breaks many other buttons when imported.
 */
export const Button = forwardRef( UnforwardedButton );