1 | import * as React from 'react';
|
2 | import type { NavigationContainerRef } from '@react-navigation/core';
|
3 | import type { DocumentTitleOptions } from './types';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export default function useDocumentTitle(
|
9 | ref: React.RefObject<NavigationContainerRef>,
|
10 | {
|
11 | enabled = true,
|
12 | formatter = (options, route) => options?.title ?? route?.name,
|
13 | }: DocumentTitleOptions = {}
|
14 | ) {
|
15 | React.useEffect(() => {
|
16 | if (!enabled) {
|
17 | return;
|
18 | }
|
19 |
|
20 | const navigation = ref.current;
|
21 |
|
22 | if (navigation) {
|
23 | const title = formatter(
|
24 | navigation.getCurrentOptions(),
|
25 | navigation.getCurrentRoute()
|
26 | );
|
27 |
|
28 | document.title = title;
|
29 | }
|
30 |
|
31 | return navigation?.addListener('options', (e) => {
|
32 | const title = formatter(e.data.options, navigation?.getCurrentRoute());
|
33 |
|
34 | document.title = title;
|
35 | });
|
36 | });
|
37 | }
|