Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add useDocumentTitle hook #102

Merged
merged 4 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-rivers-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@raddix/use-document-title': major
---

Added the useDocumentTitle hook
4 changes: 4 additions & 0 deletions docs/_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"title": "useClickOutside",
"path": "/hooks/use-click-outside"
},
{
"title": "useDocumentTitle",
"path": "/hooks/use-document-title"
},
{
"title": "useEventListener",
"path": "/hooks/use-event-listener"
Expand Down
42 changes: 42 additions & 0 deletions docs/en/use-document-title.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: useDocumentTitle
description: Dynamically update the title of a web page.
---

## Installation

Install the custom hook from your command line.

<Snippet pkg text='@raddix/use-document-title' />

## Usage

```tsx
import { useState } from 'react';
import { useDocumentTitle } from '@raddix/use-document-title';

export default function App() {
const [count, setCount] = useState(0)
useDocumentTitle(`Clicked ${count} times.`);

return (
<button onClick={() => setCount(count + 1)}>
Increment Count: {count}
</button>
)
}
```

## API

### Parameters

<ApiTable
data={[
{
name: 'title',
type: 'string',
description: 'The title to be set for the document.',
}
]}
/>
45 changes: 45 additions & 0 deletions docs/es/use-document-title.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: useDocumentTitle
description: Actualiza dinámicamente el título de una página web.
---

## Instalación

Instala el custom hook desde su linea de comando.

<Snippet pkg text='@raddix/use-document-title' />

## Uso

```tsx
import { useState } from 'react';
import { useDocumentTitle } from '@raddix/use-document-title';

export default function App() {
const [count, setCount] = useState(0)
useDocumentTitle(`Clicked ${count} times.`);

return (
<button onClick={() => setCount(count + 1)}>
Increment Count: {count}
</button>
)
}
```

## API

### Parámetros

<ApiTable
head={{
name: 'Nombre'
}}
data={[
{
name: 'title',
type: 'string',
description: 'El título que se establecerá para el documento.',
}
]}
/>
5 changes: 5 additions & 0 deletions packages/hooks/use-document-title/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# useDocumentTitle

A hook that dynamically update the title of a web page.

Please refer to the [documentation](https://raddix.dev/hooks/use-document-title) for more information.
48 changes: 48 additions & 0 deletions packages/hooks/use-document-title/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@raddix/use-document-title",
"description": "A hook that dynamically update the title of a web page.",
"version": "0.1.0",
"license": "MIT",
"main": "src/index.ts",
"author": "Moises Machuca Valverde <[email protected]> (https://www.moisesmachuca.com)",
"homepage": "https://raddix.dev/hooks/use-document-title",
"repository": {
"type": "git",
"url": "https://github.com/gdvu/raddix.git"
},
"keywords": [
"react-hook",
"react-title-hook",
"react-use-title",
"use-title",
"use-document-title",
"use-title-hook",
"hook-title"
],
"sideEffects": false,
"scripts": {
"lint": "eslint \"{src,tests}/*.{ts,tsx,css}\"",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"build": "tsup src --dts",
"prepack": "clean-package",
"postpack": "clean-package restore"
},
"files": [
"dist",
"README.md"
],
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": ">=16.8.0",
"@raddix/use-isomorphic-effect": "workspace:*"
},
"clean-package": "../../../clean-package.config.json",
"tsup": {
"clean": true,
"target": "es2019",
"format": [
"cjs",
"esm"
]
}
}
20 changes: 20 additions & 0 deletions packages/hooks/use-document-title/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useRef } from 'react';
import { useIsomorphicEffect } from '@raddix/use-isomorphic-effect';

export const useDocumentTitle = (title: string, restoreTitle = true): void => {
const defaultTitle = useRef<string | null>(null);

useIsomorphicEffect(() => {
defaultTitle.current = window.document.title;

return () => {
if (restoreTitle && defaultTitle.current) {
window.document.title = defaultTitle.current;
}
};
}, [restoreTitle]);

useIsomorphicEffect(() => {
document.title = title.trim();
}, [title]);
};
30 changes: 30 additions & 0 deletions packages/hooks/use-document-title/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { renderHook } from '@testing-library/react';
import { useDocumentTitle } from '../src';

describe('useDocumentTitle test:', () => {
beforeEach(() => {
document.title = 'initial';
});

it('should change the title of the document', () => {
renderHook(() => useDocumentTitle('test'));

expect(document.title).toEqual('test');
});

it('should restore the initial title when unmounting', () => {
const { unmount } = renderHook(() => useDocumentTitle('test'));

expect(document.title).toEqual('test');
unmount();
expect(document.title).toEqual('initial');
});

it('should not restore the initial title when unmounting.', () => {
const { unmount } = renderHook(() => useDocumentTitle('test', false));

expect(document.title).toEqual('test');
unmount();
expect(document.title).toEqual('test');
});
});
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading