Skip to content

Commit 78c684c

Browse files
committed
[ADD] useTitle hook
1 parent 901c429 commit 78c684c

8 files changed

Lines changed: 126 additions & 17 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint-disable react-hooks/rules-of-hooks */
2+
import { useEffect } from "react";
3+
import { useTitle } from "../../../../../../packages/react-tools/src"
4+
5+
/**
6+
First, in a variable outside the component is kept track of the page title. The _useTitle_ hook is invoked in the component with the _title_ __Title 1__ parameter and the returned _setTitle_ function is used. After 3 seconds the title is changed with the _setTitle_ function to _title_ __Title 2__.
7+
8+
When the component is unmounted, the title saved in the variable outside the component is set as the title.
9+
*/
10+
const previousTitle = useTitle()[0]();
11+
12+
export const UseTitle = () => {
13+
const [,setTitle] = useTitle("Title 1");
14+
15+
useEffect(() => {
16+
const id = setTimeout(() => {
17+
setTitle("Title 2");
18+
}, 3000);
19+
return () => {
20+
clearTimeout(id);
21+
setTitle(previousTitle);
22+
}
23+
}, [setTitle]);
24+
}

apps/react-tools-demo/src/constants/components.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export const COMPONENTS = [
5858
"useDocumentVisibility",
5959
"useClipboard",
6060
"useMediaQuery",
61-
"useColorScheme"
61+
"useColorScheme",
62+
"useTitle"
6263
]
6364
],
6465
//UTILS

