{"id":27111954,"url":"https://github.com/matt-d-rat/react-middle-truncate","last_synced_at":"2025-10-24T07:02:58.266Z","repository":{"id":32691284,"uuid":"139778040","full_name":"matt-d-rat/react-middle-truncate","owner":"matt-d-rat","description":"A React component for intelligently truncating text in the middle of the string.","archived":false,"fork":false,"pushed_at":"2023-03-04T01:27:53.000Z","size":4373,"stargazers_count":30,"open_issues_count":18,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T23:04:38.365Z","etag":null,"topics":["javascript","react","text","truncate","truncation"],"latest_commit_sha":null,"homepage":"https://matt-d-rat.github.io/react-middle-truncate","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matt-d-rat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-05T00:57:42.000Z","updated_at":"2024-07-26T08:54:11.000Z","dependencies_parsed_at":"2024-06-20T22:09:06.031Z","dependency_job_id":null,"html_url":"https://github.com/matt-d-rat/react-middle-truncate","commit_stats":{"total_commits":29,"total_committers":2,"mean_commits":14.5,"dds":0.03448275862068961,"last_synced_commit":"e66e843711e964aae02b2957f92f66c1c18a8d10"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-d-rat%2Freact-middle-truncate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-d-rat%2Freact-middle-truncate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-d-rat%2Freact-middle-truncate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-d-rat%2Freact-middle-truncate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matt-d-rat","download_url":"https://codeload.github.com/matt-d-rat/react-middle-truncate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247575674,"owners_count":20960816,"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":["javascript","react","text","truncate","truncation"],"created_at":"2025-04-07T01:25:32.601Z","updated_at":"2025-10-24T07:02:58.144Z","avatar_url":"https://github.com/matt-d-rat.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-middle-truncate\n\n[![][travis_img]][travis_url] [![][github_issues_img]][github_issues_url] [![github_forks_img]][github_forks_url] [![github_stars_img]][github_stars_url] [![][license_img]][license_url]\n\nA React component for intelligently truncating text in the middle of the string with an ellipsis. You can see the component in action in this [demo][url-demo].\n\n## Installation\n\n```bash\nnpm install react-middle-truncate --save\n```\n\n## Usage\n\nThe truncation point is determined by taking into account the inherited `font` CSS properties of the element, and rendering the result to [Canvas][url-docs-canvas] in order to dynamically calculate the width of the text. Should the width of the text not fit into the available space to render, truncation is applied. The result of which looks like this:\n\n```javascript\nimport MiddleTruncate from 'react-middle-truncate';\n\n\u003cMiddleTruncate\n  text=\"Hello world this is a really long string\"\n  start={/Hello\\sworld/}\n  end={6} /\u003e\n```\n\n![Result of Middle Truncation using the code sample above][img-screenshot]\n\nYou'll note from the code sample above we can use the `start` and `end` props to determine how much of the start and end of the string to preserve. These props can either be numeric, which indicates how many characters of the string from that direction to preserve, or you can provide a [Regular Expression][url-docs-regexp] to dynamically match parts of the string.\n\nIn the case of using a [Regular Expression][url-docs-regexp] for the `end` prop, remember to match from the end of the pattern using the `$` flag.\n\n### Options\n\n| Prop                 | Type               | Description                                                                                                                                                                            | Default |\n|----------------------|--------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|\n| `text`               | `String`           | The input text to truncate if there isn't enough space to render the entire string.                                                                                                    | `''`    |\n| `ellipsis`           | `String`           | The ellipsis to use when the text is truncated.                                                                                                                                        | `'...'` |\n| `start`              | `Number` `RegExp`  | The number of characters or a Regular Expression match from the start of the text to preserve.                                                                                         | `0`     |\n| `end`                | `Number` `RegExp`  | The number of characters or a Regular Expression match from the end of the text to preserve.                                                                                           | `0`     |\n| `smartCopy`          | `Boolean` `String` | Can be one of the following: `false`, `'partial'` or `'all'`. Allows the the full un-truncated text to be copied to the user's clipboard when they select and copy the truncated text. | `'all'` |\n| `onResizeDebounceMs` | `Number`           | A delay in milliseconds to debounce the window resize event listener.                                                                                                                  | `100`   |\n\n### Why and when to middle truncate?\n\nWe have access to CSS text truncation with the `text-overflow: ellipsis;` which will truncate the text at the end of a string for us. So why do you need middle truncation, and when should you use it?\n\nUltimately it boils down to whether text at the end or in the middle of the string is more likely to differentiate the item.\n\nIn the case of a street address, the end of the string is likely to be less important in helping the user to differentiate items from one another, as most strings of that category are likely to end in road, street, avenue etc... So in that case truncating at the end of the string would be a better solution.\n\nIn the case of a connection string or a URL, the start and/or end of the string is likely to contain more valuable information to help the user differentiate items from one another, such as the protocol and sub domain parts at the start, and the port number at the end of the string are more likely to be different. In that case truncating in the middle of the string would be a better solution.\n\n## Contributing\n\nPull requests of any kind are welcome from the community. Please ensure you have read the guidelines for [Contributing][url-contributing] and this project's [Code of Conduct][url-code-of-conduct] before raising a pull request.\n\n### Maintainers\n\n* Matt Fairbrass [@matt\\_d_rat][url-twitter]\n\n## License\n\nMIT License\n\n[url-demo]: https://matt-d-rat.github.io/react-middle-truncate\n[url-docs-canvas]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API\n[url-docs-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions\n[url-twitter]: https://twitter.com/matt_d_rat\n[url-contributing]: CONTRIBUTING.md\n[url-code-of-conduct]: CODE_OF_CONDUCT.md\n\n[img-screenshot]: src/demo/assets/images/screenshot.png \"Result of applying middle truncation to the text\"\n\n[license_img]: https://img.shields.io/github/license/matt-d-rat/react-middle-truncate.svg\n[license_url]: https://github.com/matt-d-rat/react-middle-truncate/blob/master/LICENSE\n[github_issues_img]: https://img.shields.io/github/issues/matt-d-rat/react-middle-truncate.svg\n[github_issues_url]: https://github.com/matt-d-rat/react-middle-truncate/issues\n[github_forks_img]: https://img.shields.io/github/forks/matt-d-rat/react-middle-truncate.svg\n[github_forks_url]: https://github.com/matt-d-rat/react-middle-truncate/network\n[github_stars_img]: https://img.shields.io/github/stars/matt-d-rat/react-middle-truncate.svg\n[github_stars_url]: https://github.com/matt-d-rat/react-middle-truncate/stargazers\n[travis_img]: https://img.shields.io/travis/matt-d-rat/react-middle-truncate.svg?style=flat-square\n[travis_url]: https://travis-ci.org/matt-d-rat/react-middle-truncate\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatt-d-rat%2Freact-middle-truncate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatt-d-rat%2Freact-middle-truncate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatt-d-rat%2Freact-middle-truncate/lists"}