diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-13 14:56:02 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-13 14:56:02 +0100 |
| commit | c7b02f02ad0a7e2888f2d7d3599719e59bbd1ee2 (patch) | |
| tree | 9f782daf2e2ff78559958f15e0b9ffe5ece78334 /static/widgets/search/index.js | |
| parent | 7ee9d320e6ba9a84542d838892c43cf98b268552 (diff) | |
frontend: design prototype
Diffstat (limited to 'static/widgets/search/index.js')
| -rw-r--r-- | static/widgets/search/index.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/static/widgets/search/index.js b/static/widgets/search/index.js new file mode 100644 index 0000000..324141b --- /dev/null +++ b/static/widgets/search/index.js @@ -0,0 +1,67 @@ +import * as sfw from 'sfw'; +const { Div, Input, Button } = sfw.element.native; + +import icons from '../../icons/index.js'; + +const css = await sfw.css(import.meta.url, './index.css'); + +export default class Search extends sfw.element.Container { + #container + #search + + constructor() { + super({ css }); + + this.onsubmit = () => {} + this.onhide = () => {} + + this.onclick = (e) => e.stopPropagation(); + this.hide = () => { + this.#container.classList.remove('visible'); + document.removeEventListener('click', this.hide); + this.onhide(); + }; + + this.body.append( + this.#container = Div.new({ + id: 'container', + children: [ + Div.new({ innerText: 'Search', id: 'title' }), + Div.new({ + id: 'search-box', + children: [ + this.#search = Input.new({ + type: 'search', + onsearch: () => this.submit(), + onkeydown: (event) => { + if (event.key === 'Enter') { + this.submit(); + } + } + }), + Button.new({ + children: [ icons.search ], + onclick: () => this.submit(), + }), + ] + }) + ] + }) + ); + } + + submit() { + this.onsubmit(this.#search.value); + this.#search.blur(); + this.hide(); + } + + toggle() { + this.#container.classList.toggle('visible'); + + if (this.#container.classList.contains('visible')) { + this.#search.focus() + document.addEventListener('click', this.hide) + } + } +} |