Skip to content
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

Fix Link Control 'Open in new tab' option not saving properly on committing link in buttons block #42073

Merged
merged 2 commits into from
Jul 13, 2022
Merged

Conversation

hz-tyfoon
Copy link
Contributor

@hz-tyfoon hz-tyfoon commented Jun 30, 2022

This PR fixes #40243

[note: The mentioned issue was not only in buttons block but in the 'link-control' component]

@getdave
Copy link
Contributor

getdave commented Jun 30, 2022

@hz-tyfoon Thank you for the PR. It's much appreciated 👏

I tested this with the standard link in the Post Editor and I was unable to reproduce. I could however, reproduce with the buttons block. This is using latest Gutenberg trunk from Github.

Screen.Capture.on.2022-06-30.at.15-45-31.mov

I believe this is likely due to the fact that the <LinkControl> is a controlled component (like a form input) and therefore each consumer must provide their own handling implementation.

At first glance, your fix seems good, but I'm going to dig a little deeper and see if there's fundamental problem with the handler in richtext vs the button block.

Also please could you resolve the unit tests? 🙇

@getdave
Copy link
Contributor

getdave commented Jun 30, 2022

The issue is that LinkControl will call onChange for any change to the value. The "value" also includes settings such as opensInNewTab.

So when the user clicks "Open in new tab" onChange is called and the following code sets the Button block's linkTarget attribute to true.

url: newURL = '',
opensInNewTab: newOpensInNewTab,

However, when the user clicks submit to "commit" the link then due to the problem that @hz-tyfoon identified the original value of opensInNewTab is not provided to the callback and thus in the Button block onChange handler newOpensInNewTab has a value of undefined. This then overwrites the original setting of the linkTarget attribute from true to undefined which is why the UI behaves as it does.

On balance I think we should update LinkControl to provide the stored value on commit as per this PR. We should update the tests to assert on the full value being provided. Please could you add opensInNewTab to the relevant tests to further improve them?

@hz-tyfoon
Copy link
Contributor Author

Hey @getdave. 🙂
Hope you're doing well.
thanks a lot for your comment. 👍

@hz-tyfoon Thank you for the PR. It's much appreciated 👏

I tested this with the standard link in the Post Editor and I was unable to reproduce. I could however, reproduce with the buttons block. This is using latest Gutenberg trunk from Github.

Screen.Capture.on.2022-06-30.at.15-45-31.mov
I believe this is likely due to the fact that the <LinkControl> is a controlled component (like a form input) and therefore each consumer must provide their own handling implementation.

At first glance, your fix seems good, but I'm going to dig a little deeper and see if there's fundamental problem with the handler in richtext vs the button block.

Also please could you resolve the unit tests? 🙇

⬆️ I was talking about this issue #42080 with the standard link in the Post Editor. I'm not sure why the unit tests always so stubborn. In my first contribution few days earlier it failed 5-6 times and finally it passed after re-running the test again.

@hz-tyfoon
Copy link
Contributor Author

The issue is that LinkControl will call onChange for any change to the value. The "value" also includes settings such as opensInNewTab.

So when the user clicks "Open in new tab" onChange is called and the following code sets the Button block's linkTarget attribute to true.

url: newURL = '',
opensInNewTab: newOpensInNewTab,

However, when the user clicks submit to "commit" the link then due to the problem that @hz-tyfoon identified the original value of opensInNewTab is not provided to the callback and thus in the Button block onChange handler newOpensInNewTab has a value of undefined. This then overwrites the original setting of the linkTarget attribute from true to undefined which is why the UI behaves as it does.

On balance I think we should update LinkControl to provide the stored value on commit as per this PR. We should update the tests to assert on the full value being provided. Please could you add opensInNewTab to the relevant tests to further improve them?

The issue is that LinkControl will call onChange for any change to the value. The "value" also includes settings such as opensInNewTab.

So when the user clicks "Open in new tab" onChange is called and the following code sets the Button block's linkTarget attribute to true.

url: newURL = '',
opensInNewTab: newOpensInNewTab,

However, when the user clicks submit to "commit" the link then due to the problem that @hz-tyfoon identified the original value of opensInNewTab is not provided to the callback and thus in the Button block onChange handler newOpensInNewTab has a value of undefined. This then overwrites the original setting of the linkTarget attribute from true to undefined which is why the UI behaves as it does.

On balance I think we should update LinkControl to provide the stored value on commit as per this PR. We should update the tests to assert on the full value being provided. Please could you add opensInNewTab to the relevant tests to further improve them?

Agreed. 👍
Thanks a ton for taking the time to check this. 💚
@getdave

@hz-tyfoon
Copy link
Contributor Author

Looks like this PR also fixing this issue #40243 as well.

@talldan talldan added [Type] Bug An existing feature does not function as intended [Feature] Link Editing Link components (LinkControl, URLInput) and integrations (RichText link formatting) labels Jul 5, 2022
@talldan
Copy link
Contributor

talldan commented Jul 5, 2022

@hz-tyfoon Thanks for working on this. There's a unit test for link editing that's failing as a result of these changes and will need to be updated - https://github.com/WordPress/gutenberg/runs/7173641105?check_suite_focus=true.

There's a comment on the other PR that mentions the same issue, and has some details for solving it - #40244 (comment).

@hz-tyfoon
Copy link
Contributor Author

There's a comment on the other PR that mentions the same issue, and has some details for solving it - #40244 (comment).

Sorry for late reply, I somehow didn't notice your comment earlier. @talldan
Updated the Unit test & looks like the failed JS test have passed now.
Thanks @talldan & @torounit

@getdave getdave changed the title bugfix: ('components'->'link-control') 'open in new tab' option not saving properly Fix Link Control 'Open in new tab' option not saving properly on committing link in buttons block Jul 13, 2022
Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - tests now streamlined and the fix seems good. The controlled component should always pass the entire value when it calls the onChange handler. This PR addresses that so I think it's good.

@talldan talldan merged commit 50d64bd into WordPress:trunk Jul 13, 2022
@github-actions github-actions bot added this to the Gutenberg 13.7 milestone Jul 13, 2022
@hz-tyfoon
Copy link
Contributor Author

Shouldn't (issue: #42032) also be closed, as this PR was originally created to solve that? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Link Editing Link components (LinkControl, URLInput) and integrations (RichText link formatting) [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When editing a link in a button block, linkTarget is reset even though only the url is changed.
3 participants