-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Update/unify link interfaces #6392 #7022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
10e4082
7a99dfe
3aaf90a
14e09dc
efdef98
2d65e82
66d91f7
b604f7e
cd05ad6
13025ab
b1aec98
2935e5f
65541c4
a79a991
3d9ce25
141bf49
0b23bea
b538789
85f9fa3
f109c4c
45193d2
5d45dfb
4bc4620
acb5b5a
0edf482
c14e4b3
345d3e7
ce4d81b
ccecb2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ import classnames from 'classnames'; | |
| import './style.scss'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Component } from '@wordpress/element'; | ||
| import { IconButton, ToggleControl } from '@wordpress/components'; | ||
| import { IconButton, ToggleControl, Popover } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -18,11 +18,11 @@ import UrlInput from './'; | |
|
|
||
| class UrlInputButton extends Component { | ||
| constructor() { | ||
| super(...arguments); | ||
| this.toggle = this.toggle.bind(this); | ||
| this.submitLink = this.submitLink.bind(this); | ||
| this.toggleLinkSettingsVisibility = this.toggleLinkSettingsVisibility.bind(this); | ||
| this.setLinkTarget = this.setLinkTarget.bind(this); | ||
| super( ...arguments ); | ||
| this.toggle = this.toggle.bind( this ); | ||
| this.submitLink = this.submitLink.bind( this ); | ||
| this.toggleLinkSettingsVisibility = this.toggleLinkSettingsVisibility.bind( this ); | ||
| this.setLinkTarget = this.setLinkTarget.bind( this ); | ||
|
|
||
| this.state = { | ||
| expanded: false, | ||
|
|
@@ -31,49 +31,40 @@ class UrlInputButton extends Component { | |
| }; | ||
| } | ||
|
|
||
| // this.state = { | ||
| // | ||
| // linkValue: '', | ||
| // }; | ||
|
|
||
| toggle() { | ||
| this.setState({ expanded: !this.state.expanded }); | ||
| this.setState( { expanded: ! this.state.expanded } ); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| submitLink(event) { | ||
| submitLink( event ) { | ||
| event.preventDefault(); | ||
| this.toggle(); | ||
| } | ||
|
|
||
| toggleLinkSettingsVisibility() { | ||
| this.setState((state) => ({ settingsVisible: !state.settingsVisible })); | ||
| this.setState( ( state ) => ( { settingsVisible: ! state.settingsVisible } ) ); | ||
| } | ||
|
|
||
| setLinkTarget(opensInNewWindow) { | ||
| this.setState({ opensInNewWindow }); | ||
| if (opensInNewWindow) { | ||
|
|
||
| this.props.setAttributes({ | ||
| setLinkTarget( opensInNewWindow ) { | ||
| this.setState( { opensInNewWindow } ); | ||
| if ( opensInNewWindow ) { | ||
| this.props.setAttributes( { | ||
| target: '_blank', | ||
| rel: opensInNewWindow ? 'noreferrer noopener' : null, | ||
| }) | ||
| } ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| render() { | ||
| const { url, onChange } = this.props; | ||
| const { expanded, settingsVisible, opensInNewWindow } = this.state; | ||
| const buttonLabel = url ? __('Edit Link') : __('Insert Link'); | ||
| const buttonLabel = url ? __( 'Edit Link' ) : __( 'Insert Link' ); | ||
|
|
||
| const linkSettings = settingsVisible && ( | ||
| <div className="editor-format-toolbar__link-modal-line editor-format-toolbar__link-settings"> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The coding guidelines for CSS naming is written such that this is not a valid class name to assign to this component. It's reflective of the fact that components are meant to compose one another, and shouldn't be inheriting behaviors, since it can lead to accidental breakage if a change to the format toolbar inadvertently affects this component. Isolating components to silo their behavior / silos helps prevent this. What I'd like to see instead, and what seemed to be hinted by the idea of "unifying" in the original pull request, is that instead of duplicating what already exists elsewhere, we ought to consider creating a new, separate component which contains the shared behavior (of link management), then use that both here and in other existing usage. |
||
| <ToggleControl | ||
| label={__('Open in new window')} | ||
| checked={opensInNewWindow} | ||
| onChange={this.setLinkTarget} | ||
| label={ __( 'Open in new window' ) } | ||
| checked={ opensInNewWindow } | ||
| onChange={ this.setLinkTarget } | ||
| /> | ||
| </div> | ||
| ); | ||
|
|
@@ -82,34 +73,39 @@ class UrlInputButton extends Component { | |
| <div className="editor-url-input__button"> | ||
| <IconButton | ||
| icon="admin-links" | ||
| label={buttonLabel} | ||
| onClick={this.toggle} | ||
| className={classnames('components-toolbar__control', { | ||
| label={ buttonLabel } | ||
| onClick={ this.toggle } | ||
| className={ classnames( 'components-toolbar__control', { | ||
| 'is-active': url, | ||
| })} | ||
| } ) } | ||
| /> | ||
| {expanded && | ||
| <form | ||
| className="editor-url-input__button-modal" | ||
| onSubmit={this.submitLink} | ||
| { expanded && | ||
| <Popover | ||
| position="bottom center" | ||
| focusOnMount={ true } | ||
| > | ||
| <div className="editor-url-input__button-modal-line"> | ||
| <UrlInput value={url || ''} onChange={onChange} data-test="UrlInput" /> | ||
| <IconButton | ||
| icon="editor-break" | ||
| label={__('Submit')} | ||
| type="submit" | ||
| /> | ||
| <IconButton | ||
| className="editor-format-toolbar__link-settings-toggle" | ||
| icon="ellipsis" | ||
| label={__('Link Settings')} | ||
| onClick={this.toggleLinkSettingsVisibility} | ||
| // aria-expanded={settingsVisible} | ||
| /> | ||
| </div> | ||
| {linkSettings} | ||
| </form> | ||
| <form | ||
| className="editor-url-input__button-modal" | ||
| onSubmit={ this.submitLink } | ||
| > | ||
| <div className="editor-url-input__button-modal-line"> | ||
| <UrlInput value={ url || '' } onChange={ onChange } data-test="UrlInput" /> | ||
| <IconButton | ||
| icon="editor-break" | ||
| label={ __( 'Submit' ) } | ||
| type="submit" | ||
| /> | ||
| <IconButton | ||
| className="editor-format-toolbar__link-settings-toggle" | ||
| icon="ellipsis" | ||
| label={ __( 'Link Settings' ) } | ||
| onClick={ this.toggleLinkSettingsVisibility } | ||
| aria-expanded={ settingsVisible } | ||
| /> | ||
| </div> | ||
| { linkSettings } | ||
| </form> | ||
| </Popover> | ||
| } | ||
| </div> | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to track this as part of state?
Or can it be inferred from props in
renderas:The hope being that we could consolidate to one source of truth (attributes), rather than tracking in two places, which is more prone to falling out of sync.
Edit: I see after the fact that this is also how we populate the initial state in the
constructor.