react-expand-text 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # react-expand-text
2
- A modern React component that shrinks and expands long text. If the `text` length is
2
+ A simple React component that shrinks and expands long text. If the `text` length is
3
3
  longer than `maxLength` the text field will collapse, and truncated text will be replaced
4
- with an ellipsis. Clicking the text will alternately expand/collapse the long text.
4
+ with an ellipsis. The ellipsis can be placed in the middle (default) or at the end of the text.
5
+ Clicking the text will alternately expand/collapse the long text.
6
+
7
+ **Now with full TypeScript support!** 🚀
5
8
 
6
9
  ## Installation
7
10
  ```bash
@@ -13,6 +16,24 @@ npm install --save react-expand-text
13
16
  - React >= 18.0.0
14
17
 
15
18
  ## Usage
19
+
20
+ ### TypeScript
21
+ ```tsx
22
+ import React from 'react';
23
+ import ExpandText from 'react-expand-text';
24
+
25
+ const MyComponent: React.FC = () => {
26
+ return (
27
+ <ExpandText
28
+ text="Your long text here"
29
+ maxLength={50}
30
+ className="optional-css-class"
31
+ />
32
+ );
33
+ };
34
+ ```
35
+
36
+ ### JavaScript
16
37
  ```javascript
17
38
  import React from 'react';
18
39
  import ExpandText from 'react-expand-text';
