Skip to content

Commit

Permalink
Fix unbind() not cleaning up shadow div.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Mar 8, 2024
1 parent 2912223 commit 2a5cbdc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/floatype.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 26 additions & 12 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ <h1>floatype.js</h1>
<br />Zero dependencies (~1200 bytes minified + gzipped). Type below to try.
</p>
<textarea autofocus></textarea>
<p><button>Toggle bind/unbind</button></p>

<p>For dropdown suggestions on inputs, see <a href="https://github.com/knadh/autocomp.js">autocomp.js</a></p>

Expand All @@ -92,21 +93,34 @@ <h1>floatype.js</h1>

// Example 1.
// Simple string results.
const f = floatype(document.querySelector("textarea"), {
onQuery: async (val) => {
// This callback returns an array of search results.
// Typically, this will be a server side fetch() request.
// Example:
// const resp = await fetch(`/search?q=${query}`);
// const res = await response.json();
// return res;
function bind() {
return floatype(document.querySelector("textarea"), {
onQuery: async (val) => {
// This callback returns an array of search results.
// Typically, this will be a server side fetch() request.
// Example:
// const resp = await fetch(`/search?q=${query}`);
// const res = await response.json();
// return res;

const q = val.trim().toLowerCase();
return WORDS.filter(s => s.startsWith(q)).slice(0, 10);
const q = val.trim().toLowerCase();
return WORDS.filter(s => s.startsWith(q)).slice(0, 10);
}
});
}

window._fl = bind();

document.querySelector("button").onclick = (e) => {
if (window._fl) {
window._fl.unbind();
window._fl = null;
} else {
window._fl = bind();
}
});

// f.unbind() to remove the binding.
document.querySelector("textarea").focus();
}
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions floatype.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ export function floatype(el, options = {}) {
return {
unbind: function() {
["input", "keydown", "blur"].forEach(k => el.removeEventListener(k, handleEvent));
destroy();
shadow.remove();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@knadh/floatype",
"version": "1.2.0",
"version": "1.2.1",
"description": "A tiny, zero-dependency, floating autocomplete / autosuggestion widget for textarea.",
"main": "floatype.js",
"files": [
Expand Down

0 comments on commit 2a5cbdc

Please sign in to comment.