{"version":3,"file":"ref-callback.cjs","names":[],"sources":["../../src/core/ref-callback.ts"],"sourcesContent":["import type { Readable } from '@vielzeug/ripple';\nimport { watch } from '@vielzeug/ripple';\n\n/** Shape every `ref` prop in this package uses — a JS-only escape hatch to the live element. */\nexport type RefCallback<T> = ((el: T | null) => void) | null | undefined;\n\n/**\n * Forwards a mounted element to a JS-only `ref` callback prop, and keeps forwarding to a\n * *new* callback if the prop is set again after mount (e.g. a parent sets it via a ref\n * callback after render, rather than at initial mount). Every text-entry-style component\n * with a `ref` escape-hatch prop (`ore-input`, `ore-textarea`, `ore-message-composer`,\n * `ore-file-input`) wires this identically — call it once from inside `onElement()`, alongside\n * whatever else that element needs wired.\n *\n * @example\n * ```ts\n * onElement(fieldRef, (el) => {\n *   const unwireField = tf.wire(el);\n *   const unwireRef = bindRefCallback(props.ref, el);\n *\n *   return () => {\n *     unwireField();\n *     unwireRef();\n *   };\n * });\n * ```\n */\nexport const bindRefCallback = <T extends Element>(ref: Readable<RefCallback<T>>, el: T): (() => void) => {\n  // Immediate fire for when the prop is already set on mount.\n  ref.value?.(el);\n\n  const sub = watch(ref, (cb) => cb?.(el));\n\n  return () => {\n    sub.dispose();\n    ref.value?.(null);\n  };\n};\n"],"mappings":"kCA2BA,IAAa,GAAsC,EAA+B,IAAwB,CAExG,EAAI,QAAQ,CAAE,EAEd,IAAM,GAAA,EAAM,EAAA,MAAA,CAAM,EAAM,GAAO,IAAK,CAAE,CAAC,EAEvC,UAAa,CACX,EAAI,QAAQ,EACZ,EAAI,QAAQ,IAAI,CAClB,CACF"}