@matteodicristofalo/text-animations 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # @mdc/text-animations
1
+ # @matteodicristofalo/text-animations
2
2
 
3
3
  This library provides a set of React components to animate text
4
4
 
package/dist/index.css CHANGED
@@ -1,6 +1,9 @@
1
1
  .split-text-reveal-module_text__-9O5o {
2
2
  position: relative;
3
3
  }
4
+ .split-text-reveal-module_text__-9O5o .split-text-reveal-module_is-hidden__2J8-i {
5
+ opacity: 0;
6
+ }
4
7
  .split-text-reveal-module_text__-9O5o .split-text-reveal-module_container__mkD-P {
5
8
  display: inline-flex;
6
9
  overflow: hidden;
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import React, { useState, useEffect, useRef, useMemo, useCallback } from 'react';
2
3
 
3
4
  function round(num, digits) {
@@ -76,34 +77,38 @@ function useIntersectionObserver(ref, options) {
76
77
 
77
78
  function r(e){var t,f,n="";if("string"==typeof e||"number"==typeof e)n+=e;else if("object"==typeof e)if(Array.isArray(e)){var o=e.length;for(t=0;t<o;t++)e[t]&&(f=r(e[t]))&&(n&&(n+=" "),n+=f);}else for(f in e)e[f]&&(n&&(n+=" "),n+=f);return n}function clsx(){for(var e,t,f=0,n="",o=arguments.length;f<o;f++)(e=arguments[f])&&(t=r(e))&&(n&&(n+=" "),n+=t);return n}
78
79
 
79
- var styles$1 = {"text":"split-text-reveal-module_text__-9O5o","container":"split-text-reveal-module_container__mkD-P","animate":"split-text-reveal-module_animate__y6o78"};
80
+ var styles$1 = {"text":"split-text-reveal-module_text__-9O5o","is-hidden":"split-text-reveal-module_is-hidden__2J8-i","container":"split-text-reveal-module_container__mkD-P","animate":"split-text-reveal-module_animate__y6o78"};
80
81
 
81
82
  var DEFAULT_DURATION$1 = 0.5;
82
83
  var DEFAULT_STAGGER$1 = 0.01;
83
84
  function SplitTextReveal(_a) {
84
85
  var _b;
85
86
  var text = _a.text, _c = _a.splitType, splitType = _c === void 0 ? "char" : _c, revealOptions = _a.revealOptions;
87
+ var _d = useState(false), hasBeenMounted = _d[0], setHasBeenMounted = _d[1];
86
88
  var ref = useRef(null);
87
89
  var memoizedOptions = useMemo(function () { return ({ once: true, threshold: 0.1 }); }, []);
88
90
  var isInView = useIntersectionObserver(ref, memoizedOptions);
89
91
  var transitionDuration = (revealOptions === null || revealOptions === void 0 ? void 0 : revealOptions.duration) || DEFAULT_DURATION$1;
90
92
  var transitionStagger = (revealOptions === null || revealOptions === void 0 ? void 0 : revealOptions.stagger) || DEFAULT_STAGGER$1;
91
- var splitFn = useMemo(function () {
93
+ var splittedText = useMemo(function () {
92
94
  switch (splitType) {
93
95
  case "sentence":
94
- return sentences;
96
+ return sentences(text);
95
97
  case "word":
96
- return words;
98
+ return words(text);
97
99
  case "char":
98
- return characters;
100
+ return characters(text);
99
101
  }
100
- }, [splitType]);
102
+ }, [splitType, text]);
103
+ useEffect(function () {
104
+ setHasBeenMounted(true);
105
+ }, []);
101
106
  return (React.createElement("span", { ref: ref, className: clsx(styles$1["text"], (_b = {}, _b[styles$1["animate"]] = isInView, _b)), style: {
102
107
  "--var-duration": "".concat(transitionDuration, "s"),
103
- } }, splitFn(text).map(function (el, i) { return (React.createElement("span", { key: i, className: styles$1["container"] },
108
+ } }, hasBeenMounted ? (splittedText.map(function (el, i) { return (React.createElement("span", { key: i, className: styles$1["container"] },
104
109
  React.createElement("span", { style: {
105
110
  "--var-delay": "".concat(round(transitionStagger * i, 3), "s"),
106
- } }, el))); })));
111
+ } }, el))); })) : (React.createElement("span", { className: styles$1["is-hidden"] }, text))));
107
112
  }
