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 sveltekit compatibility and add missing focus prop #14

Merged
merged 6 commits into from
Apr 27, 2021

Conversation

kamholz
Copy link
Contributor

@kamholz kamholz commented Apr 27, 2021

This pull request fixes two issues I ran across when using Svelecte in SvelteKit:

  • The reference to document in global scope in src/lib/utils.js breaks SSR because the document global is only available in the browser. The easy fix is to move the code into highlightSearch.
  • The docs mention a focus function exposed as a prop but it was not implemented, so I added it. Fixes Add focus() method #12

I'm fine with using my forked version for now, but I thought it might be useful to incorporate these fixes.

@kamholz
Copy link
Contributor Author

kamholz commented Apr 27, 2021

Correction, the SSR fix is only partial. Working on a complete fix now.

src/lib/utils.js Outdated
Comment on lines 54 to 55
const itemHtml = document.createElement('div');
itemHtml.className = 'sv-item-content';
Copy link
Owner

Choose a reason for hiding this comment

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

The itemHtml needs to be defined only once, no matter how many Svelecte components are being used. Can you implement it this way?

Suggested change
const itemHtml = document.createElement('div');
itemHtml.className = 'sv-item-content';
if (!itemHtml) {
itemHtml = document.createElement('div');
itemHtml.className = 'sv-item-content';
}

Copy link
Contributor Author

@kamholz kamholz Apr 27, 2021

Choose a reason for hiding this comment

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

The issue is that you can't reference document.createElement at all, because it's being generated server-side. Why does itemHtml need to be defined only once? As far as I can tell it's only referenced in highlightSearch. Is it a performance issue? I can probably find a way to call document.createElement fewer times (in the browser, where it's possible) if so.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, I did it this way because of possible performance issues with long list, but basically if that should be a concern, virtualList should be enabled. (this was implemented before virtual list support has been added)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, well in that case, the latest commit should help. It's about the best I could do with the limitations of SSR. The idea is that in most cases (hopefully all but I'm not sure), this condition will apply in SSR:

  if ($inputValue == '' || item.isSelected) return itemHtmlText;

The code that follows is not SSR-safe, but hopefully it will never get there.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, it shouldn't because it's only about text highlighting while typing. Thanks for your effort!

@mskocik
Copy link
Owner

mskocik commented Apr 27, 2021

Happy to merge, once you resolve the SSR issue completely 👍

@kamholz
Copy link
Contributor Author

kamholz commented Apr 27, 2021

The latest commits make it work for me. It's not exhaustively tested but works for the component I'm trying to use right now (multiple select, not async). The dropdownStateSubscription fix is needed because SSR can call onDestroy before onMount.

@mskocik mskocik merged commit 80031d7 into mskocik:master Apr 27, 2021
@mskocik
Copy link
Owner

mskocik commented Apr 27, 2021

Based on code, looks good to me. Tomorrow I will publish updated version. I have some other tweaks in progress.
And again thank you.

@kamholz
Copy link
Contributor Author

kamholz commented Apr 27, 2021

Thanks! Unfortunately I just realized that there's an issue with the optimization (not recreating itemHtml): if itemHtml already exists, its contents were not being updated. The latest commit to my branch fixes it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add focus() method
2 participants