defenseunicorns / UnicornUI

Material based component/css library for svelte.
https://unicorn-ui.netlify.app/
Apache License 2.0
6 stars 1 forks source link

Reusable Click Outside #122

Open TristanHoladay opened 1 year ago

TristanHoladay commented 1 year ago

Currently using a custom clickaway listener attached to :window, for example in the Text Field component. Explore constructing a generic clickOutside action.

https://svelte.dev/repl/0ace7a508bd843b798ae599940a91783?version=3.16.7

Typing Problem If you implement the click outside as done in the above tutorial you'll get a typing error when applying a custom event to a native HTMLElement. You can fix it by following these docs: https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#im-using-an-attributeevent-on-a-dom-element-and-it-throws-a-type-error

https://www.typescriptlang.org/docs/handbook/declaration-merging.html

This is not ideal though.

TristanHoladay commented 1 year ago

Could do something similar to svelte example and use a native HTML event attribute like so:

Screenshot 2023-02-07 at 1 21 04 PM

note: you don't want to re-dispatch "click" because of infinite loop

Then use like this:

Screenshot 2023-02-07 at 1 23 05 PM

Or we can take in a custom eventFn:

Screenshot 2023-02-07 at 1 19 00 PM

and use as such:

Screenshot 2023-02-07 at 1 25 49 PM