@matteodicristofalo/text-animations 1.0.5 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -70,8 +70,10 @@ You can decide how to split the text between one of this options:
70
70
 
71
71
  You can also configure the reveal animation by specifying:
72
72
 
73
- - duration
74
- - stagger
73
+ - duration (number in seconds)
74
+ - stagger (number in seconds)
75
+ - threshold (number between 0 and 1)
76
+ - once (boolean)
75
77
 
76
78
  ```js
77
79
  "use client";
@@ -84,6 +86,8 @@ export function MyComponent() {
84
86
  () => ({
85
87
  duration: 1,
86
88
  stagger: 0.5,
89
+ threshold: 0.75,
90
+ once: false,
87
91
  }),
88
92
  []
89
93
  );
@@ -7,5 +7,7 @@ type SplitType = "sentence" | "word" | "char";
7
7
  type RevealOptions = {
8
8
  duration: number;
9
9
  stagger: number;
10
+ threshold: number;
11
+ once: boolean;
10
12
  };
11
13
  export {};
@@ -1,11 +1,9 @@
1
+ import React from "react";
1
2
  import type { StoryObj } from "@storybook/react";
2
3
  import { SplitTextReveal } from "./split-text-reveal";
3
4
  declare const meta: {
4
5
  title: string;
5
6
  component: typeof SplitTextReveal;
6
- parameters: {
7
- layout: string;
8
- };
9
7
  argTypes: {
10
8
  text: {
11
9
  control: "text";
@@ -20,7 +18,18 @@ declare const meta: {
20
18
  };
21
19
  };
22
20
  };
21
+ args: {
22
+ text: string;
23
+ splitType: "char";
24
+ };
25
+ render: (args: import("./split-test-reveal.types").SplitTextRevealProps) => React.JSX.Element;
23
26
  };
24
27
  export default meta;
25
28
  type Story = StoryObj<typeof meta>;
26
29
  export declare const Default: Story;
30
+ export declare const SplitByWord: Story;
31
+ export declare const SplitBySentence: Story;
32
+ export declare const Duration: Story;
33
+ export declare const Stagger: Story;
34
+ export declare const Threshold: Story;
35
+ export declare const NotOnce: Story;
@@ -1,11 +1,9 @@
1
+ import React from "react";
1
2
  import type { StoryObj } from "@storybook/react";
2
3
  import { SplitTextRotate } from "./split-text-rotate";
3
4
  declare const meta: {
4
5
  title: string;
5
6
  component: typeof SplitTextRotate;
6
- parameters: {
7
- layout: string;
8
- };
9
7
  argTypes: {
10
8
  text: {
11
9
  control: "text";
@@ -16,7 +14,13 @@ declare const meta: {
16
14
  };
17
15
  };
18
16
  };
17
+ args: {
18
+ text: string;
19
+ };
20
+ render: (args: import("./split-text-rotate.types").SplitTextRotateProps) => React.JSX.Element;
19
21
  };
20
22
  export default meta;
21
23
  type Story = StoryObj<typeof meta>;
22
24
  export declare const Default: Story;
25
+ export declare const Duration: Story;
26
+ export declare const Stagger: Story;
package/dist/index.css CHANGED
@@ -4,6 +4,12 @@
4
4
  .split-text-reveal-module_text__-9O5o .split-text-reveal-module_is-hidden__2J8-i {
5
5
  opacity: 0;
6
6
  }
7
+ .split-text-reveal-module_text__-9O5o .split-text-reveal-module_sr-only__luZuY {
8
+ height: 0;
9
+ overflow: hidden;
10
+ position: absolute;
11
+ width: 0;
12
+ }
7
13
  .split-text-reveal-module_text__-9O5o .split-text-reveal-module_container__mkD-P {
8
14
  display: inline-flex;
9
15
  overflow: hidden;
@@ -25,6 +31,12 @@
25
31
  overflow: hidden;
26
32
  position: relative;
27
33
  }
34
+ .split-text-rotate-module_text__xzran .split-text-rotate-module_sr-only__Dbb9O {
35
+ height: 0;
36
+ overflow: hidden;
37
+ position: absolute;
38
+ width: 0;
39
+ }
28
40
  .split-text-rotate-module_text__xzran .split-text-rotate-module_container__EAJkE {
29
41
  display: flex;
30
42
  }
package/dist/index.js CHANGED
@@ -4,6 +4,9 @@ import React, { useState, useEffect, useRef, useMemo, useCallback } from 'react'
4
4
  function round(num, digits) {
5
5
  return parseFloat(num.toFixed(digits));
6
6
  }
7
+ function clamp(n, min, max) {
8
+ return Math.min(Math.max(min, n), max);
9
+ }
7
10
 
8
11
  function sentences(text) {
9
12
  return segment(text, "sentence");
@@ -77,16 +80,29 @@ function useIntersectionObserver(ref, options) {
77
80
 
78
81
  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}
79
82
 
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"};
83
+ var styles$1 = {"text":"split-text-reveal-module_text__-9O5o","is-hidden":"split-text-reveal-module_is-hidden__2J8-i","sr-only":"split-text-reveal-module_sr-only__luZuY","container":"split-text-reveal-module_container__mkD-P","animate":"split-text-reveal-module_animate__y6o78"};
81
84
 
82
85
  var DEFAULT_DURATION$1 = 0.5;
83
86
  var DEFAULT_STAGGER$1 = 0.01;
87
+ var MIN_THRESHOLD = 0;
88
+ var MAX_THRESHOLD = 1;
89
+ var DEFAULT_THRESHOLD = 0.25;
84
90
  function SplitTextReveal(_a) {
85
91
  var _b;
86
92
  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];
88
93
  var ref = useRef(null);
89
- var memoizedOptions = useMemo(function () { return ({ once: true, threshold: 0.1 }); }, []);
94
+ var _d = useState(false), hasBeenMounted = _d[0], setHasBeenMounted = _d[1];
95
+ var memoizedOptions = useMemo(function () {
96
+ var _a;
97
+ var once = (_a = revealOptions === null || revealOptions === void 0 ? void 0 : revealOptions.once) !== null && _a !== void 0 ? _a : true;
98
+ var threshold = (revealOptions === null || revealOptions === void 0 ? void 0 : revealOptions.threshold)
99
+ ? clamp(revealOptions.threshold, MIN_THRESHOLD, MAX_THRESHOLD)
100
+ : DEFAULT_THRESHOLD;
101
+ return {
102
+ once: once,
103
+ threshold: threshold,
104
+ };
105
+ }, [revealOptions]);
90
106
  var isInView = useIntersectionObserver(ref, memoizedOptions);
91
107
  var transitionDuration = (revealOptions === null || revealOptions === void 0 ? void 0 : revealOptions.duration) || DEFAULT_DURATION$1;
92
108
  var transitionStagger = (revealOptions === null || revealOptions === void 0 ? void 0 : revealOptions.stagger) || DEFAULT_STAGGER$1;
@@ -105,13 +121,15 @@ function SplitTextReveal(_a) {
105
121
  }, []);
106
122
  return (React.createElement("span", { ref: ref, className: clsx(styles$1["text"], (_b = {}, _b[styles$1["animate"]] = isInView, _b)), style: {
107
123
  "--var-duration": "".concat(transitionDuration, "s"),
108
- } }, hasBeenMounted ? (splittedText.map(function (el, i) { return (React.createElement("span", { key: i, className: styles$1["container"] },
109
- React.createElement("span", { style: {
110
- "--var-delay": "".concat(round(transitionStagger * i, 3), "s"),
111
- } }, el))); })) : (React.createElement("span", { className: styles$1["is-hidden"] }, text))));
124
+ } }, hasBeenMounted ? (React.createElement(React.Fragment, null,
125
+ React.createElement("span", { className: styles$1["sr-only"] }, text),
126
+ splittedText.map(function (el, i) { return (React.createElement("span", { key: i, className: styles$1["container"], "aria-hidden": "true" },
127
+ React.createElement("span", { "aria-hidden": "true", style: {
128
+ "--var-delay": "".concat(round(transitionStagger * i, 3), "s"),
129
+ } }, el))); }))) : (React.createElement("span", { className: styles$1["is-hidden"] }, text))));
112
130
  }
113
131
 
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"};
132
+ var styles = {"text":"split-text-rotate-module_text__xzran","sr-only":"split-text-rotate-module_sr-only__Dbb9O","container":"split-text-rotate-module_container__EAJkE","char":"split-text-rotate-module_char__PYJ-k"};
115
133
 
116
134
  var DEFAULT_DURATION = 0.25;
117
135
  var DEFAULT_STAGGER = 0.025;
@@ -135,13 +153,15 @@ function SplitTextRotate(_a) {
135
153
  }, []);
136
154
  return (React.createElement("span", { className: styles["text"], style: {
137
155
  "--var-duration": "".concat(transitionDuration, "s"),
138
- } }, hasBeenMounted ? (Array.from({ length: 2 }).map(function (_, i) { return (React.createElement("span", { key: i, className: styles["container"] }, splittedText.map(function (char, i) {
139
- var charDelays = delays(i);
140
- return (React.createElement("span", { key: i, className: styles["char"], style: {
141
- "--var-delay-in": "".concat(charDelays.in, "s"),
142
- "--var-delay-out": "".concat(charDelays.out, "s"),
143
- } }, char));
144
- }))); })) : (React.createElement("span", null, text))));
156
+ } }, hasBeenMounted ? (React.createElement(React.Fragment, null,
157
+ React.createElement("span", { className: styles["sr-only"] }, text),
158
+ Array.from({ length: 2 }).map(function (_, i) { return (React.createElement("span", { key: i, className: styles["container"], "aria-hidden": "true" }, splittedText.map(function (char, i) {
159
+ var charDelays = delays(i);
160
+ return (React.createElement("span", { key: i, className: styles["char"], "aria-hidden": "true", style: {
161
+ "--var-delay-in": "".concat(charDelays.in, "s"),
162
+ "--var-delay-out": "".concat(charDelays.out, "s"),
163
+ } }, char));
164
+ }))); }))) : (React.createElement("span", null, text))));
145
165
  }
146
166
 
147
167
  export { SplitTextReveal, SplitTextRotate };
@@ -1 +1,2 @@
1
1
  export declare function round(num: number, digits: number): number;
2
+ export declare function clamp(n: number, min: number, max: number): number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matteodicristofalo/text-animations",
3
- "version": "1.0.5",
3
+ "version": "1.1.1",
4
4
  "description": "React library to animate text",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && rollup -c",