Skip to content

Commit

Permalink
feat: Add useDocumentTitle
Browse files Browse the repository at this point in the history
  • Loading branch information
curtvict authored and juliencrn committed Sep 30, 2022
1 parent 89d0a1e commit 2b088a4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export { default as useDarkMode } from './useDarkMode/useDarkMode'
export * from './useDarkMode/useDarkMode'
export { default as useDebounce } from './useDebounce/useDebounce'
export * from './useDebounce/useDebounce'
export { default as useDocumentTitle } from './useDocumentTitle/useDocumentTitle'
export * from './useDocumentTitle/useDocumentTitle'
export { default as useEffectOnce } from './useEffectOnce/useEffectOnce'
export * from './useEffectOnce/useEffectOnce'
export { default as useElementSize } from './useElementSize/useElementSize'
Expand Down
7 changes: 7 additions & 0 deletions src/useDocumentTitle/useDocumentTitle.demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'

import { useDocumentTitle } from '..'

export default function Component() {
useDocumentTitle('foo bar')
}
6 changes: 6 additions & 0 deletions src/useDocumentTitle/useDocumentTitle.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: useDocumentTitle
date: '2022-09-29'
---

An easy way to set the title of the current document.
10 changes: 10 additions & 0 deletions src/useDocumentTitle/useDocumentTitle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { act, renderHook } from '@testing-library/react-hooks/dom'

import useDocumentTitle from './useDocumentTitle'

describe('useDocumentTitle()', () => {
test('title should be in the document', () => {
renderHook(() => useDocumentTitle('foo'))
expect(window.document.title).toEqual('foo')
})
})
9 changes: 9 additions & 0 deletions src/useDocumentTitle/useDocumentTitle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useIsomorphicLayoutEffect } from '..'

function useDocumentTitle(title: string): void {
useIsomorphicLayoutEffect(() => {
window.document.title = title
}, [title])
}

export default useDocumentTitle

0 comments on commit 2b088a4

Please sign in to comment.