anki/ts/sveltelib/subscribe-trigger.ts
Henrik Giesel 88217c5e7d
Replace (some) Bootstrap dropdowns with Floating UI (#1695)
* Implement a first version of WithFloating and Portal

* Add outside slot for Portal

* Execute computePosition from WithFloating

* Set up a first example of new WithFloating with the Latex menu

* Use autoUpdate in WithFloating

* Create sveltelib/position

* Add event-store

* Use event-store in close-on-click

* Implement subscribeToUpdates

* Introduce sass/elevation

* Split close-on-click to closing-click and subscribe-trigger

* Have closing-* stores return a symbol

- This way they act more of an EventEmitter than a store

* Allow passing show store

* Remove styling on float on updatePosition removal

* Implement a nice border for dropdowns

* Apply different border and box-shadow to Popover in dark/light theme

* Fix Ctrl+Shift+T not working

* Satisfy formatters and tests

* Add copyright header

* move copyright header to top (dae)
2022-03-02 14:21:19 +10:00

45 lines
1.2 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import type { Readable, Writable } from "svelte/store";
import { Callback, singleCallback } from "../lib/typing";
import subscribeToUpdates from "./subscribe-updates";
/**
* The goal of this action is to turn itself inactive.
* Once `active` is `true`, it will unsubscribe from `store`.
*
* @param active: If `active` is `true`, all stores will be subscribed to.
* @param stores: If any `store` updates to a true value, active will be set to false.
*/
function subscribeTrigger(
active: Writable<boolean>,
...stores: Readable<unknown>[]
): Callback {
function shouldUnset(): void {
active.set(false);
}
let destroy: Callback | null;
function doDestroy(): void {
destroy?.();
destroy = null;
}
active.subscribe((value: boolean): void => {
if (value && !destroy) {
destroy = singleCallback(
...stores.map((store) => subscribeToUpdates(store, shouldUnset)),
);
} else if (!value) {
doDestroy();
}
});
return doDestroy;
}
export default subscribeTrigger;