import type { RMLTemplateExpressions, SinkBindingConfiguration } from "../types/internal"; import type { ExplicitSink, Sink } from "../types/sink"; import { SINK_TAG } from "../constants"; export const CLOSED_SINK_TAG = 'closed'; /** * A sink that closes a <dialog> element when a source streams emits * @param dialogBox an HTMLDialogElement * @returns */ export const ClosedSink: Sink = (dialogBox: HTMLDialogElement) => dialogBox.close.bind(dialogBox) ; /** * An explicit sink that closes a <dialog> element when a source streams emits a non falsey value * * You can call this sink in the following ways: * - implicitly, by assigning it to the `rml:closed` attribute of a `` element * - explicitly, by using the `Closed` sink * - via a {@link Mixin}, by emitting a `rml:closed` key-value pair * * @returns Sink * * ## Examples * * ### Close a <dialog> box when a button is clicked * * ```ts * import { Subject } from 'rxjs'; * import { rml } from 'rimmel'; * * const Component = () => { * const close = new Subject(); * * return rml` * * * * `; * } * ``` * * ### Close a <dialog> box in 10s or when a button is clicked * * ```ts * import { Subject, merge } from 'rxjs'; * import { rml } from 'rimmel'; * * const Component = () => { * const close = new Subject(); * const timeout = new Promise(resolve => setTimeout(resolve, 10000)); * * return rml` * * * * `; * } * ``` * ### Close a <dialog> box from a {@link Mixin} * * ```ts * import { Subject } from 'rxjs'; * import { rml } from 'rimmel'; * * const Autoclose = () => { * const timeout = new Promise(resolve => setTimeout(resolve, 10000)); * * return { * 'rml:closed': timeout, * }; * }; * * const Component = () => rml` * * this box will close in 10s * * `; * } * ``` */ export const Closed: ExplicitSink<'closed'> = (source: RMLTemplateExpressions.BooleanAttributeValue<'closed'>) => >({ type: SINK_TAG, t: CLOSED_SINK_TAG, source, sink: ClosedSink, }) ;