-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
Conversation
Correction, the SSR fix is only partial. Working on a complete fix now. |
src/lib/utils.js
Outdated
const itemHtml = document.createElement('div'); | ||
itemHtml.className = 'sv-item-content'; |
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.
The itemHtml
needs to be defined only once, no matter how many Svelecte components are being used. Can you implement it this way?
const itemHtml = document.createElement('div'); | |
itemHtml.className = 'sv-item-content'; | |
if (!itemHtml) { | |
itemHtml = document.createElement('div'); | |
itemHtml.className = 'sv-item-content'; | |
} | |
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.
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.
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.
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)
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.
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.
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.
Yes, it shouldn't because it's only about text highlighting while typing. Thanks for your effort!
Happy to merge, once you resolve the SSR issue completely 👍 |
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 |
Based on code, looks good to me. Tomorrow I will publish updated version. I have some other tweaks in progress. |
Thanks! Unfortunately I just realized that there's an issue with the optimization (not recreating |
This pull request fixes two issues I ran across when using Svelecte in SvelteKit:
document
in global scope insrc/lib/utils.js
breaks SSR because the document global is only available in the browser. The easy fix is to move the code intohighlightSearch
.focus
function exposed as a prop but it was not implemented, so I added it. Fixes Add focus() method #12I'm fine with using my forked version for now, but I thought it might be useful to incorporate these fixes.