-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved "Open in new tab" toggle to separate component, using the new API
- Loading branch information
Showing
2 changed files
with
82 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/format-library/src/link/open-in-new-tab-toggle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { Fill, ToggleControl } from '@wordpress/components'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
class OpenInNewTabToggle extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.setLinkTarget = this.setLinkTarget.bind( this ); | ||
} | ||
|
||
setLinkTarget( opensInNewWindow ) { | ||
const { attributes = {}, setLinkAttributes } = this.props; | ||
const label = this.getLabel( opensInNewWindow ); | ||
|
||
if ( opensInNewWindow ) { | ||
let rel = 'noopener noreferrer'; | ||
|
||
if ( attributes.rel ) { | ||
rel = [ rel, attributes.rel ].join( ' ' ); | ||
} | ||
|
||
setLinkAttributes( { | ||
'aria-label': label, | ||
target: '_blank', | ||
rel, | ||
...attributes, | ||
} ); | ||
} else { | ||
if ( typeof attributes.rel === 'string' ) { | ||
attributes.rel = attributes.rel.split( ' ' ).filter( ( relItem ) => { | ||
return relItem !== 'noopener' && relItem !== 'noreferrer'; | ||
} ).join( ' ' ).trim(); | ||
} else { | ||
delete attributes.rel; | ||
} | ||
|
||
delete attributes.target; | ||
attributes[ 'aria-label' ] = label; | ||
|
||
setLinkAttributes( attributes ); | ||
} | ||
} | ||
|
||
getLabel( opensInNewWindow ) { | ||
const { text } = this.props; | ||
|
||
if ( opensInNewWindow ) { | ||
return sprintf( __( '%s (opens in a new tab)' ), text ); | ||
} | ||
|
||
return text; | ||
} | ||
|
||
render() { | ||
return ( | ||
<ToggleControl | ||
label={ __( 'Open in New Tab' ) } | ||
checked={ this.props.attributes.target === '_blank' } | ||
onChange={ this.setLinkTarget } | ||
/> | ||
); | ||
} | ||
} | ||
|
||
const OpenInNewTabToggleWrapper = () => { | ||
return ( | ||
<Fill name="LinkSettings"> | ||
{ ( props ) => ( | ||
<OpenInNewTabToggle { ...props } /> | ||
) } | ||
</Fill> | ||
); | ||
}; | ||
|
||
export default OpenInNewTabToggleWrapper; |