| 1 |
import { Link, LocationProvider } from '@reach/router'; |
| 2 |
import router from '@elementor/router'; |
| 3 |
import Icon from 'elementor-app/ui/atoms/icon'; |
| 4 |
|
| 5 |
export default class Button extends React.Component { |
| 6 |
static propTypes = { |
| 7 |
text: PropTypes.string.isRequired, |
| 8 |
hideText: PropTypes.bool, |
| 9 |
icon: PropTypes.string, |
| 10 |
tooltip: PropTypes.string, |
| 11 |
id: PropTypes.string, |
| 12 |
className: PropTypes.string, |
| 13 |
url: PropTypes.string, |
| 14 |
onClick: PropTypes.func, |
| 15 |
variant: PropTypes.oneOf( [ 'contained', 'underlined', 'outlined', '' ] ), |
| 16 |
color: PropTypes.oneOf( [ 'primary', 'secondary', 'cta', 'link', 'disabled' ] ), |
| 17 |
size: PropTypes.oneOf( [ 'sm', 'md', 'lg' ] ), |
| 18 |
target: PropTypes.string, |
| 19 |
rel: PropTypes.string, |
| 20 |
elRef: PropTypes.object, |
| 21 |
}; |
| 22 |
|
| 23 |
static defaultProps = { |
| 24 |
id: '', |
| 25 |
className: '', |
| 26 |
variant: '', |
| 27 |
target: '_parent', |
| 28 |
}; |
| 29 |
|
| 30 |
getCssId() { |
| 31 |
return this.props.id; |
| 32 |
} |
| 33 |
|
| 34 |
getClassName() { |
| 35 |
const baseClassName = 'eps-button', |
| 36 |
classes = [ baseClassName, this.props.className ]; |
| 37 |
|
| 38 |
return classes |
| 39 |
.concat( this.getStylePropsClasses( baseClassName ) ) |
| 40 |
.filter( ( classItem ) => '' !== classItem ) |
| 41 |
.join( ' ' ); |
| 42 |
} |
| 43 |
|
| 44 |
getStylePropsClasses( baseClassName ) { |
| 45 |
const styleProps = [ 'color', 'size', 'variant' ], |
| 46 |
stylePropClasses = []; |
| 47 |
|
| 48 |
styleProps.forEach( ( styleProp ) => { |
| 49 |
const stylePropValue = this.props[ styleProp ]; |
| 50 |
|
| 51 |
if ( stylePropValue ) { |
| 52 |
stylePropClasses.push( baseClassName + '--' + stylePropValue ); |
| 53 |
} |
| 54 |
} ); |
| 55 |
|
| 56 |
return stylePropClasses; |
| 57 |
} |
| 58 |
|
| 59 |
getIcon() { |
| 60 |
if ( this.props.icon ) { |
| 61 |
const tooltip = this.props.tooltip || this.props.text; |
| 62 |
const icon = <Icon className={ this.props.icon } aria-hidden="true" title={ tooltip } />; |
| 63 |
let screenReaderText = ''; |
| 64 |
|
| 65 |
if ( this.props.hideText ) { |
| 66 |
screenReaderText = <span className="sr-only" >{ tooltip }</span>; |
| 67 |
} |
| 68 |
|
| 69 |
return ( |
| 70 |
<> |
| 71 |
{ icon } |
| 72 |
{ screenReaderText } |
| 73 |
</> |
| 74 |
); |
| 75 |
} |
| 76 |
return ''; |
| 77 |
} |
| 78 |
|
| 79 |
getText() { |
| 80 |
return this.props.hideText ? '' : <span>{ this.props.text }</span>; |
| 81 |
} |
| 82 |
|
| 83 |
render() { |
| 84 |
const attributes = {}, |
| 85 |
id = this.getCssId(), |
| 86 |
className = this.getClassName(); |
| 87 |
|
| 88 |
// Add attributes only if they are not empty. |
| 89 |
if ( id ) { |
| 90 |
attributes.id = id; |
| 91 |
} |
| 92 |
|
| 93 |
if ( className ) { |
| 94 |
attributes.className = className; |
| 95 |
} |
| 96 |
|
| 97 |
if ( this.props.onClick ) { |
| 98 |
attributes.onClick = this.props.onClick; |
| 99 |
} |
| 100 |
|
| 101 |
if ( this.props.rel ) { |
| 102 |
attributes.rel = this.props.rel; |
| 103 |
} |
| 104 |
|
| 105 |
if ( this.props.elRef ) { |
| 106 |
attributes.ref = this.props.elRef; |
| 107 |
} |
| 108 |
|
| 109 |
const buttonContent = ( |
| 110 |
<> |
| 111 |
{ this.getIcon() } |
| 112 |
{ this.getText() } |
| 113 |
</> |
| 114 |
); |
| 115 |
|
| 116 |
if ( this.props.url ) { |
| 117 |
if ( 0 === this.props.url.indexOf( 'http' ) ) { |
| 118 |
return ( |
| 119 |
<a href={ this.props.url } target={ this.props.target } { ...attributes }> |
| 120 |
{ buttonContent } |
| 121 |
</a> |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
// @see https://reach.tech/router/example/active-links. |
| 126 |
attributes.getProps = ( props ) => { |
| 127 |
if ( props.isCurrent ) { |
| 128 |
attributes.className += ' active'; |
| 129 |
} |
| 130 |
|
| 131 |
return { |
| 132 |
className: attributes.className, |
| 133 |
}; |
| 134 |
}; |
| 135 |
|
| 136 |
return ( |
| 137 |
<LocationProvider history={ router.appHistory }> |
| 138 |
<Link to={ this.props.url } { ...attributes } > |
| 139 |
{ buttonContent } |
| 140 |
</Link> |
| 141 |
</LocationProvider> |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
return ( |
| 146 |
<div { ...attributes }> |
| 147 |
{ buttonContent } |
| 148 |
</div> |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
|