import type { ExplicitSink, Sink } from "../types/sink"; import type { RMLTemplateExpressions, SinkBindingConfiguration } from "../types/internal"; import { SINK_TAG } from "../constants"; export const REMOVED_SINK_TAG = 'removed'; /** * A sink that removes the given element from the DOM when the source emits * @param e A DOM element * @returns A sink that removes the element when called */ export const RemovedSink: Sink = (e: Element) => e.remove.bind(e) ; /** * Removed Sink * * A specialised sink to remove an element from the DOM when its source emits * Once removed the underlying element can't be restored. Most useful with boxes and other ephemeral containers * * @param source A present or future boolean value * @returns RMLTemplateExpression A template expression for the "rml:close" attribute * * ## Examples * * ### Remove an Element * * Remove an element after 5s * * ```ts * import { rml } from 'rimmel'; * * const Component = () => { * const delay = new Promise(resolve => setTimeout(resove, 5000)); * * return rml` *
* removing in 5s *
* `; * } * ``` * * ### Remove an Element on click * * Remove an element after 5s * * ```ts * import { Subject } from 'rxjs'; * import { rml } from 'rimmel'; * * const Component = () => { * const remove = new Subject(); * * return rml` *
* *
* `; * } * ``` * * ### Specifying the sink explicitly * * Remove an element after 5s, making the sink type explicit * * ```ts * import { rml } from 'rimmel'; * * const Component = () => { * const delay = new Promise(resolve => setTimeout(resove, 5000)); * * return rml` *
* removing in 5s *
* `; * } * ``` * * ### Specifying the sink from a mixin * * Remove an element after 5s, using a {@link Mixin | Mixin} * * ```ts * import { rml } from 'rimmel'; * * const Autoremove = () => { * const delay = new Promise(resolve => setTimeout(resove, 5000)); * * return { * 'rml:removed': delay, * }; * }; * * const Component = () => rml` *
* will be removed when the mixin decides... *
* `; * * ``` */ export const Removed: ExplicitSink<'mixin' | 'removed'> = (source: RMLTemplateExpressions.Any) => >({ type: SINK_TAG, t: REMOVED_SINK_TAG, source, sink: RemovedSink, }) ;