@frontfactory/readmore 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,7 @@ export declare class ReadMore {
19
19
  static init(target: string | NodeListOf<HTMLElement> | HTMLElement[], options?: ReadMoreOptions): ReadMore[];
20
20
  get expanded(): boolean;
21
21
  toggle(): void;
22
+ static getInstance(element: HTMLElement): ReadMore | undefined;
22
23
  destroy(): void;
23
24
  }
24
25
  export {};
package/dist/readmore.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region src/readmore.ts
2
- var e = "readmore-clamp", t = "readmore-clamp--height", n = {
2
+ var e = "readmore-clamp", t = "readmore-clamp--height", n = /* @__PURE__ */ new WeakMap(), r = 0, i = {
3
3
  lines: 3,
4
4
  moreText: "Read more",
5
5
  lessText: "Read less",
@@ -7,43 +7,43 @@ var e = "readmore-clamp", t = "readmore-clamp--height", n = {
7
7
  expandedClass: "is-expanded",
8
8
  openingClass: "is-opening",
9
9
  closingClass: "is-closing"
10
- }, r = class r {
10
+ }, a = class a {
11
11
  el;
12
12
  options;
13
13
  #e = null;
14
14
  #t = !1;
15
- #n = null;
16
- #r = null;
17
- #i = () => {
18
- this.#n !== null && clearTimeout(this.#n), this.#n = setTimeout(() => {
19
- this.#n = null, this.#c();
20
- }, 100);
21
- };
15
+ #n = !1;
16
+ #r;
17
+ #i = null;
22
18
  #a = () => this.toggle();
23
19
  constructor(e, t = {}) {
24
20
  if (!(e instanceof HTMLElement)) throw TypeError("ReadMore: an HTMLElement is required.");
21
+ if (n.has(e)) throw Error("ReadMore: element is already initialized. Call destroy() first.");
25
22
  this.el = e, this.options = {
26
- ...n,
23
+ ...i,
27
24
  ...t
28
- }, this.#o();
25
+ }, this.#r = new ResizeObserver(() => this.#c()), n.set(e, this), this.#o();
29
26
  }
30
27
  static init(e, t) {
31
28
  let n = typeof e == "string" ? document.querySelectorAll(e) : e;
32
- return Array.from(n).map((e) => new r(e, t));
29
+ return Array.from(n).map((e) => new a(e, t));
33
30
  }
34
31
  get expanded() {
35
32
  return this.#t;
36
33
  }
37
34
  toggle() {
38
- this.#t = !this.#t, this.el.classList.toggle(e, !this.#t), this.options.height != null && this.el.classList.toggle(t, !this.#t), this.el.classList.toggle(this.options.expandedClass, this.#t), this.#e && (this.#e.textContent = this.#t ? this.options.lessText : this.options.moreText);
35
+ this.#t = !this.#t, this.el.classList.toggle(e, !this.#t), this.options.height != null && this.el.classList.toggle(t, !this.#t), this.el.classList.toggle(this.options.expandedClass, this.#t), this.#e && (this.#e.textContent = this.#t ? this.options.lessText : this.options.moreText, this.#e.setAttribute("aria-expanded", String(this.#t)));
39
36
  let n = this.#t ? this.options.openingClass : this.options.closingClass, r = this.#t ? this.options.closingClass : this.options.openingClass;
40
37
  this.el.classList.remove(r), this.#l(n), this.#t || this.#c();
41
38
  }
39
+ static getInstance(e) {
40
+ return n.get(e);
41
+ }
42
42
  destroy() {
43
- window.removeEventListener("resize", this.#i), this.#n !== null && (clearTimeout(this.#n), this.#n = null), this.#r &&= (this.el.removeEventListener("transitionend", this.#r), null), this.#d(), this.el.classList.remove(e, t, this.options.expandedClass, this.options.openingClass, this.options.closingClass), this.el.style.removeProperty("--readmore-lines"), this.el.style.removeProperty("--readmore-height");
43
+ n.delete(this.el), this.#r.disconnect(), this.#i &&= (this.el.removeEventListener("transitionend", this.#i), null), this.#d(), this.el.classList.remove(e, t, this.options.expandedClass, this.options.openingClass, this.options.closingClass), this.el.style.removeProperty("--readmore-lines"), this.el.style.removeProperty("--readmore-height"), this.#n &&= (this.el.removeAttribute("id"), !1);
44
44
  }
45
45
  #o() {
46
- this.options.height == null ? (this.el.style.setProperty("--readmore-lines", String(this.options.lines)), this.el.classList.add(e)) : (this.el.style.setProperty("--readmore-height", `${this.options.height}px`), this.el.classList.add(e, t)), window.addEventListener("resize", this.#i), this.#c();
46
+ this.options.height == null ? (this.el.style.setProperty("--readmore-lines", String(this.options.lines)), this.el.classList.add(e)) : (this.el.style.setProperty("--readmore-height", `${this.options.height}px`), this.el.classList.add(e, t)), this.#r.observe(this.el), this.#c();
47
47
  }
48
48
  #s() {
49
49
  return this.el.scrollHeight - this.el.clientHeight > 1;
@@ -54,24 +54,25 @@ var e = "readmore-clamp", t = "readmore-clamp--height", n = {
54
54
  e && !this.#e ? this.#u() : !e && this.#e && this.#d();
55
55
  }
56
56
  #l(e) {
57
- this.#r &&= (this.el.removeEventListener("transitionend", this.#r), null), this.el.classList.add(e);
57
+ this.#i &&= (this.el.removeEventListener("transitionend", this.#i), null), this.el.classList.add(e);
58
58
  let t = () => this.el.classList.remove(e);
59
59
  if (!getComputedStyle(this.el).transitionDuration.split(",").some((e) => parseFloat(e) > 0)) {
60
60
  setTimeout(t, 0);
61
61
  return;
62
62
  }
63
63
  let n = (e) => {
64
- e.target === this.el && (this.el.removeEventListener("transitionend", n), this.#r = null, t());
64
+ e.target === this.el && (this.el.removeEventListener("transitionend", n), this.#i = null, t());
65
65
  };
66
- this.#r = n, this.el.addEventListener("transitionend", n);
66
+ this.#i = n, this.el.addEventListener("transitionend", n);
67
67
  }
68
68
  #u() {
69
+ this.el.id || (this.el.id = `readmore-${++r}`, this.#n = !0);
69
70
  let e = document.createElement("button");
70
- e.type = "button", e.className = this.options.buttonClass, e.textContent = this.options.moreText, e.addEventListener("click", this.#a), this.el.insertAdjacentElement("afterend", e), this.#e = e;
71
+ e.type = "button", e.className = this.options.buttonClass, e.textContent = this.options.moreText, e.setAttribute("aria-expanded", "false"), e.setAttribute("aria-controls", this.el.id), e.addEventListener("click", this.#a), this.el.insertAdjacentElement("afterend", e), this.#e = e;
71
72
  }
72
73
  #d() {
73
74
  this.#e &&= (this.#e.removeEventListener("click", this.#a), this.#e.remove(), null);
74
75
  }
75
76
  };
76
77
  //#endregion
77
- export { r as ReadMore };
78
+ export { a as ReadMore };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontfactory/readmore",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Lightweight plugin to clamp text to N lines with ellipsis and a read more action.",
5
5
  "type": "module",
6
6
  "scripts": {