@frontfactory/readmore 1.5.1 → 1.7.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 +56 -23
- package/dist/readmore.d.ts +42 -6
- package/dist/readmore.iife.js +1 -1
- package/dist/readmore.iife.js.map +1 -1
- package/dist/readmore.js +64 -16
- package/dist/readmore.js.map +1 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
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
|
|
10
|
+
- Zero dependencies
|
|
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
|
|
@@ -102,6 +102,18 @@ document.addEventListener('readmore:toggle', (event) => {
|
|
|
102
102
|
});
|
|
103
103
|
```
|
|
104
104
|
|
|
105
|
+
To veto a toggle, return `false` from the `onBeforeToggle` option, or call `preventDefault()` on the cancelable
|
|
106
|
+
`readmore:beforetoggle` event. Both receive the state about to be entered (`expanded`); the callback runs first, and the
|
|
107
|
+
event is not dispatched if it returned `false`. A cancelled toggle changes nothing and fires neither `onToggle` nor
|
|
108
|
+
`readmore:toggle`. If `onBeforeToggle` throws, the error propagates and the toggle does not happen. It only runs for
|
|
109
|
+
toggles that would really happen (no-ops are skipped), including the automatic expansion on focus.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
document.addEventListener('readmore:beforetoggle', (event) => {
|
|
113
|
+
if (event.detail.expanded && !userMayExpand) event.preventDefault();
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
105
117
|
Pass `true` or `false` to `toggle()` to force a state, e.g. for an "Expand all" button. Instances already in that
|
|
106
118
|
state are left untouched and fire neither `onToggle` nor the event:
|
|
107
119
|
|
|
@@ -145,20 +157,23 @@ return (
|
|
|
145
157
|
|
|
146
158
|
## Options
|
|
147
159
|
|
|
148
|
-
| Option
|
|
149
|
-
|
|
150
|
-
| `lines`
|
|
151
|
-
| `height`
|
|
152
|
-
| `
|
|
153
|
-
| `
|
|
154
|
-
| `
|
|
155
|
-
| `
|
|
156
|
-
| `
|
|
157
|
-
| `
|
|
158
|
-
| `
|
|
159
|
-
| `
|
|
160
|
-
| `
|
|
161
|
-
| `
|
|
160
|
+
| Option | Type | Default | Description |
|
|
161
|
+
|--------------------|--------------------------------------------------------------|------------------|----------------------------------------------------------------------------------------------------------------|
|
|
162
|
+
| `lines` | `number \| (el) => number` | `3` | Number of lines to clamp to (min `1`). Ignored if `height` is set. A function is called with the element. |
|
|
163
|
+
| `height` | `number \| (el) => number` | — | Max height in pixels (greater than `0`). Takes precedence over `lines`. A function is called with the element. |
|
|
164
|
+
| `threshold` | `number` | `0` | Hidden overflow in pixels tolerated before the button is mounted (min `0`; at least 1px is always required). |
|
|
165
|
+
| `moreText` | `string` | `'Read more'` | Label of the button when the text is collapsed. |
|
|
166
|
+
| `lessText` | `string` | `'Read less'` | Label of the button when the text is expanded. |
|
|
167
|
+
| `moreLabel` | `string` | — | `aria-label` of the button when collapsed. Should contain `moreText`. |
|
|
168
|
+
| `lessLabel` | `string` | — | `aria-label` of the button when expanded. Should contain `lessText`. |
|
|
169
|
+
| `buttonClass` | `string` | `'readmore-btn'` | Class applied to the toggle button. |
|
|
170
|
+
| `buttonContainer` | `HTMLElement` | — | Element the button is appended to, instead of being inserted after the text. |
|
|
171
|
+
| `expandedClass` | `string` | `'is-expanded'` | Class applied to the target element while expanded. |
|
|
172
|
+
| `openingClass` | `string` | `'is-opening'` | Transient class applied while expanding, removed after transition. |
|
|
173
|
+
| `closingClass` | `string` | `'is-closing'` | Transient class applied while collapsing, removed after transition. |
|
|
174
|
+
| `scrollOnCollapse` | `boolean` | `false` | On collapse, scroll the element back into view if its top ended up above the viewport. |
|
|
175
|
+
| `onBeforeToggle` | `(expanded: boolean, instance: ReadMore) => boolean \| void` | — | Called before a toggle with the state about to be entered; returning `false` cancels it. |
|
|
176
|
+
| `onToggle` | `(expanded: boolean, instance: ReadMore) => void` | — | Callback fired after each toggle, before the `readmore:toggle` event (still dispatched if it throws). |
|
|
162
177
|
|
|
163
178
|
For `expandedClass`, `openingClass` and `closingClass`, an empty string falls back to the default.
|
|
164
179
|
|
|
@@ -185,12 +200,12 @@ ReadMore.getInstance(element); // returns the ReadMore bound to an eleme
|
|
|
185
200
|
|
|
186
201
|
The constructor throws instead of failing silently:
|
|
187
202
|
|
|
188
|
-
| Error | When
|
|
189
|
-
|
|
190
|
-
| `TypeError` | `element` is not an `HTMLElement`.
|
|
191
|
-
| `Error` | The element is already initialized — call `destroy()` before re-initializing.
|
|
192
|
-
| `RangeError` | `lines` is not an integer `>= 1`,
|
|
193
|
-
| `RangeError` | `expandedClass`, `openingClass` or `closingClass` is not a string or contains whitespace.
|
|
203
|
+
| Error | When |
|
|
204
|
+
|--------------|------------------------------------------------------------------------------------------------------------------------|
|
|
205
|
+
| `TypeError` | `element` is not an `HTMLElement`. |
|
|
206
|
+
| `Error` | The element is already initialized — call `destroy()` before re-initializing. |
|
|
207
|
+
| `RangeError` | `lines` is not an integer `>= 1`, `height` is not a finite number `> 0`, or `threshold` is not a finite number `>= 0`. |
|
|
208
|
+
| `RangeError` | `expandedClass`, `openingClass` or `closingClass` is not a string or contains whitespace. |
|
|
194
209
|
|
|
195
210
|
A `buttonContainer` that is not an `HTMLElement` also throws a `TypeError`.
|
|
196
211
|
|
|
@@ -199,9 +214,23 @@ A `buttonContainer` that is not an `HTMLElement` also throws a `TypeError`.
|
|
|
199
214
|
Importing the module is safe without a browser. There, `ReadMore.init()` is a no-op that returns `[]`, and the
|
|
200
215
|
constructor throws a `TypeError` (no `HTMLElement`).
|
|
201
216
|
|
|
202
|
-
`lines` and `
|
|
217
|
+
`lines`, `height` and `threshold` also accept numeric strings such as `'3'` (e.g. read from a `data-*` attribute); blank or
|
|
203
218
|
non-numeric strings throw a `RangeError`.
|
|
204
219
|
|
|
220
|
+
`lines` and `height` can also be a function, called with the element, whose returned value is validated like a literal
|
|
221
|
+
one (a `RangeError` if invalid). It runs at construction and on each `refresh()`, not on every resize: call
|
|
222
|
+
`refresh()` from your own breakpoint listener to re-evaluate it.
|
|
223
|
+
|
|
224
|
+
```js
|
|
225
|
+
ReadMore.init('.excerpt', {
|
|
226
|
+
lines: (el) => (window.innerWidth < 600 ? 2 : Number(el.dataset.lines) || 4)
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
matchMedia('(max-width: 600px)').addEventListener('change', () => {
|
|
230
|
+
document.querySelectorAll('.excerpt').forEach((el) => ReadMore.getInstance(el)?.refresh());
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
205
234
|
## Accessibility
|
|
206
235
|
|
|
207
236
|
When the button is mounted, it gets `aria-expanded` (kept in sync on every toggle) and `aria-controls`
|
|
@@ -223,7 +252,8 @@ new ReadMore(el, {
|
|
|
223
252
|
The plugin sets a CSS custom property on the target element and toggles `.readmore-clamp`. In **lines mode** (default),
|
|
224
253
|
it sets `--readmore-lines` and the bundled stylesheet applies `-webkit-line-clamp`, producing an ellipsis at the end of
|
|
225
254
|
the last visible line. In **height mode** (`height` option), it sets `--readmore-height` and applies `max-height` via
|
|
226
|
-
the `.readmore-clamp--height` modifier class instead.
|
|
255
|
+
the `.readmore-clamp--height` modifier class instead. When expanding in height mode, it also sets `--readmore-full-height`
|
|
256
|
+
(the full content height in `px`), handy as the `max-height` target of an animation. The toggle button is mounted (right after the element, or inside
|
|
227
257
|
`buttonContainer` when set) only when the content actually overflows; a single `ResizeObserver`, shared by every instance, watches the elements and unmounts the
|
|
228
258
|
button again if the text ends up fitting (or mounts it later if it starts overflowing). All resized elements are
|
|
229
259
|
measured before any button is added or removed, to avoid a layout per element.
|
|
@@ -278,3 +308,6 @@ the `--readmore-fade-size` custom property:
|
|
|
278
308
|
For rich content, prefer height mode.
|
|
279
309
|
- Lines mode cannot be animated: neither `display` nor `line-clamp` can transition. For an animated expand/collapse, use
|
|
280
310
|
height mode with a `max-height` transition.
|
|
311
|
+
- In lines mode on Safari/WebKit, `-webkit-line-clamp` places hidden inline elements inside the visible area, so the
|
|
312
|
+
automatic expansion on focus does not trigger. It works in height mode on all browsers, and in lines mode on Chromium
|
|
313
|
+
and Firefox. For focusable content (links, buttons) in clamped text, prefer height mode.
|
package/dist/readmore.d.ts
CHANGED
|
@@ -6,16 +6,29 @@ export interface ReadMoreOptions {
|
|
|
6
6
|
* Number of lines to clamp to. Ignored when {@link ReadMoreOptions.height} is set.
|
|
7
7
|
*
|
|
8
8
|
* @defaultValue 3
|
|
9
|
+
* Can also be a function called with the element; the returned value is
|
|
10
|
+
* validated like a literal one and re-evaluated on {@link ReadMore.refresh}.
|
|
11
|
+
*
|
|
9
12
|
* @throws RangeError - if not an integer greater than or equal to 1.
|
|
10
13
|
*/
|
|
11
|
-
lines?: number;
|
|
14
|
+
lines?: number | ((el: HTMLElement) => number);
|
|
12
15
|
/**
|
|
13
16
|
* Max height in pixels. Takes precedence over {@link ReadMoreOptions.lines}
|
|
14
17
|
* and clamps with `max-height` instead of `-webkit-line-clamp`.
|
|
18
|
+
* Can also be a function called with the element; the returned value is
|
|
19
|
+
* validated like a literal one and re-evaluated on {@link ReadMore.refresh}.
|
|
15
20
|
*
|
|
16
21
|
* @throws RangeError - if not a finite number greater than 0.
|
|
17
22
|
*/
|
|
18
|
-
height?: number;
|
|
23
|
+
height?: number | ((el: HTMLElement) => number);
|
|
24
|
+
/**
|
|
25
|
+
* Hidden overflow in pixels tolerated before the button is mounted: the
|
|
26
|
+
* button only appears when more than this is hidden (at least 1px).
|
|
27
|
+
*
|
|
28
|
+
* @defaultValue 0
|
|
29
|
+
* @throws RangeError - if not a finite number greater than or equal to 0.
|
|
30
|
+
*/
|
|
31
|
+
threshold?: number;
|
|
19
32
|
/**
|
|
20
33
|
* Label of the toggle button while the text is collapsed.
|
|
21
34
|
*
|
|
@@ -75,6 +88,13 @@ export interface ReadMoreOptions {
|
|
|
75
88
|
* @defaultValue 'is-closing'
|
|
76
89
|
*/
|
|
77
90
|
closingClass?: string;
|
|
91
|
+
/**
|
|
92
|
+
* Scrolls the element back into view when collapsing, if its top edge ended
|
|
93
|
+
* up above the viewport.
|
|
94
|
+
*
|
|
95
|
+
* @defaultValue false
|
|
96
|
+
*/
|
|
97
|
+
scrollOnCollapse?: boolean;
|
|
78
98
|
/**
|
|
79
99
|
* Called after each toggle with the resulting state, right before the
|
|
80
100
|
* `readmore:toggle` event is dispatched.
|
|
@@ -83,8 +103,18 @@ export interface ReadMoreOptions {
|
|
|
83
103
|
* @param instance - The instance that was toggled.
|
|
84
104
|
*/
|
|
85
105
|
onToggle?: (expanded: boolean, instance: ReadMore) => void;
|
|
106
|
+
/**
|
|
107
|
+
* Called before a toggle that would really happen, with the state it is
|
|
108
|
+
* about to enter. Returning exactly `false` cancels it (the
|
|
109
|
+
* `readmore:beforetoggle` event is then not dispatched). If it throws, the
|
|
110
|
+
* error propagates and the toggle does not happen.
|
|
111
|
+
*
|
|
112
|
+
* @param expanded - `true` if about to expand, `false` if about to collapse.
|
|
113
|
+
* @param instance - The instance about to be toggled.
|
|
114
|
+
*/
|
|
115
|
+
onBeforeToggle?: (expanded: boolean, instance: ReadMore) => boolean | void;
|
|
86
116
|
}
|
|
87
|
-
type OptionalKeys = 'height' | 'buttonContainer' | 'moreLabel' | 'lessLabel' | 'onToggle';
|
|
117
|
+
type OptionalKeys = 'height' | 'buttonContainer' | 'moreLabel' | 'lessLabel' | 'onToggle' | 'onBeforeToggle';
|
|
88
118
|
/**
|
|
89
119
|
* {@link ReadMoreOptions} merged with the defaults, as exposed on
|
|
90
120
|
* {@link ReadMore.options}.
|
|
@@ -100,15 +130,18 @@ export interface ReadMoreToggleDetail {
|
|
|
100
130
|
declare global {
|
|
101
131
|
interface HTMLElementEventMap {
|
|
102
132
|
'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;
|
|
133
|
+
'readmore:beforetoggle': CustomEvent<ReadMoreToggleDetail>;
|
|
103
134
|
}
|
|
104
135
|
interface DocumentEventMap {
|
|
105
136
|
'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;
|
|
137
|
+
'readmore:beforetoggle': CustomEvent<ReadMoreToggleDetail>;
|
|
106
138
|
}
|
|
107
139
|
}
|
|
108
140
|
/**
|
|
109
141
|
* Clamps an element to a number of lines (or a fixed height) and mounts a
|
|
110
142
|
* `Read more` / `Read less` button right after it, but only while the content
|
|
111
|
-
* actually overflows.
|
|
143
|
+
* actually overflows. Focus landing on an element in the hidden part expands
|
|
144
|
+
* the text.
|
|
112
145
|
*
|
|
113
146
|
* @example
|
|
114
147
|
* ```ts
|
|
@@ -132,7 +165,9 @@ export declare class ReadMore {
|
|
|
132
165
|
* @throws Error - if `element` is already initialized; call
|
|
133
166
|
* {@link ReadMore.destroy} first.
|
|
134
167
|
* @throws RangeError - if `lines` is not an integer greater than or equal
|
|
135
|
-
* to 1,
|
|
168
|
+
* to 1, `height` is not a finite number greater than 0, or `threshold` is
|
|
169
|
+
* not a finite number greater than or equal to 0 (for `lines` and `height`,
|
|
170
|
+
* the value returned by a function is checked).
|
|
136
171
|
* @throws RangeError - if `expandedClass`, `openingClass` or `closingClass`
|
|
137
172
|
* is not a string or contains whitespace (an empty string means the default).
|
|
138
173
|
* @throws TypeError - if `buttonContainer` is set but is not an `HTMLElement`.
|
|
@@ -162,7 +197,8 @@ export declare class ReadMore {
|
|
|
162
197
|
*
|
|
163
198
|
* @param force - `true` to expand, `false` to collapse; the state is
|
|
164
199
|
* inverted when omitted. Does nothing if the text is already in the
|
|
165
|
-
* requested state.
|
|
200
|
+
* requested state. Before a real toggle, {@link ReadMoreOptions.onBeforeToggle}
|
|
201
|
+
* and a cancelable `readmore:beforetoggle` event can cancel it.
|
|
166
202
|
*/
|
|
167
203
|
toggle(force?: boolean): void;
|
|
168
204
|
/**
|
package/dist/readmore.iife.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=`readmore-clamp`,n=`readmore-clamp--height`,r=/* @__PURE__ */ new WeakMap,i=0,a={lines:3,moreText:`Read more`,lessText:`Read less`,buttonClass:`readmore-btn`,expandedClass:`is-expanded`,openingClass:`is-opening`,closingClass:`is-closing
|
|
1
|
+
(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=`readmore-clamp`,n=`readmore-clamp--height`,r=/* @__PURE__ */ new WeakMap,i=0,a={lines:3,threshold:0,moreText:`Read more`,lessText:`Read less`,buttonClass:`readmore-btn`,expandedClass:`is-expanded`,openingClass:`is-opening`,closingClass:`is-closing`,scrollOnCollapse:!1};function o(e){return e.split(`,`).map(e=>parseFloat(e)*1e3||0)}e.ReadMore=class e{static#e;static#t=null;static#n=null;static#r=/* @__PURE__ */ new Set;static#i;el;options;#a=null;#o=!1;#s=!1;#c=null;#l=()=>this.toggle();#u=a.lines;#d;#f=e=>{let{target:t}=e;this.#o||!this.#a||t===this.el||!(t instanceof Element)||t.getBoundingClientRect().bottom+this.el.scrollTop>this.el.getBoundingClientRect().bottom+1&&this.toggle(!0)};get#p(){return r.get(this.el)!==this}constructor(e,t={}){if(typeof HTMLElement>`u`||!(e instanceof HTMLElement))throw TypeError(`ReadMore: an HTMLElement is required.`);if(r.has(e))throw Error(`ReadMore: element is already initialized. Call destroy() first.`);this.el=e,this.options={...a,...Object.fromEntries(Object.entries(t).filter(e=>e[1]!==void 0&&!(e[1]===``&&e[0].endsWith(`Class`))))};for(let e of[`height`,`lines`,`threshold`]){let t=this.options[e];typeof t==`string`&&t.trim()!==``&&(this.options[e]=Number(t))}let{threshold:n,buttonContainer:i}=this.options;if(i!=null&&!(i instanceof HTMLElement))throw TypeError(`ReadMore: buttonContainer must be an HTMLElement.`);if(this.#m(),!(Number.isFinite(n)&&n>=0))throw RangeError(`ReadMore: threshold must be a finite number of at least 0.`);for(let e of[`expandedClass`,`openingClass`,`closingClass`]){let t=this.options[e];if(typeof t!=`string`||!/^\S+$/.test(t))throw RangeError(`ReadMore: ${e} must be a string without whitespace.`)}r.set(e,this),this.#y()}static init(t,n,i){if(typeof HTMLElement>`u`||typeof document>`u`)return[];let a=typeof t==`string`?(i??document).querySelectorAll(t):t instanceof HTMLElement?[t]:t,o=[];e.#t=o;try{return Array.from(a).map(t=>r.get(t)??new e(t,n))}finally{e.#t=null,e.#v(o)}}get expanded(){return this.#o}toggle(e){let r=typeof e==`boolean`?e:!this.#o;if(this.#p||r===this.#o||this.options.onBeforeToggle?.(r,this)===!1)return;let i=new CustomEvent(`readmore:beforetoggle`,{bubbles:!0,cancelable:!0,detail:{expanded:r,instance:this}});if(!this.el.dispatchEvent(i))return;r&&this.options.height!=null&&this.el.style.setProperty(`--readmore-full-height`,`${this.el.scrollHeight}px`),this.#o=r,this.el.classList.toggle(t,!this.#o),this.options.height!=null&&this.el.classList.toggle(n,!this.#o),this.el.classList.toggle(this.options.expandedClass,this.#o),this.#E();let a=this.#o?this.options.openingClass:this.options.closingClass,o=this.#o?this.options.closingClass:this.options.openingClass;this.el.classList.remove(o),this.#C(a),!this.#o&&this.options.scrollOnCollapse&&this.el.getBoundingClientRect().top<0&&this.el.scrollIntoView?.({block:`nearest`});try{this.options.onToggle?.(this.#o,this)}finally{this.el.dispatchEvent(new CustomEvent(`readmore:toggle`,{bubbles:!0,detail:{expanded:this.#o,instance:this}}))}}refresh(){this.#p||(this.#m(),this.#h()),this.#S(this.#x())}#m(){let e=e=>{let t=typeof e==`function`?e(this.el):e;return typeof t==`string`&&t.trim()!==``?Number(t):t},t=e(this.options.height),n=e(this.options.lines);if(t!=null&&!(Number.isFinite(t)&&t>0))throw RangeError(`ReadMore: height must be a finite number greater than 0.`);if(!(Number.isInteger(n)&&n>=1))throw RangeError(`ReadMore: lines must be an integer of at least 1.`);this.#d=t,this.#u=n}#h(){this.options.height==null?this.el.style.setProperty(`--readmore-lines`,String(this.#u)):this.el.style.setProperty(`--readmore-height`,`${this.#d}px`)}static getInstance(e){return r.get(e)}destroy(){this.#p||(r.delete(this.el),this.el.removeEventListener(`focusin`,this.#f),e.#e?.unobserve(this.el),e.#r.delete(this)&&e.#r.size===0&&(e.#i?.removeEventListener?.(`loadingdone`,e.#_),e.#i=void 0),this.#c!==null&&(clearTimeout(this.#c),this.#c=null),this.#D(),this.el.classList.remove(t,n,this.options.expandedClass,this.options.openingClass,this.options.closingClass),this.el.style.removeProperty(`--readmore-lines`),this.el.style.removeProperty(`--readmore-height`),this.el.style.removeProperty(`--readmore-full-height`),this.#s&&=(this.el.removeAttribute(`id`),!1))}static#g(t){e.#v(t.map(e=>r.get(e.target)))}static#_=()=>{e.#v(Array.from(e.#r))};static#v(e){let t=/* @__PURE__ */ new Map;for(let n of e)n&&t.set(n,n.#x());t.forEach((e,t)=>t.#S(e))}#y(){this.#h(),this.el.classList.add(t),this.options.height!=null&&this.el.classList.add(n),this.el.addEventListener(`focusin`,this.#f),e.#e??=new ResizeObserver(e.#g),e.#e.observe(this.el),e.#t?e.#t.push(this):this.refresh(),this.#b()}#b(){if(typeof document>`u`||!document.fonts)return;if(e.#i!==document.fonts&&(e.#i?.removeEventListener?.(`loadingdone`,e.#_),e.#i=document.fonts,e.#i.addEventListener?.(`loadingdone`,e.#_)),e.#r.add(this),e.#n){e.#n.push(this);return}let t=e.#n=[this];document.fonts.ready.then(()=>{e.#n=null,e.#v(t.filter(e=>!e.#p))})}#x(){return this.el.scrollHeight-this.el.clientHeight>Math.max(1,this.options.threshold)}#S(e){this.#o||this.#c!==null||this.#p||(e&&!this.#a?this.#T():!e&&this.#a&&this.#D())}#C(e){this.#c!==null&&clearTimeout(this.#c),this.el.classList.add(e);let t=setTimeout(()=>{let n=this.el.getAnimations?.().filter(e=>e instanceof CSSTransition)??[],r=()=>{this.#c===t&&(this.#c=null,this.el.classList.remove(e),this.refresh())};n.length?Promise.allSettled(n.map(e=>e.finished)).then(r):r()},this.#w());this.#c=t}#w(){let e=getComputedStyle(this.el),t=o(e.transitionDuration),n=o(e.transitionDelay),r=0;for(let e=0;e<Math.max(t.length,n.length);e++)r=Math.max(r,t[e%t.length]+n[e%n.length]);return r}#T(){if(!this.el.id){let e;do e=`readmore-${++i}`;while(document.getElementById(e));this.el.id=e,this.#s=!0}let e=document.createElement(`button`);e.type=`button`,e.className=this.options.buttonClass,e.setAttribute(`aria-controls`,this.el.id),e.addEventListener(`click`,this.#l),this.#a=e,this.#E(),this.options.buttonContainer?this.options.buttonContainer.appendChild(e):this.el.insertAdjacentElement(`afterend`,e)}#E(){if(!this.#a)return;let{moreText:e,lessText:t,moreLabel:n,lessLabel:r}=this.options,i=this.#o?r:n;this.#a.textContent=this.#o?t:e,this.#a.setAttribute(`aria-expanded`,String(this.#o)),i?this.#a.setAttribute(`aria-label`,i):this.#a.removeAttribute(`aria-label`)}#D(){if(this.#a){if(this.#a.removeEventListener(`click`,this.#l),document.activeElement===this.#a){let{el:e}=this;e.hasAttribute(`tabindex`)||(e.tabIndex=-1,e.addEventListener(`blur`,()=>e.removeAttribute(`tabindex`),{once:!0})),e.focus()}this.#a.remove(),this.#a=null}}}})(this.FrontFactory=this.FrontFactory||{});
|
|
2
2
|
//# sourceMappingURL=readmore.iife.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readmore.iife.js","names":["exports","CLAMP_CLASS","CLAMP_HEIGHT_CLASS","instances","idCounter","DEFAULTS","toMs","list","v","ReadMore","#resizeObserver","#batch","#fontQueue","#live","#fonts","#destroyed","element","options","entry","key","value","buttonContainer","height","lines","#init","nodes","target","root","batch","n","#syncAll","#expanded","expanded","force","#updateButton","stateClass","otherClass","#applyTransientClass","#sync","#isOverflowing","#onFontsLoaded","#transientTimer","#unmountButton","#generatedId","#onResize","entries","measured","instance","overflowing","#refreshOnFonts","queue","#button","#mountButton","timer","running","a","done","#transitionTime","style","durations","delays","max","i","id","btn","#onClick","label","lessLabel","moreLabel","lessText","moreText","el"],"sources":["../src/readmore.ts"],"sourcesContent":["/**\n * Options accepted by {@link ReadMore}. Every field is optional.\n */\nexport interface ReadMoreOptions {\n /**\n * Number of lines to clamp to. Ignored when {@link ReadMoreOptions.height} is set.\n *\n * @defaultValue 3\n * @throws RangeError - if not an integer greater than or equal to 1.\n */\n lines?: number;\n\n /**\n * Max height in pixels. Takes precedence over {@link ReadMoreOptions.lines}\n * and clamps with `max-height` instead of `-webkit-line-clamp`.\n *\n * @throws RangeError - if not a finite number greater than 0.\n */\n height?: number;\n\n /**\n * Label of the toggle button while the text is collapsed.\n *\n * @defaultValue 'Read more'\n */\n moreText?: string;\n\n /**\n * Label of the toggle button while the text is expanded.\n *\n * @defaultValue 'Read less'\n */\n lessText?: string;\n\n /**\n * Accessible name (`aria-label`) of the toggle button while the text is\n * collapsed, e.g. `'Read more about Product A'` when several buttons share\n * the same visible text. It should contain {@link ReadMoreOptions.moreText}.\n */\n moreLabel?: string;\n\n /**\n * Accessible name (`aria-label`) of the toggle button while the text is\n * expanded. It should contain {@link ReadMoreOptions.lessText}.\n */\n lessLabel?: string;\n\n /**\n * Class applied to the toggle button.\n *\n * @defaultValue 'readmore-btn'\n */\n buttonClass?: string;\n\n /**\n * Element the toggle button is appended to, instead of being inserted right\n * after the clamped element. Use an element owned by your framework (a ref)\n * so reconciliation cannot drop the button.\n *\n * @throws TypeError - if not an `HTMLElement`.\n */\n buttonContainer?: HTMLElement;\n\n /**\n * Class applied to the target element for the whole expanded state.\n *\n * @defaultValue 'is-expanded'\n */\n expandedClass?: string;\n\n /**\n * Transient class applied when expanding, removed once the longest CSS\n * transition on the element (duration + delay) has elapsed — or on the next\n * tick if no transition is declared.\n *\n * @defaultValue 'is-opening'\n */\n openingClass?: string;\n\n /**\n * Transient class applied when collapsing, removed once the longest CSS\n * transition on the element (duration + delay) has elapsed — or on the next\n * tick if no transition is declared.\n *\n * @defaultValue 'is-closing'\n */\n closingClass?: string;\n\n /**\n * Called after each toggle with the resulting state, right before the\n * `readmore:toggle` event is dispatched.\n *\n * @param expanded - `true` once expanded, `false` once collapsed.\n * @param instance - The instance that was toggled.\n */\n onToggle?: (expanded: boolean, instance: ReadMore) => void;\n}\n\ntype OptionalKeys = 'height' | 'buttonContainer' | 'moreLabel' | 'lessLabel' | 'onToggle';\n\n/**\n * {@link ReadMoreOptions} merged with the defaults, as exposed on\n * {@link ReadMore.options}.\n */\nexport type ResolvedOptions = Required<Omit<ReadMoreOptions, OptionalKeys>> & Pick<ReadMoreOptions, OptionalKeys>;\n\n/** `detail` of the `readmore:toggle` event. */\nexport interface ReadMoreToggleDetail {\n /** `true` once expanded, `false` once collapsed. */\n expanded: boolean;\n\n /** The instance that was toggled. */\n instance: ReadMore;\n}\n\n// Types `event.detail` in `addEventListener('readmore:toggle', …)`, on elements\n// and on `document` (the event bubbles).\ndeclare global {\n interface HTMLElementEventMap {\n 'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;\n }\n\n interface DocumentEventMap {\n 'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;\n }\n}\n\nconst CLAMP_CLASS = 'readmore-clamp';\nconst CLAMP_HEIGHT_CLASS = 'readmore-clamp--height';\n\nconst instances = new WeakMap<HTMLElement, ReadMore>();\nlet idCounter = 0;\n\nconst DEFAULTS: ResolvedOptions = {\n lines: 3,\n moreText: 'Read more',\n lessText: 'Read less',\n buttonClass: 'readmore-btn',\n expandedClass: 'is-expanded',\n openingClass: 'is-opening',\n closingClass: 'is-closing'\n};\n\n/**\n * Parses a computed `transition-duration` / `transition-delay` list into\n * milliseconds. Computed times are always serialized in seconds.\n */\nfunction toMs(list: string): number[] {\n return list.split(',').map((v) => parseFloat(v) * 1000 || 0);\n}\n\n/**\n * Clamps an element to a number of lines (or a fixed height) and mounts a\n * `Read more` / `Read less` button right after it, but only while the content\n * actually overflows.\n *\n * @example\n * ```ts\n * import { ReadMore } from '@frontfactory/readmore';\n * import '@frontfactory/readmore/style.css';\n *\n * ReadMore.init('.excerpt', { lines: 3 });\n * ```\n */\nexport class ReadMore {\n /** Shared by every instance, created with the first one. */\n static #resizeObserver: ResizeObserver | undefined;\n\n /** Instances created by the running {@link ReadMore.init}, synced together once all exist. */\n static #batch: ReadMore[] | null = null;\n\n /** Instances waiting for `document.fonts.ready`, measured together. */\n static #fontQueue: ReadMore[] | null = null;\n\n /** Live instances resynced by the shared `document.fonts` `loadingdone` listener. */\n static #live = new Set<ReadMore>();\n\n /** The `document.fonts` the shared listener is attached to. */\n static #fonts: FontFaceSet | undefined;\n\n /** The clamped element. */\n readonly el: HTMLElement;\n\n /** The options this instance was built with, merged with the defaults. */\n readonly options: Readonly<ResolvedOptions>;\n\n #button: HTMLButtonElement | null = null;\n #expanded = false;\n #generatedId = false;\n #transientTimer: ReturnType<typeof setTimeout> | null = null;\n #onClick = (): void => this.toggle();\n\n // Destroyed once the element is no longer bound to this instance: after\n // destroy(), or if the element was re-initialized by another instance.\n get #destroyed(): boolean {\n return instances.get(this.el) !== this;\n }\n\n /**\n * @param element - The element to clamp.\n * @param options - See {@link ReadMoreOptions}.\n * @throws TypeError - if `element` is not an `HTMLElement`, or there is no\n * `HTMLElement` at all (no browser, e.g. SSR).\n * @throws Error - if `element` is already initialized; call\n * {@link ReadMore.destroy} first.\n * @throws RangeError - if `lines` is not an integer greater than or equal\n * to 1, or `height` is not a finite number greater than 0.\n * @throws RangeError - if `expandedClass`, `openingClass` or `closingClass`\n * is not a string or contains whitespace (an empty string means the default).\n * @throws TypeError - if `buttonContainer` is set but is not an `HTMLElement`.\n */\n constructor(element: HTMLElement, options: ReadMoreOptions = {}) {\n if (typeof HTMLElement === 'undefined' || !(element instanceof HTMLElement)) {\n throw new TypeError('ReadMore: an HTMLElement is required.');\n }\n\n if (instances.has(element)) {\n throw new Error('ReadMore: element is already initialized. Call destroy() first.');\n }\n\n this.el = element;\n // Options explicitly set to `undefined` (or an empty state class) must not override the defaults.\n this.options = {\n ...DEFAULTS,\n ...Object.fromEntries(Object.entries(options).filter((entry) => entry[1] !== undefined\n && !(entry[1] === '' && entry[0].endsWith('Class'))))\n };\n\n // Numeric strings (data-* attributes, framework props) are accepted; blank or non-numeric ones still fail below.\n for (const key of [\n 'height',\n 'lines'\n ] as const) {\n const value: unknown = this.options[key];\n\n if (typeof value === 'string' && value.trim() !== '') {\n (this.options as Record<string, unknown>)[key] = Number(value);\n }\n }\n\n const { height, lines, buttonContainer } = this.options;\n\n if (buttonContainer != null && !(buttonContainer instanceof HTMLElement)) {\n throw new TypeError('ReadMore: buttonContainer must be an HTMLElement.');\n }\n\n if (height != null && !(Number.isFinite(height) && height > 0)) {\n throw new RangeError('ReadMore: height must be a finite number greater than 0.');\n }\n\n if (!(Number.isInteger(lines) && lines >= 1)) {\n throw new RangeError('ReadMore: lines must be an integer of at least 1.');\n }\n\n for (const key of [\n 'expandedClass',\n 'openingClass',\n 'closingClass'\n ] as const) {\n const value: unknown = this.options[key];\n\n if (typeof value !== 'string' || !/^\\S+$/.test(value)) {\n throw new RangeError(`ReadMore: ${key} must be a string without whitespace.`);\n }\n }\n\n instances.set(element, this);\n this.#init();\n }\n\n /**\n * Instantiates {@link ReadMore} on every matching element. Elements that are\n * already initialized are left untouched and their existing instance is\n * returned, so it is safe to call again after new content is added.\n *\n * @param target - A CSS selector, an element, or any iterable or array-like\n * of elements (`NodeList`, `HTMLCollection`, array, `Set`…).\n * @param options - See {@link ReadMoreOptions}. Not applied to elements\n * that are already initialized.\n * @param root - Where to look for `target` when it is a selector. Defaults to `document`.\n * @returns One instance per element, in document order; `[]` without a\n * browser (e.g. SSR).\n */\n static init(\n target: string | HTMLElement | Iterable<HTMLElement> | ArrayLike<HTMLElement>,\n options?: ReadMoreOptions,\n root?: ParentNode\n ): ReadMore[] {\n if (typeof HTMLElement === 'undefined' || typeof document === 'undefined') {\n return [];\n }\n\n const nodes =\n typeof target === 'string'\n ? (root ?? document).querySelectorAll<HTMLElement>(target)\n : target instanceof HTMLElement ? [\n target\n ] : target;\n\n const batch: ReadMore[] = [];\n\n ReadMore.#batch = batch;\n\n try {\n return Array.from(nodes).map((n) => instances.get(n) ?? new ReadMore(n, options));\n } finally {\n ReadMore.#batch = null;\n ReadMore.#syncAll(batch);\n }\n }\n\n /** Whether the text is currently expanded. */\n get expanded(): boolean {\n return this.#expanded;\n }\n\n /**\n * Expands or collapses the text, updating the button label, the\n * `aria-expanded` attribute and the state classes, then calls\n * {@link ReadMoreOptions.onToggle} and dispatches a bubbling\n * `readmore:toggle` event on the element. Does nothing once destroyed.\n *\n * @param force - `true` to expand, `false` to collapse; the state is\n * inverted when omitted. Does nothing if the text is already in the\n * requested state.\n */\n toggle(force?: boolean): void {\n // Only a real boolean forces the state, so `toggle` still works as an\n // event listener (`addEventListener('click', rm.toggle.bind(rm))`).\n const expanded = typeof force === 'boolean' ? force : !this.#expanded;\n\n if (this.#destroyed || expanded === this.#expanded) {\n return;\n }\n\n this.#expanded = expanded;\n this.el.classList.toggle(CLAMP_CLASS, !this.#expanded);\n\n if (this.options.height != null) {\n this.el.classList.toggle(CLAMP_HEIGHT_CLASS, !this.#expanded);\n }\n\n this.el.classList.toggle(this.options.expandedClass, this.#expanded);\n this.#updateButton();\n\n const stateClass = this.#expanded ? this.options.openingClass : this.options.closingClass;\n const otherClass = this.#expanded ? this.options.closingClass : this.options.openingClass;\n\n this.el.classList.remove(otherClass);\n this.#applyTransientClass(stateClass);\n\n try {\n this.options.onToggle?.(this.#expanded, this);\n } finally {\n // The event is dispatched even if `onToggle` throws (the error still propagates).\n this.el.dispatchEvent(new CustomEvent<ReadMoreToggleDetail>('readmore:toggle', {\n bubbles: true,\n detail: {\n expanded: this.#expanded,\n instance: this\n }\n }));\n }\n }\n\n /**\n * Checks again whether the content overflows, mounting or unmounting the\n * button. Resizes of the element are already tracked; call it after\n * changing the content in a way that keeps the element's size. Does nothing\n * while expanded, during a toggle transition, or once destroyed.\n */\n refresh(): void {\n this.#sync(this.#isOverflowing());\n }\n\n /**\n * @param element - An element possibly initialized by this plugin.\n * @returns The instance bound to it, or `undefined`.\n */\n static getInstance(element: HTMLElement): ReadMore | undefined {\n return instances.get(element);\n }\n\n /**\n * Restores the element: removes the button, the classes, the CSS custom\n * properties, the listeners and the generated `id`. The element can then be\n * initialized again. Calling it again does nothing.\n */\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n instances.delete(this.el);\n ReadMore.#resizeObserver?.unobserve(this.el);\n\n if (ReadMore.#live.delete(this) && ReadMore.#live.size === 0) {\n ReadMore.#fonts?.removeEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n ReadMore.#fonts = undefined;\n }\n\n if (this.#transientTimer !== null) {\n clearTimeout(this.#transientTimer);\n this.#transientTimer = null;\n }\n\n this.#unmountButton();\n this.el.classList.remove(\n CLAMP_CLASS,\n CLAMP_HEIGHT_CLASS,\n this.options.expandedClass,\n this.options.openingClass,\n this.options.closingClass\n );\n\n this.el.style.removeProperty('--readmore-lines');\n this.el.style.removeProperty('--readmore-height');\n\n if (this.#generatedId) {\n this.el.removeAttribute('id');\n this.#generatedId = false;\n }\n }\n\n // Measures every resized element before touching the DOM, so mounting a\n // button does not force a new layout for each following element.\n static #onResize(entries: ResizeObserverEntry[]): void {\n ReadMore.#syncAll(entries.map((entry) => instances.get(entry.target as HTMLElement)));\n }\n\n static #onFontsLoaded = (): void => {\n ReadMore.#syncAll(Array.from(ReadMore.#live));\n };\n\n static #syncAll(list: (ReadMore | undefined)[]): void {\n const measured = new Map<ReadMore, boolean>();\n\n for (const instance of list) {\n if (instance) {\n measured.set(instance, instance.#isOverflowing());\n }\n }\n\n measured.forEach((overflowing, instance) => instance.#sync(overflowing));\n }\n\n #init(): void {\n if (this.options.height != null) {\n this.el.style.setProperty('--readmore-height', `${ this.options.height }px`);\n this.el.classList.add(CLAMP_CLASS, CLAMP_HEIGHT_CLASS);\n } else {\n this.el.style.setProperty('--readmore-lines', String(this.options.lines));\n this.el.classList.add(CLAMP_CLASS);\n }\n\n ReadMore.#resizeObserver ??= new ResizeObserver(ReadMore.#onResize);\n ReadMore.#resizeObserver.observe(this.el);\n\n // Inside init(), the batch measures all elements at once afterwards.\n if (ReadMore.#batch) {\n ReadMore.#batch.push(this);\n } else {\n this.refresh();\n }\n\n this.#refreshOnFonts();\n }\n\n // A font swap can change the overflow without resizing the element, which the\n // ResizeObserver would miss. Instances created in the same tick share one\n // measurement; destroyed ones are skipped by #sync.\n #refreshOnFonts(): void {\n if (typeof document === 'undefined' || !document.fonts) {\n return;\n }\n\n // Fonts loaded later (after `ready`) are caught by one shared listener.\n if (ReadMore.#fonts !== document.fonts) {\n ReadMore.#fonts?.removeEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n ReadMore.#fonts = document.fonts;\n ReadMore.#fonts.addEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n }\n\n ReadMore.#live.add(this);\n\n if (ReadMore.#fontQueue) {\n ReadMore.#fontQueue.push(this);\n\n return;\n }\n\n const queue: ReadMore[] = ReadMore.#fontQueue = [\n this\n ];\n\n void document.fonts.ready.then(() => {\n ReadMore.#fontQueue = null;\n ReadMore.#syncAll(queue.filter((instance) => !instance.#destroyed));\n });\n }\n\n #isOverflowing(): boolean {\n return this.el.scrollHeight - this.el.clientHeight > 1;\n }\n\n #sync(overflowing: boolean): void {\n // While a toggle transition runs, the element's size is not final: measuring\n // it mid-collapse would remove the button. The timer refreshes once it ends.\n if (this.#expanded || this.#transientTimer !== null || this.#destroyed) {\n return;\n }\n\n if (overflowing && !this.#button) {\n this.#mountButton();\n } else if (!overflowing && this.#button) {\n this.#unmountButton();\n }\n }\n\n #applyTransientClass(stateClass: string): void {\n if (this.#transientTimer !== null) {\n clearTimeout(this.#transientTimer);\n }\n\n this.el.classList.add(stateClass);\n\n // A timer rather than `transitionend`, which never fires when no declared\n // property actually changes (e.g. `transition: all` in lines mode).\n const timer = setTimeout(() => {\n // Some engines (WebKit) start the transition later than the timer assumes:\n // wait for it to end, or the size is measured mid-way and the button remounted.\n const running = this.el.getAnimations?.().filter(a => a instanceof CSSTransition) ?? [];\n const done = () => {\n // Superseded by another toggle, or destroyed meanwhile.\n if (this.#transientTimer !== timer) {\n return;\n }\n\n this.#transientTimer = null;\n this.el.classList.remove(stateClass);\n this.refresh();\n };\n\n if (running.length) {\n Promise.allSettled(running.map(a => a.finished)).then(done);\n } else {\n done();\n }\n }, this.#transitionTime());\n\n this.#transientTimer = timer;\n }\n\n /** Longest `duration + delay` among the transitions declared on the element, in ms. */\n #transitionTime(): number {\n const style = getComputedStyle(this.el);\n const durations = toMs(style.transitionDuration);\n const delays = toMs(style.transitionDelay);\n let max = 0;\n\n // Shorter lists repeat to match the longer one, as CSS does.\n for (let i = 0; i < Math.max(durations.length, delays.length); i++) {\n max = Math.max(max, durations[i % durations.length] + delays[i % delays.length]);\n }\n\n return max;\n }\n\n #mountButton(): void {\n if (!this.el.id) {\n let id: string;\n\n // Skip ids already used in the page (server-rendered markup, another copy of the plugin…).\n do {\n id = `readmore-${++idCounter}`;\n } while (document.getElementById(id));\n\n this.el.id = id;\n this.#generatedId = true;\n }\n\n const btn = document.createElement('button');\n\n btn.type = 'button';\n btn.className = this.options.buttonClass;\n btn.setAttribute('aria-controls', this.el.id);\n btn.addEventListener('click', this.#onClick);\n this.#button = btn;\n this.#updateButton();\n\n if (this.options.buttonContainer) {\n this.options.buttonContainer.appendChild(btn);\n } else {\n this.el.insertAdjacentElement('afterend', btn);\n }\n }\n\n /** Syncs the button's text, `aria-expanded` and `aria-label` with the state. */\n #updateButton(): void {\n if (!this.#button) {\n return;\n }\n\n const { moreText, lessText, moreLabel, lessLabel } = this.options;\n const label = this.#expanded ? lessLabel : moreLabel;\n\n this.#button.textContent = this.#expanded ? lessText : moreText;\n this.#button.setAttribute('aria-expanded', String(this.#expanded));\n\n if (label) {\n this.#button.setAttribute('aria-label', label);\n } else {\n this.#button.removeAttribute('aria-label');\n }\n }\n\n #unmountButton(): void {\n if (!this.#button) {\n return;\n }\n\n this.#button.removeEventListener('click', this.#onClick);\n\n // Removing the focused button would drop focus to <body>: keep it on the element.\n if (document.activeElement === this.#button) {\n const { el } = this;\n\n if (!el.hasAttribute('tabindex')) {\n el.tabIndex = -1;\n el.addEventListener('blur', () => el.removeAttribute('tabindex'), {\n once: true\n });\n }\n\n el.focus();\n }\n\n this.#button.remove();\n this.#button = null;\n }\n}\n"],"mappings":"C,yEA+HA,IAAMC,EAAc,iBACdC,EAAqB,yBAErBC,kBAAY,IAAI,QAClBC,EAAY,EAEVC,EAA4B,CAC9B,MAAO,EACP,SAAU,YACV,SAAU,YACV,YAAa,eACb,cAAe,cACf,aAAc,aACd,aAAc,YAClB,EAMA,SAASC,EAAK,EAAwB,CAClC,OAAOC,EAAK,MAAM,GAAG,CAAC,CAAC,IAAK,GAAM,WAAWC,CAAC,EAAI,KAAQ,CAAC,CAC/D,C,iBAeaC,CAAS,CAElB,MAAOC,GAGP,MAAOC,GAA4B,KAGnC,MAAOC,GAAgC,KAGvC,MAAOC,mBAAQ,IAAI,IAGnB,MAAOC,GAGP,GAGA,QAEA,GAAoC,KACpC,GAAY,GACZ,GAAe,GACf,GAAwD,KACxD,OAAuB,KAAK,OAAO,EAInC,GAAIC,IAAsB,CACtB,OAAOZ,EAAU,IAAI,KAAK,EAAE,IAAM,IACtC,CAeA,YAAY,EAAsB,EAA2B,CAAC,EAAG,CAC7D,GAAI,OAAO,YAAgB,KAAe,EAAEa,aAAmB,aAC3D,MAAU,UAAU,uCAAuC,EAG/D,GAAIb,EAAU,IAAIa,CAAO,EACrB,MAAU,MAAM,iEAAiE,EAGrF,KAAK,GAAKA,EAEV,KAAK,QAAU,CACX,GAAGX,EACH,GAAG,OAAO,YAAY,OAAO,QAAQY,CAAO,CAAC,CAAC,OAAQ,GAAUC,EAAM,KAAO,IAAA,IACtE,EAAEA,EAAM,KAAO,IAAMA,EAAM,EAAE,CAAC,SAAS,OAAO,EAAE,CAAC,CAC5D,EAGA,IAAK,IAAMC,IAAO,CACd,SACA,OACJ,EAAY,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M,EACV,CAAC,GAGLA,EAAG,MAAM,CACb,CAEA,KAAKpB,GAAQ,OAAO,EACpB,KAAKA,GAAU,IAjBwC,CAkB3D,CACJ,C"}
|
|
1
|
+
{"version":3,"file":"readmore.iife.js","names":["exports","CLAMP_CLASS","CLAMP_HEIGHT_CLASS","instances","idCounter","DEFAULTS","toMs","list","v","ReadMore","#resizeObserver","#batch","#fontQueue","#live","#fonts","#expanded","#button","target","#destroyed","element","options","entry","key","value","buttonContainer","#resolveSizes","threshold","#init","nodes","root","batch","n","#syncAll","expanded","force","before","#updateButton","stateClass","otherClass","#applyTransientClass","#applySizes","#sync","#isOverflowing","read","result","height","lines","#height","#lines","#onFocusIn","#onFontsLoaded","#transientTimer","#unmountButton","#generatedId","#onResize","entries","measured","instance","overflowing","#refreshOnFonts","queue","#mountButton","timer","running","a","done","#transitionTime","style","durations","delays","max","i","id","btn","#onClick","label","lessLabel","moreLabel","lessText","moreText","el"],"sources":["../src/readmore.ts"],"sourcesContent":["/**\n * Options accepted by {@link ReadMore}. Every field is optional.\n */\nexport interface ReadMoreOptions {\n /**\n * Number of lines to clamp to. Ignored when {@link ReadMoreOptions.height} is set.\n *\n * @defaultValue 3\n * Can also be a function called with the element; the returned value is\n * validated like a literal one and re-evaluated on {@link ReadMore.refresh}.\n *\n * @throws RangeError - if not an integer greater than or equal to 1.\n */\n lines?: number | ((el: HTMLElement) => number);\n\n /**\n * Max height in pixels. Takes precedence over {@link ReadMoreOptions.lines}\n * and clamps with `max-height` instead of `-webkit-line-clamp`.\n * Can also be a function called with the element; the returned value is\n * validated like a literal one and re-evaluated on {@link ReadMore.refresh}.\n *\n * @throws RangeError - if not a finite number greater than 0.\n */\n height?: number | ((el: HTMLElement) => number);\n\n /**\n * Hidden overflow in pixels tolerated before the button is mounted: the\n * button only appears when more than this is hidden (at least 1px).\n *\n * @defaultValue 0\n * @throws RangeError - if not a finite number greater than or equal to 0.\n */\n threshold?: number;\n\n /**\n * Label of the toggle button while the text is collapsed.\n *\n * @defaultValue 'Read more'\n */\n moreText?: string;\n\n /**\n * Label of the toggle button while the text is expanded.\n *\n * @defaultValue 'Read less'\n */\n lessText?: string;\n\n /**\n * Accessible name (`aria-label`) of the toggle button while the text is\n * collapsed, e.g. `'Read more about Product A'` when several buttons share\n * the same visible text. It should contain {@link ReadMoreOptions.moreText}.\n */\n moreLabel?: string;\n\n /**\n * Accessible name (`aria-label`) of the toggle button while the text is\n * expanded. It should contain {@link ReadMoreOptions.lessText}.\n */\n lessLabel?: string;\n\n /**\n * Class applied to the toggle button.\n *\n * @defaultValue 'readmore-btn'\n */\n buttonClass?: string;\n\n /**\n * Element the toggle button is appended to, instead of being inserted right\n * after the clamped element. Use an element owned by your framework (a ref)\n * so reconciliation cannot drop the button.\n *\n * @throws TypeError - if not an `HTMLElement`.\n */\n buttonContainer?: HTMLElement;\n\n /**\n * Class applied to the target element for the whole expanded state.\n *\n * @defaultValue 'is-expanded'\n */\n expandedClass?: string;\n\n /**\n * Transient class applied when expanding, removed once the longest CSS\n * transition on the element (duration + delay) has elapsed — or on the next\n * tick if no transition is declared.\n *\n * @defaultValue 'is-opening'\n */\n openingClass?: string;\n\n /**\n * Transient class applied when collapsing, removed once the longest CSS\n * transition on the element (duration + delay) has elapsed — or on the next\n * tick if no transition is declared.\n *\n * @defaultValue 'is-closing'\n */\n closingClass?: string;\n\n /**\n * Scrolls the element back into view when collapsing, if its top edge ended\n * up above the viewport.\n *\n * @defaultValue false\n */\n scrollOnCollapse?: boolean;\n\n /**\n * Called after each toggle with the resulting state, right before the\n * `readmore:toggle` event is dispatched.\n *\n * @param expanded - `true` once expanded, `false` once collapsed.\n * @param instance - The instance that was toggled.\n */\n onToggle?: (expanded: boolean, instance: ReadMore) => void;\n\n /**\n * Called before a toggle that would really happen, with the state it is\n * about to enter. Returning exactly `false` cancels it (the\n * `readmore:beforetoggle` event is then not dispatched). If it throws, the\n * error propagates and the toggle does not happen.\n *\n * @param expanded - `true` if about to expand, `false` if about to collapse.\n * @param instance - The instance about to be toggled.\n */\n onBeforeToggle?: (expanded: boolean, instance: ReadMore) => boolean | void;\n}\n\ntype OptionalKeys = 'height' | 'buttonContainer' | 'moreLabel' | 'lessLabel' | 'onToggle' | 'onBeforeToggle';\n\n/**\n * {@link ReadMoreOptions} merged with the defaults, as exposed on\n * {@link ReadMore.options}.\n */\nexport type ResolvedOptions = Required<Omit<ReadMoreOptions, OptionalKeys>> & Pick<ReadMoreOptions, OptionalKeys>;\n\n/** `detail` of the `readmore:toggle` event. */\nexport interface ReadMoreToggleDetail {\n /** `true` once expanded, `false` once collapsed. */\n expanded: boolean;\n\n /** The instance that was toggled. */\n instance: ReadMore;\n}\n\n// Types `event.detail` in `addEventListener('readmore:toggle', …)`, on elements\n// and on `document` (the event bubbles). `readmore:beforetoggle` is cancelable\n// and carries the state about to be entered.\ndeclare global {\n interface HTMLElementEventMap {\n 'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;\n 'readmore:beforetoggle': CustomEvent<ReadMoreToggleDetail>;\n }\n\n interface DocumentEventMap {\n 'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;\n 'readmore:beforetoggle': CustomEvent<ReadMoreToggleDetail>;\n }\n}\n\nconst CLAMP_CLASS = 'readmore-clamp';\nconst CLAMP_HEIGHT_CLASS = 'readmore-clamp--height';\n\nconst instances = new WeakMap<HTMLElement, ReadMore>();\nlet idCounter = 0;\n\nconst DEFAULTS: ResolvedOptions = {\n lines: 3,\n threshold: 0,\n moreText: 'Read more',\n lessText: 'Read less',\n buttonClass: 'readmore-btn',\n expandedClass: 'is-expanded',\n openingClass: 'is-opening',\n closingClass: 'is-closing',\n scrollOnCollapse: false\n};\n\n/**\n * Parses a computed `transition-duration` / `transition-delay` list into\n * milliseconds. Computed times are always serialized in seconds.\n */\nfunction toMs(list: string): number[] {\n return list.split(',').map((v) => parseFloat(v) * 1000 || 0);\n}\n\n/**\n * Clamps an element to a number of lines (or a fixed height) and mounts a\n * `Read more` / `Read less` button right after it, but only while the content\n * actually overflows. Focus landing on an element in the hidden part expands\n * the text.\n *\n * @example\n * ```ts\n * import { ReadMore } from '@frontfactory/readmore';\n * import '@frontfactory/readmore/style.css';\n *\n * ReadMore.init('.excerpt', { lines: 3 });\n * ```\n */\nexport class ReadMore {\n /** Shared by every instance, created with the first one. */\n static #resizeObserver: ResizeObserver | undefined;\n\n /** Instances created by the running {@link ReadMore.init}, synced together once all exist. */\n static #batch: ReadMore[] | null = null;\n\n /** Instances waiting for `document.fonts.ready`, measured together. */\n static #fontQueue: ReadMore[] | null = null;\n\n /** Live instances resynced by the shared `document.fonts` `loadingdone` listener. */\n static #live = new Set<ReadMore>();\n\n /** The `document.fonts` the shared listener is attached to. */\n static #fonts: FontFaceSet | undefined;\n\n /** The clamped element. */\n readonly el: HTMLElement;\n\n /** The options this instance was built with, merged with the defaults. */\n readonly options: Readonly<ResolvedOptions>;\n\n #button: HTMLButtonElement | null = null;\n #expanded = false;\n #generatedId = false;\n #transientTimer: ReturnType<typeof setTimeout> | null = null;\n #onClick = (): void => this.toggle();\n #lines = DEFAULTS.lines as number;\n #height: number | undefined;\n\n #onFocusIn = (e: FocusEvent): void => {\n const { target } = e;\n\n if (this.#expanded || !this.#button || target === this.el || !(target instanceof Element)) {\n return;\n }\n\n // The browser may already have scrolled the hidden overflow to reveal the target: add it back.\n if (target.getBoundingClientRect().bottom + this.el.scrollTop > this.el.getBoundingClientRect().bottom + 1) {\n this.toggle(true);\n }\n };\n\n // Destroyed once the element is no longer bound to this instance: after\n // destroy(), or if the element was re-initialized by another instance.\n get #destroyed(): boolean {\n return instances.get(this.el) !== this;\n }\n\n /**\n * @param element - The element to clamp.\n * @param options - See {@link ReadMoreOptions}.\n * @throws TypeError - if `element` is not an `HTMLElement`, or there is no\n * `HTMLElement` at all (no browser, e.g. SSR).\n * @throws Error - if `element` is already initialized; call\n * {@link ReadMore.destroy} first.\n * @throws RangeError - if `lines` is not an integer greater than or equal\n * to 1, `height` is not a finite number greater than 0, or `threshold` is\n * not a finite number greater than or equal to 0 (for `lines` and `height`,\n * the value returned by a function is checked).\n * @throws RangeError - if `expandedClass`, `openingClass` or `closingClass`\n * is not a string or contains whitespace (an empty string means the default).\n * @throws TypeError - if `buttonContainer` is set but is not an `HTMLElement`.\n */\n constructor(element: HTMLElement, options: ReadMoreOptions = {}) {\n if (typeof HTMLElement === 'undefined' || !(element instanceof HTMLElement)) {\n throw new TypeError('ReadMore: an HTMLElement is required.');\n }\n\n if (instances.has(element)) {\n throw new Error('ReadMore: element is already initialized. Call destroy() first.');\n }\n\n this.el = element;\n // Options explicitly set to `undefined` (or an empty state class) must not override the defaults.\n this.options = {\n ...DEFAULTS,\n ...Object.fromEntries(Object.entries(options).filter((entry) => entry[1] !== undefined\n && !(entry[1] === '' && entry[0].endsWith('Class'))))\n };\n\n // Numeric strings (data-* attributes, framework props) are accepted; blank or non-numeric ones still fail below.\n for (const key of [\n 'height',\n 'lines',\n 'threshold'\n ] as const) {\n const value: unknown = this.options[key];\n\n if (typeof value === 'string' && value.trim() !== '') {\n (this.options as Record<string, unknown>)[key] = Number(value);\n }\n }\n\n const { threshold, buttonContainer } = this.options;\n\n if (buttonContainer != null && !(buttonContainer instanceof HTMLElement)) {\n throw new TypeError('ReadMore: buttonContainer must be an HTMLElement.');\n }\n\n this.#resolveSizes();\n\n if (!(Number.isFinite(threshold) && threshold >= 0)) {\n throw new RangeError('ReadMore: threshold must be a finite number of at least 0.');\n }\n\n for (const key of [\n 'expandedClass',\n 'openingClass',\n 'closingClass'\n ] as const) {\n const value: unknown = this.options[key];\n\n if (typeof value !== 'string' || !/^\\S+$/.test(value)) {\n throw new RangeError(`ReadMore: ${key} must be a string without whitespace.`);\n }\n }\n\n instances.set(element, this);\n this.#init();\n }\n\n /**\n * Instantiates {@link ReadMore} on every matching element. Elements that are\n * already initialized are left untouched and their existing instance is\n * returned, so it is safe to call again after new content is added.\n *\n * @param target - A CSS selector, an element, or any iterable or array-like\n * of elements (`NodeList`, `HTMLCollection`, array, `Set`…).\n * @param options - See {@link ReadMoreOptions}. Not applied to elements\n * that are already initialized.\n * @param root - Where to look for `target` when it is a selector. Defaults to `document`.\n * @returns One instance per element, in document order; `[]` without a\n * browser (e.g. SSR).\n */\n static init(\n target: string | HTMLElement | Iterable<HTMLElement> | ArrayLike<HTMLElement>,\n options?: ReadMoreOptions,\n root?: ParentNode\n ): ReadMore[] {\n if (typeof HTMLElement === 'undefined' || typeof document === 'undefined') {\n return [];\n }\n\n const nodes =\n typeof target === 'string'\n ? (root ?? document).querySelectorAll<HTMLElement>(target)\n : target instanceof HTMLElement ? [\n target\n ] : target;\n\n const batch: ReadMore[] = [];\n\n ReadMore.#batch = batch;\n\n try {\n return Array.from(nodes).map((n) => instances.get(n) ?? new ReadMore(n, options));\n } finally {\n ReadMore.#batch = null;\n ReadMore.#syncAll(batch);\n }\n }\n\n /** Whether the text is currently expanded. */\n get expanded(): boolean {\n return this.#expanded;\n }\n\n /**\n * Expands or collapses the text, updating the button label, the\n * `aria-expanded` attribute and the state classes, then calls\n * {@link ReadMoreOptions.onToggle} and dispatches a bubbling\n * `readmore:toggle` event on the element. Does nothing once destroyed.\n *\n * @param force - `true` to expand, `false` to collapse; the state is\n * inverted when omitted. Does nothing if the text is already in the\n * requested state. Before a real toggle, {@link ReadMoreOptions.onBeforeToggle}\n * and a cancelable `readmore:beforetoggle` event can cancel it.\n */\n toggle(force?: boolean): void {\n // Only a real boolean forces the state, so `toggle` still works as an\n // event listener (`addEventListener('click', rm.toggle.bind(rm))`).\n const expanded = typeof force === 'boolean' ? force : !this.#expanded;\n\n if (this.#destroyed || expanded === this.#expanded) {\n return;\n }\n\n if (this.options.onBeforeToggle?.(expanded, this) === false) {\n return;\n }\n\n const before = new CustomEvent<ReadMoreToggleDetail>('readmore:beforetoggle', {\n bubbles: true,\n cancelable: true,\n detail: {\n expanded,\n instance: this\n }\n });\n\n if (!this.el.dispatchEvent(before)) {\n return;\n }\n\n // Measured while still clamped: scrollHeight is then the full content height.\n if (expanded && this.options.height != null) {\n this.el.style.setProperty('--readmore-full-height', `${ this.el.scrollHeight }px`);\n }\n\n this.#expanded = expanded;\n this.el.classList.toggle(CLAMP_CLASS, !this.#expanded);\n\n if (this.options.height != null) {\n this.el.classList.toggle(CLAMP_HEIGHT_CLASS, !this.#expanded);\n }\n\n this.el.classList.toggle(this.options.expandedClass, this.#expanded);\n this.#updateButton();\n\n const stateClass = this.#expanded ? this.options.openingClass : this.options.closingClass;\n const otherClass = this.#expanded ? this.options.closingClass : this.options.openingClass;\n\n this.el.classList.remove(otherClass);\n this.#applyTransientClass(stateClass);\n\n if (!this.#expanded && this.options.scrollOnCollapse && this.el.getBoundingClientRect().top < 0) {\n this.el.scrollIntoView?.({\n block: 'nearest'\n });\n }\n\n try {\n this.options.onToggle?.(this.#expanded, this);\n } finally {\n // The event is dispatched even if `onToggle` throws (the error still propagates).\n this.el.dispatchEvent(new CustomEvent<ReadMoreToggleDetail>('readmore:toggle', {\n bubbles: true,\n detail: {\n expanded: this.#expanded,\n instance: this\n }\n }));\n }\n }\n\n /**\n * Checks again whether the content overflows, mounting or unmounting the\n * button. Resizes of the element are already tracked; call it after\n * changing the content in a way that keeps the element's size. Does nothing\n * while expanded, during a toggle transition, or once destroyed.\n */\n refresh(): void {\n if (!this.#destroyed) {\n this.#resolveSizes();\n this.#applySizes();\n }\n\n this.#sync(this.#isOverflowing());\n }\n\n // Evaluates function values of `lines` / `height` (on the element), then validates like literals.\n #resolveSizes(): void {\n const read = (value: unknown): unknown => {\n const result = typeof value === 'function' ? value(this.el) : value;\n\n return typeof result === 'string' && result.trim() !== '' ? Number(result) : result;\n };\n\n const height = read(this.options.height) as number | undefined;\n const lines = read(this.options.lines) as number;\n\n if (height != null && !(Number.isFinite(height) && height > 0)) {\n throw new RangeError('ReadMore: height must be a finite number greater than 0.');\n }\n\n if (!(Number.isInteger(lines) && lines >= 1)) {\n throw new RangeError('ReadMore: lines must be an integer of at least 1.');\n }\n\n this.#height = height;\n this.#lines = lines;\n }\n\n #applySizes(): void {\n if (this.options.height != null) {\n this.el.style.setProperty('--readmore-height', `${ this.#height }px`);\n } else {\n this.el.style.setProperty('--readmore-lines', String(this.#lines));\n }\n }\n\n /**\n * @param element - An element possibly initialized by this plugin.\n * @returns The instance bound to it, or `undefined`.\n */\n static getInstance(element: HTMLElement): ReadMore | undefined {\n return instances.get(element);\n }\n\n /**\n * Restores the element: removes the button, the classes, the CSS custom\n * properties, the listeners and the generated `id`. The element can then be\n * initialized again. Calling it again does nothing.\n */\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n instances.delete(this.el);\n this.el.removeEventListener('focusin', this.#onFocusIn);\n ReadMore.#resizeObserver?.unobserve(this.el);\n\n if (ReadMore.#live.delete(this) && ReadMore.#live.size === 0) {\n ReadMore.#fonts?.removeEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n ReadMore.#fonts = undefined;\n }\n\n if (this.#transientTimer !== null) {\n clearTimeout(this.#transientTimer);\n this.#transientTimer = null;\n }\n\n this.#unmountButton();\n this.el.classList.remove(\n CLAMP_CLASS,\n CLAMP_HEIGHT_CLASS,\n this.options.expandedClass,\n this.options.openingClass,\n this.options.closingClass\n );\n\n this.el.style.removeProperty('--readmore-lines');\n this.el.style.removeProperty('--readmore-height');\n this.el.style.removeProperty('--readmore-full-height');\n\n if (this.#generatedId) {\n this.el.removeAttribute('id');\n this.#generatedId = false;\n }\n }\n\n // Measures every resized element before touching the DOM, so mounting a\n // button does not force a new layout for each following element.\n static #onResize(entries: ResizeObserverEntry[]): void {\n ReadMore.#syncAll(entries.map((entry) => instances.get(entry.target as HTMLElement)));\n }\n\n static #onFontsLoaded = (): void => {\n ReadMore.#syncAll(Array.from(ReadMore.#live));\n };\n\n static #syncAll(list: (ReadMore | undefined)[]): void {\n const measured = new Map<ReadMore, boolean>();\n\n for (const instance of list) {\n if (instance) {\n measured.set(instance, instance.#isOverflowing());\n }\n }\n\n measured.forEach((overflowing, instance) => instance.#sync(overflowing));\n }\n\n #init(): void {\n this.#applySizes();\n this.el.classList.add(CLAMP_CLASS);\n\n if (this.options.height != null) {\n this.el.classList.add(CLAMP_HEIGHT_CLASS);\n }\n\n this.el.addEventListener('focusin', this.#onFocusIn);\n ReadMore.#resizeObserver ??= new ResizeObserver(ReadMore.#onResize);\n ReadMore.#resizeObserver.observe(this.el);\n\n // Inside init(), the batch measures all elements at once afterwards.\n if (ReadMore.#batch) {\n ReadMore.#batch.push(this);\n } else {\n this.refresh();\n }\n\n this.#refreshOnFonts();\n }\n\n // A font swap can change the overflow without resizing the element, which the\n // ResizeObserver would miss. Instances created in the same tick share one\n // measurement; destroyed ones are skipped by #sync.\n #refreshOnFonts(): void {\n if (typeof document === 'undefined' || !document.fonts) {\n return;\n }\n\n // Fonts loaded later (after `ready`) are caught by one shared listener.\n if (ReadMore.#fonts !== document.fonts) {\n ReadMore.#fonts?.removeEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n ReadMore.#fonts = document.fonts;\n ReadMore.#fonts.addEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n }\n\n ReadMore.#live.add(this);\n\n if (ReadMore.#fontQueue) {\n ReadMore.#fontQueue.push(this);\n\n return;\n }\n\n const queue: ReadMore[] = ReadMore.#fontQueue = [\n this\n ];\n\n void document.fonts.ready.then(() => {\n ReadMore.#fontQueue = null;\n ReadMore.#syncAll(queue.filter((instance) => !instance.#destroyed));\n });\n }\n\n #isOverflowing(): boolean {\n return this.el.scrollHeight - this.el.clientHeight > Math.max(1, this.options.threshold);\n }\n\n #sync(overflowing: boolean): void {\n // While a toggle transition runs, the element's size is not final: measuring\n // it mid-collapse would remove the button. The timer refreshes once it ends.\n if (this.#expanded || this.#transientTimer !== null || this.#destroyed) {\n return;\n }\n\n if (overflowing && !this.#button) {\n this.#mountButton();\n } else if (!overflowing && this.#button) {\n this.#unmountButton();\n }\n }\n\n #applyTransientClass(stateClass: string): void {\n if (this.#transientTimer !== null) {\n clearTimeout(this.#transientTimer);\n }\n\n this.el.classList.add(stateClass);\n\n // A timer rather than `transitionend`, which never fires when no declared\n // property actually changes (e.g. `transition: all` in lines mode).\n const timer = setTimeout(() => {\n // Some engines (WebKit) start the transition later than the timer assumes:\n // wait for it to end, or the size is measured mid-way and the button remounted.\n const running = this.el.getAnimations?.().filter(a => a instanceof CSSTransition) ?? [];\n const done = () => {\n // Superseded by another toggle, or destroyed meanwhile.\n if (this.#transientTimer !== timer) {\n return;\n }\n\n this.#transientTimer = null;\n this.el.classList.remove(stateClass);\n this.refresh();\n };\n\n if (running.length) {\n Promise.allSettled(running.map(a => a.finished)).then(done);\n } else {\n done();\n }\n }, this.#transitionTime());\n\n this.#transientTimer = timer;\n }\n\n /** Longest `duration + delay` among the transitions declared on the element, in ms. */\n #transitionTime(): number {\n const style = getComputedStyle(this.el);\n const durations = toMs(style.transitionDuration);\n const delays = toMs(style.transitionDelay);\n let max = 0;\n\n // Shorter lists repeat to match the longer one, as CSS does.\n for (let i = 0; i < Math.max(durations.length, delays.length); i++) {\n max = Math.max(max, durations[i % durations.length] + delays[i % delays.length]);\n }\n\n return max;\n }\n\n #mountButton(): void {\n if (!this.el.id) {\n let id: string;\n\n // Skip ids already used in the page (server-rendered markup, another copy of the plugin…).\n do {\n id = `readmore-${++idCounter}`;\n } while (document.getElementById(id));\n\n this.el.id = id;\n this.#generatedId = true;\n }\n\n const btn = document.createElement('button');\n\n btn.type = 'button';\n btn.className = this.options.buttonClass;\n btn.setAttribute('aria-controls', this.el.id);\n btn.addEventListener('click', this.#onClick);\n this.#button = btn;\n this.#updateButton();\n\n if (this.options.buttonContainer) {\n this.options.buttonContainer.appendChild(btn);\n } else {\n this.el.insertAdjacentElement('afterend', btn);\n }\n }\n\n /** Syncs the button's text, `aria-expanded` and `aria-label` with the state. */\n #updateButton(): void {\n if (!this.#button) {\n return;\n }\n\n const { moreText, lessText, moreLabel, lessLabel } = this.options;\n const label = this.#expanded ? lessLabel : moreLabel;\n\n this.#button.textContent = this.#expanded ? lessText : moreText;\n this.#button.setAttribute('aria-expanded', String(this.#expanded));\n\n if (label) {\n this.#button.setAttribute('aria-label', label);\n } else {\n this.#button.removeAttribute('aria-label');\n }\n }\n\n #unmountButton(): void {\n if (!this.#button) {\n return;\n }\n\n this.#button.removeEventListener('click', this.#onClick);\n\n // Removing the focused button would drop focus to <body>: keep it on the element.\n if (document.activeElement === this.#button) {\n const { el } = this;\n\n if (!el.hasAttribute('tabindex')) {\n el.tabIndex = -1;\n el.addEventListener('blur', () => el.removeAttribute('tabindex'), {\n once: true\n });\n }\n\n el.focus();\n }\n\n this.#button.remove();\n this.#button = null;\n }\n}\n"],"mappings":"C,yEAmKA,IAAMC,EAAc,iBACdC,EAAqB,yBAErBC,kBAAY,IAAI,QAClBC,EAAY,EAEVC,EAA4B,CAC9B,MAAO,EACP,UAAW,EACX,SAAU,YACV,SAAU,YACV,YAAa,eACb,cAAe,cACf,aAAc,aACd,aAAc,aACd,iBAAkB,EACtB,EAMA,SAASC,EAAK,EAAwB,CAClC,OAAOC,EAAK,MAAM,GAAG,CAAC,CAAC,IAAK,GAAM,WAAWC,CAAC,EAAI,KAAQ,CAAC,CAC/D,C,iBAgBaC,CAAS,CAElB,MAAOC,GAGP,MAAOC,GAA4B,KAGnC,MAAOC,GAAgC,KAGvC,MAAOC,mBAAQ,IAAI,IAGnB,MAAOC,GAGP,GAGA,QAEA,GAAoC,KACpC,GAAY,GACZ,GAAe,GACf,GAAwD,KACxD,OAAuB,KAAK,OAAO,EACnC,GAAST,EAAS,MAClB,GAEA,GAAc,GAAwB,CAClC,GAAM,CAAE,UAAW,EAEf,KAAKU,IAAa,CAAC,KAAKC,IAAWC,IAAW,KAAK,IAAM,EAAEA,aAAkB,UAK7EA,EAAO,sBAAsB,CAAC,CAAC,OAAS,KAAK,GAAG,UAAY,KAAK,GAAG,sBAAsB,CAAC,CAAC,OAAS,GACrG,KAAK,OAAO,EAAI,CAExB,EAIA,GAAIC,IAAsB,CACtB,OAAOf,EAAU,IAAI,KAAK,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,EAAS,CAAC,EAE7D4D,EACA,KAAK3D,GAAQ,aAAa,aAAc2D,CAAK,EAE7C,KAAK3D,GAAQ,gBAAgB,YAAY,CAEjD,CAEA,IAAuB,CACnB,GAAK,KAAKA,GAIV,CAGA,GAHA,KAAKA,GAAQ,oBAAoB,QAAS,KAAK0D,EAAQ,EAGnD,SAAS,gBAAkB,KAAK1D,GAAS,CACzC,GAAM,CAAE,MAAO,KAEVgE,EAAG,aAAa,UAAU,IAC3B,EAAG,SAAW,GACdA,EAAG,iBAAiB,WAAcA,EAAG,gBAAgB,UAAU,EAAG,CAC9D,KAAM,EACV,CAAC,GAGLA,EAAG,MAAM,CACb,CAEA,KAAKhE,GAAQ,OAAO,EACpB,KAAKA,GAAU,IAjBwC,CAkB3D,CACJ,C"}
|
package/dist/readmore.js
CHANGED
|
@@ -5,12 +5,14 @@ var instances = /* @__PURE__ */ new WeakMap();
|
|
|
5
5
|
var idCounter = 0;
|
|
6
6
|
var DEFAULTS = {
|
|
7
7
|
lines: 3,
|
|
8
|
+
threshold: 0,
|
|
8
9
|
moreText: "Read more",
|
|
9
10
|
lessText: "Read less",
|
|
10
11
|
buttonClass: "readmore-btn",
|
|
11
12
|
expandedClass: "is-expanded",
|
|
12
13
|
openingClass: "is-opening",
|
|
13
|
-
closingClass: "is-closing"
|
|
14
|
+
closingClass: "is-closing",
|
|
15
|
+
scrollOnCollapse: false
|
|
14
16
|
};
|
|
15
17
|
/**
|
|
16
18
|
* Parses a computed `transition-duration` / `transition-delay` list into
|
|
@@ -22,7 +24,8 @@ function toMs(list) {
|
|
|
22
24
|
/**
|
|
23
25
|
* Clamps an element to a number of lines (or a fixed height) and mounts a
|
|
24
26
|
* `Read more` / `Read less` button right after it, but only while the content
|
|
25
|
-
* actually overflows.
|
|
27
|
+
* actually overflows. Focus landing on an element in the hidden part expands
|
|
28
|
+
* the text.
|
|
26
29
|
*
|
|
27
30
|
* @example
|
|
28
31
|
* ```ts
|
|
@@ -52,6 +55,13 @@ var ReadMore = class ReadMore {
|
|
|
52
55
|
#generatedId = false;
|
|
53
56
|
#transientTimer = null;
|
|
54
57
|
#onClick = () => this.toggle();
|
|
58
|
+
#lines = DEFAULTS.lines;
|
|
59
|
+
#height;
|
|
60
|
+
#onFocusIn = (e) => {
|
|
61
|
+
const { target } = e;
|
|
62
|
+
if (this.#expanded || !this.#button || target === this.el || !(target instanceof Element)) return;
|
|
63
|
+
if (target.getBoundingClientRect().bottom + this.el.scrollTop > this.el.getBoundingClientRect().bottom + 1) this.toggle(true);
|
|
64
|
+
};
|
|
55
65
|
get #destroyed() {
|
|
56
66
|
return instances.get(this.el) !== this;
|
|
57
67
|
}
|
|
@@ -63,7 +73,9 @@ var ReadMore = class ReadMore {
|
|
|
63
73
|
* @throws Error - if `element` is already initialized; call
|
|
64
74
|
* {@link ReadMore.destroy} first.
|
|
65
75
|
* @throws RangeError - if `lines` is not an integer greater than or equal
|
|
66
|
-
* to 1,
|
|
76
|
+
* to 1, `height` is not a finite number greater than 0, or `threshold` is
|
|
77
|
+
* not a finite number greater than or equal to 0 (for `lines` and `height`,
|
|
78
|
+
* the value returned by a function is checked).
|
|
67
79
|
* @throws RangeError - if `expandedClass`, `openingClass` or `closingClass`
|
|
68
80
|
* is not a string or contains whitespace (an empty string means the default).
|
|
69
81
|
* @throws TypeError - if `buttonContainer` is set but is not an `HTMLElement`.
|
|
@@ -76,14 +88,18 @@ var ReadMore = class ReadMore {
|
|
|
76
88
|
...DEFAULTS,
|
|
77
89
|
...Object.fromEntries(Object.entries(options).filter((entry) => entry[1] !== void 0 && !(entry[1] === "" && entry[0].endsWith("Class"))))
|
|
78
90
|
};
|
|
79
|
-
for (const key of [
|
|
91
|
+
for (const key of [
|
|
92
|
+
"height",
|
|
93
|
+
"lines",
|
|
94
|
+
"threshold"
|
|
95
|
+
]) {
|
|
80
96
|
const value = this.options[key];
|
|
81
97
|
if (typeof value === "string" && value.trim() !== "") this.options[key] = Number(value);
|
|
82
98
|
}
|
|
83
|
-
const {
|
|
99
|
+
const { threshold, buttonContainer } = this.options;
|
|
84
100
|
if (buttonContainer != null && !(buttonContainer instanceof HTMLElement)) throw new TypeError("ReadMore: buttonContainer must be an HTMLElement.");
|
|
85
|
-
|
|
86
|
-
if (!(Number.
|
|
101
|
+
this.#resolveSizes();
|
|
102
|
+
if (!(Number.isFinite(threshold) && threshold >= 0)) throw new RangeError("ReadMore: threshold must be a finite number of at least 0.");
|
|
87
103
|
for (const key of [
|
|
88
104
|
"expandedClass",
|
|
89
105
|
"openingClass",
|
|
@@ -132,11 +148,23 @@ var ReadMore = class ReadMore {
|
|
|
132
148
|
*
|
|
133
149
|
* @param force - `true` to expand, `false` to collapse; the state is
|
|
134
150
|
* inverted when omitted. Does nothing if the text is already in the
|
|
135
|
-
* requested state.
|
|
151
|
+
* requested state. Before a real toggle, {@link ReadMoreOptions.onBeforeToggle}
|
|
152
|
+
* and a cancelable `readmore:beforetoggle` event can cancel it.
|
|
136
153
|
*/
|
|
137
154
|
toggle(force) {
|
|
138
155
|
const expanded = typeof force === "boolean" ? force : !this.#expanded;
|
|
139
156
|
if (this.#destroyed || expanded === this.#expanded) return;
|
|
157
|
+
if (this.options.onBeforeToggle?.(expanded, this) === false) return;
|
|
158
|
+
const before = new CustomEvent("readmore:beforetoggle", {
|
|
159
|
+
bubbles: true,
|
|
160
|
+
cancelable: true,
|
|
161
|
+
detail: {
|
|
162
|
+
expanded,
|
|
163
|
+
instance: this
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
if (!this.el.dispatchEvent(before)) return;
|
|
167
|
+
if (expanded && this.options.height != null) this.el.style.setProperty("--readmore-full-height", `${this.el.scrollHeight}px`);
|
|
140
168
|
this.#expanded = expanded;
|
|
141
169
|
this.el.classList.toggle(CLAMP_CLASS, !this.#expanded);
|
|
142
170
|
if (this.options.height != null) this.el.classList.toggle(CLAMP_HEIGHT_CLASS, !this.#expanded);
|
|
@@ -146,6 +174,7 @@ var ReadMore = class ReadMore {
|
|
|
146
174
|
const otherClass = this.#expanded ? this.options.closingClass : this.options.openingClass;
|
|
147
175
|
this.el.classList.remove(otherClass);
|
|
148
176
|
this.#applyTransientClass(stateClass);
|
|
177
|
+
if (!this.#expanded && this.options.scrollOnCollapse && this.el.getBoundingClientRect().top < 0) this.el.scrollIntoView?.({ block: "nearest" });
|
|
149
178
|
try {
|
|
150
179
|
this.options.onToggle?.(this.#expanded, this);
|
|
151
180
|
} finally {
|
|
@@ -165,8 +194,28 @@ var ReadMore = class ReadMore {
|
|
|
165
194
|
* while expanded, during a toggle transition, or once destroyed.
|
|
166
195
|
*/
|
|
167
196
|
refresh() {
|
|
197
|
+
if (!this.#destroyed) {
|
|
198
|
+
this.#resolveSizes();
|
|
199
|
+
this.#applySizes();
|
|
200
|
+
}
|
|
168
201
|
this.#sync(this.#isOverflowing());
|
|
169
202
|
}
|
|
203
|
+
#resolveSizes() {
|
|
204
|
+
const read = (value) => {
|
|
205
|
+
const result = typeof value === "function" ? value(this.el) : value;
|
|
206
|
+
return typeof result === "string" && result.trim() !== "" ? Number(result) : result;
|
|
207
|
+
};
|
|
208
|
+
const height = read(this.options.height);
|
|
209
|
+
const lines = read(this.options.lines);
|
|
210
|
+
if (height != null && !(Number.isFinite(height) && height > 0)) throw new RangeError("ReadMore: height must be a finite number greater than 0.");
|
|
211
|
+
if (!(Number.isInteger(lines) && lines >= 1)) throw new RangeError("ReadMore: lines must be an integer of at least 1.");
|
|
212
|
+
this.#height = height;
|
|
213
|
+
this.#lines = lines;
|
|
214
|
+
}
|
|
215
|
+
#applySizes() {
|
|
216
|
+
if (this.options.height != null) this.el.style.setProperty("--readmore-height", `${this.#height}px`);
|
|
217
|
+
else this.el.style.setProperty("--readmore-lines", String(this.#lines));
|
|
218
|
+
}
|
|
170
219
|
/**
|
|
171
220
|
* @param element - An element possibly initialized by this plugin.
|
|
172
221
|
* @returns The instance bound to it, or `undefined`.
|
|
@@ -182,6 +231,7 @@ var ReadMore = class ReadMore {
|
|
|
182
231
|
destroy() {
|
|
183
232
|
if (this.#destroyed) return;
|
|
184
233
|
instances.delete(this.el);
|
|
234
|
+
this.el.removeEventListener("focusin", this.#onFocusIn);
|
|
185
235
|
ReadMore.#resizeObserver?.unobserve(this.el);
|
|
186
236
|
if (ReadMore.#live.delete(this) && ReadMore.#live.size === 0) {
|
|
187
237
|
ReadMore.#fonts?.removeEventListener?.("loadingdone", ReadMore.#onFontsLoaded);
|
|
@@ -195,6 +245,7 @@ var ReadMore = class ReadMore {
|
|
|
195
245
|
this.el.classList.remove(CLAMP_CLASS, CLAMP_HEIGHT_CLASS, this.options.expandedClass, this.options.openingClass, this.options.closingClass);
|
|
196
246
|
this.el.style.removeProperty("--readmore-lines");
|
|
197
247
|
this.el.style.removeProperty("--readmore-height");
|
|
248
|
+
this.el.style.removeProperty("--readmore-full-height");
|
|
198
249
|
if (this.#generatedId) {
|
|
199
250
|
this.el.removeAttribute("id");
|
|
200
251
|
this.#generatedId = false;
|
|
@@ -212,13 +263,10 @@ var ReadMore = class ReadMore {
|
|
|
212
263
|
measured.forEach((overflowing, instance) => instance.#sync(overflowing));
|
|
213
264
|
}
|
|
214
265
|
#init() {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
this.el.style.setProperty("--readmore-lines", String(this.options.lines));
|
|
220
|
-
this.el.classList.add(CLAMP_CLASS);
|
|
221
|
-
}
|
|
266
|
+
this.#applySizes();
|
|
267
|
+
this.el.classList.add(CLAMP_CLASS);
|
|
268
|
+
if (this.options.height != null) this.el.classList.add(CLAMP_HEIGHT_CLASS);
|
|
269
|
+
this.el.addEventListener("focusin", this.#onFocusIn);
|
|
222
270
|
ReadMore.#resizeObserver ??= new ResizeObserver(ReadMore.#onResize);
|
|
223
271
|
ReadMore.#resizeObserver.observe(this.el);
|
|
224
272
|
if (ReadMore.#batch) ReadMore.#batch.push(this);
|
|
@@ -244,7 +292,7 @@ var ReadMore = class ReadMore {
|
|
|
244
292
|
});
|
|
245
293
|
}
|
|
246
294
|
#isOverflowing() {
|
|
247
|
-
return this.el.scrollHeight - this.el.clientHeight > 1;
|
|
295
|
+
return this.el.scrollHeight - this.el.clientHeight > Math.max(1, this.options.threshold);
|
|
248
296
|
}
|
|
249
297
|
#sync(overflowing) {
|
|
250
298
|
if (this.#expanded || this.#transientTimer !== null || this.#destroyed) return;
|
package/dist/readmore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readmore.js","names":[],"sources":["../src/readmore.ts"],"sourcesContent":["/**\n * Options accepted by {@link ReadMore}. Every field is optional.\n */\nexport interface ReadMoreOptions {\n /**\n * Number of lines to clamp to. Ignored when {@link ReadMoreOptions.height} is set.\n *\n * @defaultValue 3\n * @throws RangeError - if not an integer greater than or equal to 1.\n */\n lines?: number;\n\n /**\n * Max height in pixels. Takes precedence over {@link ReadMoreOptions.lines}\n * and clamps with `max-height` instead of `-webkit-line-clamp`.\n *\n * @throws RangeError - if not a finite number greater than 0.\n */\n height?: number;\n\n /**\n * Label of the toggle button while the text is collapsed.\n *\n * @defaultValue 'Read more'\n */\n moreText?: string;\n\n /**\n * Label of the toggle button while the text is expanded.\n *\n * @defaultValue 'Read less'\n */\n lessText?: string;\n\n /**\n * Accessible name (`aria-label`) of the toggle button while the text is\n * collapsed, e.g. `'Read more about Product A'` when several buttons share\n * the same visible text. It should contain {@link ReadMoreOptions.moreText}.\n */\n moreLabel?: string;\n\n /**\n * Accessible name (`aria-label`) of the toggle button while the text is\n * expanded. It should contain {@link ReadMoreOptions.lessText}.\n */\n lessLabel?: string;\n\n /**\n * Class applied to the toggle button.\n *\n * @defaultValue 'readmore-btn'\n */\n buttonClass?: string;\n\n /**\n * Element the toggle button is appended to, instead of being inserted right\n * after the clamped element. Use an element owned by your framework (a ref)\n * so reconciliation cannot drop the button.\n *\n * @throws TypeError - if not an `HTMLElement`.\n */\n buttonContainer?: HTMLElement;\n\n /**\n * Class applied to the target element for the whole expanded state.\n *\n * @defaultValue 'is-expanded'\n */\n expandedClass?: string;\n\n /**\n * Transient class applied when expanding, removed once the longest CSS\n * transition on the element (duration + delay) has elapsed — or on the next\n * tick if no transition is declared.\n *\n * @defaultValue 'is-opening'\n */\n openingClass?: string;\n\n /**\n * Transient class applied when collapsing, removed once the longest CSS\n * transition on the element (duration + delay) has elapsed — or on the next\n * tick if no transition is declared.\n *\n * @defaultValue 'is-closing'\n */\n closingClass?: string;\n\n /**\n * Called after each toggle with the resulting state, right before the\n * `readmore:toggle` event is dispatched.\n *\n * @param expanded - `true` once expanded, `false` once collapsed.\n * @param instance - The instance that was toggled.\n */\n onToggle?: (expanded: boolean, instance: ReadMore) => void;\n}\n\ntype OptionalKeys = 'height' | 'buttonContainer' | 'moreLabel' | 'lessLabel' | 'onToggle';\n\n/**\n * {@link ReadMoreOptions} merged with the defaults, as exposed on\n * {@link ReadMore.options}.\n */\nexport type ResolvedOptions = Required<Omit<ReadMoreOptions, OptionalKeys>> & Pick<ReadMoreOptions, OptionalKeys>;\n\n/** `detail` of the `readmore:toggle` event. */\nexport interface ReadMoreToggleDetail {\n /** `true` once expanded, `false` once collapsed. */\n expanded: boolean;\n\n /** The instance that was toggled. */\n instance: ReadMore;\n}\n\n// Types `event.detail` in `addEventListener('readmore:toggle', …)`, on elements\n// and on `document` (the event bubbles).\ndeclare global {\n interface HTMLElementEventMap {\n 'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;\n }\n\n interface DocumentEventMap {\n 'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;\n }\n}\n\nconst CLAMP_CLASS = 'readmore-clamp';\nconst CLAMP_HEIGHT_CLASS = 'readmore-clamp--height';\n\nconst instances = new WeakMap<HTMLElement, ReadMore>();\nlet idCounter = 0;\n\nconst DEFAULTS: ResolvedOptions = {\n lines: 3,\n moreText: 'Read more',\n lessText: 'Read less',\n buttonClass: 'readmore-btn',\n expandedClass: 'is-expanded',\n openingClass: 'is-opening',\n closingClass: 'is-closing'\n};\n\n/**\n * Parses a computed `transition-duration` / `transition-delay` list into\n * milliseconds. Computed times are always serialized in seconds.\n */\nfunction toMs(list: string): number[] {\n return list.split(',').map((v) => parseFloat(v) * 1000 || 0);\n}\n\n/**\n * Clamps an element to a number of lines (or a fixed height) and mounts a\n * `Read more` / `Read less` button right after it, but only while the content\n * actually overflows.\n *\n * @example\n * ```ts\n * import { ReadMore } from '@frontfactory/readmore';\n * import '@frontfactory/readmore/style.css';\n *\n * ReadMore.init('.excerpt', { lines: 3 });\n * ```\n */\nexport class ReadMore {\n /** Shared by every instance, created with the first one. */\n static #resizeObserver: ResizeObserver | undefined;\n\n /** Instances created by the running {@link ReadMore.init}, synced together once all exist. */\n static #batch: ReadMore[] | null = null;\n\n /** Instances waiting for `document.fonts.ready`, measured together. */\n static #fontQueue: ReadMore[] | null = null;\n\n /** Live instances resynced by the shared `document.fonts` `loadingdone` listener. */\n static #live = new Set<ReadMore>();\n\n /** The `document.fonts` the shared listener is attached to. */\n static #fonts: FontFaceSet | undefined;\n\n /** The clamped element. */\n readonly el: HTMLElement;\n\n /** The options this instance was built with, merged with the defaults. */\n readonly options: Readonly<ResolvedOptions>;\n\n #button: HTMLButtonElement | null = null;\n #expanded = false;\n #generatedId = false;\n #transientTimer: ReturnType<typeof setTimeout> | null = null;\n #onClick = (): void => this.toggle();\n\n // Destroyed once the element is no longer bound to this instance: after\n // destroy(), or if the element was re-initialized by another instance.\n get #destroyed(): boolean {\n return instances.get(this.el) !== this;\n }\n\n /**\n * @param element - The element to clamp.\n * @param options - See {@link ReadMoreOptions}.\n * @throws TypeError - if `element` is not an `HTMLElement`, or there is no\n * `HTMLElement` at all (no browser, e.g. SSR).\n * @throws Error - if `element` is already initialized; call\n * {@link ReadMore.destroy} first.\n * @throws RangeError - if `lines` is not an integer greater than or equal\n * to 1, or `height` is not a finite number greater than 0.\n * @throws RangeError - if `expandedClass`, `openingClass` or `closingClass`\n * is not a string or contains whitespace (an empty string means the default).\n * @throws TypeError - if `buttonContainer` is set but is not an `HTMLElement`.\n */\n constructor(element: HTMLElement, options: ReadMoreOptions = {}) {\n if (typeof HTMLElement === 'undefined' || !(element instanceof HTMLElement)) {\n throw new TypeError('ReadMore: an HTMLElement is required.');\n }\n\n if (instances.has(element)) {\n throw new Error('ReadMore: element is already initialized. Call destroy() first.');\n }\n\n this.el = element;\n // Options explicitly set to `undefined` (or an empty state class) must not override the defaults.\n this.options = {\n ...DEFAULTS,\n ...Object.fromEntries(Object.entries(options).filter((entry) => entry[1] !== undefined\n && !(entry[1] === '' && entry[0].endsWith('Class'))))\n };\n\n // Numeric strings (data-* attributes, framework props) are accepted; blank or non-numeric ones still fail below.\n for (const key of [\n 'height',\n 'lines'\n ] as const) {\n const value: unknown = this.options[key];\n\n if (typeof value === 'string' && value.trim() !== '') {\n (this.options as Record<string, unknown>)[key] = Number(value);\n }\n }\n\n const { height, lines, buttonContainer } = this.options;\n\n if (buttonContainer != null && !(buttonContainer instanceof HTMLElement)) {\n throw new TypeError('ReadMore: buttonContainer must be an HTMLElement.');\n }\n\n if (height != null && !(Number.isFinite(height) && height > 0)) {\n throw new RangeError('ReadMore: height must be a finite number greater than 0.');\n }\n\n if (!(Number.isInteger(lines) && lines >= 1)) {\n throw new RangeError('ReadMore: lines must be an integer of at least 1.');\n }\n\n for (const key of [\n 'expandedClass',\n 'openingClass',\n 'closingClass'\n ] as const) {\n const value: unknown = this.options[key];\n\n if (typeof value !== 'string' || !/^\\S+$/.test(value)) {\n throw new RangeError(`ReadMore: ${key} must be a string without whitespace.`);\n }\n }\n\n instances.set(element, this);\n this.#init();\n }\n\n /**\n * Instantiates {@link ReadMore} on every matching element. Elements that are\n * already initialized are left untouched and their existing instance is\n * returned, so it is safe to call again after new content is added.\n *\n * @param target - A CSS selector, an element, or any iterable or array-like\n * of elements (`NodeList`, `HTMLCollection`, array, `Set`…).\n * @param options - See {@link ReadMoreOptions}. Not applied to elements\n * that are already initialized.\n * @param root - Where to look for `target` when it is a selector. Defaults to `document`.\n * @returns One instance per element, in document order; `[]` without a\n * browser (e.g. SSR).\n */\n static init(\n target: string | HTMLElement | Iterable<HTMLElement> | ArrayLike<HTMLElement>,\n options?: ReadMoreOptions,\n root?: ParentNode\n ): ReadMore[] {\n if (typeof HTMLElement === 'undefined' || typeof document === 'undefined') {\n return [];\n }\n\n const nodes =\n typeof target === 'string'\n ? (root ?? document).querySelectorAll<HTMLElement>(target)\n : target instanceof HTMLElement ? [\n target\n ] : target;\n\n const batch: ReadMore[] = [];\n\n ReadMore.#batch = batch;\n\n try {\n return Array.from(nodes).map((n) => instances.get(n) ?? new ReadMore(n, options));\n } finally {\n ReadMore.#batch = null;\n ReadMore.#syncAll(batch);\n }\n }\n\n /** Whether the text is currently expanded. */\n get expanded(): boolean {\n return this.#expanded;\n }\n\n /**\n * Expands or collapses the text, updating the button label, the\n * `aria-expanded` attribute and the state classes, then calls\n * {@link ReadMoreOptions.onToggle} and dispatches a bubbling\n * `readmore:toggle` event on the element. Does nothing once destroyed.\n *\n * @param force - `true` to expand, `false` to collapse; the state is\n * inverted when omitted. Does nothing if the text is already in the\n * requested state.\n */\n toggle(force?: boolean): void {\n // Only a real boolean forces the state, so `toggle` still works as an\n // event listener (`addEventListener('click', rm.toggle.bind(rm))`).\n const expanded = typeof force === 'boolean' ? force : !this.#expanded;\n\n if (this.#destroyed || expanded === this.#expanded) {\n return;\n }\n\n this.#expanded = expanded;\n this.el.classList.toggle(CLAMP_CLASS, !this.#expanded);\n\n if (this.options.height != null) {\n this.el.classList.toggle(CLAMP_HEIGHT_CLASS, !this.#expanded);\n }\n\n this.el.classList.toggle(this.options.expandedClass, this.#expanded);\n this.#updateButton();\n\n const stateClass = this.#expanded ? this.options.openingClass : this.options.closingClass;\n const otherClass = this.#expanded ? this.options.closingClass : this.options.openingClass;\n\n this.el.classList.remove(otherClass);\n this.#applyTransientClass(stateClass);\n\n try {\n this.options.onToggle?.(this.#expanded, this);\n } finally {\n // The event is dispatched even if `onToggle` throws (the error still propagates).\n this.el.dispatchEvent(new CustomEvent<ReadMoreToggleDetail>('readmore:toggle', {\n bubbles: true,\n detail: {\n expanded: this.#expanded,\n instance: this\n }\n }));\n }\n }\n\n /**\n * Checks again whether the content overflows, mounting or unmounting the\n * button. Resizes of the element are already tracked; call it after\n * changing the content in a way that keeps the element's size. Does nothing\n * while expanded, during a toggle transition, or once destroyed.\n */\n refresh(): void {\n this.#sync(this.#isOverflowing());\n }\n\n /**\n * @param element - An element possibly initialized by this plugin.\n * @returns The instance bound to it, or `undefined`.\n */\n static getInstance(element: HTMLElement): ReadMore | undefined {\n return instances.get(element);\n }\n\n /**\n * Restores the element: removes the button, the classes, the CSS custom\n * properties, the listeners and the generated `id`. The element can then be\n * initialized again. Calling it again does nothing.\n */\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n instances.delete(this.el);\n ReadMore.#resizeObserver?.unobserve(this.el);\n\n if (ReadMore.#live.delete(this) && ReadMore.#live.size === 0) {\n ReadMore.#fonts?.removeEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n ReadMore.#fonts = undefined;\n }\n\n if (this.#transientTimer !== null) {\n clearTimeout(this.#transientTimer);\n this.#transientTimer = null;\n }\n\n this.#unmountButton();\n this.el.classList.remove(\n CLAMP_CLASS,\n CLAMP_HEIGHT_CLASS,\n this.options.expandedClass,\n this.options.openingClass,\n this.options.closingClass\n );\n\n this.el.style.removeProperty('--readmore-lines');\n this.el.style.removeProperty('--readmore-height');\n\n if (this.#generatedId) {\n this.el.removeAttribute('id');\n this.#generatedId = false;\n }\n }\n\n // Measures every resized element before touching the DOM, so mounting a\n // button does not force a new layout for each following element.\n static #onResize(entries: ResizeObserverEntry[]): void {\n ReadMore.#syncAll(entries.map((entry) => instances.get(entry.target as HTMLElement)));\n }\n\n static #onFontsLoaded = (): void => {\n ReadMore.#syncAll(Array.from(ReadMore.#live));\n };\n\n static #syncAll(list: (ReadMore | undefined)[]): void {\n const measured = new Map<ReadMore, boolean>();\n\n for (const instance of list) {\n if (instance) {\n measured.set(instance, instance.#isOverflowing());\n }\n }\n\n measured.forEach((overflowing, instance) => instance.#sync(overflowing));\n }\n\n #init(): void {\n if (this.options.height != null) {\n this.el.style.setProperty('--readmore-height', `${ this.options.height }px`);\n this.el.classList.add(CLAMP_CLASS, CLAMP_HEIGHT_CLASS);\n } else {\n this.el.style.setProperty('--readmore-lines', String(this.options.lines));\n this.el.classList.add(CLAMP_CLASS);\n }\n\n ReadMore.#resizeObserver ??= new ResizeObserver(ReadMore.#onResize);\n ReadMore.#resizeObserver.observe(this.el);\n\n // Inside init(), the batch measures all elements at once afterwards.\n if (ReadMore.#batch) {\n ReadMore.#batch.push(this);\n } else {\n this.refresh();\n }\n\n this.#refreshOnFonts();\n }\n\n // A font swap can change the overflow without resizing the element, which the\n // ResizeObserver would miss. Instances created in the same tick share one\n // measurement; destroyed ones are skipped by #sync.\n #refreshOnFonts(): void {\n if (typeof document === 'undefined' || !document.fonts) {\n return;\n }\n\n // Fonts loaded later (after `ready`) are caught by one shared listener.\n if (ReadMore.#fonts !== document.fonts) {\n ReadMore.#fonts?.removeEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n ReadMore.#fonts = document.fonts;\n ReadMore.#fonts.addEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n }\n\n ReadMore.#live.add(this);\n\n if (ReadMore.#fontQueue) {\n ReadMore.#fontQueue.push(this);\n\n return;\n }\n\n const queue: ReadMore[] = ReadMore.#fontQueue = [\n this\n ];\n\n void document.fonts.ready.then(() => {\n ReadMore.#fontQueue = null;\n ReadMore.#syncAll(queue.filter((instance) => !instance.#destroyed));\n });\n }\n\n #isOverflowing(): boolean {\n return this.el.scrollHeight - this.el.clientHeight > 1;\n }\n\n #sync(overflowing: boolean): void {\n // While a toggle transition runs, the element's size is not final: measuring\n // it mid-collapse would remove the button. The timer refreshes once it ends.\n if (this.#expanded || this.#transientTimer !== null || this.#destroyed) {\n return;\n }\n\n if (overflowing && !this.#button) {\n this.#mountButton();\n } else if (!overflowing && this.#button) {\n this.#unmountButton();\n }\n }\n\n #applyTransientClass(stateClass: string): void {\n if (this.#transientTimer !== null) {\n clearTimeout(this.#transientTimer);\n }\n\n this.el.classList.add(stateClass);\n\n // A timer rather than `transitionend`, which never fires when no declared\n // property actually changes (e.g. `transition: all` in lines mode).\n const timer = setTimeout(() => {\n // Some engines (WebKit) start the transition later than the timer assumes:\n // wait for it to end, or the size is measured mid-way and the button remounted.\n const running = this.el.getAnimations?.().filter(a => a instanceof CSSTransition) ?? [];\n const done = () => {\n // Superseded by another toggle, or destroyed meanwhile.\n if (this.#transientTimer !== timer) {\n return;\n }\n\n this.#transientTimer = null;\n this.el.classList.remove(stateClass);\n this.refresh();\n };\n\n if (running.length) {\n Promise.allSettled(running.map(a => a.finished)).then(done);\n } else {\n done();\n }\n }, this.#transitionTime());\n\n this.#transientTimer = timer;\n }\n\n /** Longest `duration + delay` among the transitions declared on the element, in ms. */\n #transitionTime(): number {\n const style = getComputedStyle(this.el);\n const durations = toMs(style.transitionDuration);\n const delays = toMs(style.transitionDelay);\n let max = 0;\n\n // Shorter lists repeat to match the longer one, as CSS does.\n for (let i = 0; i < Math.max(durations.length, delays.length); i++) {\n max = Math.max(max, durations[i % durations.length] + delays[i % delays.length]);\n }\n\n return max;\n }\n\n #mountButton(): void {\n if (!this.el.id) {\n let id: string;\n\n // Skip ids already used in the page (server-rendered markup, another copy of the plugin…).\n do {\n id = `readmore-${++idCounter}`;\n } while (document.getElementById(id));\n\n this.el.id = id;\n this.#generatedId = true;\n }\n\n const btn = document.createElement('button');\n\n btn.type = 'button';\n btn.className = this.options.buttonClass;\n btn.setAttribute('aria-controls', this.el.id);\n btn.addEventListener('click', this.#onClick);\n this.#button = btn;\n this.#updateButton();\n\n if (this.options.buttonContainer) {\n this.options.buttonContainer.appendChild(btn);\n } else {\n this.el.insertAdjacentElement('afterend', btn);\n }\n }\n\n /** Syncs the button's text, `aria-expanded` and `aria-label` with the state. */\n #updateButton(): void {\n if (!this.#button) {\n return;\n }\n\n const { moreText, lessText, moreLabel, lessLabel } = this.options;\n const label = this.#expanded ? lessLabel : moreLabel;\n\n this.#button.textContent = this.#expanded ? lessText : moreText;\n this.#button.setAttribute('aria-expanded', String(this.#expanded));\n\n if (label) {\n this.#button.setAttribute('aria-label', label);\n } else {\n this.#button.removeAttribute('aria-label');\n }\n }\n\n #unmountButton(): void {\n if (!this.#button) {\n return;\n }\n\n this.#button.removeEventListener('click', this.#onClick);\n\n // Removing the focused button would drop focus to <body>: keep it on the element.\n if (document.activeElement === this.#button) {\n const { el } = this;\n\n if (!el.hasAttribute('tabindex')) {\n el.tabIndex = -1;\n el.addEventListener('blur', () => el.removeAttribute('tabindex'), {\n once: true\n });\n }\n\n el.focus();\n }\n\n this.#button.remove();\n this.#button = null;\n }\n}\n"],"mappings":";AA+HA,IAAM,cAAc;AACpB,IAAM,qBAAqB;AAE3B,IAAM,4BAAY,IAAI,QAA+B;AACrD,IAAI,YAAY;AAEhB,IAAM,WAA4B;CAC9B,OAAO;CACP,UAAU;CACV,UAAU;CACV,aAAa;CACb,eAAe;CACf,cAAc;CACd,cAAc;AAClB;;;;;AAMA,SAAS,KAAK,MAAwB;CAClC,OAAO,KAAK,MAAM,GAAG,CAAC,CAAC,KAAK,MAAM,WAAW,CAAC,IAAI,OAAQ,CAAC;AAC/D;;;;;;;;;;;;;;AAeA,IAAa,WAAb,MAAa,SAAS;;CAElB,OAAO;;CAGP,OAAO,SAA4B;;CAGnC,OAAO,aAAgC;;CAGvC,OAAO,wBAAQ,IAAI,IAAc;;CAGjC,OAAO;;CAGP;;CAGA;CAEA,UAAoC;CACpC,YAAY;CACZ,eAAe;CACf,kBAAwD;CACxD,iBAAuB,KAAK,OAAO;CAInC,IAAI,aAAsB;EACtB,OAAO,UAAU,IAAI,KAAK,EAAE,MAAM;CACtC;;;;;;;;;;;;;;CAeA,YAAY,SAAsB,UAA2B,CAAC,GAAG;EAC7D,IAAI,OAAO,gBAAgB,eAAe,EAAE,mBAAmB,cAC3D,MAAM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BAAiB,OAAO,KAAK,SAAS,CAAC;EAEjE,IAAI,OACA,KAAK,QAAQ,aAAa,cAAc,KAAK;OAE7C,KAAK,QAAQ,gBAAgB,YAAY;CAEjD;CAEA,iBAAuB;EACnB,IAAI,CAAC,KAAK,SACN;EAGJ,KAAK,QAAQ,oBAAoB,SAAS,KAAK,QAAQ;EAGvD,IAAI,SAAS,kBAAkB,KAAK,SAAS;GACzC,MAAM,EAAE,OAAO;GAEf,IAAI,CAAC,GAAG,aAAa,UAAU,GAAG;IAC9B,GAAG,WAAW;IACd,GAAG,iBAAiB,cAAc,GAAG,gBAAgB,UAAU,GAAG,EAC9D,MAAM,KACV,CAAC;GACL;GAEA,GAAG,MAAM;EACb;EAEA,KAAK,QAAQ,OAAO;EACpB,KAAK,UAAU;CACnB;AACJ"}
|
|
1
|
+
{"version":3,"file":"readmore.js","names":[],"sources":["../src/readmore.ts"],"sourcesContent":["/**\n * Options accepted by {@link ReadMore}. Every field is optional.\n */\nexport interface ReadMoreOptions {\n /**\n * Number of lines to clamp to. Ignored when {@link ReadMoreOptions.height} is set.\n *\n * @defaultValue 3\n * Can also be a function called with the element; the returned value is\n * validated like a literal one and re-evaluated on {@link ReadMore.refresh}.\n *\n * @throws RangeError - if not an integer greater than or equal to 1.\n */\n lines?: number | ((el: HTMLElement) => number);\n\n /**\n * Max height in pixels. Takes precedence over {@link ReadMoreOptions.lines}\n * and clamps with `max-height` instead of `-webkit-line-clamp`.\n * Can also be a function called with the element; the returned value is\n * validated like a literal one and re-evaluated on {@link ReadMore.refresh}.\n *\n * @throws RangeError - if not a finite number greater than 0.\n */\n height?: number | ((el: HTMLElement) => number);\n\n /**\n * Hidden overflow in pixels tolerated before the button is mounted: the\n * button only appears when more than this is hidden (at least 1px).\n *\n * @defaultValue 0\n * @throws RangeError - if not a finite number greater than or equal to 0.\n */\n threshold?: number;\n\n /**\n * Label of the toggle button while the text is collapsed.\n *\n * @defaultValue 'Read more'\n */\n moreText?: string;\n\n /**\n * Label of the toggle button while the text is expanded.\n *\n * @defaultValue 'Read less'\n */\n lessText?: string;\n\n /**\n * Accessible name (`aria-label`) of the toggle button while the text is\n * collapsed, e.g. `'Read more about Product A'` when several buttons share\n * the same visible text. It should contain {@link ReadMoreOptions.moreText}.\n */\n moreLabel?: string;\n\n /**\n * Accessible name (`aria-label`) of the toggle button while the text is\n * expanded. It should contain {@link ReadMoreOptions.lessText}.\n */\n lessLabel?: string;\n\n /**\n * Class applied to the toggle button.\n *\n * @defaultValue 'readmore-btn'\n */\n buttonClass?: string;\n\n /**\n * Element the toggle button is appended to, instead of being inserted right\n * after the clamped element. Use an element owned by your framework (a ref)\n * so reconciliation cannot drop the button.\n *\n * @throws TypeError - if not an `HTMLElement`.\n */\n buttonContainer?: HTMLElement;\n\n /**\n * Class applied to the target element for the whole expanded state.\n *\n * @defaultValue 'is-expanded'\n */\n expandedClass?: string;\n\n /**\n * Transient class applied when expanding, removed once the longest CSS\n * transition on the element (duration + delay) has elapsed — or on the next\n * tick if no transition is declared.\n *\n * @defaultValue 'is-opening'\n */\n openingClass?: string;\n\n /**\n * Transient class applied when collapsing, removed once the longest CSS\n * transition on the element (duration + delay) has elapsed — or on the next\n * tick if no transition is declared.\n *\n * @defaultValue 'is-closing'\n */\n closingClass?: string;\n\n /**\n * Scrolls the element back into view when collapsing, if its top edge ended\n * up above the viewport.\n *\n * @defaultValue false\n */\n scrollOnCollapse?: boolean;\n\n /**\n * Called after each toggle with the resulting state, right before the\n * `readmore:toggle` event is dispatched.\n *\n * @param expanded - `true` once expanded, `false` once collapsed.\n * @param instance - The instance that was toggled.\n */\n onToggle?: (expanded: boolean, instance: ReadMore) => void;\n\n /**\n * Called before a toggle that would really happen, with the state it is\n * about to enter. Returning exactly `false` cancels it (the\n * `readmore:beforetoggle` event is then not dispatched). If it throws, the\n * error propagates and the toggle does not happen.\n *\n * @param expanded - `true` if about to expand, `false` if about to collapse.\n * @param instance - The instance about to be toggled.\n */\n onBeforeToggle?: (expanded: boolean, instance: ReadMore) => boolean | void;\n}\n\ntype OptionalKeys = 'height' | 'buttonContainer' | 'moreLabel' | 'lessLabel' | 'onToggle' | 'onBeforeToggle';\n\n/**\n * {@link ReadMoreOptions} merged with the defaults, as exposed on\n * {@link ReadMore.options}.\n */\nexport type ResolvedOptions = Required<Omit<ReadMoreOptions, OptionalKeys>> & Pick<ReadMoreOptions, OptionalKeys>;\n\n/** `detail` of the `readmore:toggle` event. */\nexport interface ReadMoreToggleDetail {\n /** `true` once expanded, `false` once collapsed. */\n expanded: boolean;\n\n /** The instance that was toggled. */\n instance: ReadMore;\n}\n\n// Types `event.detail` in `addEventListener('readmore:toggle', …)`, on elements\n// and on `document` (the event bubbles). `readmore:beforetoggle` is cancelable\n// and carries the state about to be entered.\ndeclare global {\n interface HTMLElementEventMap {\n 'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;\n 'readmore:beforetoggle': CustomEvent<ReadMoreToggleDetail>;\n }\n\n interface DocumentEventMap {\n 'readmore:toggle': CustomEvent<ReadMoreToggleDetail>;\n 'readmore:beforetoggle': CustomEvent<ReadMoreToggleDetail>;\n }\n}\n\nconst CLAMP_CLASS = 'readmore-clamp';\nconst CLAMP_HEIGHT_CLASS = 'readmore-clamp--height';\n\nconst instances = new WeakMap<HTMLElement, ReadMore>();\nlet idCounter = 0;\n\nconst DEFAULTS: ResolvedOptions = {\n lines: 3,\n threshold: 0,\n moreText: 'Read more',\n lessText: 'Read less',\n buttonClass: 'readmore-btn',\n expandedClass: 'is-expanded',\n openingClass: 'is-opening',\n closingClass: 'is-closing',\n scrollOnCollapse: false\n};\n\n/**\n * Parses a computed `transition-duration` / `transition-delay` list into\n * milliseconds. Computed times are always serialized in seconds.\n */\nfunction toMs(list: string): number[] {\n return list.split(',').map((v) => parseFloat(v) * 1000 || 0);\n}\n\n/**\n * Clamps an element to a number of lines (or a fixed height) and mounts a\n * `Read more` / `Read less` button right after it, but only while the content\n * actually overflows. Focus landing on an element in the hidden part expands\n * the text.\n *\n * @example\n * ```ts\n * import { ReadMore } from '@frontfactory/readmore';\n * import '@frontfactory/readmore/style.css';\n *\n * ReadMore.init('.excerpt', { lines: 3 });\n * ```\n */\nexport class ReadMore {\n /** Shared by every instance, created with the first one. */\n static #resizeObserver: ResizeObserver | undefined;\n\n /** Instances created by the running {@link ReadMore.init}, synced together once all exist. */\n static #batch: ReadMore[] | null = null;\n\n /** Instances waiting for `document.fonts.ready`, measured together. */\n static #fontQueue: ReadMore[] | null = null;\n\n /** Live instances resynced by the shared `document.fonts` `loadingdone` listener. */\n static #live = new Set<ReadMore>();\n\n /** The `document.fonts` the shared listener is attached to. */\n static #fonts: FontFaceSet | undefined;\n\n /** The clamped element. */\n readonly el: HTMLElement;\n\n /** The options this instance was built with, merged with the defaults. */\n readonly options: Readonly<ResolvedOptions>;\n\n #button: HTMLButtonElement | null = null;\n #expanded = false;\n #generatedId = false;\n #transientTimer: ReturnType<typeof setTimeout> | null = null;\n #onClick = (): void => this.toggle();\n #lines = DEFAULTS.lines as number;\n #height: number | undefined;\n\n #onFocusIn = (e: FocusEvent): void => {\n const { target } = e;\n\n if (this.#expanded || !this.#button || target === this.el || !(target instanceof Element)) {\n return;\n }\n\n // The browser may already have scrolled the hidden overflow to reveal the target: add it back.\n if (target.getBoundingClientRect().bottom + this.el.scrollTop > this.el.getBoundingClientRect().bottom + 1) {\n this.toggle(true);\n }\n };\n\n // Destroyed once the element is no longer bound to this instance: after\n // destroy(), or if the element was re-initialized by another instance.\n get #destroyed(): boolean {\n return instances.get(this.el) !== this;\n }\n\n /**\n * @param element - The element to clamp.\n * @param options - See {@link ReadMoreOptions}.\n * @throws TypeError - if `element` is not an `HTMLElement`, or there is no\n * `HTMLElement` at all (no browser, e.g. SSR).\n * @throws Error - if `element` is already initialized; call\n * {@link ReadMore.destroy} first.\n * @throws RangeError - if `lines` is not an integer greater than or equal\n * to 1, `height` is not a finite number greater than 0, or `threshold` is\n * not a finite number greater than or equal to 0 (for `lines` and `height`,\n * the value returned by a function is checked).\n * @throws RangeError - if `expandedClass`, `openingClass` or `closingClass`\n * is not a string or contains whitespace (an empty string means the default).\n * @throws TypeError - if `buttonContainer` is set but is not an `HTMLElement`.\n */\n constructor(element: HTMLElement, options: ReadMoreOptions = {}) {\n if (typeof HTMLElement === 'undefined' || !(element instanceof HTMLElement)) {\n throw new TypeError('ReadMore: an HTMLElement is required.');\n }\n\n if (instances.has(element)) {\n throw new Error('ReadMore: element is already initialized. Call destroy() first.');\n }\n\n this.el = element;\n // Options explicitly set to `undefined` (or an empty state class) must not override the defaults.\n this.options = {\n ...DEFAULTS,\n ...Object.fromEntries(Object.entries(options).filter((entry) => entry[1] !== undefined\n && !(entry[1] === '' && entry[0].endsWith('Class'))))\n };\n\n // Numeric strings (data-* attributes, framework props) are accepted; blank or non-numeric ones still fail below.\n for (const key of [\n 'height',\n 'lines',\n 'threshold'\n ] as const) {\n const value: unknown = this.options[key];\n\n if (typeof value === 'string' && value.trim() !== '') {\n (this.options as Record<string, unknown>)[key] = Number(value);\n }\n }\n\n const { threshold, buttonContainer } = this.options;\n\n if (buttonContainer != null && !(buttonContainer instanceof HTMLElement)) {\n throw new TypeError('ReadMore: buttonContainer must be an HTMLElement.');\n }\n\n this.#resolveSizes();\n\n if (!(Number.isFinite(threshold) && threshold >= 0)) {\n throw new RangeError('ReadMore: threshold must be a finite number of at least 0.');\n }\n\n for (const key of [\n 'expandedClass',\n 'openingClass',\n 'closingClass'\n ] as const) {\n const value: unknown = this.options[key];\n\n if (typeof value !== 'string' || !/^\\S+$/.test(value)) {\n throw new RangeError(`ReadMore: ${key} must be a string without whitespace.`);\n }\n }\n\n instances.set(element, this);\n this.#init();\n }\n\n /**\n * Instantiates {@link ReadMore} on every matching element. Elements that are\n * already initialized are left untouched and their existing instance is\n * returned, so it is safe to call again after new content is added.\n *\n * @param target - A CSS selector, an element, or any iterable or array-like\n * of elements (`NodeList`, `HTMLCollection`, array, `Set`…).\n * @param options - See {@link ReadMoreOptions}. Not applied to elements\n * that are already initialized.\n * @param root - Where to look for `target` when it is a selector. Defaults to `document`.\n * @returns One instance per element, in document order; `[]` without a\n * browser (e.g. SSR).\n */\n static init(\n target: string | HTMLElement | Iterable<HTMLElement> | ArrayLike<HTMLElement>,\n options?: ReadMoreOptions,\n root?: ParentNode\n ): ReadMore[] {\n if (typeof HTMLElement === 'undefined' || typeof document === 'undefined') {\n return [];\n }\n\n const nodes =\n typeof target === 'string'\n ? (root ?? document).querySelectorAll<HTMLElement>(target)\n : target instanceof HTMLElement ? [\n target\n ] : target;\n\n const batch: ReadMore[] = [];\n\n ReadMore.#batch = batch;\n\n try {\n return Array.from(nodes).map((n) => instances.get(n) ?? new ReadMore(n, options));\n } finally {\n ReadMore.#batch = null;\n ReadMore.#syncAll(batch);\n }\n }\n\n /** Whether the text is currently expanded. */\n get expanded(): boolean {\n return this.#expanded;\n }\n\n /**\n * Expands or collapses the text, updating the button label, the\n * `aria-expanded` attribute and the state classes, then calls\n * {@link ReadMoreOptions.onToggle} and dispatches a bubbling\n * `readmore:toggle` event on the element. Does nothing once destroyed.\n *\n * @param force - `true` to expand, `false` to collapse; the state is\n * inverted when omitted. Does nothing if the text is already in the\n * requested state. Before a real toggle, {@link ReadMoreOptions.onBeforeToggle}\n * and a cancelable `readmore:beforetoggle` event can cancel it.\n */\n toggle(force?: boolean): void {\n // Only a real boolean forces the state, so `toggle` still works as an\n // event listener (`addEventListener('click', rm.toggle.bind(rm))`).\n const expanded = typeof force === 'boolean' ? force : !this.#expanded;\n\n if (this.#destroyed || expanded === this.#expanded) {\n return;\n }\n\n if (this.options.onBeforeToggle?.(expanded, this) === false) {\n return;\n }\n\n const before = new CustomEvent<ReadMoreToggleDetail>('readmore:beforetoggle', {\n bubbles: true,\n cancelable: true,\n detail: {\n expanded,\n instance: this\n }\n });\n\n if (!this.el.dispatchEvent(before)) {\n return;\n }\n\n // Measured while still clamped: scrollHeight is then the full content height.\n if (expanded && this.options.height != null) {\n this.el.style.setProperty('--readmore-full-height', `${ this.el.scrollHeight }px`);\n }\n\n this.#expanded = expanded;\n this.el.classList.toggle(CLAMP_CLASS, !this.#expanded);\n\n if (this.options.height != null) {\n this.el.classList.toggle(CLAMP_HEIGHT_CLASS, !this.#expanded);\n }\n\n this.el.classList.toggle(this.options.expandedClass, this.#expanded);\n this.#updateButton();\n\n const stateClass = this.#expanded ? this.options.openingClass : this.options.closingClass;\n const otherClass = this.#expanded ? this.options.closingClass : this.options.openingClass;\n\n this.el.classList.remove(otherClass);\n this.#applyTransientClass(stateClass);\n\n if (!this.#expanded && this.options.scrollOnCollapse && this.el.getBoundingClientRect().top < 0) {\n this.el.scrollIntoView?.({\n block: 'nearest'\n });\n }\n\n try {\n this.options.onToggle?.(this.#expanded, this);\n } finally {\n // The event is dispatched even if `onToggle` throws (the error still propagates).\n this.el.dispatchEvent(new CustomEvent<ReadMoreToggleDetail>('readmore:toggle', {\n bubbles: true,\n detail: {\n expanded: this.#expanded,\n instance: this\n }\n }));\n }\n }\n\n /**\n * Checks again whether the content overflows, mounting or unmounting the\n * button. Resizes of the element are already tracked; call it after\n * changing the content in a way that keeps the element's size. Does nothing\n * while expanded, during a toggle transition, or once destroyed.\n */\n refresh(): void {\n if (!this.#destroyed) {\n this.#resolveSizes();\n this.#applySizes();\n }\n\n this.#sync(this.#isOverflowing());\n }\n\n // Evaluates function values of `lines` / `height` (on the element), then validates like literals.\n #resolveSizes(): void {\n const read = (value: unknown): unknown => {\n const result = typeof value === 'function' ? value(this.el) : value;\n\n return typeof result === 'string' && result.trim() !== '' ? Number(result) : result;\n };\n\n const height = read(this.options.height) as number | undefined;\n const lines = read(this.options.lines) as number;\n\n if (height != null && !(Number.isFinite(height) && height > 0)) {\n throw new RangeError('ReadMore: height must be a finite number greater than 0.');\n }\n\n if (!(Number.isInteger(lines) && lines >= 1)) {\n throw new RangeError('ReadMore: lines must be an integer of at least 1.');\n }\n\n this.#height = height;\n this.#lines = lines;\n }\n\n #applySizes(): void {\n if (this.options.height != null) {\n this.el.style.setProperty('--readmore-height', `${ this.#height }px`);\n } else {\n this.el.style.setProperty('--readmore-lines', String(this.#lines));\n }\n }\n\n /**\n * @param element - An element possibly initialized by this plugin.\n * @returns The instance bound to it, or `undefined`.\n */\n static getInstance(element: HTMLElement): ReadMore | undefined {\n return instances.get(element);\n }\n\n /**\n * Restores the element: removes the button, the classes, the CSS custom\n * properties, the listeners and the generated `id`. The element can then be\n * initialized again. Calling it again does nothing.\n */\n destroy(): void {\n if (this.#destroyed) {\n return;\n }\n\n instances.delete(this.el);\n this.el.removeEventListener('focusin', this.#onFocusIn);\n ReadMore.#resizeObserver?.unobserve(this.el);\n\n if (ReadMore.#live.delete(this) && ReadMore.#live.size === 0) {\n ReadMore.#fonts?.removeEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n ReadMore.#fonts = undefined;\n }\n\n if (this.#transientTimer !== null) {\n clearTimeout(this.#transientTimer);\n this.#transientTimer = null;\n }\n\n this.#unmountButton();\n this.el.classList.remove(\n CLAMP_CLASS,\n CLAMP_HEIGHT_CLASS,\n this.options.expandedClass,\n this.options.openingClass,\n this.options.closingClass\n );\n\n this.el.style.removeProperty('--readmore-lines');\n this.el.style.removeProperty('--readmore-height');\n this.el.style.removeProperty('--readmore-full-height');\n\n if (this.#generatedId) {\n this.el.removeAttribute('id');\n this.#generatedId = false;\n }\n }\n\n // Measures every resized element before touching the DOM, so mounting a\n // button does not force a new layout for each following element.\n static #onResize(entries: ResizeObserverEntry[]): void {\n ReadMore.#syncAll(entries.map((entry) => instances.get(entry.target as HTMLElement)));\n }\n\n static #onFontsLoaded = (): void => {\n ReadMore.#syncAll(Array.from(ReadMore.#live));\n };\n\n static #syncAll(list: (ReadMore | undefined)[]): void {\n const measured = new Map<ReadMore, boolean>();\n\n for (const instance of list) {\n if (instance) {\n measured.set(instance, instance.#isOverflowing());\n }\n }\n\n measured.forEach((overflowing, instance) => instance.#sync(overflowing));\n }\n\n #init(): void {\n this.#applySizes();\n this.el.classList.add(CLAMP_CLASS);\n\n if (this.options.height != null) {\n this.el.classList.add(CLAMP_HEIGHT_CLASS);\n }\n\n this.el.addEventListener('focusin', this.#onFocusIn);\n ReadMore.#resizeObserver ??= new ResizeObserver(ReadMore.#onResize);\n ReadMore.#resizeObserver.observe(this.el);\n\n // Inside init(), the batch measures all elements at once afterwards.\n if (ReadMore.#batch) {\n ReadMore.#batch.push(this);\n } else {\n this.refresh();\n }\n\n this.#refreshOnFonts();\n }\n\n // A font swap can change the overflow without resizing the element, which the\n // ResizeObserver would miss. Instances created in the same tick share one\n // measurement; destroyed ones are skipped by #sync.\n #refreshOnFonts(): void {\n if (typeof document === 'undefined' || !document.fonts) {\n return;\n }\n\n // Fonts loaded later (after `ready`) are caught by one shared listener.\n if (ReadMore.#fonts !== document.fonts) {\n ReadMore.#fonts?.removeEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n ReadMore.#fonts = document.fonts;\n ReadMore.#fonts.addEventListener?.('loadingdone', ReadMore.#onFontsLoaded);\n }\n\n ReadMore.#live.add(this);\n\n if (ReadMore.#fontQueue) {\n ReadMore.#fontQueue.push(this);\n\n return;\n }\n\n const queue: ReadMore[] = ReadMore.#fontQueue = [\n this\n ];\n\n void document.fonts.ready.then(() => {\n ReadMore.#fontQueue = null;\n ReadMore.#syncAll(queue.filter((instance) => !instance.#destroyed));\n });\n }\n\n #isOverflowing(): boolean {\n return this.el.scrollHeight - this.el.clientHeight > Math.max(1, this.options.threshold);\n }\n\n #sync(overflowing: boolean): void {\n // While a toggle transition runs, the element's size is not final: measuring\n // it mid-collapse would remove the button. The timer refreshes once it ends.\n if (this.#expanded || this.#transientTimer !== null || this.#destroyed) {\n return;\n }\n\n if (overflowing && !this.#button) {\n this.#mountButton();\n } else if (!overflowing && this.#button) {\n this.#unmountButton();\n }\n }\n\n #applyTransientClass(stateClass: string): void {\n if (this.#transientTimer !== null) {\n clearTimeout(this.#transientTimer);\n }\n\n this.el.classList.add(stateClass);\n\n // A timer rather than `transitionend`, which never fires when no declared\n // property actually changes (e.g. `transition: all` in lines mode).\n const timer = setTimeout(() => {\n // Some engines (WebKit) start the transition later than the timer assumes:\n // wait for it to end, or the size is measured mid-way and the button remounted.\n const running = this.el.getAnimations?.().filter(a => a instanceof CSSTransition) ?? [];\n const done = () => {\n // Superseded by another toggle, or destroyed meanwhile.\n if (this.#transientTimer !== timer) {\n return;\n }\n\n this.#transientTimer = null;\n this.el.classList.remove(stateClass);\n this.refresh();\n };\n\n if (running.length) {\n Promise.allSettled(running.map(a => a.finished)).then(done);\n } else {\n done();\n }\n }, this.#transitionTime());\n\n this.#transientTimer = timer;\n }\n\n /** Longest `duration + delay` among the transitions declared on the element, in ms. */\n #transitionTime(): number {\n const style = getComputedStyle(this.el);\n const durations = toMs(style.transitionDuration);\n const delays = toMs(style.transitionDelay);\n let max = 0;\n\n // Shorter lists repeat to match the longer one, as CSS does.\n for (let i = 0; i < Math.max(durations.length, delays.length); i++) {\n max = Math.max(max, durations[i % durations.length] + delays[i % delays.length]);\n }\n\n return max;\n }\n\n #mountButton(): void {\n if (!this.el.id) {\n let id: string;\n\n // Skip ids already used in the page (server-rendered markup, another copy of the plugin…).\n do {\n id = `readmore-${++idCounter}`;\n } while (document.getElementById(id));\n\n this.el.id = id;\n this.#generatedId = true;\n }\n\n const btn = document.createElement('button');\n\n btn.type = 'button';\n btn.className = this.options.buttonClass;\n btn.setAttribute('aria-controls', this.el.id);\n btn.addEventListener('click', this.#onClick);\n this.#button = btn;\n this.#updateButton();\n\n if (this.options.buttonContainer) {\n this.options.buttonContainer.appendChild(btn);\n } else {\n this.el.insertAdjacentElement('afterend', btn);\n }\n }\n\n /** Syncs the button's text, `aria-expanded` and `aria-label` with the state. */\n #updateButton(): void {\n if (!this.#button) {\n return;\n }\n\n const { moreText, lessText, moreLabel, lessLabel } = this.options;\n const label = this.#expanded ? lessLabel : moreLabel;\n\n this.#button.textContent = this.#expanded ? lessText : moreText;\n this.#button.setAttribute('aria-expanded', String(this.#expanded));\n\n if (label) {\n this.#button.setAttribute('aria-label', label);\n } else {\n this.#button.removeAttribute('aria-label');\n }\n }\n\n #unmountButton(): void {\n if (!this.#button) {\n return;\n }\n\n this.#button.removeEventListener('click', this.#onClick);\n\n // Removing the focused button would drop focus to <body>: keep it on the element.\n if (document.activeElement === this.#button) {\n const { el } = this;\n\n if (!el.hasAttribute('tabindex')) {\n el.tabIndex = -1;\n el.addEventListener('blur', () => el.removeAttribute('tabindex'), {\n once: true\n });\n }\n\n el.focus();\n }\n\n this.#button.remove();\n this.#button = null;\n }\n}\n"],"mappings":";AAmKA,IAAM,cAAc;AACpB,IAAM,qBAAqB;AAE3B,IAAM,4BAAY,IAAI,QAA+B;AACrD,IAAI,YAAY;AAEhB,IAAM,WAA4B;CAC9B,OAAO;CACP,WAAW;CACX,UAAU;CACV,UAAU;CACV,aAAa;CACb,eAAe;CACf,cAAc;CACd,cAAc;CACd,kBAAkB;AACtB;;;;;AAMA,SAAS,KAAK,MAAwB;CAClC,OAAO,KAAK,MAAM,GAAG,CAAC,CAAC,KAAK,MAAM,WAAW,CAAC,IAAI,OAAQ,CAAC;AAC/D;;;;;;;;;;;;;;;AAgBA,IAAa,WAAb,MAAa,SAAS;;CAElB,OAAO;;CAGP,OAAO,SAA4B;;CAGnC,OAAO,aAAgC;;CAGvC,OAAO,wBAAQ,IAAI,IAAc;;CAGjC,OAAO;;CAGP;;CAGA;CAEA,UAAoC;CACpC,YAAY;CACZ,eAAe;CACf,kBAAwD;CACxD,iBAAuB,KAAK,OAAO;CACnC,SAAS,SAAS;CAClB;CAEA,cAAc,MAAwB;EAClC,MAAM,EAAE,WAAW;EAEnB,IAAI,KAAK,aAAa,CAAC,KAAK,WAAW,WAAW,KAAK,MAAM,EAAE,kBAAkB,UAC7E;EAIJ,IAAI,OAAO,sBAAsB,CAAC,CAAC,SAAS,KAAK,GAAG,YAAY,KAAK,GAAG,sBAAsB,CAAC,CAAC,SAAS,GACrG,KAAK,OAAO,IAAI;CAExB;CAIA,IAAI,aAAs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YAAY;EAE3C,KAAK,QAAQ,cAAc,KAAK,YAAY,WAAW;EACvD,KAAK,QAAQ,aAAa,iBAAiB,OAAO,KAAK,SAAS,CAAC;EAEjE,IAAI,OACA,KAAK,QAAQ,aAAa,cAAc,KAAK;OAE7C,KAAK,QAAQ,gBAAgB,YAAY;CAEjD;CAEA,iBAAuB;EACnB,IAAI,CAAC,KAAK,SACN;EAGJ,KAAK,QAAQ,oBAAoB,SAAS,KAAK,QAAQ;EAGvD,IAAI,SAAS,kBAAkB,KAAK,SAAS;GACzC,MAAM,EAAE,OAAO;GAEf,IAAI,CAAC,GAAG,aAAa,UAAU,GAAG;IAC9B,GAAG,WAAW;IACd,GAAG,iBAAiB,cAAc,GAAG,gBAAgB,UAAU,GAAG,EAC9D,MAAM,KACV,CAAC;GACL;GAEA,GAAG,MAAM;EACb;EAEA,KAAK,QAAQ,OAAO;EACpB,KAAK,UAAU;CACnB;AACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontfactory/readmore",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
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": {
|
|
@@ -40,8 +40,10 @@
|
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@commitlint/cli": "^21.2.4",
|
|
42
42
|
"@commitlint/config-conventional": "^21.2.4",
|
|
43
|
+
"@fontsource/inter": "^5.3.0",
|
|
43
44
|
"@front-factory/eslint-config": "^3.0.0",
|
|
44
|
-
"@frontfactory/vitepress-theme": "^1.
|
|
45
|
+
"@frontfactory/vitepress-theme": "^1.8.0",
|
|
46
|
+
"@resvg/resvg-js": "^2.6.2",
|
|
45
47
|
"@semantic-release/changelog": "^7.0.0",
|
|
46
48
|
"@semantic-release/git": "^11.0.1",
|
|
47
49
|
"@semantic-release/github": "^12.0.10",
|
|
@@ -54,6 +56,7 @@
|
|
|
54
56
|
"jsdom": "^30.1.3",
|
|
55
57
|
"lightningcss": "^1.33.0",
|
|
56
58
|
"playwright": "^1.64.1",
|
|
59
|
+
"satori": "^0.63.2",
|
|
57
60
|
"semantic-release": "^25.0.9",
|
|
58
61
|
"typescript": "^6.0.3",
|
|
59
62
|
"typescript-eslint": "^8.72.0",
|