@teneff/use-title-effect
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@teneff/use-title-effect

React hook for changing document.title

NPM version Build Status Coverage Status GitHub issues GitHub stars

Installation

yarn add @teneff/use-title-effect

Preview

Preview

Usage

import useTitleEffect from '@teneff/use-title-effect'

function App() {

  useTitleEffect('React App Title', {
    messages: ['message 1', 'message 2']
  });

  return (
    <div className="App">
     React App Here
    </div>
  );
}

ℹ️ Consider using the hook in the outer components like App

Version 1.0.0 does not revert the original title of the page once the component is unmounted

Options

options.title: string (default: "")

Title is used to provide a permanent page title and it's also provided to the formatter to be enchanced

options.formatter: Formatter<T> (default: see formatter/message)

Formatter is functino accepting object with title and messages and returning array of strings, which are rotated in the given duration

options.duration: number (default: 800)

Time in milliseconds to switch between the different messages resulted from the formatter

Usage with custom message format

  import formatters, { Formatter } from './formatter';

  const messages = [
    {
      message: 'Hello!',
      author: {
        name: 'John Doe'
      }
    },
    {
      message: 'Hey!',
      author: {
        name: 'Jane Doe'
      }
    },
    {
      message: 'U there?',
      author: {
        name: 'John Doe'
      }
    },
  ]

  useTitleEffect(
    "My awesome app",
    {
      messages,
      // reusing the default formatter
      formatter: ({ title, messages }) => formatters.message({
      title,
      messages: messages.map(message => message.author.name)
    })
  })

  useTitleEffect(
    "My awesome app",
    {
      messages,
      // custom formatter
      formatter: ({ title, messages }) => {
        const uniqueAuthors = new Set(messages.map(message => message.author.name))
        return messages.length ? [
          `(${messages.length}) ${title}`,
          `(${messages.length}) You've got messages from ${Array.from(uniqueAuthors).length} contacts`
        ] : [title]
      }
    });

Package Sidebar

Install

npm i @teneff/use-title-effect

Weekly Downloads

12

Version

1.0.1

License

MIT

Unpacked Size

13 kB

Total Files

22

Last publish

Collaborators

  • teneff