@frontfactory/readmore 1.2.0 → 1.3.1

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
@@ -7,10 +7,11 @@
7
7
  Lightweight, framework-agnostic plugin to clamp text to N lines (or a fixed pixel height) with an ellipsis and a
8
8
  `Read more` / `Read less` toggle button.
9
9
 
10
- - Zero dependencies, ~1 KB
10
+ - Zero dependencies, ~1.3 KB gzipped
11
11
  - TypeScript types included
12
12
  - CSS-driven clamping via `-webkit-line-clamp` or `max-height`
13
13
  - The toggle button is only created when the text actually overflows
14
+ - Accessible by default: `aria-expanded` and `aria-controls` on the toggle button
14
15
 
15
16
  ## Installation
16
17
 
@@ -18,6 +19,9 @@ Lightweight, framework-agnostic plugin to clamp text to N lines (or a fixed pixe
18
19
  npm install @frontfactory/readmore
19
20
  ```
20
21
 
22
+ Requires Node.js `>=20.19` to build from source. The published package runs in any modern browser supporting
23
+ `ResizeObserver`.
24
+
21
25
  ## Playground
22
26
 
23
27
  Try it live on StackBlitz — no installation required:
@@ -47,55 +51,85 @@ ReadMore.init('.excerpt', {
47
51
  });
48
52
  ```
49
53
 
54
+ `ReadMore.init()` also accepts a `NodeList` or an array of elements:
55
+
56
+ ```ts
57
+ ReadMore.init(document.querySelectorAll<HTMLElement>('.excerpt'), {
58
+ lines: 3
59
+ });
60
+ ```
61
+
50
62
  Or instantiate against a single element:
51
63
 
52
64
  ```ts
53
65
  const el = document.querySelector<HTMLElement>('#bio');
54
66
 
55
67
  if (el) new ReadMore(el, {
56
- lines: 4
68
+ lines: 4,
69
+ onToggle: (expanded) => console.log('expanded:', expanded)
57
70
  });
58
71
  ```
59
72
 
60
73
  ## Options
61
74
 
62
- | Option | Type | Default | Description |
63
- |-----------------|----------|------------------|--------------------------------------------------------------------|
64
- | `lines` | `number` | `3` | Number of lines to clamp to. Ignored if `height` is set. |
65
- | `height` | `number` | — | Max height in pixels. Takes precedence over `lines` when provided. |
66
- | `moreText` | `string` | `'Read more'` | Label of the button when the text is collapsed. |
67
- | `lessText` | `string` | `'Read less'` | Label of the button when the text is expanded. |
68
- | `buttonClass` | `string` | `'readmore-btn'` | Class applied to the toggle button. |
69
- | `expandedClass` | `string` | `'is-expanded'` | Class applied to the target element while expanded. |
70
- | `openingClass` | `string` | `'is-opening'` | Transient class applied while expanding, removed after transition. |
71
- | `closingClass` | `string` | `'is-closing'` | Transient class applied while collapsing, removed after transition.|
75
+ | Option | Type | Default | Description |
76
+ |-----------------|-------------------------------|------------------|---------------------------------------------------------------------|
77
+ | `lines` | `number` | `3` | Number of lines to clamp to (min `1`). Ignored if `height` is set. |
78
+ | `height` | `number` | — | Max height in pixels (min `1`). Takes precedence over `lines`. |
79
+ | `moreText` | `string` | `'Read more'` | Label of the button when the text is collapsed. |
80
+ | `lessText` | `string` | `'Read less'` | Label of the button when the text is expanded. |
81
+ | `buttonClass` | `string` | `'readmore-btn'` | Class applied to the toggle button. |
82
+ | `expandedClass` | `string` | `'is-expanded'` | Class applied to the target element while expanded. |
83
+ | `openingClass` | `string` | `'is-opening'` | Transient class applied while expanding, removed after transition. |
84
+ | `closingClass` | `string` | `'is-closing'` | Transient class applied while collapsing, removed after transition. |
85
+ | `onToggle` | `(expanded: boolean) => void` | — | Callback fired after each toggle with the current `expanded` state. |
72
86
 
73
87
  ## API
74
88
 
75
89
  ```ts
76
90
  const instance = new ReadMore(element, options);
77
91
 
92
+ instance.el; // the target element
93
+ instance.options; // resolved options
78
94
  instance.toggle(); // expand / collapse programmatically
79
95
  instance.expanded; // boolean getter
80
- instance.destroy(); // remove button, classes and listeners
96
+ instance.destroy(); // remove button, classes, listeners and generated id
81
97
 
82
- ReadMore.init(selector, options); // returns ReadMore[]
98
+ ReadMore.init(target, options); // target: selector | NodeList | HTMLElement[] — returns ReadMore[]
99
+ ReadMore.getInstance(element); // returns the ReadMore bound to an element, or undefined
83
100
  ```
84
101
 
102
+ ### Errors
103
+
104
+ The constructor throws instead of failing silently:
105
+
106
+ | Error | When |
107
+ |--------------|-------------------------------------------------------------------------------|
108
+ | `TypeError` | `element` is not an `HTMLElement`. |
109
+ | `Error` | The element is already initialized — call `destroy()` before re-initializing. |
110
+ | `RangeError` | `lines` is lower than `1`, or `height` is lower than or equal to `0`. |
111
+
112
+ ## Accessibility
113
+
114
+ When the button is mounted, it gets `aria-expanded` (kept in sync on every toggle) and `aria-controls`
115
+ pointing to the target element. If the element has no `id`, a unique one (`readmore-1`, `readmore-2`, …)
116
+ is generated and removed again on `destroy()`; an existing `id` is always preserved.
117
+
85
118
  ## How it works
86
119
 
87
120
  The plugin sets a CSS custom property on the target element and toggles `.readmore-clamp`. In **lines mode** (default),
88
121
  it sets `--readmore-lines` and the bundled stylesheet applies `-webkit-line-clamp`, producing an ellipsis at the end of
89
122
  the last visible line. In **height mode** (`height` option), it sets `--readmore-height` and applies `max-height` via
90
123
  the `.readmore-clamp--height` modifier class instead. The toggle button is mounted right after the element only when the
91
- content actually overflows, and unmounted again on resize if the text ends up fitting.
124
+ content actually overflows; a `ResizeObserver` watches the element and unmounts the button again if the text ends up
125
+ fitting (or mounts it later if it starts overflowing).
92
126
 
93
127
  ### Transient classes for animations
94
128
 
95
129
  In addition to `expandedClass` (which stays applied for the entire expanded state), the plugin applies short-lived
96
- `openingClass` and `closingClass` markers on the target element at the moment of expand and collapse respectively.
97
- Each is added when the toggle starts and removed automatically once the CSS transition on the element ends — or on the
98
- next tick if no transition is declared. Use them as hooks to drive enter/leave animations:
130
+ `openingClass` and `closingClass` markers on the target element at the moment of expand and collapse respectively. Each
131
+ is added when the toggle starts and removed automatically once the CSS transition on the element ends — or on the next
132
+ tick if no transition is declared. Use them as hooks to drive enter/leave animations:
99
133
 
100
134
  ```css
101
135
  .is-opening {
@@ -1,25 +1,128 @@
1
+ /**
2
+ * Options accepted by {@link ReadMore}. Every field is optional.
3
+ */
1
4
  export interface ReadMoreOptions {
5
+ /**
6
+ * Number of lines to clamp to. Ignored when {@link ReadMoreOptions.height} is set.
7
+ *
8
+ * @defaultValue 3
9
+ * @throws RangeError - if lower than 1.
10
+ */
2
11
  lines?: number;
12
+ /**
13
+ * Max height in pixels. Takes precedence over {@link ReadMoreOptions.lines}
14
+ * and clamps with `max-height` instead of `-webkit-line-clamp`.
15
+ *
16
+ * @throws RangeError - if lower than or equal to 0.
17
+ */
3
18
  height?: number;
19
+ /**
20
+ * Label of the toggle button while the text is collapsed.
21
+ *
22
+ * @defaultValue 'Read more'
23
+ */
4
24
  moreText?: string;
25
+ /**
26
+ * Label of the toggle button while the text is expanded.
27
+ *
28
+ * @defaultValue 'Read less'
29
+ */
5
30
  lessText?: string;
31
+ /**
32
+ * Class applied to the toggle button.
33
+ *
34
+ * @defaultValue 'readmore-btn'
35
+ */
6
36
  buttonClass?: string;
37
+ /**
38
+ * Class applied to the target element for the whole expanded state.
39
+ *
40
+ * @defaultValue 'is-expanded'
41
+ */
7
42
  expandedClass?: string;
43
+ /**
44
+ * Transient class applied when expanding, removed once the CSS transition
45
+ * on the element ends — or on the next tick if no transition is declared.
46
+ *
47
+ * @defaultValue 'is-opening'
48
+ */
8
49
  openingClass?: string;
50
+ /**
51
+ * Transient class applied when collapsing, removed once the CSS transition
52
+ * on the element ends — or on the next tick if no transition is declared.
53
+ *
54
+ * @defaultValue 'is-closing'
55
+ */
9
56
  closingClass?: string;
57
+ /**
58
+ * Called after each toggle with the resulting state.
59
+ *
60
+ * @param expanded - `true` once expanded, `false` once collapsed.
61
+ */
62
+ onToggle?: (expanded: boolean) => void;
10
63
  }
11
- type ResolvedOptions = Required<Omit<ReadMoreOptions, 'height'>> & {
64
+ /**
65
+ * {@link ReadMoreOptions} merged with the defaults, as exposed on
66
+ * {@link ReadMore.options}.
67
+ */
68
+ export type ResolvedOptions = Required<Omit<ReadMoreOptions, 'height' | 'onToggle'>> & {
12
69
  height?: number;
70
+ onToggle?: (expanded: boolean) => void;
13
71
  };
72
+ /**
73
+ * Clamps an element to a number of lines (or a fixed height) and mounts a
74
+ * `Read more` / `Read less` button right after it, but only while the content
75
+ * actually overflows.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * import { ReadMore } from '@frontfactory/readmore';
80
+ * import '@frontfactory/readmore/style.css';
81
+ *
82
+ * ReadMore.init('.excerpt', { lines: 3 });
83
+ * ```
84
+ */
14
85
  export declare class ReadMore {
15
86
  #private;
87
+ /** The clamped element. */
16
88
  readonly el: HTMLElement;
89
+ /** The options this instance was built with, merged with the defaults. */
17
90
  readonly options: ResolvedOptions;
91
+ /**
92
+ * @param element - The element to clamp.
93
+ * @param options - See {@link ReadMoreOptions}.
94
+ * @throws TypeError - if `element` is not an `HTMLElement`.
95
+ * @throws Error - if `element` is already initialized; call
96
+ * {@link ReadMore.destroy} first.
97
+ * @throws RangeError - if `lines` is lower than 1, or `height` lower than
98
+ * or equal to 0.
99
+ */
18
100
  constructor(element: HTMLElement, options?: ReadMoreOptions);
101
+ /**
102
+ * Instantiates {@link ReadMore} on every matching element.
103
+ *
104
+ * @param target - A CSS selector, a `NodeList` or an array of elements.
105
+ * @param options - See {@link ReadMoreOptions}.
106
+ * @returns One instance per element, in document order.
107
+ */
19
108
  static init(target: string | NodeListOf<HTMLElement> | HTMLElement[], options?: ReadMoreOptions): ReadMore[];
109
+ /** Whether the text is currently expanded. */
20
110
  get expanded(): boolean;
111
+ /**
112
+ * Expands or collapses the text, updating the button label, the
113
+ * `aria-expanded` attribute and the state classes, then calls
114
+ * {@link ReadMoreOptions.onToggle}.
115
+ */
21
116
  toggle(): void;
117
+ /**
118
+ * @param element - An element possibly initialized by this plugin.
119
+ * @returns The instance bound to it, or `undefined`.
120
+ */
22
121
  static getInstance(element: HTMLElement): ReadMore | undefined;
122
+ /**
123
+ * Restores the element: removes the button, the classes, the CSS custom
124
+ * properties, the listeners and the generated `id`. The element can then be
125
+ * initialized again.
126
+ */
23
127
  destroy(): void;
24
128
  }
25
- export {};
package/dist/readmore.js CHANGED
@@ -19,10 +19,12 @@ var e = "readmore-clamp", t = "readmore-clamp--height", n = /* @__PURE__ */ new
19
19
  constructor(e, t = {}) {
20
20
  if (!(e instanceof HTMLElement)) throw TypeError("ReadMore: an HTMLElement is required.");
21
21
  if (n.has(e)) throw Error("ReadMore: element is already initialized. Call destroy() first.");
22
- this.el = e, this.options = {
22
+ if (this.el = e, this.options = {
23
23
  ...i,
24
24
  ...t
25
- }, this.#r = new ResizeObserver(() => this.#c()), n.set(e, this), this.#o();
25
+ }, this.options.height != null && this.options.height <= 0) throw RangeError("ReadMore: height must be greater than 0.");
26
+ if (this.options.lines < 1) throw RangeError("ReadMore: lines must be at least 1.");
27
+ this.#r = new ResizeObserver(() => this.#c()), n.set(e, this), this.#o();
26
28
  }
27
29
  static init(e, t) {
28
30
  let n = typeof e == "string" ? document.querySelectorAll(e) : e;
@@ -34,7 +36,7 @@ var e = "readmore-clamp", t = "readmore-clamp--height", n = /* @__PURE__ */ new
34
36
  toggle() {
35
37
  this.#t = !this.#t, this.el.classList.toggle(e, !this.#t), this.options.height != null && this.el.classList.toggle(t, !this.#t), this.el.classList.toggle(this.options.expandedClass, this.#t), this.#e && (this.#e.textContent = this.#t ? this.options.lessText : this.options.moreText, this.#e.setAttribute("aria-expanded", String(this.#t)));
36
38
  let n = this.#t ? this.options.openingClass : this.options.closingClass, r = this.#t ? this.options.closingClass : this.options.openingClass;
37
- this.el.classList.remove(r), this.#l(n), this.#t || this.#c();
39
+ this.el.classList.remove(r), this.#l(n), this.#t || this.#c(), this.options.onToggle?.(this.#t);
38
40
  }
39
41
  static getInstance(e) {
40
42
  return n.get(e);
package/package.json CHANGED
@@ -1,28 +1,31 @@
1
1
  {
2
2
  "name": "@frontfactory/readmore",
3
- "version": "1.2.0",
4
- "description": "Lightweight plugin to clamp text to N lines with ellipsis and a read more action.",
3
+ "version": "1.3.1",
4
+ "description": "Zero-dependency plugin to truncate text to N lines or a fixed height, with an ellipsis and an accessible read more / read less toggle.",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",
8
+ "lint:js": "eslint --fix .",
8
9
  "build": "vite build",
9
10
  "preview": "vite preview",
10
11
  "typecheck": "tsc --noEmit",
11
12
  "test": "vitest run",
12
13
  "test:watch": "vitest",
13
14
  "test:coverage": "vitest run --coverage",
14
- "prepare": "husky && npm run build",
15
+ "prepare": "husky",
16
+ "prepack": "npm run build",
15
17
  "release": "semantic-release"
16
18
  },
17
19
  "engines": {
18
- "node": ">=20.19"
20
+ "node": ">=20"
19
21
  },
20
22
  "main": "./dist/readmore.js",
21
23
  "types": "./dist/readmore.d.ts",
22
24
  "exports": {
23
25
  ".": {
24
26
  "types": "./dist/readmore.d.ts",
25
- "import": "./dist/readmore.js"
27
+ "import": "./dist/readmore.js",
28
+ "default": "./dist/readmore.js"
26
29
  },
27
30
  "./style.css": "./dist/readmore.css",
28
31
  "./package.json": "./package.json"
@@ -30,30 +33,40 @@
30
33
  "devDependencies": {
31
34
  "@commitlint/cli": "^20.5.3",
32
35
  "@commitlint/config-conventional": "^20.5.3",
36
+ "@front-factory/eslint-config": "^2.4.2",
33
37
  "@semantic-release/changelog": "^6.0.3",
34
38
  "@semantic-release/git": "^10.0.1",
35
39
  "@semantic-release/github": "^12.0.6",
36
40
  "@vitest/coverage-v8": "^4.1.5",
37
41
  "conventional-changelog-conventionalcommits": "^9.3.1",
42
+ "eslint": "^10.3.0",
38
43
  "husky": "^9.1.7",
39
44
  "jsdom": "^29.1.1",
40
45
  "lightningcss": "^1.32.0",
41
46
  "semantic-release": "^25.0.3",
42
47
  "typescript": "^5.4.0",
48
+ "typescript-eslint": "^8.59.2",
43
49
  "vite": "^8.0.11",
44
50
  "vite-plugin-dts": "^5.0.0",
45
51
  "vite-plugin-static-copy": "^4.1.0",
46
52
  "vitest": "^4.1.5"
47
53
  },
48
- "files": [
49
- "dist"
50
- ],
51
54
  "keywords": [
52
55
  "readmore",
56
+ "read-more",
57
+ "show-more",
58
+ "truncate",
59
+ "text-truncate",
60
+ "line-clamp",
61
+ "clamp",
62
+ "ellipsis",
53
63
  "expand",
54
64
  "collapse",
55
- "front-end"
65
+ "typescript",
66
+ "vanilla-js",
67
+ "a11y"
56
68
  ],
69
+ "author": "Front Factory",
57
70
  "license": "MIT",
58
71
  "homepage": "https://github.com/front-factory/readmore",
59
72
  "bugs": {
@@ -63,6 +76,12 @@
63
76
  "type": "git",
64
77
  "url": "git+https://github.com/front-factory/readmore.git"
65
78
  },
79
+ "sideEffects": [
80
+ "**/*.css"
81
+ ],
82
+ "files": [
83
+ "dist"
84
+ ],
66
85
  "publishConfig": {
67
86
  "access": "public",
68
87
  "provenance": true