@@ -32,8 +53,30 @@ const MyComponent = () => {
32
53
  * `text`: Text to display (string, required)
33
54
  * `maxLength`: Max length of text (number, required)
34
55
  * `className`: Optional CSS class name to be applied to the span element
56
+ * `truncateAtEnd`: If true, truncates at the end with ellipsis. If false (default), shows beginning and end with ellipsis in the middle (boolean, optional)
35
57
 
36
- ## Example
58
+ ## Examples
59
+
60
+ ### Middle truncation (default)
61
+ ```javascript
62
+ <ExpandText
63
+ maxLength={30}
64
+ text="I am a long string that is longer than max length"
65
+ />
66
+ // Output: "I am a long st...max length" (click to expand)
67
+ ```
68
+
69
+ ### End truncation
70
+ ```javascript
71
+ <ExpandText
72
+ maxLength={30}
73
+ text="I am a long string that is longer than max length"
74
+ truncateAtEnd
75
+ />
76
+ // Output: "I am a long string that is lo..." (click to expand)
77
+ ```
78
+
79
+ ### Complete example
37
80
  ```javascript
38
81
  import React from 'react';
39
82
  import { createRoot } from 'react-dom/client';
@@ -41,15 +84,26 @@ import ExpandText from 'react-expand-text';
41
84
 
42
85
  const App = () => {
43
86
  return (
44
- <ExpandText
45
- maxLength={10}
46
- className='my-css-class'
47
- text={'I am a long string that is longer than max length'}
48
- />
87
+ <div>
88
+ <h3>Middle truncation (default):</h3>
89
+ <ExpandText
90
+ maxLength={30}
91
+ className='my-css-class'
92
+ text={'I am a long string that is longer than max length'}
93
+ />
94
+
95
+ <h3>End truncation:</h3>
96
+ <ExpandText
97
+ maxLength={30}
98
+ className='my-css-class'
99
+ text={'I am a long string that is longer than max length'}
100
+ truncateAtEnd
101
+ />
102
+ </div>
49
103
  );
50
104
  };
51
105
 
52
106
  const container = document.getElementById('root');
53
107
  const root = createRoot(container);
54
108
  root.render(<App />);
55
- ```
109
+ ```
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+ interface ExpandTextProps {
3
+ text: string;
4
+ maxLength: number;
5
+ className?: string;
6
+ truncateAtEnd?: boolean;
7
+ }
8
+ declare const ExpandText: React.FC<ExpandTextProps>;
9
+ export default ExpandText;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports["default"] = void 0;
8
+ var _react = _interopRequireWildcard(require("react"));
9
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
10
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
11
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
12
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
13
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
14
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
15
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
16
+ var ExpandText = function ExpandText(_ref) {
17
+ var text = _ref.text,
18
+ maxLength = _ref.maxLength,
19
+ _ref$className = _ref.className,
20
+ className = _ref$className === void 0 ? "" : _ref$className,
21
+ _ref$truncateAtEnd = _ref.truncateAtEnd,
22
+ truncateAtEnd = _ref$truncateAtEnd === void 0 ? false : _ref$truncateAtEnd;
23
+ var _useState = (0, _react.useState)(false),
24
+ _useState2 = _slicedToArray(_useState, 2),
25
+ showFull = _useState2[0],
26
+ setShowFull = _useState2[1];
27
+ var visibleText;
28
+ if (showFull || text.length <= maxLength) {
29
+ visibleText = text;
30
+ } else {
31
+ if (truncateAtEnd) {
32
+ visibleText = "".concat(text.substring(0, maxLength), "...");
33
+ } else {
34
+ var firstHalf = text.substring(0, maxLength / 2);
35
+ var secondHalf = text.substring(text.length - maxLength / 2, text.length);
36
+ visibleText = "".concat(firstHalf, "...").concat(secondHalf);
37
+ }
38
+ }
39
+ var handleClick = function handleClick() {
40
+ setShowFull(!showFull);
41
+ };
42
+ return /*#__PURE__*/_react["default"].createElement("span", {
43
+ onClick: handleClick,
44
+ className: className
45
+ }, visibleText);
46
+ };
47
+ var _default = exports["default"] = ExpandText;
package/package.json CHANGED
@@ -1,12 +1,19 @@
1
1
  {
2
2
  "name": "react-expand-text",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "description": "A simple react component that shrinks and expands long text",
5
5
  "main": "index.js",
6
+ "types": "build/expandtext.d.ts",
7
+ "files": [
8
+ "build",
9
+ "index.js",
10
+ "LICENSE",
11
+ "README.md"
12
+ ],
6
13
  "scripts": {
7
14
  "test": "echo \"Error: no test specified\" && exit 1",
8
- "example": "webpack --config webpack-example.config.js && open example/index.html",
9
- "build": "babel src --out-dir build"
15
+ "example": "npm run build && webpack --config webpack-example.config.js && open example/index.html",
16
+ "build": "babel src --out-dir build --extensions \".ts,.tsx,.js,.jsx\" && tsc --emitDeclarationOnly"
10
17
  },
11
18
  "repository": {
12
19
  "type": "git",
@@ -22,17 +29,21 @@
22
29
  "node": ">=18.0.0"
23
30
  },
24
31
  "dependencies": {
32
+ "prop-types": "^15.8.1",
25
33
  "react": "^18.0.0",
26
- "react-dom": "^18.0.0",
27
- "prop-types": "^15.8.1"
34
+ "react-dom": "^18.0.0"
28
35
  },
29
36
  "devDependencies": {
30
37
  "@babel/cli": "^7.23.0",
31
38
  "@babel/core": "^7.23.0",
39
+ "@babel/plugin-transform-react-jsx": "^7.23.0",
32
40
  "@babel/preset-env": "^7.23.0",
33
41
  "@babel/preset-react": "^7.23.0",
34
- "@babel/plugin-transform-react-jsx": "^7.23.0",
42
+ "@babel/preset-typescript": "^7.28.5",
43
+ "@types/react": "^19.2.8",
44
+ "@types/react-dom": "^19.2.3",
35
45
  "babel-loader": "^9.1.0",
46
+ "typescript": "^5.9.3",
36
47
  "webpack": "^5.89.0",
37
48
  "webpack-cli": "^5.1.0"
38
49
  }
package/.babelrc DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "presets": ["@babel/preset-env", "@babel/preset-react"]
3
- }
package/example/App.js DELETED
@@ -1,19 +0,0 @@
1
- import React from 'react';
2
- import { createRoot } from 'react-dom/client';
3
- import ExpandText from '../src/expandtext.js';
4
-
5
- const App = () => {
6
- return (
7
- <div>
8
- <ExpandText
9
- maxLength={10}
10
- className='my-css-class'
11
- text={'I am a long string that is longer than max length'}
12
- />
13
- </div>
14
- );
15
- };
16
-
17
- const container = document.getElementById('root');
18
- const root = createRoot(container);
19
- root.render(<App />);
@@ -1,8 +0,0 @@
1
- <html>
2
- <head>
3
- </head>
4
- <body>
5
- <div id="root"></div>
6
- <script src="bundle.js"></script>
7
- </body>
8
- </html>
package/src/expandtext.js DELETED
@@ -1,29 +0,0 @@
1
- import React, { useState } from 'react';
2
- import PropTypes from 'prop-types';
3
-
4
- const ExpandText = ({ text, maxLength, className = "" }) => {
5
- const [showFull, setShowFull] = useState(false);
6
-
7
- let visibleText;
8
- if (showFull || text.length <= maxLength) {
9
- visibleText = text;
10
- } else {
11
- const firstHalf = text.substring(0, maxLength / 2);
12
- const secondHalf = text.substring(text.length - (maxLength / 2), text.length);
13
- visibleText = `${firstHalf}...${secondHalf}`;
14
- }
15
-
16
- const handleClick = () => {
17
- setShowFull(!showFull);
18
- };
19
-
20
- return <span onClick={handleClick} className={className}>{visibleText}</span>;
21
- };
22
-
23
- ExpandText.propTypes = {
24
- text: PropTypes.string.isRequired,
25
- maxLength: PropTypes.number.isRequired,
26
- className: PropTypes.string
27
- };
28
-
29
- export default ExpandText;
@@ -1,24 +0,0 @@
1
- const path = require('path');
2
-
3
- module.exports = {
4
- entry: './example/App.js',
5
- output: {
6
- filename: 'bundle.js',
7
- path: path.resolve(__dirname, 'example')
8
- },
9
- module: {
10
- rules: [
11
- {
12
- test: /\.js$/,
13
- use: {
14
- loader: 'babel-loader',
15
- options: {
16
- presets: ['@babel/preset-env', '@babel/preset-react']
17
- }
18
- },
19
- exclude: /node_modules/
20
- }
21
- ]
22
- },
23
- mode: 'development'
24
- };