apps/react-tools-demo/src/markdown/useColorScheme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const UseColorScheme = () => {
1414

1515
return (
1616
<div className="container-themed" data-color={value}>
17-
<button onClick={()=>update(value === "dark" ? "light" : "dark")}>Change color</button>
17+
<p>Current color-scheme is: {value}</p>
18+
<button onClick={() => update(value === "dark" ? "light" : "dark")}>Change color</button>
1819
</div>
1920
);
2021
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# useTitle
2+
Hook to handling app page title. It works _outside Component_ also and it returns array of two functions to read and write title.
3+
4+
## Usage
5+
6+
```tsx
7+
const previousTitle = useTitle()[0]();
8+
9+
export const UseTitle = () => {
10+
const [,setTitle] = useTitle("Title 1");
11+
12+
useEffect(() => {
13+
const id = setTimeout(() => {
14+
setTitle("Title 2");
15+
}, 3000);
16+
return () => {
17+
clearTimeout(id);
18+
setTitle(previousTitle);
19+
}
20+
}, [setTitle]);
21+
}
22+
```
23+
24+
> First, in a variable outside the component is kept track of the page title. The _useTitle_ hook is invoked in the component with the _title_ __Title 1__ parameter and the returned _setTitle_ function is used. After 3 seconds the title is changed with the _setTitle_ function to _title_ __Title 2__. When the component is unmounted, the title saved in the variable outside the component is set as the title.
25+
26+
27+
## API
28+
29+
```tsx
30+
useTitle (title?: string): [() => string, (title: string) => void]
31+
```
32+
33+
> ### Params
34+
>
35+
> - __title?__: _string_
36+
>
37+
38+
> ### Returns
39+
>
40+
> __array__
41+
> - __Array__:
42+
> - _() => string_
43+
> - _(title: string) => void_
44+
>

packages/react-tools/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [x] useLayoutEffectOnce
2727
- [x] useRerender
2828
- [x] useIsMounted
29+
- [ ] useInsertionEffect (polyfill???)
2930
- [ ] useDeferredValue (polyfill - an idea can be use setTimeout inside useEffect)
3031

3132
- __PERFORMANCE__
@@ -40,19 +41,19 @@
4041
- [x] useEventListener
4142
- [x] useEventDispatcher
4243
- [x] usePerformAction
43-
- [ ] useIntersectionObserver
44-
- [ ] useMutationObserver
45-
- [ ] useKeysEvents
46-
- [ ] useSize (=useMeasuree?)
4744
- [ ] useHover
4845
- [ ] useResponsive
4946
- [ ] useClickOutside
47+
- [ ] useSize (=useMeasuree?)
5048
- [ ] useScrollIntoView
49+
- [ ] useKeysEvents
5150
- [ ] useMouse
51+
- [ ] useLongPress
52+
- [ ] useIntersectionObserver
53+
- [ ] useInViewport (with area ratio of element: check if is equal to useIntersectionObserver)
54+
- [ ] useMutationObserver
5255
- [ ] useInfiniteScroll
5356
- [ ] useDragAndDrop (check for mobile usage)
54-
- [ ] useInViewport (with area ratio of element: check if is equal to useIntersectionObserver)
55-
- [ ] useLongPress
5657

5758

5859
- __API DOM__
@@ -68,16 +69,20 @@
6869
- [x] useClipboard
6970
- [x] useMediaQuery
7071
- [x] useColorScheme
71-
- [ ] useTitle (change document.title but also document.head.title nodeElement)
72-
- [ ] useFetch
73-
- [ ] useAsync
74-
- [ ] useImageOnLoad
75-
- [ ] useMediaStream
76-
- [ ] useScreenShare
72+
- [x] useTitle (change document.title but also document.head.title nodeElement)
73+
- [ ] useId (polyfill???)
7774
- [ ] useNetwork
7875
- [ ] useOnline
7976
- [ ] useFullscreen (check browser compatibility)
8077
- [ ] useLanguage (?)
78+
- [ ] useOrientation
79+
- [ ] useScreenShare
80+
- [ ] useFetch (with suspense ???)
81+
- [ ] useAsync
82+
- [ ] useImageOnLoad
83+
- [ ] useMediaStream
84+
- [ ] usePinchZoom
85+
- [ ] useObservable — tracks latest value of an Observable
8186

8287
- __UTILS__
8388
- [x] isShallowEqual
@@ -86,11 +91,18 @@
8691
- [x] isTouchEvent
8792
- [x] isClient
8893
- [x] isAsync
94+
- [ ] lazy: lazy react-like customized
95+
- [ ] fetch-client (???ARTS-like)
8996

9097
- __COMPONENT__
91-
- [ ] Show (component to render or not a component by a condition)
98+
- [ ] Show component to render or not a component by a condition. Props: when, fallback, keyed. Keyed is a boolean and needs to avoid rerenders children when it is a function.
99+
- [ ] Switch and Match components with fallback and when props
100+
- [ ] For: A referentially keyed loop with efficient updating of only changed items. The callback takes the current item as the first argument
101+
- [ ] Index: Non-keyed list iteration (rendered nodes are keyed to an array index). This is useful when there is no conceptual key, like if the data consists of primitives and it is the index that is fixed rather than the value.
92102
- [ ] RestrictedRoute (maybe)
93103
- [ ] ErrorBoundary (??)
104+
- [ ] Suspense: Suspence compontent react-like for async component
105+
- [ ] Dynamic: This component lets you insert an arbitrary Component or tag and passes the props through to it.
94106

95107
## ESlint configuration
96108
To validate dependencies of custom hooks like `useMemoCompare`, configure `exhaustive-deps` with the `additionalHooks` option

packages/react-tools/src/hooks/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ export { useTextSelection } from './useTextSelection';
3939
export { useDocumentVisibility } from './useDocumentVisibility';
4040
export { useClipboard } from './useClipboard';
4141
export { useMediaQuery } from './useMediaQuery';
42-
export { useColorScheme } from './useColorScheme';
42+
export { useColorScheme } from './useColorScheme';
43+
export { useTitle } from './useTitle'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useLayoutEffect } from "react";
2+
3+
const setTitle = (title: string) => {
4+
document.title = title;
5+
};
6+
7+
const getTitle = () => document.title;
8+
9+
/**
10+
* **`useTitle`**: Hook to handling app page title. It works _outside Component_ also and it returns array of two functions to read and write title.
11+
* @param {string} [title]
12+
* @returns {[() => string, (title: string) => void]} array
13+
*/
14+
export const useTitle = (title?: string): [() => string, (title: string) => void] => {
15+
title && (document.title = title);
16+
try {
17+
useLayoutEffect(() => {
18+
title && (document.title = title);
19+
// eslint-disable-next-line react-hooks/exhaustive-deps
20+
}, [])
21+
} catch (error) {
22+
return [getTitle, setTitle];
23+
}
24+
return [getTitle, setTitle];
25+
}

packages/react-tools/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export {
4848
useDocumentVisibility,
4949
useClipboard,
5050
useMediaQuery,
51-
useColorScheme
51+
useColorScheme,
52+
useTitle
5253
} from './hooks'
5354

5455
export {

0 commit comments

Comments
 (0)