Skip to content

Commit

Permalink
feat: return restore in useTitle
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Jun 21, 2024
1 parent b2e3fb8 commit fbdabd2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
13 changes: 3 additions & 10 deletions src/use-title/demo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button, Card, Input, Zone } from '@/components'
import { useControlledComponent } from '@shined/react-use'
import { useMount, useTitle, useToggle } from '@shined/react-use'
import { useTitle, useToggle } from '@shined/react-use'

export function App() {
const [show, toggle] = useToggle(true)
Expand All @@ -14,23 +14,16 @@ export function App() {
}

function Child() {
const input = useControlledComponent('')
const input = useControlledComponent('useTitle')

const initialTitle = useTitle(input.value, {
useTitle(input.value, {
template: '🌟 %s 🌟',
restoreOnUnmount: true,
})

useMount(() => input.setValue(initialTitle()))

function resetTitle() {
document.title = initialTitle()
}

return (
<Zone border="primary">
<Input placeholder="Title" {...input.props} />
<Button onClick={resetTitle}>Reset title</Button>
</Zone>
)
}
6 changes: 1 addition & 5 deletions src/use-title/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,4 @@ export type UseTitleOptions = {
### Returns
import { Tip } from '@/components'
<Tip type="ref-getter" name="initialTitle" />
A Ref Getter that returns the current title of the document.
A function to restore to the original title.
13 changes: 7 additions & 6 deletions src/use-title/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRef } from 'react'
import { useCreation } from '@shined/react-use'
import { useDeepCompareEffect } from '../use-deep-compare-effect'
import { useGetterRef } from '../use-getter-ref'
import { useLatest } from '../use-latest'
import { useMount } from '../use-mount'
import { useStableFn } from '../use-stable-fn'
Expand All @@ -26,22 +27,22 @@ export type UseTitleOptions = {
template?: UseTitleTemplate
}

export function useTitle(newTitle: string, options: UseTitleOptions = {}): () => string {
const initialTitle = useRef('')
export function useTitle(newTitle: string, options: UseTitleOptions = {}) {
const [originalTitleRef, originalTitle] = useGetterRef('')
const { template = '%s', restoreOnUnmount = false } = options

const latest = useLatest({ template, restoreOnUnmount })

useMount(() => {
initialTitle.current = document.title
originalTitleRef.current = document.title
})

useDeepCompareEffect(() => {
document.title = format(newTitle, latest.current.template)
}, [newTitle])

const restore = useStableFn(() => {
document.title = initialTitle.current
document.title = originalTitle()
})

useUnmount(() => {
Expand All @@ -50,7 +51,7 @@ export function useTitle(newTitle: string, options: UseTitleOptions = {}): () =>
}
})

return useStableFn(() => initialTitle.current)
return restore
}

function format(text: string, template: UseTitleTemplate) {
Expand Down

0 comments on commit fbdabd2

Please sign in to comment.