@matteodicristofalo/text-animations 1.0.1 → 1.0.2

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
@@ -76,34 +76,38 @@ function useIntersectionObserver(ref, options) {
76
76
 
77
77
  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
78
 
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"};
79
+ 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
80
 
81
81
  var DEFAULT_DURATION$1 = 0.5;
82
82
  var DEFAULT_STAGGER$1 = 0.01;
83
83
  function SplitTextReveal(_a) {
84
84
  var _b;
85
85
  var text = _a.text, _c = _a.splitType, splitType = _c === void 0 ? "char" : _c, revealOptions = _a.revealOptions;
86
+ var _d = useState(false), hasBeenMounted = _d[0], setHasBeenMounted = _d[1];
86
87
  var ref = useRef(null);
87
88
  var memoizedOptions = useMemo(function () { return ({ once: true, threshold: 0.1 }); }, []);
88
89
  var isInView = useIntersectionObserver(ref, memoizedOptions);
89
90
  var transitionDuration = (revealOptions === null || revealOptions === void 0 ? void 0 : revealOptions.duration) || DEFAULT_DURATION$1;
90
91
  var transitionStagger = (revealOptions === null || revealOptions === void 0 ? void 0 : revealOptions.stagger) || DEFAULT_STAGGER$1;
91
- var splitFn = useMemo(function () {
92
+ var splittedText = useMemo(function () {
92
93
  switch (splitType) {
93
94
  case "sentence":
94
- return sentences;
95
+ return sentences(text);
95
96
  case "word":
96
- return words;
97
+ return words(text);
97
98
  case "char":
98
- return characters;
99
+ return characters(text);
99
100
  }
100
- }, [splitType]);
101
+ }, [splitType, text]);
102
+ useEffect(function () {
103
+ setHasBeenMounted(true);
104
+ }, []);
101
105
  return (React.createElement("span", { ref: ref, className: clsx(styles$1["text"], (_b = {}, _b[styles$1["animate"]] = isInView, _b)), style: {
102
106
  "--var-duration": "".concat(transitionDuration, "s"),
103
- } }, splitFn(text).map(function (el, i) { return (React.createElement("span", { key: i, className: styles$1["container"] },
107
+ } }, hasBeenMounted ? (splittedText.map(function (el, i) { return (React.createElement("span", { key: i, className: styles$1["container"] },
104
108
  React.createElement("span", { style: {
105
109
  "--var-delay": "".concat(round(transitionStagger * i, 3), "s"),
106
- } }, el))); })));
110
+ } }, el))); })) : (React.createElement("span", { className: styles$1["is-hidden"] }, text))));
107
111
  }
108
112
 
109
113
  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 +116,12 @@ var DEFAULT_DURATION = 0.25;
112
116
  var DEFAULT_STAGGER = 0.025;
113
117
  function SplitTextRotate(_a) {
114
118
  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);
119
+ var _b = useState(false), hasBeenMounted = _b[0], setHasBeenMounted = _b[1];
120
+ var splittedText = useMemo(function () { return characters(text); }, [text]);
121
+ var _c = useMemo(function () { return ({
122
+ transitionDuration: (rotateOptions === null || rotateOptions === void 0 ? void 0 : rotateOptions.duration) || DEFAULT_DURATION,
123
+ transitionStagger: (rotateOptions === null || rotateOptions === void 0 ? void 0 : rotateOptions.stagger) || DEFAULT_STAGGER,
124
+ }); }, [rotateOptions]), transitionDuration = _c.transitionDuration, transitionStagger = _c.transitionStagger;
118
125
  var delays = useCallback(function (index) {
119
126
  var noElementsZeroBased = splittedText.length - 1;
120
127
  return {
@@ -122,15 +129,18 @@ function SplitTextRotate(_a) {
122
129
  out: round(transitionStagger * (noElementsZeroBased - index), 3),
123
130
  };
124
131
  }, [splittedText, transitionStagger]);
132
+ useEffect(function () {
133
+ setHasBeenMounted(true);
134
+ }, []);
125
135
  return (React.createElement("span", { className: styles["text"], style: {
126
136
  "--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) {
137
+ } }, hasBeenMounted ? (Array.from({ length: 2 }).map(function (_, i) { return (React.createElement("span", { key: i, className: styles["container"] }, splittedText.map(function (char, i) {
128
138
  var charDelays = delays(i);
129
139
  return (React.createElement("span", { key: i, className: styles["char"], style: {
130
140
  "--var-delay-in": "".concat(charDelays.in, "s"),
131
141
  "--var-delay-out": "".concat(charDelays.out, "s"),
132
142
  } }, char));
133
- }))); })));
143
+ }))); })) : (React.createElement("span", null, text))));
134
144
  }
135
145
 
136
146
  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.2",
4
4
  "description": "React library to animate text",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && rollup -c",