Skip to content

artalar/splitTitle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

splitTitle

This is not a NPM package, this is simple snipet to copy paste it.

This code demonstrate not a perfect, but simple and useful way of how to split words in title pretty

Just copy the code below and paste it to your project.

/**
 * Split text to binary tree for more beautiful text wrapping
 * @example
 * return <h1>{splitTitle(props.titile)}</h1>
 * @see {@link github.com/artalar/splitTitle}
 */
export function splitTitle(text: string) {
  if (text.length < 5 || !text.includes(" ")) {
    return text;
  }
  const median = Math.floor(text.length / 2);
  for (let step = 0; step < median; step++) {
    for (const i of [median - step, median + step]) {
      if (text[i] === " ") {
        return (
          <span style={{ display: "inline-block" }}>
            {splitTitle(text.substring(0, i))}{" "}
            {splitTitle(text.substring(i + 1))}
          </span>
        );
      }
    }
  }
}

Example

codesandbox with splitTitle example

CleanShot 2022-01-20 at 17 58 13@2x

CleanShot 2022-01-20 at 17 58 51@2x

CleanShot 2022-01-20 at 17 59 12@2x

CleanShot 2022-01-20 at 17 59 30@2x

About

Example of how to split a title easy and efficient

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors