npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-title-config

v1.0.1

Published

Set document title based on current path and configuration schema

Downloads

6

Readme

react-title-config

Install

npm install react-title-config

Usage

import React from "react"
import TitleConfig from "react-title-config"

 // NOTE: it might be any router you want
import { BrowserRouter } from "react-router-dom"

// your main page or whatever
import MainPage from "/pages/Main"


const config = [
  {
    path: "/about",
    exact: true,
    skipPrefix: true,
    title: "About us",
  },
  {
    exact: true,
    path: ["/login", "/registration", "/reset-password"],
    title: "User actions",
  },
  {
    path: /^\/accounts\/[A-Za-z0-9]+/i,
    title: "Account page",
  },
  {
    path: pathname => pathname.length === "42",
    title: (params, descriptor, extras) => {
      const tab = params.queryParams.tab

      switch (tab) {
        case "one": return "The Hitchhiker's Guide to the Galaxy"
        case "two": return "The Restaurant at the End of the Universe"
        case "three": return "Life, the Universe and Everything"
        case "four": return "So Long, and Thanks for All the Fish"
        case "five": return "\tMostly Harmless"
        default: return "Not sure about this one"
      }
    },
  },
]

const defaultTitle = "No matches"
const titlePrefix = "Prefixed. "

const App = () => (
  <BrowserRouter>
    <>
      <TitleConfig
        config={titlesConfig}
        defaultTitle={defaultTitle}
        prefix={titlePrefix}
        customProperty="All extra props will be passed to title functtion as second parameter"
      />
      <MainPage/>
    </>
  </BrowserRouter>
)

Props

config (required) : Array

An array of title descriptors

defaultTitle : String | default: ''

This title will be set in case there's no matches

prefix : String | default: ''

A static string prefixing each title without skipPrefix: true

Config Descriptor

path (required) : String | RegExp | PathFunction | Array<string | RegExp | PathFunction>

A path to match with current pathname.

PathFunction accepts current pathname as parameter and returns whether the pathname matches: (pathname: String) => Boolean

title (required) : String | TitleFunction

A title which will be rendered in case of match.

TitleFunction accepts current location params, matched descriptor and object containing all extra props passed to component: (params: PathParams, descriptor: ConfigDescriptor, extras: Object) => Boolean

PathParams is an object of following shape:

{
  hash: String,
  href: String,
  pathname: String,
  queryParams: Object
}

exact : Boolean | default: false

Defines matching rule for plain strings. With exact set to true title will be set only in case of full match.

path: "/accounts" will match /accounts/9379992 by default and it will match only /accounts in case of exact: true

skipPrefix : Boolean | default: false

Current title will not be prefixes if skipPrefix is set to true

License

MIT © Alex Khismatulin