{"version":3,"sources":["../../src/use-title/index.ts"],"sourcesContent":["import { useDeepCompareEffect } from '../use-deep-compare-effect'\nimport { useGetterRef } from '../use-getter-ref'\nimport { useLatest } from '../use-latest'\nimport { useMount } from '../use-mount'\nimport { useStableFn } from '../use-stable-fn'\nimport { useUnmount } from '../use-unmount'\nimport { isFunction } from '../utils/basic'\nimport { unwrapGettable } from '../utils/unwrap'\n\nimport type { Gettable } from '../utils/basic'\n\n/**\n * `%s` will be replaced by the title if the template is a `string`\n */\nexport type UseTitleOptionsTemplate = string | ((title: string) => string)\n\nexport type UseTitleOptions = {\n  /**\n   * Restore the original title when unmounted\n   *\n   * @defaultValue false\n   */\n  restoreOnUnmount?: boolean\n  /**\n   * A template to format the title\n   *\n   * @defaultValue '%s'\n   */\n  template?: UseTitleOptionsTemplate\n}\n\n/**\n * A React Hook that helps to manage the title of the document.\n */\nexport function useTitle(newTitle: Gettable<string>, options: UseTitleOptions = {}): () => void {\n  const [originalTitleRef, originalTitle] = useGetterRef('')\n  const { template = '%s', restoreOnUnmount = false } = options\n\n  const latest = useLatest({ template, restoreOnUnmount })\n  const nextTitle = unwrapGettable(newTitle)\n\n  useMount(() => {\n    originalTitleRef.current = document.title\n  })\n\n  useDeepCompareEffect(() => {\n    document.title = format(nextTitle, latest.current.template)\n  }, [nextTitle])\n\n  const restore = useStableFn(() => {\n    document.title = originalTitle()\n  })\n\n  useUnmount(() => {\n    if (latest.current.restoreOnUnmount) {\n      restore()\n    }\n  })\n\n  return restore\n}\n\nfunction format(text: string, template: UseTitleOptionsTemplate) {\n  return isFunction(template) ? template(text) : template.replace(/%s/g, text)\n}\n"],"names":["useTitle","newTitle","options","useGetterRef","originalTitleRef","originalTitle","template","restoreOnUnmount","latest","useLatest","nextTitle","unwrapGettable","useMount","current","document","title","useDeepCompareEffect","format","restore","useStableFn","useUnmount","text","isFunction","replace"],"mappings":";;;;+BAkCgBA;;;eAAAA;;;oCAlCqB;4BACR;yBACH;wBACD;2BACG;0BACD;qBACA;sBACI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BxB,SAASA,SAASC,QAA0B;QAAEC,UAAAA,iEAA2B,CAAC;IAC/E,IAA0CC,iCAAAA,IAAAA,0BAAY,EAAC,SAAhDC,mBAAmCD,kBAAjBE,gBAAiBF;IAC1C,wBAAsDD,QAA9CI,UAAAA,0CAAW,sDAAmCJ,QAA7BK,kBAAAA,0DAAmB;IAE5C,IAAMC,SAASC,IAAAA,oBAAS,EAAC;QAAEH,UAAAA;QAAUC,kBAAAA;IAAiB;IACtD,IAAMG,YAAYC,IAAAA,sBAAc,EAACV;IAEjCW,IAAAA,kBAAQ,EAAC;QACPR,iBAAiBS,OAAO,GAAGC,SAASC,KAAK;IAC3C;IAEAC,IAAAA,0CAAoB,EAAC;QACnBF,SAASC,KAAK,GAAGE,OAAOP,WAAWF,OAAOK,OAAO,CAACP,QAAQ;IAC5D,GAAG;QAACI;KAAU;IAEd,IAAMQ,UAAUC,IAAAA,wBAAW,EAAC;QAC1BL,SAASC,KAAK,GAAGV;IACnB;IAEAe,IAAAA,sBAAU,EAAC;QACT,IAAIZ,OAAOK,OAAO,CAACN,gBAAgB,EAAE;YACnCW;QACF;IACF;IAEA,OAAOA;AACT;AAEA,SAASD,OAAOI,IAAY,EAAEf,QAAiC;IAC7D,OAAOgB,IAAAA,iBAAU,EAAChB,YAAYA,SAASe,QAAQf,SAASiB,OAAO,CAAC,OAAOF;AACzE"}