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: setFocus methods should wait for component to be ready. #6156

Closed
wants to merge 30 commits into from

Conversation

driskull
Copy link
Member

@driskull driskull commented Dec 21, 2022

Related Issue: #3580

Summary

fix: setFocus methods should wait for component to be ready..

See: https://github.com/ionic-team/stencil/blob/main/src/runtime/readme.md#lifecycle-process

async setFocus(): Promise<void> {
  const { el } = this;
  await componentReady(el);
  el.focus();
}

@github-actions github-actions bot added the bug Bug reports for broken functionality. Issues should include a reproduction of the bug. label Dec 21, 2022
@driskull driskull changed the title fix: setFocus should wait for componentOnReady and requestAnimationframe methods fix: setFocus should wait for componentOnReady method Dec 22, 2022
@driskull driskull changed the title fix: setFocus should wait for componentOnReady method fix: setFocus methods should wait for componentOnReady Dec 22, 2022
@driskull
Copy link
Member Author

@benelan it seems like only await el.componentOnReady(); is needed and not requestAnimationframe. Can you confirm?

This leads me to think we should maybe be documenting componentOnReady or at least come up with our own method that wraps it so we can document how a user can wait for a component to be ready.

This following scenario requires a user use componentOnReady().

const modal = document.querySelector("calcite-modal");
const input = document.createElement("calcite-input");
content.appendChild(input);
await input.componentOnReady(); // Needed so that the input has been set up with all its internal elements
await modal.updateFocusTrapElements();
input.setFocus();

@eriklharper @benelan What are your thoughts on whether we should document the stencil method componentOnReady or document our own method that wraps it incase they change the method.

@benelan
Copy link
Member

benelan commented Dec 22, 2022

It depends on the component and the time it takes to load. Try it with inline-editable and I think it will only work with requestAnimationFrame. I had Kitty document componentOnReady based on the info we recieved from support:
https://developers.arcgis.com/calcite-design-system/web-concepts/#component-on-ready

But I'll clean it up this/next week with the correct info.

@driskull
Copy link
Member Author

Try it with inline-editable and I think it will only work with requestAnimationFrame

Thats opposite of what I was seeing with that component LOL. When I had requestAnimationFrame inside setFocus it would break the disabled commonTest.

@benelan
Copy link
Member

benelan commented Dec 22, 2022

lol weird. Did you test it in the demos? When I tested in the demos with inline editable I'm pretty sure it needed requestAnimationFrame. Might need some tweaks to the disabled commonTest, idk.

Try using the demo on this branch: benelan/setFocus-refactor
And this codepen: https://codepen.io/benesri/pen/MWBgJBR

@driskull driskull changed the title fix: setFocus methods should wait for componentOnReady fix: setFocus methods should wait for component to be ready. Dec 22, 2022
@eriklharper
Copy link
Contributor

@eriklharper @benelan What are your thoughts on whether we should document the stencil method componentOnReady or document our own method that wraps it incase they change the method.

Wholeheartedly yes I think it would be beneficial to document this, not just for our users but for contributors as well. I've been bit by this so many times, I created my own snippet in VS Code for inserting this code into an HTML script tag for demo work:

"Component On Ready": {
    "prefix": "onready",
    "body": [
      "(async () => {",
      "	await customElements.whenDefined(\"$1\");",
      "	const element = document.querySelector(\"$1\");",
      "	await element.componentOnReady();",
      "	$0",
      "})();"
    ],
    "description": "HTML script snippet for waiting until a component is ready before interacting with it."
  }

@benelan
Copy link
Member

benelan commented Dec 23, 2022

Yeah I'll clean up the existing doc right now

@benelan
Copy link
Member

benelan commented Dec 27, 2022

@driskull
Copy link
Member Author

@benelan every component seems to be good except for calcite-tile-select. Could you take a look?

@benelan
Copy link
Member

benelan commented Dec 28, 2022

code changes LGTM, I'll do some demo testing in the next couple days just to be sure

@benelan
Copy link
Member

benelan commented Dec 30, 2022

Did some testing and looks good!

@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2023

This PR has been automatically marked as stale because it has not had recent activity. Please close your PR if it is no longer relevant. Thank you for your contributions.

@github-actions github-actions bot added the Stale Issues or pull requests that have not had recent activity. label Jan 7, 2023
@jcfranco
Copy link
Member

I think the componentOnReady approach won't work across the board as this API is only available in the lazy-loaded output target (www).

@benelan We should be able to proceed on this by reverting the switch to componentOnReady, resolving merge conflicts and updating the test helper, right?

WRT documentation, I think it's a good idea to add a snippet, but let's try to create an issue on Stencil's doc site. The burden of doc'ing componentOnReady shouldn't land on us.

@github-actions github-actions bot removed the Stale Issues or pull requests that have not had recent activity. label Jan 19, 2023
@github-actions
Copy link
Contributor

This PR has been automatically marked as stale because it has not had recent activity. Please close your PR if it is no longer relevant. Thank you for your contributions.

@github-actions github-actions bot added the Stale Issues or pull requests that have not had recent activity. label Jan 27, 2023
@driskull driskull closed this Apr 7, 2023
@driskull driskull deleted the dris0000/loadable-use-componentOnReady branch April 7, 2023 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug reports for broken functionality. Issues should include a reproduction of the bug. Stale Issues or pull requests that have not had recent activity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants