{"id":13425908,"url":"https://github.com/lukePeavey/SplitType","last_synced_at":"2025-03-15T20:31:32.637Z","repository":{"id":22983925,"uuid":"26334219","full_name":"lukePeavey/SplitType","owner":"lukePeavey","description":"Javascript utility that splits text into lines, words, characters for animation ","archived":false,"fork":false,"pushed_at":"2023-12-03T19:43:13.000Z","size":4513,"stargazers_count":643,"open_issues_count":29,"forks_count":41,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-10T13:31:53.211Z","etag":null,"topics":["animation","javascript","split-text","typography"],"latest_commit_sha":null,"homepage":"https://lukepeavey.github.io/SplitType/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lukePeavey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2014-11-07T19:21:45.000Z","updated_at":"2025-03-09T14:35:12.000Z","dependencies_parsed_at":"2023-12-03T20:39:00.172Z","dependency_job_id":null,"html_url":"https://github.com/lukePeavey/SplitType","commit_stats":{"total_commits":69,"total_committers":10,"mean_commits":6.9,"dds":0.5652173913043479,"last_synced_commit":"2b7ac11ac7da53e6504bbe39119c2cbaad6c7e51"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukePeavey%2FSplitType","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukePeavey%2FSplitType/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukePeavey%2FSplitType/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukePeavey%2FSplitType/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukePeavey","download_url":"https://codeload.github.com/lukePeavey/SplitType/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243790939,"owners_count":20348378,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["animation","javascript","split-text","typography"],"created_at":"2024-07-31T00:01:21.822Z","updated_at":"2025-03-15T20:31:30.108Z","avatar_url":"https://github.com/lukePeavey.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# SplitType\n\n[![npm version](https://badge.fury.io/js/split-type.svg)](https://www.npmjs.com/package/split-type)\n\nSplitType is a small javascript library that splits HTML text into elements so that lines, words, and characters can be animated independently. It was inspired by GSAP's SplitText plugin, and can be used with any animation library.\n\nUnder the hood, SplitType changes the html structure of the target elements, wrapping each line, word, and/or character in a element.\n\n- [Features](#features)\n- [Supported Browsers](#supported-browsers)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Splitting Text](#splitting-text)\n  - [The types option](#the-types-option)\n  - [Accessing split text nodes](#accessing-split-text-nodes)\n  - [Reverting Split Text](#reverting-split-text)\n  - [Nested Elements](#nested-elements)\n  - [Absolute vs Relative position](#absolute-vs-relative-position)\n  - [Responsive Text](#responsive-text)\n- [API Reference](#api-reference)\n  - [SplitType(target, [options])](#splittypetarget-options)\n  - [Instance Properties](#instance-properties)\n  - [Static Properties](#static-properties)\n- [Examples](#examples)\n\n## Features\n\n- Splits text into lines, words, and/or characters\n- Customizable class names for split text elements\n- Detects natural lines breaks in the text\n- Preserves explicit lines breaks (`\u003cbr\u003e` tags)\n- Preserves nested HTML elements inside the target elements\n- Supports unicode symbols such as emojis\n\n## Supported Browsers\n\nSplitType works in all modern browsers. Internet Explorer is [no longer supported](https://github.com/lukePeavey/SplitType/issues/29).\n\n- ✅ Chrome\n- ✅ Firefox\n- ✅ Edge\n- ✅ Safari\n- ❌ Internet Explorer\n\n## Installation\n\n**Yarn/NPM**\n\nYou can install SplitType as a dependency using yarn or npm.\n\n```SHELL\nyarn add 'split-type'\n```\n\n```js\nimport SplitType from 'split-type'\n```\n\n**CDN**\n\nOr, include the following `\u003cscript\u003e` tag to load SplitType from a CDN.\n\n```html\n\u003c!-- Minified UMD bundle --\u003e\n\u003cscript src=\"https://unpkg.com/split-type\"\u003e\u003c/script\u003e\n```\n\n## Usage\n\n### Splitting Text\n\nThe SplitType class \"splits\" the text content of the target elements using the provided options. It returns a `SplitType` instance which provides access to the split text nodes. By default, text will be split into lines, words, and characters, using relative position.\n\nBecause SplitType is a class, it cannot be called without the `new` keyword. As an alternative, you can use the static method `SplitType.create().`\n\n```js\nconst text = new SplitType('#target')\n// or\nconst text = SplitType.create('#target')\n\n// Array of line elements\ntext.lines\n// Array of word elements\ntext.words\n// Array of character elements\ntext.chars\n```\n\n**Important: The following style should be applied to all target elements.** This prevents the characters from shifting slightly when text is split/reverted.\n\n```css\n.target {\n  font-kerning: none;\n}\n```\n\nAlso, if the target elements are inside a flex container, they need to have a defined `width` to prevent the text from moving when it gets split.\n\n### The types option\n\nThe `types` option lets you specify which units the text will be broken up into. There are three types: lines, words, and characters. You can specify any combination of these types. However, splitting text into only characters is not recommended. To maintain normal line breaks, you need to include words and/or lines.\n\n```js\n// Splits text into lines, words, characters (default)\nconst text = new SplitType('#target')\n// Splits text into words and characters\nconst text = new SplitType('#target', { types: 'words, chars' })\n// Splits text into lines\nconst text = new SplitType('#target', { types: 'words' })\n```\n\n### Accessing split text nodes\n\nYou can access the split lines/words/characters via properties on the `SplitType` instance.\n\n```js\n// Splits text in element with ID=\"target\"\nconst text = new SplitType('#target')\n\n// An array of the all line elements\nconsole.log(text.lines)\n// An array of all word elements\nconsole.log(text.words)\n// An array of all character elements\nconsole.log(text.chars)\n```\n\nYou can also use selectors to access split text elements\n\n```js\nconst text = new SplitType('#target')\nconst words = document.querySelectorAll('#target .word')\n```\n\n### Reverting Split Text\n\nThe revert method will restore the target element(s) to their original html content. It also removes all data associated with the target elements from SplitType's internal data store. It is recommended to revert split text once it is no longer needed (for example at the end of an animation, or before the component is unmounted).\n\nText can be reverted using the instance method:\n\n```js\ninstance.revert()\n```\n\nOr using the static method, and specify the target elements to revert.\n\n```js\nSplitType.revert('#target')\n```\n\n### Nested Elements\n\nAs of `v0.3`, nested elements inside the target elements will be preserved when text is split. This makes it possible to:\n\n- Apply custom styles to specific parts of the test\n- Include interactive elements such links are buttons inside split text.\n\n```html\n\u003cp id=\"target\"\u003eFoo \u003cem\u003eBar\u003c/em\u003e\u003c/p\u003e\n```\n\n```js\nSplitType.create('#target')\n```\n\nResult\n\n```html\n\u003cdiv class=\"target\"\u003e\n  \u003cdiv class=\"line\" style=\"display: block; text-align: start; width: 100%\"\u003e\n    \u003cdiv class=\"word\" style=\"display: inline-block; position: relative\"\u003e\n      \u003cdiv class=\"char\" style=\"display: inline-block\"\u003eF\u003c/div\u003e\n      \u003cdiv class=\"char\" style=\"display: inline-block\"\u003eo\u003c/div\u003e\n      \u003cdiv class=\"char\" style=\"display: inline-block\"\u003eo\u003c/div\u003e\n    \u003c/div\u003e\n    \u003cem style=\"display: inline-block; position: relative\"\n      \u003e\u003cdiv class=\"word\" style=\"display: inline-block; position: relative\"\u003e\n        \u003cdiv class=\"char\" style=\"display: inline-block\"\u003eB\u003c/div\u003e\n        \u003cdiv class=\"char\" style=\"display: inline-block\"\u003ea\u003c/div\u003e\n        \u003cdiv class=\"char\" style=\"display: inline-block\"\u003er\u003c/div\u003e\n      \u003c/div\u003e\n    \u003c/em\u003e\n  \u003c/div\u003e\n\u003c/div\u003e\n```\n\nCaveat: this feature is not compatible with splitting text into lines. When split lines is enabled, if the text content of a nested element gets broken onto multiple lines, it will result in unexpected line breaks in the split text.\n\n### Absolute vs Relative position\n\nBy default, split text nodes are set to relative position and `display:inline-block`. SplitType also supports absolute position for split text nodes by setting `{ absolute: true }`. When this is enabled, each line/word/character will be set to absolute position, which can improve performance for some animations.\n\n### Responsive Text\n\nWhen text is split into words and characters using relative position, the text will automatically reflow when the container is resized. However, when absolute position is enabled, or text is split into lines (default), the text will not reflow naturally if the viewport is resized. In this case, you will need to re-split text after the container is resized. This can be accomplished using an event listener or ResizeObserver and calling `instance.split()` after the container has been resized.\n\nFor a complete example, see [`__stories__/components/Example.svelte`](https://github.com/lukePeavey/SplitType/blob/master/__stories__/components/Example.svelte)\n\n```js\nconst text = new SplitType('#target')\n\n// Reposition text after the container is resized (simplified version)\n// This example uses lodash#debounce to ensure the split method only\n// gets called once after the resize is complete.\nconst resizeObserver = new ResizeObserver(\n  debounce(([entry]) =\u003e {\n    // Note: you should add additional logic so the `split` method is only\n    // called when the **width** of the container element has changed.\n    text.split()\n  }, 500)\n)\nresizeObserver.observe(containerElement)\n```\n\n## API Reference\n\n### SplitType(target, [options])\n\n**`target`**\n\nThe target element(s) for the SplitType call. This can be a selector string, a single element, or a collection of elements (ie NodeList, jQuery object, or array).\n\n**`options`**\n\n| name       | type      | default                 | description                                                      |\n| ---------- | --------- | ----------------------- | ---------------------------------------------------------------- |\n| absolute   | `boolean` | `false`                 | If true, absolute position will be used to for split text nodes. |\n| tagName    | `string`  | `\"div\"`                 | The HTML tag that will be used for split text nodes              |\n| lineClass  | `string`  | `\"line\"`                | The className all split line elements                            |\n| wordClass  | `string`  | `\"word\"`                | The className for split word elements                            |\n| charClass  | `string`  | `\"char\"`                | The className for split character elements                       |\n| splitClass | `string`  | `null`                  | A className for all split text elements                          |\n| types      | `string`  | `\"lines, words, chars\"` | Comma separated list of types                                    |\n| split      | `string`  | \"\"                      | Alias for `types`                                                |\n\n### Instance Properties\n\n`get` **`instance.lines`** \u0026nbsp;:\u0026nbsp; `HTMLElement[]`\n\nAn array of the split line elements in the splitType instance\n\n`get` **`instance.words`** \u0026nbsp;:\u0026nbsp; `HTMLElement[]`\n\nAn array of the split word elements in the splitType instance\n\n`get` **`instance.chars`** \u0026nbsp;:\u0026nbsp; `HTMLElement[]`\n\nAn array of the split character elements\n\n`get` **`instance.settings`** \u0026nbsp;:\u0026nbsp; `SplitTypeOptions`\n\nThe settings for this splitType instance.\n\n`get` **`instance.isSplit`** \u0026nbsp;:\u0026nbsp; `boolean`\n\nIndicates if the target elements are currently split\n\n`method` **`instance.split(options)`** \u0026nbsp; `=\u003e void`\n\nThe split method is called automatically when a new SplitType instance is created. It can be called manually to re-split the target elements. By default it will use the same options that were passed to the constructor when the instance was created. You can also provide new options. This method is useful when you need to re-split text after the browser or container element has been re-sized.\n\n`method` **`instance.revert()`** \u0026nbsp; `=\u003e void`\n\nRestores the target elements to their original html content. It also removes data associated with the target elements from SplitTypes internal data store.\n\n### Static Properties\n\n`get` **`SplitType.defaults`**\u0026nbsp;:\u0026nbsp;`{SplitTypeOptions}`\n\nGets the current default settings for all SplitType instances. The default settings can be modified using the `setDefaults` methods.\n\n`method` **`SplitType.setDefaults(options: any)`** \u0026nbsp;`=\u003e SplitTypeOptions`\n\nSets the default options for all `SplitType` instances. **The provided object will be merged with the existing `SplitType.defaults` object**. Returns the new defaults object.\n\n`method` **`SplitType.create(target, options)`** \u0026nbsp;`=\u003e SplitType`\n\nCreates a new `SplitType` instance using the provided parameters. This method can be used to call SplitType without using the `new` keyword.\n\n`method` **`SplitType.revert(target)`** \u0026nbsp;`=\u003e void`\n\nReverts the target element(s) if they are currently split. This provides a way to revert split text without a reference to the `SplitType` instance.\n\n## Examples\n\n**Text Animation with GSAP**\n\n```js\n// Split text into words and characters\nconst text = new SplitType('#target', { types: 'words, chars' })\n\n// Animate characters into view with a stagger effect\ngsap.from(text.chars, {\n  opacity: 0,\n  y: 20,\n  duration: 0.5,\n  stagger: { amount: 0.1 },\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FlukePeavey%2FSplitType","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FlukePeavey%2FSplitType","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FlukePeavey%2FSplitType/lists"}