108
113
 
109
114
  var styles = {"text":"split-text-rotate-module_text__xzran","container":"split-text-rotate-module_container__EAJkE","char":"split-text-rotate-module_char__PYJ-k"};
@@ -112,9 +117,12 @@ var DEFAULT_DURATION = 0.25;
112
117
  var DEFAULT_STAGGER = 0.025;
113
118
  function SplitTextRotate(_a) {
114
119
  var text = _a.text, rotateOptions = _a.rotateOptions;
115
- var transitionDuration = (rotateOptions === null || rotateOptions === void 0 ? void 0 : rotateOptions.duration) || DEFAULT_DURATION;
116
- var transitionStagger = (rotateOptions === null || rotateOptions === void 0 ? void 0 : rotateOptions.stagger) || DEFAULT_STAGGER;
117
- var splittedText = characters(text);
120
+ var _b = useState(false), hasBeenMounted = _b[0], setHasBeenMounted = _b[1];
121
+ var splittedText = useMemo(function () { return characters(text); }, [text]);
122
+ var _c = useMemo(function () { return ({
123
+ transitionDuration: (rotateOptions === null || rotateOptions === void 0 ? void 0 : rotateOptions.duration) || DEFAULT_DURATION,
124
+ transitionStagger: (rotateOptions === null || rotateOptions === void 0 ? void 0 : rotateOptions.stagger) || DEFAULT_STAGGER,
125
+ }); }, [rotateOptions]), transitionDuration = _c.transitionDuration, transitionStagger = _c.transitionStagger;
118
126
  var delays = useCallback(function (index) {
119
127
  var noElementsZeroBased = splittedText.length - 1;
120
128
  return {
@@ -122,15 +130,18 @@ function SplitTextRotate(_a) {
122
130
  out: round(transitionStagger * (noElementsZeroBased - index), 3),
123
131
  };
124
132
  }, [splittedText, transitionStagger]);
133
+ useEffect(function () {
134
+ setHasBeenMounted(true);
135
+ }, []);
125
136
  return (React.createElement("span", { className: styles["text"], style: {
126
137
  "--var-duration": "".concat(transitionDuration, "s"),
127
- } }, Array.from({ length: 2 }).map(function (_, i) { return (React.createElement("span", { key: i, className: styles["container"] }, splittedText.map(function (char, i) {
138
+ } }, hasBeenMounted ? (Array.from({ length: 2 }).map(function (_, i) { return (React.createElement("span", { key: i, className: styles["container"] }, splittedText.map(function (char, i) {
128
139
  var charDelays = delays(i);
129
140
  return (React.createElement("span", { key: i, className: styles["char"], style: {
130
141
  "--var-delay-in": "".concat(charDelays.in, "s"),
131
142
  "--var-delay-out": "".concat(charDelays.out, "s"),
132
143
  } }, char));
133
- }))); })));
144
+ }))); })) : (React.createElement("span", null, text))));
134
145
  }
135
146
 
136
147
  export { SplitTextReveal, SplitTextRotate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matteodicristofalo/text-animations",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "React library to animate text",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && rollup -c",
@@ -27,6 +27,7 @@
27
27
  "rollup": "^4.28.0",
28
28
  "rollup-plugin-peer-deps-external": "^2.2.4",
29
29
  "rollup-plugin-postcss": "^4.0.2",
30
+ "rollup-preserve-directives": "^1.1.3",
30
31
  "sass": "^1.81.0",
31
32
  "storybook": "^8.4.6",
32
33
  "tslib": "^2.8.1",