From 2b088a404ad792a6c8e6ac6c0c3960ecd2929426 Mon Sep 17 00:00:00 2001 From: Curt Howard Date: Thu, 29 Sep 2022 20:10:24 -0700 Subject: [PATCH] feat: Add useDocumentTitle --- src/index.ts | 2 ++ src/useDocumentTitle/useDocumentTitle.demo.tsx | 7 +++++++ src/useDocumentTitle/useDocumentTitle.mdx | 6 ++++++ src/useDocumentTitle/useDocumentTitle.test.ts | 10 ++++++++++ src/useDocumentTitle/useDocumentTitle.ts | 9 +++++++++ 5 files changed, 34 insertions(+) create mode 100644 src/useDocumentTitle/useDocumentTitle.demo.tsx create mode 100644 src/useDocumentTitle/useDocumentTitle.mdx create mode 100644 src/useDocumentTitle/useDocumentTitle.test.ts create mode 100644 src/useDocumentTitle/useDocumentTitle.ts diff --git a/src/index.ts b/src/index.ts index c94fdb4a..b3479d56 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/useDocumentTitle/useDocumentTitle.demo.tsx b/src/useDocumentTitle/useDocumentTitle.demo.tsx new file mode 100644 index 00000000..d6238a84 --- /dev/null +++ b/src/useDocumentTitle/useDocumentTitle.demo.tsx @@ -0,0 +1,7 @@ +import React from 'react' + +import { useDocumentTitle } from '..' + +export default function Component() { + useDocumentTitle('foo bar') +} diff --git a/src/useDocumentTitle/useDocumentTitle.mdx b/src/useDocumentTitle/useDocumentTitle.mdx new file mode 100644 index 00000000..4c4172b9 --- /dev/null +++ b/src/useDocumentTitle/useDocumentTitle.mdx @@ -0,0 +1,6 @@ +--- +title: useDocumentTitle +date: '2022-09-29' +--- + +An easy way to set the title of the current document. diff --git a/src/useDocumentTitle/useDocumentTitle.test.ts b/src/useDocumentTitle/useDocumentTitle.test.ts new file mode 100644 index 00000000..a38167fc --- /dev/null +++ b/src/useDocumentTitle/useDocumentTitle.test.ts @@ -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') + }) +}) diff --git a/src/useDocumentTitle/useDocumentTitle.ts b/src/useDocumentTitle/useDocumentTitle.ts new file mode 100644 index 00000000..7b4bcb3f --- /dev/null +++ b/src/useDocumentTitle/useDocumentTitle.ts @@ -0,0 +1,9 @@ +import { useIsomorphicLayoutEffect } from '..' + +function useDocumentTitle(title: string): void { + useIsomorphicLayoutEffect(() => { + window.document.title = title + }, [title]) +} + +export default useDocumentTitle