@gecko-ui-kit/motion 0.8.1 → 0.9.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 +130 -0
- package/index.js +16 -0
- package/motion.css +113 -0
- package/package.json +2 -2
- package/src/highlighter.js +76 -0
- package/src/marquee.js +151 -0
- package/src/rotator.js +120 -0
- package/src/scrollreveal.js +215 -0
- package/src/shine.js +81 -0
- package/src/spotlight.js +92 -0
- package/src/tilt.js +92 -0
- package/src/typewriter.js +137 -0
package/README.md
CHANGED
|
@@ -28,6 +28,14 @@ import { indicator, rise, title, count, swap, drag, theme } from '@gecko-ui-kit/
|
|
|
28
28
|
| `expandSearch(el, opts)` / `searchField(opts)` | Search icon that opens into a field: it stretches out, the magnifier swings, the hint types itself in and a close × draws itself |
|
|
29
29
|
| `edgeProgress(button, opts)` | Download button: the outline fills round the edge as progress (or chases when the size is unknown), then the button fills and a tick draws |
|
|
30
30
|
| `holdConfirm(button, opts)` | Hold to confirm: colour floods the button (or a ring fills round it) while held, drains with a reminder if let go early, bursts into a tick when done |
|
|
31
|
+
| `typewriter(el, opts)` | Types phrases letter by letter with a caret, deletes and types the next; screen readers get the whole text |
|
|
32
|
+
| `rotator(el, opts)` | One word in a sentence rolls (drops, slides, blurs) through a list; the slot's width glides to fit |
|
|
33
|
+
| `shine(el, opts)` | A band of light sweeps across text: on a loop, on view or on hover (CSS-only with `shine--loop`) |
|
|
34
|
+
| `highlighter(el, opts)` | A marker stroke draws behind the words in `<mark>` when they scroll into view |
|
|
35
|
+
| `scrollReveal(targets, opts)` / `blurIn` / `scrollProgress` | Blocks or words fade, rise, slide, zoom or blur in on scroll; a reading-progress bar |
|
|
36
|
+
| `marquee(el, opts)` | Endless sideways strip of logos or items; eases to a stop on hover; pause button |
|
|
37
|
+
| `tilt(targets, opts)` | Card leans toward the pointer in 3D with glare and `data-depth` layers |
|
|
38
|
+
| `spotlight(group, opts)` | A soft light follows the pointer across a group of cards and lights their edges |
|
|
31
39
|
| `binEat(button, opts)` / `binEat.reset` | Delete button: the bin opens its lid and eats the word letter by letter, then “Deleted” rises in |
|
|
32
40
|
| `sendOff(button, opts)` / `sendOff.reset` | Send button: the plane flies off (climb, straight or loop) with a dotted trail, then “Sent” and a drawn tick roll in |
|
|
33
41
|
| `speed(x)`, `reduceMotion(bool)` | Global speed / force reduced motion |
|
|
@@ -72,3 +80,125 @@ burger(otherButton, true) // just the morph: ✕ (and aria-expanded="true")
|
|
|
72
80
|
```
|
|
73
81
|
`bindBurger` options: `onToggle`, `open`, `duration` (0.45), `words: { open, close }` (the label), `controls`, `escape`
|
|
74
82
|
(Escape closes and refocuses). Size with `--burger-w`, `--burger-h`, `--burger-t`, `--burger-r`, or `burger--sm` / `burger--lg`.
|
|
83
|
+
|
|
84
|
+
## Typewriter
|
|
85
|
+
Text appears letter by letter behind a caret, with a slightly uneven, human rhythm (a little longer after commas and
|
|
86
|
+
full stops). With several phrases it types one, waits, deletes it and types the next. Thai vowels and tone marks arrive
|
|
87
|
+
together with their letter. Screen readers hear the whole text once (several phrases as a list), never single letters.
|
|
88
|
+
```js
|
|
89
|
+
import { typewriter } from '@gecko-ui-kit/motion'
|
|
90
|
+
|
|
91
|
+
// <p>We design <span id="what"></span></p>
|
|
92
|
+
const tw = typewriter('#what', { phrases: ['websites', 'mobile apps', 'online shops'] })
|
|
93
|
+
typewriter(message, { phrases: ['Your table is booked.'], loop: false, caret: 'block' })
|
|
94
|
+
tw.pause(); tw.play(); tw.set(['new', 'phrases']); tw.destroy()
|
|
95
|
+
```
|
|
96
|
+
Options: `speed` (0.06 s a letter), `jitter` (0.6), `eraseSpeed` (0.03), `erase` (`'backspace'|'select'`), `hold` (1.6),
|
|
97
|
+
`gap` (0.35), `delay` (0.3), `loop` (true), `caret` (`'bar'|'block'|'underscore'|false`), `whenVisible` (true), `locale`,
|
|
98
|
+
`label`, `onPhrase`. Caret colour `--tw-caret`, selection `--tw-sel`. With reduced motion each phrase appears whole.
|
|
99
|
+
|
|
100
|
+
## Word rotator
|
|
101
|
+
One word in a sentence keeps changing: the old word leaves, the new one rolls into the same slot (or drops, slides,
|
|
102
|
+
comes out of a blur, or rolls up letter by letter) and the slot's width glides to fit, so the rest of the sentence
|
|
103
|
+
moves over smoothly. Screen readers hear the sentence once with every word as a list.
|
|
104
|
+
```js
|
|
105
|
+
import { rotator } from '@gecko-ui-kit/motion'
|
|
106
|
+
|
|
107
|
+
// <h2>Rent a <span id="vehicle" class="rot--pill">scooter</span> by the hour</h2>
|
|
108
|
+
const r = rotator('#vehicle', { words: ['scooter', 'city bike', 'e-bike'], effect: 'roll', every: 2.2 })
|
|
109
|
+
r.next(); r.go(2); r.pause(); r.play(); r.set(['a', 'b']); r.destroy()
|
|
110
|
+
```
|
|
111
|
+
Options: `words` (or `data-words="a|b"`, or child elements), `effect` (`'roll'|'drop'|'slide'|'blur'|'letters'`), `every`
|
|
112
|
+
(2.2 s), `duration` (0.6), `ease` (`back.out(1.6)`), `stagger` (letters), `auto` (true), `whenVisible` (true), `locale`,
|
|
113
|
+
`label`, `onChange`. `rot--pill` adds a highlight box (`--rot-bg`, `--rot-c`); `--rot-lh` is the line height inside the slot.
|
|
114
|
+
With reduced motion the word simply changes.
|
|
115
|
+
|
|
116
|
+
## Shine text
|
|
117
|
+
A slanted band of light sweeps across the letters: repeating with a rest in between, once when it scrolls into view, or
|
|
118
|
+
when the pointer (or keyboard focus) arrives. The text stays real, selectable text.
|
|
119
|
+
```js
|
|
120
|
+
import { shine } from '@gecko-ui-kit/motion'
|
|
121
|
+
|
|
122
|
+
shine('.hero') // brand gradient letters, white glint, repeats
|
|
123
|
+
shine('.status', { look: 'ink', duration: 1.2 }) // grey letters, ink band: a "loading…" shimmer
|
|
124
|
+
shine('.cta', { look: 'color', trigger: 'hover' })
|
|
125
|
+
```
|
|
126
|
+
```html
|
|
127
|
+
<!-- no JavaScript: CSS repeats the sweep (turned off with reduced motion) -->
|
|
128
|
+
<span class="shine shine--ink shine--loop" style="--shine-dur: 2.4s">Brewing your order…</span>
|
|
129
|
+
```
|
|
130
|
+
Options: `look` (`'gradient'|'ink'|'color'`), `trigger` (`'loop'|'view'|'hover'`), `duration` (1.4), `repeatDelay` (1.8),
|
|
131
|
+
`delay`, `ease`, `angle` (110°), `width` (12), `color`. Returns `{ play, pause, destroy, tween }`. CSS: `--shine-c` (band),
|
|
132
|
+
`--shine-base` (the letters' fill). With reduced motion there is no sweep.
|
|
133
|
+
|
|
134
|
+
## Highlighter
|
|
135
|
+
A marker stroke with soft, uneven ends draws behind the words in `<mark>`, left to right and one after another, when the
|
|
136
|
+
paragraph scrolls into view. A phrase that wraps gets a stroke on each line.
|
|
137
|
+
```js
|
|
138
|
+
import { highlighter } from '@gecko-ui-kit/motion'
|
|
139
|
+
|
|
140
|
+
// <p class="story">We roast in <mark>small batches</mark> every morning…</p>
|
|
141
|
+
const hl = highlighter('.story', { tone: 'amber' })
|
|
142
|
+
highlighter('.terms', { look: 'underline', once: false })
|
|
143
|
+
hl.reset(); hl.play()
|
|
144
|
+
```
|
|
145
|
+
Options: `selector` (`'mark'`), `look` (`'marker'|'underline'`), `tone` (`accent amber green rose sky violet`), `color`,
|
|
146
|
+
`trigger` (`'view'|'now'|'manual'`), `threshold` (0.6), `once` (true), `duration` (0.8), `stagger` (0.35), `delay`, `ease`.
|
|
147
|
+
`<mark class="hl">` on its own is a still highlight; `--hl-c` colour, `--hl-h` stroke height. Without the script, or with
|
|
148
|
+
reduced motion, the words are simply highlighted.
|
|
149
|
+
|
|
150
|
+
## Scroll reveal & blur-in text
|
|
151
|
+
Blocks wait out of sight and arrive as they scroll into view: they fade, rise, slide, zoom or come into focus from a
|
|
152
|
+
blur, top to bottom when several arrive together. With `words: true` a paragraph arrives word by word (screen readers
|
|
153
|
+
still get the sentence once). It uses an IntersectionObserver, once or every time.
|
|
154
|
+
```js
|
|
155
|
+
import { scrollReveal, blurIn, scrollProgress } from '@gecko-ui-kit/motion'
|
|
156
|
+
|
|
157
|
+
scrollReveal('[data-reveal]') // rise in, once
|
|
158
|
+
scrollReveal('.step', { effect: 'slide', once: false, root: panel }) // every time, inside a scrolling box
|
|
159
|
+
scrollReveal('.quote', { effect: 'blur', words: true }) // words come into focus one by one
|
|
160
|
+
blurIn('.hero p', { blur: 12 }) // the same word blur, right now
|
|
161
|
+
scrollProgress('.scroll-progress') // a reading line: --progress 0 → 1
|
|
162
|
+
```
|
|
163
|
+
Options: `effect` (`fade rise slide zoom blur`), `words`, `y` (28), `x` (−32), `scale`, `blur` (10), `duration`, `stagger`
|
|
164
|
+
(0.08), `wordStagger` (0.04), `delay`, `ease`, `once` (true), `threshold` (0.2), `root`, `rootMargin`, `onEnter`, `onLeave`.
|
|
165
|
+
Returns `{ show(), reset(), destroy() }`. Put `data-reveal` on the targets so they are hidden before the script runs.
|
|
166
|
+
With reduced motion everything is simply there.
|
|
167
|
+
|
|
168
|
+
## Marquee
|
|
169
|
+
An endless strip of logos or items sliding sideways. The list is copied just enough to fill the strip (copies are
|
|
170
|
+
`aria-hidden` and `inert`) and the loop has no seam. Hover or focus slows it to a gentle stop; it stops off screen.
|
|
171
|
+
With reduced motion it is a still row you can scroll by hand.
|
|
172
|
+
```js
|
|
173
|
+
import { marquee } from '@gecko-ui-kit/motion'
|
|
174
|
+
|
|
175
|
+
// <div class="marquee" aria-label="Our customers"><ul><li>…</li><li>…</li></ul></div>
|
|
176
|
+
const strip = marquee('.marquee', { speed: 50, direction: 'left', button: '#pause-logos' })
|
|
177
|
+
strip.set({ speed: 80 }) // also play(), pause(), toggle(), refresh(), destroy()
|
|
178
|
+
```
|
|
179
|
+
Options: `speed` (px/s, 50), `direction` (`'left'|'right'`), `pauseOnHover` (true), `gap` (`--marquee-gap`, 40px),
|
|
180
|
+
`fade` (soft edges, true), `button` (a pause/play button: aria-pressed + label), `words: { pause, play }`.
|
|
181
|
+
|
|
182
|
+
## Tilt card
|
|
183
|
+
The card leans toward the pointer in 3D and grows a touch, a soft glare slides across it, and `data-depth` layers float
|
|
184
|
+
above it and drift against the tilt. It springs back with a small wobble. Mouse and trackpad only; flat with reduced motion.
|
|
185
|
+
```js
|
|
186
|
+
import { tilt } from '@gecko-ui-kit/motion'
|
|
187
|
+
|
|
188
|
+
// <article class="product"><img data-depth="30" …><h3 data-depth="15">…</h3></article>
|
|
189
|
+
tilt('.product', { max: 12, scale: 1.03, glare: true })
|
|
190
|
+
```
|
|
191
|
+
Options: `max` (12°), `scale` (1.03), `perspective` (900), `glare` (true; colour `--tilt-glare`), `parallax` (0.25),
|
|
192
|
+
`follow` (0.35 s), `spring` (1 s). Returns `{ reset(), destroy() }`. Keep `overflow` visible on the card.
|
|
193
|
+
|
|
194
|
+
## Spotlight cards
|
|
195
|
+
A soft light follows the pointer across a group of cards: the edges it touches light up and a faint glow fills them.
|
|
196
|
+
Tabbing to a card brings the light to it. Touch is ignored; with reduced motion the light doesn't glide or fade.
|
|
197
|
+
```js
|
|
198
|
+
import { spotlight } from '@gecko-ui-kit/motion'
|
|
199
|
+
|
|
200
|
+
spotlight('.features') // every child is a card
|
|
201
|
+
spotlight(grid, { items: '.plan', size: 300, follow: 0 })
|
|
202
|
+
```
|
|
203
|
+
Options: `items`, `size` (240 px), `follow` (0.2 s), `border` (true), `fill` (true), `focus` (true). Colour `--spot-c`
|
|
204
|
+
(default `--accent`), edge `--spot-ring`. Uses the cards' `::before` / `::after`.
|
package/index.js
CHANGED
|
@@ -23,3 +23,19 @@ export { odometer } from './src/odometer.js';
|
|
|
23
23
|
export { confetti } from './src/confetti.js';
|
|
24
24
|
/** Burger icon: three lines that morph to ✕ and back (burger), with click/Escape wiring and aria-expanded (bindBurger). See src/burger.js. */
|
|
25
25
|
export { burgerIcon, burger, bindBurger } from './src/burger.js';
|
|
26
|
+
/** Typewriter: text typed letter by letter behind a caret; several phrases are deleted and retyped in turn (screen readers get the whole text). See src/typewriter.js. */
|
|
27
|
+
export { typewriter } from './src/typewriter.js';
|
|
28
|
+
/** Word rotator: one word in a sentence keeps changing (roll, drop, slide, blur or letters) while its width glides to fit. See src/rotator.js. */
|
|
29
|
+
export { rotator } from './src/rotator.js';
|
|
30
|
+
/** Shine text: a slanted band of light sweeps across the letters (loop, on view or on hover; CSS-only with shine--loop). See src/shine.js. */
|
|
31
|
+
export { shine } from './src/shine.js';
|
|
32
|
+
/** Highlighter: a marker stroke draws behind (or under) the words in <mark>, one after another, when they scroll into view. See src/highlighter.js. */
|
|
33
|
+
export { highlighter } from './src/highlighter.js';
|
|
34
|
+
/** Scroll reveal: blocks (or their words) fade, rise, slide, zoom or blur in when they scroll into view, once or every time; blurIn plays the word blur now; scrollProgress fills a reading bar. See src/scrollreveal.js. */
|
|
35
|
+
export { scrollReveal, blurIn, scrollProgress } from './src/scrollreveal.js';
|
|
36
|
+
/** Marquee: an endless sideways strip of logos or items, seamless loop, eases to a stop on hover/focus, static scrollable row with reduced motion. See src/marquee.js. */
|
|
37
|
+
export { marquee } from './src/marquee.js';
|
|
38
|
+
/** Tilt card: leans toward the pointer in 3D with a soft glare and data-depth layers, springs back on leave (mouse/trackpad only). See src/tilt.js. */
|
|
39
|
+
export { tilt } from './src/tilt.js';
|
|
40
|
+
/** Spotlight cards: a soft light follows the pointer across a group of cards and lights up their edges. See src/spotlight.js. */
|
|
41
|
+
export { spotlight } from './src/spotlight.js';
|
package/motion.css
CHANGED
|
@@ -135,3 +135,116 @@ body.is-dragging, body.is-dragging * { cursor: grabbing !important; user-select:
|
|
|
135
135
|
.burger > i:nth-child(3) { top: calc(100% - var(--burger-t)); }
|
|
136
136
|
.burger--lg { --burger-w: 24px; --burger-h: 17px; --burger-t: 2.5px; }
|
|
137
137
|
.burger--sm { --burger-w: 16px; --burger-h: 11px; --burger-t: 1.75px; }
|
|
138
|
+
|
|
139
|
+
/* ---------- typewriter (typewriter) ----------
|
|
140
|
+
Caret colour: --tw-caret (default the accent). Selection colour for erase: 'select': --tw-sel. */
|
|
141
|
+
.tw { --tw-caret: var(--accent, #5b4cf0); }
|
|
142
|
+
.tw__sr, .rot__sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
|
|
143
|
+
.tw__text { border-radius: .12em; }
|
|
144
|
+
.tw__text.is-sel { background: var(--tw-sel, var(--accent-soft, #ecebff)); box-shadow: 0 0 0 .06em var(--tw-sel, var(--accent-soft, #ecebff)); }
|
|
145
|
+
.tw__caret { display: inline-block; width: max(2px, .08em); height: 1.05em; margin-left: .06em; vertical-align: -.14em; border-radius: 2px; background: var(--tw-caret); animation: tw-blink 1.05s steps(1) infinite; }
|
|
146
|
+
.tw--block .tw__caret { width: .52em; margin-left: .04em; border-radius: .08em; opacity: .85; }
|
|
147
|
+
.tw--underscore .tw__caret { width: .55em; height: max(2px, .09em); margin-left: .04em; vertical-align: -.06em; }
|
|
148
|
+
.tw--none .tw__caret { display: none; }
|
|
149
|
+
.tw.is-typing .tw__caret, .tw--still .tw__caret { animation: none; }
|
|
150
|
+
@keyframes tw-blink { 50% { opacity: 0; } }
|
|
151
|
+
@media (prefers-reduced-motion: reduce) { .tw__caret { animation: none; } }
|
|
152
|
+
|
|
153
|
+
/* ---------- word rotator (rotator) ----------
|
|
154
|
+
The slot is an inline box whose width the script glides; words sit on top of each other inside.
|
|
155
|
+
Line height in the slot: --rot-lh. rot--pill adds a highlight box: --rot-bg, --rot-c. */
|
|
156
|
+
.rot { white-space: nowrap; }
|
|
157
|
+
.rot__box { position: relative; display: inline-block; box-sizing: content-box; vertical-align: baseline; line-height: var(--rot-lh, 1.2); white-space: nowrap;
|
|
158
|
+
clip-path: inset(-.22em -.12em); } /* room for Thai marks above / below, clipped further out */
|
|
159
|
+
.rot__sizer { visibility: hidden; }
|
|
160
|
+
.rot__w { position: absolute; top: 0; left: var(--rot-px, 0); white-space: nowrap; will-change: transform; }
|
|
161
|
+
.rot__ch { display: inline-block; }
|
|
162
|
+
.rot--pill .rot__box { --rot-px: .22em; padding: 0 var(--rot-px); border-radius: .28em; background: var(--rot-bg, var(--accent-soft, #ecebff)); color: var(--rot-c, var(--accent, #5b4cf0));
|
|
163
|
+
clip-path: inset(0 round .28em); }
|
|
164
|
+
|
|
165
|
+
/* ---------- shine text (shine) ----------
|
|
166
|
+
The letters are painted by two background layers: a slanted band of light (--shine-c) over the
|
|
167
|
+
base (--shine-base, the text colour by default). shine--loop repeats the sweep with CSS alone. */
|
|
168
|
+
.shine { --shine-c: color-mix(in srgb, currentColor 30%, #fff); }
|
|
169
|
+
.shine--gradient { color: var(--accent, #5b4cf0); --shine-c: rgba(255, 255, 255, .9);
|
|
170
|
+
--shine-base: linear-gradient(135deg, var(--accent, #5b4cf0), var(--hue-rose, #e25577) 55%, var(--accent, #5b4cf0)); }
|
|
171
|
+
.shine--ink { color: var(--muted, #6b7280); --shine-c: var(--ink, #14161c); }
|
|
172
|
+
@supports (background-clip: text) or (-webkit-background-clip: text) {
|
|
173
|
+
.shine {
|
|
174
|
+
background-image: linear-gradient(var(--shine-angle, 110deg), transparent calc(50% - var(--shine-w, 12%)), var(--shine-c) 50%, transparent calc(50% + var(--shine-w, 12%))),
|
|
175
|
+
var(--shine-base, linear-gradient(currentColor, currentColor));
|
|
176
|
+
background-size: 250% 100%, 100% 100%;
|
|
177
|
+
background-repeat: no-repeat;
|
|
178
|
+
background-position: calc(var(--shine-x, 110) * 1%) 0, 0 0;
|
|
179
|
+
-webkit-background-clip: text; background-clip: text;
|
|
180
|
+
-webkit-text-fill-color: transparent;
|
|
181
|
+
-webkit-box-decoration-break: clone; box-decoration-break: clone;
|
|
182
|
+
}
|
|
183
|
+
.shine--loop { animation: shine-sweep var(--shine-dur, 3.2s) ease-in-out infinite; }
|
|
184
|
+
}
|
|
185
|
+
@keyframes shine-sweep { 0% { background-position: 110% 0, 0 0; } 55%, 100% { background-position: -10% 0, 0 0; } }
|
|
186
|
+
@media (prefers-reduced-motion: reduce) { .shine--loop { animation: none; } }
|
|
187
|
+
|
|
188
|
+
/* ---------- highlighter (highlighter) ----------
|
|
189
|
+
A marker stroke with soft uneven ends behind each <mark class="hl">. The script draws it by
|
|
190
|
+
moving --hl-p from 0 to 100; without the script it is simply shown. Colour: --hl-c. */
|
|
191
|
+
.hl { --hl-c: color-mix(in srgb, var(--accent, #5b4cf0) 30%, transparent);
|
|
192
|
+
margin: 0 -.12em; padding: .06em .12em; border-radius: .7em .25em .6em .3em; color: inherit;
|
|
193
|
+
background: linear-gradient(103deg, transparent .1em, var(--hl-c) .4em calc(100% - .45em), transparent calc(100% - .1em)) no-repeat 0 70% / calc(var(--hl-p, 100) * 1%) var(--hl-h, 78%);
|
|
194
|
+
-webkit-box-decoration-break: clone; box-decoration-break: clone; }
|
|
195
|
+
.hl--underline { --hl-h: .32em; background-position: 0 92%; border-radius: .2em; }
|
|
196
|
+
.hl--amber { --hl-c: color-mix(in srgb, var(--hue-amber, #e39a14) 38%, transparent); }
|
|
197
|
+
.hl--green { --hl-c: color-mix(in srgb, var(--hue-green, #2f9e57) 30%, transparent); }
|
|
198
|
+
.hl--rose { --hl-c: color-mix(in srgb, var(--hue-rose, #e25577) 30%, transparent); }
|
|
199
|
+
.hl--sky { --hl-c: color-mix(in srgb, var(--hue-sky, #2b8fd6) 30%, transparent); }
|
|
200
|
+
.hl--violet { --hl-c: color-mix(in srgb, var(--hue-violet, #9b51e0) 30%, transparent); }
|
|
201
|
+
.hl--underline.hl--accent { --hl-c: color-mix(in srgb, var(--accent, #5b4cf0) 45%, transparent); }
|
|
202
|
+
@media (prefers-reduced-motion: reduce) { .hl { --hl-p: 100 !important; } }
|
|
203
|
+
|
|
204
|
+
/* ---------- scroll reveal (scrollReveal / blurIn / scrollProgress) ----------
|
|
205
|
+
data-reveal hides a block before the script runs; once scrollReveal() takes over (.is-armed) its
|
|
206
|
+
inline styles do the hiding. .is-shown is added when a block has been revealed (a hook for your CSS). */
|
|
207
|
+
[data-reveal]:not(.is-armed) { opacity: 0; }
|
|
208
|
+
@media (scripting: none) { [data-reveal]:not(.is-armed) { opacity: 1; } }
|
|
209
|
+
.reveal__w { display: inline-block; }
|
|
210
|
+
.reveal__sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
|
|
211
|
+
/* a thin reading-progress line: set --progress 0 → 1 (scrollProgress does) */
|
|
212
|
+
.scroll-progress { position: relative; height: var(--scroll-progress-h, 3px); overflow: hidden; background: color-mix(in srgb, var(--ink, #14161c) 8%, transparent); pointer-events: none; }
|
|
213
|
+
.scroll-progress::after { content: ""; position: absolute; inset: 0; background: var(--scroll-progress-c, var(--accent, #5b4cf0)); transform: scaleX(var(--progress, 0)); transform-origin: left center; }
|
|
214
|
+
@media (prefers-reduced-motion: reduce) { [data-reveal]:not(.is-armed) { opacity: 1; } }
|
|
215
|
+
|
|
216
|
+
/* ---------- marquee (marquee) ----------
|
|
217
|
+
Space between items: --marquee-gap. Edge fade width: --marquee-fade. */
|
|
218
|
+
.marquee { --marquee-gap: 40px; --marquee-fade: 12%; display: flex; overflow: hidden; max-width: 100%; }
|
|
219
|
+
.marquee--fade { -webkit-mask-image: linear-gradient(90deg, transparent, #000 var(--marquee-fade), #000 calc(100% - var(--marquee-fade)), transparent);
|
|
220
|
+
mask-image: linear-gradient(90deg, transparent, #000 var(--marquee-fade), #000 calc(100% - var(--marquee-fade)), transparent); }
|
|
221
|
+
.marquee__track { display: flex; flex: none; will-change: transform; }
|
|
222
|
+
.marquee__list { display: flex; flex: none; align-items: center; gap: var(--marquee-gap); margin: 0; padding: 0 var(--marquee-gap) 0 0; list-style: none; }
|
|
223
|
+
.marquee__list > * { flex: none; }
|
|
224
|
+
.marquee.is-static { overflow-x: auto; scrollbar-width: thin; -webkit-mask-image: none; mask-image: none; }
|
|
225
|
+
.marquee.is-static .marquee__track { will-change: auto; }
|
|
226
|
+
.marquee.is-static .marquee__list { padding-left: var(--marquee-gap); }
|
|
227
|
+
.marquee:focus-visible { outline: 2px solid var(--accent, #5b4cf0); outline-offset: 2px; border-radius: 6px; }
|
|
228
|
+
|
|
229
|
+
/* ---------- tilt card (tilt) ----------
|
|
230
|
+
Glare colour: --tilt-glare. Layers with data-depth float above the card. */
|
|
231
|
+
:where(.tilt) { position: relative; }
|
|
232
|
+
.tilt, .tilt :has([data-depth]) { transform-style: preserve-3d; }
|
|
233
|
+
.tilt { will-change: transform; }
|
|
234
|
+
.tilt__glare { position: absolute; inset: 0; z-index: 2; border-radius: inherit; pointer-events: none; opacity: 0;
|
|
235
|
+
background: radial-gradient(circle at var(--gx, 50%) var(--gy, 0%), var(--tilt-glare, rgba(255, 255, 255, .38)), transparent 60%); }
|
|
236
|
+
[data-theme="dark"] .tilt__glare { --tilt-glare: rgba(255, 255, 255, .14); }
|
|
237
|
+
|
|
238
|
+
/* ---------- spotlight cards (spotlight) ----------
|
|
239
|
+
Light colour: --spot-c; size: --spot-size (set by the script); edge thickness: --spot-ring. */
|
|
240
|
+
:where(.spotlight__card) { position: relative; isolation: isolate; }
|
|
241
|
+
.spotlight__card::before, .spotlight__card::after { content: ""; position: absolute; border-radius: inherit; pointer-events: none; opacity: 0; transition: opacity .4s; }
|
|
242
|
+
.spotlight__card::before { inset: -1px; z-index: 1; padding: var(--spot-ring, 1.5px);
|
|
243
|
+
background: radial-gradient(calc(var(--spot-size, 240px) * .75) circle at calc(var(--spot-x, -999px) + 1px) calc(var(--spot-y, -999px) + 1px), var(--spot-c, var(--accent, #5b4cf0)), transparent 70%);
|
|
244
|
+
-webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0); -webkit-mask-composite: xor;
|
|
245
|
+
mask: linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0); }
|
|
246
|
+
.spotlight__card::after { inset: 0; z-index: -1;
|
|
247
|
+
background: radial-gradient(var(--spot-size, 240px) circle at var(--spot-x, -999px) var(--spot-y, -999px), color-mix(in srgb, var(--spot-c, var(--accent, #5b4cf0)) 12%, transparent), transparent 70%); }
|
|
248
|
+
.spotlight.is-lit .spotlight__card::before, .spotlight.is-lit .spotlight__card::after { opacity: 1; }
|
|
249
|
+
.spotlight--no-ring .spotlight__card::before, .spotlight--no-fill .spotlight__card::after { display: none; }
|
|
250
|
+
@media (prefers-reduced-motion: reduce) { .spotlight__card::before, .spotlight__card::after { transition: none; } }
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gecko-ui-kit/motion",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Animation helpers: stretchy indicator, page swap, text reveal, count-up, odometer, confetti, burger morph, drag & drop, theme reveal and more.",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "Animation helpers: stretchy indicator, page swap, text reveal, typewriter, word rotator, shine, highlighter, scroll reveal, marquee, tilt, spotlight, count-up, odometer, confetti, burger morph, drag & drop, theme reveal and more.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vanilla-js",
|
|
7
7
|
"ui",
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/* Highlighter: key words get a marker stroke drawn behind them, left to right, one after another,
|
|
2
|
+
when the paragraph scrolls into view, as if someone went over the page with a highlighter pen.
|
|
3
|
+
The stroke has soft, slightly uneven ends and rounded corners so it looks hand-made, and when
|
|
4
|
+
a highlighted phrase wraps onto two lines each line gets its own stroke. Wrap the words in
|
|
5
|
+
<mark> (or any element you pick with `selector`). Two looks: 'marker' (behind the words) and
|
|
6
|
+
'underline' (a thick stroke under them). Colours follow the theme (accent, or a tone such as
|
|
7
|
+
amber), or any colour you pass. <mark> keeps its meaning for screen readers; nothing is hidden.
|
|
8
|
+
Without JavaScript, or with reduced motion, the words are simply highlighted. */
|
|
9
|
+
import { g, RM, $$ } from './core.js';
|
|
10
|
+
|
|
11
|
+
const state = new WeakMap();
|
|
12
|
+
const TONES = new Set(['accent', 'amber', 'green', 'rose', 'sky', 'violet']);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* const hl = highlighter(el, {
|
|
16
|
+
* selector: 'mark', // which words inside el get the stroke
|
|
17
|
+
* look: 'marker' | 'underline',
|
|
18
|
+
* tone: 'accent' | 'amber' | 'green' | 'rose' | 'sky' | 'violet',
|
|
19
|
+
* color: null, // any CSS colour instead of a tone (keep it see-through so the text stays readable)
|
|
20
|
+
* trigger: 'view' | 'now' | 'manual', // when it scrolls into view · straight away · only when you call hl.play()
|
|
21
|
+
* threshold: 0.6, // 'view': how much of el must be on screen (0–1)
|
|
22
|
+
* once: true, // 'view': false draws it again every time it comes back
|
|
23
|
+
* duration: 0.8, // seconds one stroke takes
|
|
24
|
+
* stagger: 0.35, // seconds between strokes
|
|
25
|
+
* delay: 0,
|
|
26
|
+
* ease: 'power2.inOut',
|
|
27
|
+
* })
|
|
28
|
+
* hl.play() · hl.reset() · hl.destroy() · hl.marks
|
|
29
|
+
* Or just add class="hl" to a <mark> for a still highlight (CSS only).
|
|
30
|
+
*/
|
|
31
|
+
export const highlighter = (el, opts = {}) => {
|
|
32
|
+
el = $$(el)[0];
|
|
33
|
+
if (!el) return null;
|
|
34
|
+
state.get(el)?.destroy();
|
|
35
|
+
const o = { selector: 'mark', look: 'marker', tone: 'accent', color: null, trigger: 'view', threshold: 0.6, once: true, duration: 0.8, stagger: 0.35, delay: 0, ease: 'power2.inOut', ...opts };
|
|
36
|
+
const marks = $$(o.selector, el);
|
|
37
|
+
const cls = ['hl', `hl--${o.look === 'underline' ? 'underline' : 'marker'}`, `hl--${TONES.has(o.tone) ? o.tone : 'accent'}`];
|
|
38
|
+
marks.forEach((m) => { m.classList.add(...cls); if (o.color) m.style.setProperty('--hl-c', o.color); });
|
|
39
|
+
|
|
40
|
+
let tl = null;
|
|
41
|
+
const reset = () => { tl?.kill(); tl = null; g.set(marks, { '--hl-p': RM() ? 100 : 0 }); };
|
|
42
|
+
const play = () => {
|
|
43
|
+
tl?.kill();
|
|
44
|
+
if (RM()) { g.set(marks, { '--hl-p': 100 }); return; }
|
|
45
|
+
tl = g.fromTo(marks, { '--hl-p': 0 }, { '--hl-p': 100, duration: o.duration, stagger: o.stagger, delay: o.delay, ease: o.ease });
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
let io = null;
|
|
49
|
+
if (o.trigger === 'now') play();
|
|
50
|
+
else {
|
|
51
|
+
reset();
|
|
52
|
+
if (o.trigger === 'view' && typeof IntersectionObserver === 'function') {
|
|
53
|
+
let shown = false;
|
|
54
|
+
io = new IntersectionObserver(([e]) => {
|
|
55
|
+
// tall blocks may never be 60% on screen: half the screen filled counts too
|
|
56
|
+
const enough = e.intersectionRatio >= o.threshold || (e.rootBounds && e.intersectionRect.height >= e.rootBounds.height * 0.5);
|
|
57
|
+
if (e.isIntersecting && enough && !shown) { shown = true; play(); if (o.once) io.disconnect(); }
|
|
58
|
+
else if (!e.isIntersecting && !o.once) { shown = false; reset(); }
|
|
59
|
+
}, { threshold: [0, o.threshold, 1] });
|
|
60
|
+
io.observe(el);
|
|
61
|
+
} else if (o.trigger === 'view') play();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const h = {
|
|
65
|
+
el, marks,
|
|
66
|
+
play() { play(); return h; },
|
|
67
|
+
reset() { reset(); return h; },
|
|
68
|
+
destroy() {
|
|
69
|
+
tl?.kill(); io?.disconnect();
|
|
70
|
+
marks.forEach((m) => { m.classList.remove(...cls); m.style.removeProperty('--hl-c'); m.style.removeProperty('--hl-p'); });
|
|
71
|
+
state.delete(el);
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
state.set(el, h);
|
|
75
|
+
return h;
|
|
76
|
+
};
|
package/src/marquee.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/* Marquee: a strip of logos (or any items) that slides sideways forever. The list is copied as many
|
|
2
|
+
times as it takes to fill the strip (the copies are hidden from screen readers and can't be
|
|
3
|
+
tabbed into), and the track moves exactly one list-width before it starts over, so the loop has
|
|
4
|
+
no seam. Hovering or focusing the strip slows it to a gentle stop instead of freezing it, and it
|
|
5
|
+
picks up speed again when the pointer leaves. It also stops while the strip is off screen.
|
|
6
|
+
With reduced motion nothing moves: the items sit in one row you can scroll by hand. */
|
|
7
|
+
import { g, RM, D, $$ } from './core.js';
|
|
8
|
+
|
|
9
|
+
const WORDS = { pause: 'Pause scrolling', play: 'Play scrolling' };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* marquee(el, options) → { play(), pause(), toggle(), set(options), refresh(), destroy(), paused }
|
|
13
|
+
*
|
|
14
|
+
* <div class="marquee" aria-label="Our customers">
|
|
15
|
+
* <ul><li>Lumen</li><li>Orbitly</li>…</ul>
|
|
16
|
+
* </div>
|
|
17
|
+
* marquee('.marquee', { speed: 50, direction: 'left' })
|
|
18
|
+
* marquee(row2, { direction: 'right', speed: 40, button: '#pause-logos' })
|
|
19
|
+
*
|
|
20
|
+
* Options:
|
|
21
|
+
* speed: 50 pixels per second
|
|
22
|
+
* direction: 'left' 'left' | 'right'
|
|
23
|
+
* pauseOnHover: true hover or keyboard focus slows the strip to a stop
|
|
24
|
+
* gap: null space between items (number = px, or any CSS length); default --marquee-gap (40px)
|
|
25
|
+
* fade: true soften the left and right edges
|
|
26
|
+
* button: null a button (or selector) that pauses and plays; gets aria-pressed and a label,
|
|
27
|
+
* and is hidden while nothing moves. One button may drive several strips.
|
|
28
|
+
* words: { pause, play } the button's label in another language
|
|
29
|
+
* One element child is the list; several children are wrapped into one list for you.
|
|
30
|
+
*/
|
|
31
|
+
export const marquee = (el, opts = {}) => {
|
|
32
|
+
el = $$(el)[0];
|
|
33
|
+
const noop = { play() {}, pause() {}, toggle() {}, set() {}, refresh() {}, destroy() {}, paused: true };
|
|
34
|
+
if (!el) return noop;
|
|
35
|
+
let o = { speed: 50, direction: 'left', pauseOnHover: true, gap: null, fade: true, button: null, words: null, ...opts };
|
|
36
|
+
const words = { ...WORDS, ...(o.words || {}) };
|
|
37
|
+
const before = el.innerHTML;
|
|
38
|
+
|
|
39
|
+
el.classList.add('marquee');
|
|
40
|
+
el.classList.toggle('marquee--fade', !!o.fade);
|
|
41
|
+
if (o.gap != null) el.style.setProperty('--marquee-gap', typeof o.gap === 'number' ? `${o.gap}px` : o.gap);
|
|
42
|
+
|
|
43
|
+
// the list, inside a track
|
|
44
|
+
let list = el.children.length === 1 ? el.firstElementChild : null;
|
|
45
|
+
if (!list) { list = document.createElement('div'); list.append(...el.childNodes); }
|
|
46
|
+
list.classList.add('marquee__list');
|
|
47
|
+
const track = document.createElement('div');
|
|
48
|
+
track.className = 'marquee__track';
|
|
49
|
+
track.append(list);
|
|
50
|
+
el.replaceChildren(track);
|
|
51
|
+
|
|
52
|
+
const st = { user: false, hover: false, focus: false, off: false };
|
|
53
|
+
let tween = null, width = 0, raf = 0;
|
|
54
|
+
|
|
55
|
+
const sync = () => {
|
|
56
|
+
if (!tween) return;
|
|
57
|
+
if (st.off) { tween.pause(); return; }
|
|
58
|
+
tween.resume();
|
|
59
|
+
const stop = st.user || (o.pauseOnHover && (st.hover || st.focus));
|
|
60
|
+
if (st.user) { g.killTweensOf(tween); tween.timeScale(0); return; } // a pause button stops at once
|
|
61
|
+
g.to(tween, { timeScale: stop ? 0 : 1, duration: D(stop ? 0.6 : 0.9), ease: stop ? 'power2.out' : 'power2.in', overwrite: true });
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const build = () => {
|
|
65
|
+
raf = 0;
|
|
66
|
+
const progress = tween ? tween.progress() : 0;
|
|
67
|
+
tween?.kill(); tween = null;
|
|
68
|
+
g.killTweensOf(track);
|
|
69
|
+
track.querySelectorAll('.marquee__list[data-copy]').forEach((c) => c.remove());
|
|
70
|
+
g.set(track, { clearProps: 'transform' });
|
|
71
|
+
width = list.getBoundingClientRect().width;
|
|
72
|
+
const still = RM() || !width;
|
|
73
|
+
el.classList.toggle('is-static', still);
|
|
74
|
+
if (btn) btn.hidden = still; // nothing to pause
|
|
75
|
+
// a strip you can scroll by hand gets keyboard focus when it overflows
|
|
76
|
+
if (still && el.scrollWidth > el.clientWidth + 1) el.tabIndex = 0; else el.removeAttribute('tabindex');
|
|
77
|
+
if (still) return;
|
|
78
|
+
const copies = Math.max(1, Math.ceil(el.clientWidth / width));
|
|
79
|
+
for (let i = 0; i < copies; i++) {
|
|
80
|
+
const c = list.cloneNode(true);
|
|
81
|
+
c.dataset.copy = '';
|
|
82
|
+
c.setAttribute('aria-hidden', 'true');
|
|
83
|
+
c.inert = true;
|
|
84
|
+
track.append(c);
|
|
85
|
+
}
|
|
86
|
+
const left = o.direction !== 'right';
|
|
87
|
+
tween = g.fromTo(track, { x: left ? 0 : -width }, { x: left ? -width : 0, duration: width / Math.max(1, o.speed), ease: 'none', repeat: -1 });
|
|
88
|
+
tween.progress(progress);
|
|
89
|
+
tween.timeScale(st.user || (o.pauseOnHover && (st.hover || st.focus)) ? 0 : 1);
|
|
90
|
+
if (st.off) tween.pause();
|
|
91
|
+
};
|
|
92
|
+
const later = () => { if (!raf) raf = requestAnimationFrame(build); };
|
|
93
|
+
|
|
94
|
+
// events
|
|
95
|
+
const enter = (e) => { if (e.pointerType !== 'touch') { st.hover = true; sync(); } };
|
|
96
|
+
const leave = () => { st.hover = false; sync(); };
|
|
97
|
+
const fin = () => { st.focus = true; sync(); };
|
|
98
|
+
const fout = (e) => { if (!el.contains(e.relatedTarget)) { st.focus = false; sync(); } };
|
|
99
|
+
el.addEventListener('pointerenter', enter);
|
|
100
|
+
el.addEventListener('pointerleave', leave);
|
|
101
|
+
el.addEventListener('focusin', fin);
|
|
102
|
+
el.addEventListener('focusout', fout);
|
|
103
|
+
const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(later) : null;
|
|
104
|
+
ro?.observe(el); ro?.observe(list);
|
|
105
|
+
const io = typeof IntersectionObserver === 'function' ? new IntersectionObserver(([e]) => { st.off = !e.isIntersecting; sync(); }) : null;
|
|
106
|
+
io?.observe(el);
|
|
107
|
+
|
|
108
|
+
const btn = o.button ? $$(o.button)[0] : null;
|
|
109
|
+
const label = () => {
|
|
110
|
+
if (!btn) return;
|
|
111
|
+
btn.setAttribute('aria-pressed', String(st.user));
|
|
112
|
+
btn.setAttribute('aria-label', st.user ? words.play : words.pause);
|
|
113
|
+
btn.classList.toggle('is-paused', st.user);
|
|
114
|
+
};
|
|
115
|
+
const setUser = (v) => { st.user = v; label(); sync(); };
|
|
116
|
+
const onBtn = () => setUser(!st.user);
|
|
117
|
+
btn?.addEventListener('click', onBtn);
|
|
118
|
+
label();
|
|
119
|
+
|
|
120
|
+
build();
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
play: () => setUser(false),
|
|
124
|
+
pause: () => setUser(true),
|
|
125
|
+
toggle: () => setUser(!st.user),
|
|
126
|
+
get paused() { return st.user; },
|
|
127
|
+
/** Change speed, direction, gap or fade; the strip keeps its place. */
|
|
128
|
+
set: (next = {}) => {
|
|
129
|
+
o = { ...o, ...next };
|
|
130
|
+
el.classList.toggle('marquee--fade', !!o.fade);
|
|
131
|
+
if (next.gap != null) el.style.setProperty('--marquee-gap', typeof o.gap === 'number' ? `${o.gap}px` : o.gap);
|
|
132
|
+
build();
|
|
133
|
+
},
|
|
134
|
+
/** Measure again (after you change the items). */
|
|
135
|
+
refresh: build,
|
|
136
|
+
destroy: () => {
|
|
137
|
+
cancelAnimationFrame(raf);
|
|
138
|
+
tween?.kill(); g.killTweensOf(track);
|
|
139
|
+
ro?.disconnect(); io?.disconnect();
|
|
140
|
+
el.removeEventListener('pointerenter', enter);
|
|
141
|
+
el.removeEventListener('pointerleave', leave);
|
|
142
|
+
el.removeEventListener('focusin', fin);
|
|
143
|
+
el.removeEventListener('focusout', fout);
|
|
144
|
+
btn?.removeEventListener('click', onBtn);
|
|
145
|
+
if (btn) btn.hidden = false;
|
|
146
|
+
el.classList.remove('marquee--fade', 'is-static');
|
|
147
|
+
el.removeAttribute('tabindex');
|
|
148
|
+
el.innerHTML = before;
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
};
|
package/src/rotator.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/* Word rotator: one word in a sentence keeps changing ("We build websites → apps → brands").
|
|
2
|
+
The old word leaves and the new one arrives in the same slot: it rolls up like a drum (roll),
|
|
3
|
+
drops from above (drop), slides in sideways (slide), comes into focus from a blur (blur) or
|
|
4
|
+
rolls up letter by letter (letters). At the same time the slot's width glides to fit the new
|
|
5
|
+
word with a small overshoot, so the rest of the sentence moves over smoothly instead of jumping.
|
|
6
|
+
Add the class rot--pill for a soft highlight box behind the word. Screen readers hear the
|
|
7
|
+
sentence once with every word as a list ("websites, apps and brands"), never the changes.
|
|
8
|
+
It pauses while off screen. With reduced motion the word simply changes. */
|
|
9
|
+
import { g, RM, $$, esc } from './core.js';
|
|
10
|
+
import { listText } from './typewriter.js';
|
|
11
|
+
|
|
12
|
+
const state = new WeakMap();
|
|
13
|
+
const EFFECTS = {
|
|
14
|
+
roll: { in: { yPercent: 100, opacity: 0 }, out: { yPercent: -100, opacity: 0 } },
|
|
15
|
+
drop: { in: { yPercent: -100, opacity: 0 }, out: { yPercent: 100, opacity: 0 } },
|
|
16
|
+
slide: { in: { xPercent: 35, opacity: 0 }, out: { xPercent: -35, opacity: 0 } },
|
|
17
|
+
blur: { in: { opacity: 0, scale: 0.92, filter: 'blur(6px)' }, out: { opacity: 0, scale: 1.06, filter: 'blur(6px)' } },
|
|
18
|
+
letters: { in: { yPercent: 110, rotation: 8, opacity: 0 }, out: { yPercent: -110, rotation: -6, opacity: 0 } },
|
|
19
|
+
};
|
|
20
|
+
const graphemes = (s) => (typeof Intl !== 'undefined' && Intl.Segmenter ? [...new Intl.Segmenter(undefined, { granularity: 'grapheme' }).segment(s)].map((x) => x.segment) : [...s]);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* const r = rotator(el, {
|
|
24
|
+
* words: ['websites', 'mobile apps', 'brands'], // default: data-words="a|b|c", the children's text, or el's text
|
|
25
|
+
* every: 2.2, // seconds each word stays
|
|
26
|
+
* effect: 'roll' | 'drop' | 'slide' | 'blur' | 'letters',
|
|
27
|
+
* duration: 0.6, // seconds the change takes
|
|
28
|
+
* ease: 'back.out(1.6)', // a small overshoot as the word lands
|
|
29
|
+
* stagger: 0.03, // 'letters' only: seconds between letters
|
|
30
|
+
* auto: true, // false: only changes when you call r.next() / r.go(i)
|
|
31
|
+
* whenVisible: true, // pause while off screen
|
|
32
|
+
* locale, label, // the screen-reader list ("a, b and c") in this language, or your own text
|
|
33
|
+
* onChange(i, word),
|
|
34
|
+
* })
|
|
35
|
+
* <h2>We build <span class="rot rot--pill" id="what">websites</span></h2> → rotator('#what', { words: [...] })
|
|
36
|
+
* r.next() · r.go(i) · r.play() · r.pause() · r.set(words) · r.destroy() · r.index
|
|
37
|
+
* Line height inside the slot: --rot-lh (default 1.2). Pill colours: --rot-bg, --rot-c.
|
|
38
|
+
*/
|
|
39
|
+
export const rotator = (el, opts = {}) => {
|
|
40
|
+
el = $$(el)[0];
|
|
41
|
+
if (!el) return null;
|
|
42
|
+
state.get(el)?.destroy();
|
|
43
|
+
const o = { every: 2.2, effect: 'roll', duration: 0.6, ease: 'back.out(1.6)', stagger: 0.03, auto: true, whenVisible: true, locale: undefined, label: undefined, onChange: null, ...opts };
|
|
44
|
+
const original = el.innerHTML;
|
|
45
|
+
const lang = o.locale || el.closest('[lang]')?.lang || undefined;
|
|
46
|
+
const clean = (list) => list.map((t) => String(t).trim()).filter(Boolean);
|
|
47
|
+
let words = clean(o.words ?? (el.dataset.words ? el.dataset.words.split('|') : el.children.length ? [...el.children].map((c) => c.textContent) : [el.textContent]));
|
|
48
|
+
if (!words.length) words = [''];
|
|
49
|
+
|
|
50
|
+
el.classList.add('rot');
|
|
51
|
+
el.innerHTML = '<span class="rot__sr"></span><span class="rot__box" aria-hidden="true"><span class="rot__sizer"></span></span>';
|
|
52
|
+
const sr = el.firstChild, box = el.lastChild;
|
|
53
|
+
const label = () => { sr.textContent = o.label ?? listText(words, lang); };
|
|
54
|
+
label();
|
|
55
|
+
|
|
56
|
+
const fx = () => EFFECTS[o.effect] || EFFECTS.roll;
|
|
57
|
+
const make = (word) => {
|
|
58
|
+
const w = document.createElement('span');
|
|
59
|
+
w.className = 'rot__w';
|
|
60
|
+
w.innerHTML = o.effect === 'letters' ? graphemes(word).map((c) => `<span class="rot__ch">${c === ' ' ? ' ' : esc(c)}</span>`).join('') : esc(word);
|
|
61
|
+
box.append(w);
|
|
62
|
+
return w;
|
|
63
|
+
};
|
|
64
|
+
let i = 0, cur = make(words[0]), tl = null, call = null, visible = !o.whenVisible, paused = !o.auto, dead = false;
|
|
65
|
+
const fit = () => { if (!tl?.isActive()) box.style.width = cur.offsetWidth + 'px'; };
|
|
66
|
+
fit();
|
|
67
|
+
const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(fit) : null; // fonts arriving, the page resizing
|
|
68
|
+
ro?.observe(cur);
|
|
69
|
+
|
|
70
|
+
const schedule = () => {
|
|
71
|
+
call?.kill(); call = null;
|
|
72
|
+
if (dead || paused || !visible || words.length < 2) return;
|
|
73
|
+
call = g.delayedCall(o.every, () => { go(i + 1); });
|
|
74
|
+
};
|
|
75
|
+
const go = (to) => {
|
|
76
|
+
if (dead) return;
|
|
77
|
+
to = ((to % words.length) + words.length) % words.length;
|
|
78
|
+
tl?.progress(1).kill();
|
|
79
|
+
const old = cur;
|
|
80
|
+
i = to;
|
|
81
|
+
cur = make(words[i]);
|
|
82
|
+
ro?.disconnect(); ro?.observe(cur);
|
|
83
|
+
if (RM()) { old.remove(); box.style.width = cur.offsetWidth + 'px'; }
|
|
84
|
+
else {
|
|
85
|
+
const e = fx(), letters = o.effect === 'letters', d = o.duration;
|
|
86
|
+
const outs = letters ? old.querySelectorAll('.rot__ch') : old, ins = letters ? cur.querySelectorAll('.rot__ch') : cur;
|
|
87
|
+
if (letters) g.set(cur, { opacity: 1 });
|
|
88
|
+
tl = g.timeline({ onComplete: () => { old.remove(); g.set(cur, { clearProps: 'all' }); if (letters) g.set(ins, { clearProps: 'all' }); } });
|
|
89
|
+
const w0 = parseFloat(box.style.width) || 0, w1 = cur.offsetWidth; // a shrinking slot waits a moment so the old word is not cut off
|
|
90
|
+
tl.to(box, { width: w1, duration: d * 0.85, ease: 'back.out(1.3)' }, w1 < w0 ? d * 0.25 : 0)
|
|
91
|
+
.to(outs, { ...e.out, duration: d * 0.55, ease: 'power2.in', stagger: letters ? o.stagger * 0.6 : 0 }, 0)
|
|
92
|
+
.fromTo(ins, e.in, { yPercent: 0, xPercent: 0, rotation: 0, scale: 1, opacity: 1, filter: 'blur(0px)', duration: d, ease: o.ease, stagger: letters ? o.stagger : 0 }, d * 0.12);
|
|
93
|
+
}
|
|
94
|
+
o.onChange?.(i, words[i]);
|
|
95
|
+
schedule();
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const io = o.whenVisible && typeof IntersectionObserver === 'function' ? new IntersectionObserver(([e]) => {
|
|
99
|
+
const was = visible; visible = e.isIntersecting;
|
|
100
|
+
if (visible && !was) schedule(); else if (!visible) { call?.kill(); call = null; }
|
|
101
|
+
}) : null;
|
|
102
|
+
if (io) io.observe(el); else { visible = true; schedule(); }
|
|
103
|
+
|
|
104
|
+
const h = {
|
|
105
|
+
el,
|
|
106
|
+
get index() { return i; },
|
|
107
|
+
next() { go(i + 1); return h; },
|
|
108
|
+
go(to) { go(to); return h; },
|
|
109
|
+
play() { paused = false; schedule(); return h; },
|
|
110
|
+
pause() { paused = true; call?.kill(); call = null; return h; },
|
|
111
|
+
/** New words: the next change shows the first one. */
|
|
112
|
+
set(list) { words = clean(list); if (!words.length) words = ['']; label(); go(0); return h; },
|
|
113
|
+
destroy() {
|
|
114
|
+
dead = true; call?.kill(); tl?.kill(); io?.disconnect(); ro?.disconnect();
|
|
115
|
+
el.classList.remove('rot'); el.innerHTML = original; state.delete(el);
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
state.set(el, h);
|
|
119
|
+
return h;
|
|
120
|
+
};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/* Scroll reveal: blocks (or the words of a paragraph) stay hidden until they scroll into view, then
|
|
2
|
+
arrive: they fade, rise, slide, zoom or come into focus from a blur. Blocks that enter together
|
|
3
|
+
arrive one after the other, top to bottom; with words: true the words of each block come in one by
|
|
4
|
+
one. It watches with an IntersectionObserver, so nothing runs while you scroll. once: true plays
|
|
5
|
+
each block a single time; once: false hides it again when it leaves, so it plays every time.
|
|
6
|
+
blurIn() is the same word-by-word blur, played straight away. scrollProgress() fills a bar with how
|
|
7
|
+
far a page (or a scrolling box) has been read.
|
|
8
|
+
Screen readers always get the whole text: split words sit in an aria-hidden copy beside a hidden
|
|
9
|
+
plain one. With reduced motion everything is simply visible. */
|
|
10
|
+
import { g, RM, D, $$ } from './core.js';
|
|
11
|
+
|
|
12
|
+
/* ---------------- splitting a block into words ---------------- */
|
|
13
|
+
const INTERACTIVE = 'a, button, input, select, textarea, [tabindex], [contenteditable]';
|
|
14
|
+
|
|
15
|
+
/** Wrap every word of el's text in <span class="reveal__w"> (inline markup such as <b> or <a> survives).
|
|
16
|
+
Returns the word spans; a second call returns the same spans. */
|
|
17
|
+
const splitWords = (el) => {
|
|
18
|
+
if (el.__words) return el.__words.list;
|
|
19
|
+
const html = el.innerHTML;
|
|
20
|
+
const words = [];
|
|
21
|
+
const walk = (node) => [...node.childNodes].forEach((n) => {
|
|
22
|
+
if (n.nodeType === 3) {
|
|
23
|
+
if (!n.textContent.trim()) return;
|
|
24
|
+
const frag = document.createDocumentFragment();
|
|
25
|
+
n.textContent.split(/(\s+)/).forEach((part) => {
|
|
26
|
+
if (!part) return;
|
|
27
|
+
if (!part.trim()) { frag.append(part); return; }
|
|
28
|
+
const w = document.createElement('span');
|
|
29
|
+
w.className = 'reveal__w';
|
|
30
|
+
w.textContent = part;
|
|
31
|
+
words.push(w); frag.append(w);
|
|
32
|
+
});
|
|
33
|
+
n.replaceWith(frag);
|
|
34
|
+
} else if (n.nodeType === 1 && !/^(svg|script|style|img|br)$/i.test(n.tagName)) walk(n);
|
|
35
|
+
});
|
|
36
|
+
if (el.querySelector(INTERACTIVE)) {
|
|
37
|
+
walk(el); // links must stay reachable: split in place
|
|
38
|
+
} else {
|
|
39
|
+
// a plain copy for screen readers, the split copy for the eyes
|
|
40
|
+
const text = el.textContent.replace(/\s+/g, ' ').trim();
|
|
41
|
+
const shown = document.createElement('span');
|
|
42
|
+
shown.setAttribute('aria-hidden', 'true');
|
|
43
|
+
shown.innerHTML = html;
|
|
44
|
+
walk(shown);
|
|
45
|
+
const sr = document.createElement('span');
|
|
46
|
+
sr.className = 'reveal__sr';
|
|
47
|
+
sr.textContent = text;
|
|
48
|
+
el.replaceChildren(sr, shown);
|
|
49
|
+
}
|
|
50
|
+
el.__words = { html, list: words };
|
|
51
|
+
return words;
|
|
52
|
+
};
|
|
53
|
+
const unsplit = (el) => { if (el.__words) { el.innerHTML = el.__words.html; delete el.__words; } };
|
|
54
|
+
|
|
55
|
+
/* ---------------- where each piece starts from ---------------- */
|
|
56
|
+
const fromOf = (o, words) => {
|
|
57
|
+
const s = { opacity: 0 };
|
|
58
|
+
const e = o.effect;
|
|
59
|
+
if (e === 'rise') Object.assign(s, words ? { yPercent: 60 } : { y: o.y });
|
|
60
|
+
if (e === 'slide') Object.assign(s, words ? { xPercent: o.x < 0 ? -40 : 40 } : { x: o.x });
|
|
61
|
+
if (e === 'zoom') s.scale = words ? 0.7 : o.scale;
|
|
62
|
+
if (e === 'blur') Object.assign(s, { filter: `blur(${o.blur}px)` }, words ? { yPercent: 25 } : { y: o.y * 0.4 });
|
|
63
|
+
return s;
|
|
64
|
+
};
|
|
65
|
+
const toOf = (from) => {
|
|
66
|
+
const end = { opacity: 1, x: 0, y: 0, xPercent: 0, yPercent: 0, scale: 1, filter: 'blur(0px)' };
|
|
67
|
+
return Object.fromEntries(Object.keys(from).map((k) => [k, end[k]]));
|
|
68
|
+
};
|
|
69
|
+
const CLEAR = 'opacity,transform,filter';
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* scrollReveal(targets, options) → { els, show(), reset(), destroy() }
|
|
73
|
+
*
|
|
74
|
+
* scrollReveal('.feature') // each .feature rises into view once
|
|
75
|
+
* scrollReveal('.step', { effect: 'slide', x: -40, once: false, root: panel })
|
|
76
|
+
* scrollReveal('.quote', { effect: 'blur', words: true }) // the words come into focus one by one
|
|
77
|
+
*
|
|
78
|
+
* Options:
|
|
79
|
+
* effect: 'rise' 'fade' | 'rise' | 'slide' | 'zoom' | 'blur'
|
|
80
|
+
* words: false split each target into words and reveal them one after another
|
|
81
|
+
* y: 28, x: -32 px to rise / slide from (x < 0 = from the left)
|
|
82
|
+
* scale: 0.94, blur: 10 zoom start size, blur in px
|
|
83
|
+
* duration seconds per block (0.8) or per word (0.7)
|
|
84
|
+
* stagger: 0.08 seconds between blocks that enter together
|
|
85
|
+
* wordStagger: 0.04 seconds between words
|
|
86
|
+
* delay: 0, ease ease: 'expo.out' for blocks, 'power3.out' for words
|
|
87
|
+
* once: true false = hide again on leaving, play every time
|
|
88
|
+
* threshold: 0.2 how much of a block must be visible (0–1) before it plays
|
|
89
|
+
* rootMargin, root IntersectionObserver options; root = the scrolling box (default the page)
|
|
90
|
+
* onEnter(el), onLeave(el)
|
|
91
|
+
* Add data-reveal to the targets in your HTML so they are hidden before the script runs (motion.css).
|
|
92
|
+
*/
|
|
93
|
+
export const scrollReveal = (targets, opts = {}) => {
|
|
94
|
+
const words = !!opts.words;
|
|
95
|
+
const o = {
|
|
96
|
+
effect: 'rise', y: 28, x: -32, scale: 0.94, blur: 10,
|
|
97
|
+
duration: words ? 0.7 : 0.8, stagger: 0.08, wordStagger: 0.04, delay: 0,
|
|
98
|
+
ease: words ? 'power3.out' : 'expo.out', once: true, threshold: 0.2,
|
|
99
|
+
rootMargin: '0px 0px -6% 0px', root: null, onEnter: null, onLeave: null, ...opts,
|
|
100
|
+
};
|
|
101
|
+
const els = $$(targets);
|
|
102
|
+
const from = fromOf(o, words), to = toOf(from);
|
|
103
|
+
const st = new Map(); // el → { pieces, shown, tween }
|
|
104
|
+
const pieces = (el) => (words ? splitWords(el) : [el]);
|
|
105
|
+
|
|
106
|
+
const hide = (el) => {
|
|
107
|
+
const s = st.get(el); if (!s) return;
|
|
108
|
+
s.tween?.kill(); s.shown = false;
|
|
109
|
+
el.classList.remove('is-shown');
|
|
110
|
+
if (!RM()) g.set(s.pieces, from);
|
|
111
|
+
};
|
|
112
|
+
const show = (el, delay = 0) => {
|
|
113
|
+
const s = st.get(el); if (!s || s.shown) return;
|
|
114
|
+
s.shown = true;
|
|
115
|
+
el.classList.add('is-shown');
|
|
116
|
+
o.onEnter?.(el);
|
|
117
|
+
if (RM()) { g.set(s.pieces, { clearProps: CLEAR }); return; }
|
|
118
|
+
s.tween = g.fromTo(s.pieces, from, {
|
|
119
|
+
...to, duration: D(o.duration), ease: o.ease, delay: D(o.delay + delay),
|
|
120
|
+
stagger: words ? D(o.wordStagger) : 0, clearProps: CLEAR,
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// thresholds from 0 up to `threshold`, so a block taller than the window still reaches its mark
|
|
125
|
+
const steps = [0];
|
|
126
|
+
for (let t = 0.05; t < o.threshold; t += 0.05) steps.push(+t.toFixed(2));
|
|
127
|
+
steps.push(o.threshold);
|
|
128
|
+
const io = typeof IntersectionObserver === 'function' ? new IntersectionObserver((entries) => {
|
|
129
|
+
const entering = [];
|
|
130
|
+
entries.forEach((e) => {
|
|
131
|
+
const s = st.get(e.target); if (!s) return;
|
|
132
|
+
const tall = e.boundingClientRect.height > 0 && e.rootBounds ? e.rootBounds.height / e.boundingClientRect.height : 1;
|
|
133
|
+
const need = Math.min(o.threshold, tall * 0.6);
|
|
134
|
+
if (e.isIntersecting && e.intersectionRatio >= need - 0.001) { if (!s.shown) entering.push(e.target); }
|
|
135
|
+
else if (!e.isIntersecting && s.shown && !o.once) { hide(e.target); o.onLeave?.(e.target); }
|
|
136
|
+
});
|
|
137
|
+
// blocks that arrive together play top to bottom
|
|
138
|
+
entering.sort((a, b) => (a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1));
|
|
139
|
+
let at = 0;
|
|
140
|
+
entering.forEach((el) => {
|
|
141
|
+
show(el, at);
|
|
142
|
+
at += words ? Math.min(0.5, st.get(el).pieces.length * o.wordStagger * 0.5) : o.stagger;
|
|
143
|
+
if (o.once) io.unobserve(el);
|
|
144
|
+
});
|
|
145
|
+
}, { root: o.root ? $$(o.root)[0] : null, rootMargin: o.rootMargin, threshold: steps }) : null;
|
|
146
|
+
|
|
147
|
+
const arm = () => els.forEach((el) => {
|
|
148
|
+
st.set(el, { pieces: pieces(el), shown: false, tween: null });
|
|
149
|
+
hide(el);
|
|
150
|
+
el.classList.add('is-armed'); // from here on the inline styles do the hiding
|
|
151
|
+
if (RM() || !io) show(el);
|
|
152
|
+
if (io && !(RM() && o.once)) io.observe(el);
|
|
153
|
+
});
|
|
154
|
+
arm();
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
els,
|
|
158
|
+
/** Reveal everything now (for example before printing). */
|
|
159
|
+
show: () => els.forEach((el, i) => { show(el, i * o.stagger); io?.unobserve(el); }),
|
|
160
|
+
/** Hide everything again and wait for it to scroll into view (a replay). */
|
|
161
|
+
reset: () => { io?.disconnect(); els.forEach((el) => st.get(el)?.tween?.kill()); arm(); },
|
|
162
|
+
destroy: () => {
|
|
163
|
+
io?.disconnect();
|
|
164
|
+
els.forEach((el) => {
|
|
165
|
+
const s = st.get(el); s?.tween?.kill();
|
|
166
|
+
if (s) g.set(s.pieces, { clearProps: CLEAR });
|
|
167
|
+
el.classList.remove('is-armed', 'is-shown');
|
|
168
|
+
if (words) unsplit(el);
|
|
169
|
+
});
|
|
170
|
+
st.clear();
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* blurIn(targets, options) → GSAP tween
|
|
177
|
+
* The words of each target come into focus one after another, right now (scrollReveal with
|
|
178
|
+
* { effect: 'blur', words: true } does the same when it scrolls into view).
|
|
179
|
+
* blurIn('.hero p', { blur: 12, stagger: 0.05, delay: 0.3 })
|
|
180
|
+
* Options: blur (10 px), y (25 = a quarter of a line), duration (0.7), stagger (0.04), delay, ease ('power3.out').
|
|
181
|
+
*/
|
|
182
|
+
export const blurIn = (targets, { blur = 10, y = 25, duration = 0.7, stagger = 0.04, delay = 0, ease = 'power3.out' } = {}) => {
|
|
183
|
+
const ws = $$(targets).flatMap(splitWords);
|
|
184
|
+
if (!ws.length) return g.timeline();
|
|
185
|
+
if (RM()) { g.set(ws, { clearProps: CLEAR }); return g.timeline(); }
|
|
186
|
+
return g.fromTo(ws, { opacity: 0, filter: `blur(${blur}px)`, yPercent: y },
|
|
187
|
+
{ opacity: 1, filter: 'blur(0px)', yPercent: 0, duration: D(duration), stagger: D(stagger), delay: D(delay), ease, clearProps: CLEAR });
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* scrollProgress(bar, { root, onChange }) → { destroy() }
|
|
192
|
+
* Sets --progress (0 → 1) on bar as the page (or the scrolling box root) is read; .scroll-progress in
|
|
193
|
+
* motion.css draws it as a thin line that fills from the left. It follows the scroll directly, so it
|
|
194
|
+
* also runs with reduced motion.
|
|
195
|
+
*/
|
|
196
|
+
export const scrollProgress = (bar, { root = null, onChange = null } = {}) => {
|
|
197
|
+
bar = $$(bar)[0];
|
|
198
|
+
const box = root ? $$(root)[0] : null;
|
|
199
|
+
if (!bar) return { destroy() {} };
|
|
200
|
+
let raf = 0;
|
|
201
|
+
const read = () => {
|
|
202
|
+
raf = 0;
|
|
203
|
+
const top = box ? box.scrollTop : scrollY;
|
|
204
|
+
const max = box ? box.scrollHeight - box.clientHeight : document.documentElement.scrollHeight - innerHeight;
|
|
205
|
+
const p = max > 0 ? Math.min(1, Math.max(0, top / max)) : 1;
|
|
206
|
+
bar.style.setProperty('--progress', p.toFixed(4));
|
|
207
|
+
onChange?.(p);
|
|
208
|
+
};
|
|
209
|
+
const on = () => { if (!raf) raf = requestAnimationFrame(read); };
|
|
210
|
+
const target = box || window;
|
|
211
|
+
target.addEventListener('scroll', on, { passive: true });
|
|
212
|
+
addEventListener('resize', on, { passive: true });
|
|
213
|
+
read();
|
|
214
|
+
return { destroy: () => { cancelAnimationFrame(raf); target.removeEventListener('scroll', on); removeEventListener('resize', on); } };
|
|
215
|
+
};
|
package/src/shine.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* Shine text: a band of light sweeps across the letters, like light catching a shiny sign.
|
|
2
|
+
The text itself is painted with a background clipped to the letters: the bottom layer is the
|
|
3
|
+
text's colour (or a gradient), the top layer is a narrow, slanted band of light that slides
|
|
4
|
+
from one side to the other. It can repeat forever with a rest between sweeps, sweep once when
|
|
5
|
+
it scrolls into view, or sweep when the pointer (or keyboard focus) arrives. Three looks:
|
|
6
|
+
'gradient' (brand gradient letters with a white glint), 'ink' (soft grey letters with a band of
|
|
7
|
+
full-strength ink: the "loading…" shimmer, fine in light and dark) and 'color' (keeps the
|
|
8
|
+
text's own colour, the band is a lighter mix of it). No JavaScript? Add the classes
|
|
9
|
+
shine shine--loop and CSS repeats the sweep on its own. With reduced motion there is no sweep. */
|
|
10
|
+
import { g, RM, $$ } from './core.js';
|
|
11
|
+
|
|
12
|
+
const state = new WeakMap();
|
|
13
|
+
const LOOKS = new Set(['gradient', 'ink', 'color']);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* const s = shine(el, {
|
|
17
|
+
* look: 'gradient' | 'ink' | 'color',
|
|
18
|
+
* trigger: 'loop' | 'view' | 'hover', // forever (paused off screen) · once each time it scrolls into view · on pointer / focus
|
|
19
|
+
* duration: 1.4, // seconds one sweep takes
|
|
20
|
+
* repeatDelay: 1.8, // 'loop': seconds of rest between sweeps
|
|
21
|
+
* delay: 0, // seconds before the first sweep
|
|
22
|
+
* ease: 'power2.inOut',
|
|
23
|
+
* angle: 110, // slant of the band in degrees (90 = upright)
|
|
24
|
+
* width: 12, // band width (percent of the sweep layer; 6 thin … 24 wide)
|
|
25
|
+
* color: '#fff', // colour of the band (default: from the look)
|
|
26
|
+
* })
|
|
27
|
+
* s.play() (one sweep now) · s.pause() · s.destroy() · s.tween
|
|
28
|
+
* CSS knobs: --shine-c (band), --shine-base (bottom layer, any background-image), --shine-angle, --shine-w.
|
|
29
|
+
*/
|
|
30
|
+
export const shine = (el, opts = {}) => {
|
|
31
|
+
el = $$(el)[0];
|
|
32
|
+
if (!el) return null;
|
|
33
|
+
state.get(el)?.destroy();
|
|
34
|
+
const o = { look: 'gradient', trigger: 'loop', duration: 1.4, repeatDelay: 1.8, delay: 0, ease: 'power2.inOut', angle: 110, width: 12, color: null, ...opts };
|
|
35
|
+
const look = LOOKS.has(o.look) ? o.look : 'gradient';
|
|
36
|
+
el.classList.add('shine', `shine--${look}`);
|
|
37
|
+
el.style.setProperty('--shine-angle', o.angle + 'deg');
|
|
38
|
+
el.style.setProperty('--shine-w', o.width + '%');
|
|
39
|
+
if (o.color) el.style.setProperty('--shine-c', o.color);
|
|
40
|
+
|
|
41
|
+
// The band sits in the middle of a layer 2.5× as wide as the text; these positions put it just
|
|
42
|
+
// off the left edge (start) and just off the right edge (end), so no time is spent off screen.
|
|
43
|
+
const half = (o.width / 100) * 2.5 * 1.25; // a little extra for the slant
|
|
44
|
+
const from = ((1.25 + half) / 1.5) * 100, to = ((0.25 - half) / 1.5) * 100;
|
|
45
|
+
const set = (x) => el.style.setProperty('--shine-x', x.toFixed(2));
|
|
46
|
+
set(from);
|
|
47
|
+
|
|
48
|
+
const loop = o.trigger === 'loop';
|
|
49
|
+
const tween = g.fromTo(el, { '--shine-x': from }, {
|
|
50
|
+
'--shine-x': to, duration: o.duration, ease: o.ease, paused: true,
|
|
51
|
+
repeat: loop ? -1 : 0, repeatDelay: o.repeatDelay, delay: loop ? o.delay : 0,
|
|
52
|
+
onComplete: () => set(from),
|
|
53
|
+
});
|
|
54
|
+
const play = () => { if (!RM()) tween.restart(true); };
|
|
55
|
+
|
|
56
|
+
let io = null;
|
|
57
|
+
const on = () => { if (!tween.isActive()) play(); };
|
|
58
|
+
if (o.trigger === 'hover') { el.addEventListener('pointerenter', on); el.addEventListener('focusin', on); }
|
|
59
|
+
else if (typeof IntersectionObserver === 'function') {
|
|
60
|
+
io = new IntersectionObserver(([e]) => {
|
|
61
|
+
if (loop) { if (e.isIntersecting && !RM()) (tween.progress() === 0 && !tween.isActive() ? tween.restart(true) : tween.resume()); else tween.pause(); }
|
|
62
|
+
else if (e.isIntersecting) play();
|
|
63
|
+
}, { threshold: loop ? 0 : 0.6 });
|
|
64
|
+
io.observe(el);
|
|
65
|
+
} else if (loop && !RM()) tween.restart(true);
|
|
66
|
+
|
|
67
|
+
const h = {
|
|
68
|
+
el, tween,
|
|
69
|
+
play() { tween.restart(); if (RM()) tween.pause(0); return h; },
|
|
70
|
+
pause() { tween.pause(); return h; },
|
|
71
|
+
destroy() {
|
|
72
|
+
tween.kill(); io?.disconnect();
|
|
73
|
+
el.removeEventListener('pointerenter', on); el.removeEventListener('focusin', on);
|
|
74
|
+
el.classList.remove('shine', `shine--${look}`);
|
|
75
|
+
['--shine-x', '--shine-angle', '--shine-w', '--shine-c'].forEach((k) => el.style.removeProperty(k));
|
|
76
|
+
state.delete(el);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
state.set(el, h);
|
|
80
|
+
return h;
|
|
81
|
+
};
|
package/src/spotlight.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/* Spotlight cards: a soft round light follows the pointer across a group of cards. Where it passes,
|
|
2
|
+
the edges of every card it touches light up in the accent colour and a faint glow fills the card
|
|
3
|
+
underneath, so the light seems to slide behind the whole grid, not just the card under the pointer.
|
|
4
|
+
The light glides a moment behind the pointer and fades in and out as the pointer comes and goes.
|
|
5
|
+
Tabbing to a card moves the light onto it, so keyboard users see it too. Touch is ignored (no
|
|
6
|
+
hover there). With reduced motion the light sits on the pointer without gliding or fading. */
|
|
7
|
+
import { g, RM, $$ } from './core.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* spotlight(group, options) → { moveTo(x, y), destroy() }
|
|
11
|
+
*
|
|
12
|
+
* <div class="spotlight">
|
|
13
|
+
* <div class="card spotlight__card">…</div>
|
|
14
|
+
* <div class="card spotlight__card">…</div>
|
|
15
|
+
* </div>
|
|
16
|
+
* spotlight('.spotlight')
|
|
17
|
+
* spotlight(grid, { items: '.plan', size: 300, follow: 0 })
|
|
18
|
+
*
|
|
19
|
+
* Options:
|
|
20
|
+
* items: null the cards (selector inside group, or elements); default the group's children
|
|
21
|
+
* size: 240 px across the light
|
|
22
|
+
* follow: 0.2 seconds the light takes to catch up with the pointer (0 = glued to it)
|
|
23
|
+
* border: true light up the edges
|
|
24
|
+
* fill: true a faint glow inside the cards
|
|
25
|
+
* focus: true a focused card pulls the light onto itself
|
|
26
|
+
* Colour: --spot-c (default --accent); edge thickness: --spot-ring (1.5px).
|
|
27
|
+
*/
|
|
28
|
+
export const spotlight = (group, opts = {}) => {
|
|
29
|
+
group = $$(group)[0];
|
|
30
|
+
if (!group) return { moveTo() {}, destroy() {} };
|
|
31
|
+
const o = { items: null, size: 240, follow: 0.2, border: true, fill: true, focus: true, ...opts };
|
|
32
|
+
const cards = o.items ? $$(o.items, typeof o.items === 'string' ? group : document) : [...group.children];
|
|
33
|
+
group.classList.add('spotlight');
|
|
34
|
+
group.classList.toggle('spotlight--no-ring', !o.border);
|
|
35
|
+
group.classList.toggle('spotlight--no-fill', !o.fill);
|
|
36
|
+
group.style.setProperty('--spot-size', `${o.size}px`);
|
|
37
|
+
cards.forEach((c) => c.classList.add('spotlight__card'));
|
|
38
|
+
|
|
39
|
+
const p = { x: -9999, y: -9999 };
|
|
40
|
+
let rects = [], inside = false;
|
|
41
|
+
const measure = () => { rects = cards.map((c) => c.getBoundingClientRect()); };
|
|
42
|
+
const paint = () => cards.forEach((c, i) => {
|
|
43
|
+
const r = rects[i]; if (!r) return;
|
|
44
|
+
c.style.setProperty('--spot-x', `${(p.x - r.left).toFixed(1)}px`);
|
|
45
|
+
c.style.setProperty('--spot-y', `${(p.y - r.top).toFixed(1)}px`);
|
|
46
|
+
});
|
|
47
|
+
const go = (x, y, jump) => {
|
|
48
|
+
if (jump || RM() || !o.follow) { g.killTweensOf(p); p.x = x; p.y = y; paint(); return; }
|
|
49
|
+
g.to(p, { x, y, duration: o.follow, ease: 'power3.out', overwrite: true, onUpdate: paint });
|
|
50
|
+
};
|
|
51
|
+
const lit = (on) => group.classList.toggle('is-lit', on);
|
|
52
|
+
|
|
53
|
+
const enter = (e) => { if (e.pointerType === 'touch') return; inside = true; measure(); go(e.clientX, e.clientY, !group.classList.contains('is-lit')); lit(true); };
|
|
54
|
+
const move = (e) => { if (e.pointerType === 'touch') return; if (!inside) enter(e); else go(e.clientX, e.clientY); };
|
|
55
|
+
const leave = () => { inside = false; if (!group.contains(document.activeElement) || !o.focus) lit(false); };
|
|
56
|
+
const focusin = (e) => {
|
|
57
|
+
if (!o.focus || inside) return;
|
|
58
|
+
const i = cards.findIndex((c) => c.contains(e.target)); if (i < 0) return;
|
|
59
|
+
measure();
|
|
60
|
+
const r = rects[i];
|
|
61
|
+
go(r.left + r.width / 2, r.top + r.height / 2, !group.classList.contains('is-lit'));
|
|
62
|
+
lit(true);
|
|
63
|
+
};
|
|
64
|
+
const focusout = (e) => { if (!inside && !group.contains(e.relatedTarget)) lit(false); };
|
|
65
|
+
const rescroll = () => { if (group.classList.contains('is-lit')) { measure(); paint(); } };
|
|
66
|
+
|
|
67
|
+
group.addEventListener('pointerenter', enter);
|
|
68
|
+
group.addEventListener('pointermove', move);
|
|
69
|
+
group.addEventListener('pointerleave', leave);
|
|
70
|
+
group.addEventListener('focusin', focusin);
|
|
71
|
+
group.addEventListener('focusout', focusout);
|
|
72
|
+
addEventListener('scroll', rescroll, { passive: true, capture: true });
|
|
73
|
+
addEventListener('resize', rescroll, { passive: true });
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
/** Put the light at a point on screen (client coordinates), e.g. for a demo. */
|
|
77
|
+
moveTo: (x, y) => { measure(); go(x, y, !group.classList.contains('is-lit')); lit(true); },
|
|
78
|
+
destroy: () => {
|
|
79
|
+
g.killTweensOf(p);
|
|
80
|
+
group.removeEventListener('pointerenter', enter);
|
|
81
|
+
group.removeEventListener('pointermove', move);
|
|
82
|
+
group.removeEventListener('pointerleave', leave);
|
|
83
|
+
group.removeEventListener('focusin', focusin);
|
|
84
|
+
group.removeEventListener('focusout', focusout);
|
|
85
|
+
removeEventListener('scroll', rescroll, { capture: true });
|
|
86
|
+
removeEventListener('resize', rescroll);
|
|
87
|
+
group.classList.remove('is-lit', 'spotlight--no-ring', 'spotlight--no-fill');
|
|
88
|
+
group.style.removeProperty('--spot-size');
|
|
89
|
+
cards.forEach((c) => { c.style.removeProperty('--spot-x'); c.style.removeProperty('--spot-y'); });
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
};
|
package/src/tilt.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/* Tilt card: the card leans toward the pointer in 3D, as if you pressed the corner nearest to it,
|
|
2
|
+
and grows a touch. A soft glare slides across it from the side the light comes from, and layers
|
|
3
|
+
marked data-depth float above the card and drift a little against the tilt (parallax). When the
|
|
4
|
+
pointer leaves, the card springs back flat with a small wobble. Only for a mouse or trackpad
|
|
5
|
+
(touch screens scroll as normal) and never with reduced motion. */
|
|
6
|
+
import { g, RM, fine, $$ } from './core.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* tilt(targets, options) → { els, reset(), destroy() }
|
|
10
|
+
*
|
|
11
|
+
* <article class="card" data-tilt>
|
|
12
|
+
* <img data-depth="40" src="lamp.png" alt=""> ← floats 40px above the card
|
|
13
|
+
* <h3 data-depth="20">Ember lamp</h3>
|
|
14
|
+
* </article>
|
|
15
|
+
* tilt('[data-tilt]', { max: 12, glare: true })
|
|
16
|
+
*
|
|
17
|
+
* Options:
|
|
18
|
+
* max: 12 degrees at the card's edge
|
|
19
|
+
* scale: 1.03 size while the pointer is on it
|
|
20
|
+
* perspective: 900 px; smaller = stronger 3D
|
|
21
|
+
* glare: true a soft light that follows the pointer (colour: --tilt-glare)
|
|
22
|
+
* parallax: 0.25 how far data-depth layers drift, as a share of their depth (0 = none)
|
|
23
|
+
* follow: 0.35 seconds the card takes to catch up with the pointer
|
|
24
|
+
* spring: 1 seconds it takes to settle back; ease 'elastic.out(1, 0.5)'
|
|
25
|
+
* Keep overflow visible on the card and anything between it and a data-depth layer, or the layers flatten.
|
|
26
|
+
*/
|
|
27
|
+
export const tilt = (targets, opts = {}) => {
|
|
28
|
+
const o = { max: 12, scale: 1.03, perspective: 900, glare: true, parallax: 0.25, follow: 0.35, spring: 1, ...opts };
|
|
29
|
+
const els = $$(targets);
|
|
30
|
+
const offs = [];
|
|
31
|
+
|
|
32
|
+
els.forEach((el) => {
|
|
33
|
+
if (el.__tilt) return;
|
|
34
|
+
const layers = [...el.querySelectorAll('[data-depth]')];
|
|
35
|
+
const s = { rx: 0, ry: 0, sc: 1, gx: 50, gy: 50, ga: 0 };
|
|
36
|
+
el.classList.add('tilt');
|
|
37
|
+
let glare = null;
|
|
38
|
+
if (o.glare && fine.matches) {
|
|
39
|
+
glare = el.querySelector(':scope > .tilt__glare');
|
|
40
|
+
if (!glare) { glare = document.createElement('span'); glare.className = 'tilt__glare'; glare.setAttribute('aria-hidden', 'true'); glare.dataset.made = ''; el.append(glare); }
|
|
41
|
+
}
|
|
42
|
+
g.set(el, { transformPerspective: o.perspective });
|
|
43
|
+
layers.forEach((l) => g.set(l, { z: +l.dataset.depth || 0 }));
|
|
44
|
+
// the glare floats just above the highest layer (depths add up when layers sit inside layers)
|
|
45
|
+
const top = layers.reduce((m, l) => { let d = 0; for (let n = l; n && n !== el; n = n.parentElement) d += +n.dataset.depth || 0; return Math.max(m, d); }, 0);
|
|
46
|
+
if (glare) g.set(glare, { z: top + 1, scale: (o.perspective - top - 1) / o.perspective }); // shrunk so it still covers just the card
|
|
47
|
+
|
|
48
|
+
const apply = () => {
|
|
49
|
+
g.set(el, { rotationX: s.rx, rotationY: s.ry, scale: s.sc });
|
|
50
|
+
if (glare) { glare.style.setProperty('--gx', `${s.gx}%`); glare.style.setProperty('--gy', `${s.gy}%`); glare.style.opacity = s.ga; }
|
|
51
|
+
if (o.parallax) layers.forEach((l) => {
|
|
52
|
+
const d = (+l.dataset.depth || 0) * o.parallax;
|
|
53
|
+
g.set(l, { x: (-s.ry / o.max) * d, y: (s.rx / o.max) * d });
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
const move = (e) => {
|
|
57
|
+
if (e.pointerType === 'touch' || !fine.matches || RM()) return;
|
|
58
|
+
const r = el.getBoundingClientRect();
|
|
59
|
+
const px = (e.clientX - r.left) / r.width, py = (e.clientY - r.top) / r.height; // 0 … 1
|
|
60
|
+
g.to(s, { rx: (0.5 - py) * 2 * o.max, ry: (px - 0.5) * 2 * o.max, sc: o.scale, gx: px * 100, gy: py * 100, ga: 1,
|
|
61
|
+
duration: o.follow, ease: 'power3.out', overwrite: true, onUpdate: apply });
|
|
62
|
+
};
|
|
63
|
+
const back = () => {
|
|
64
|
+
if (!s.ga && !s.rx && !s.ry && s.sc === 1) return;
|
|
65
|
+
g.to(s, { rx: 0, ry: 0, sc: 1, duration: RM() ? 0 : o.spring, ease: 'elastic.out(1, 0.5)', overwrite: true, onUpdate: apply });
|
|
66
|
+
g.to(s, { ga: 0, duration: RM() ? 0 : 0.4, ease: 'power2.out', onUpdate: apply });
|
|
67
|
+
};
|
|
68
|
+
el.addEventListener('pointermove', move);
|
|
69
|
+
el.addEventListener('pointerleave', back);
|
|
70
|
+
el.addEventListener('pointercancel', back);
|
|
71
|
+
el.__tilt = { back };
|
|
72
|
+
offs.push(() => {
|
|
73
|
+
el.removeEventListener('pointermove', move);
|
|
74
|
+
el.removeEventListener('pointerleave', back);
|
|
75
|
+
el.removeEventListener('pointercancel', back);
|
|
76
|
+
g.killTweensOf(s);
|
|
77
|
+
g.set(el, { clearProps: 'transform' });
|
|
78
|
+
g.set(layers, { clearProps: 'transform' });
|
|
79
|
+
if (glare) g.set(glare, { clearProps: 'transform,opacity' });
|
|
80
|
+
if (glare?.dataset.made !== undefined) glare.remove();
|
|
81
|
+
el.classList.remove('tilt');
|
|
82
|
+
delete el.__tilt;
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
els,
|
|
88
|
+
/** Spring every card back to flat. */
|
|
89
|
+
reset: () => els.forEach((el) => el.__tilt?.back()),
|
|
90
|
+
destroy: () => { offs.forEach((f) => f()); offs.length = 0; },
|
|
91
|
+
};
|
|
92
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/* Typewriter: text appears letter by letter behind a caret, as if someone were typing it. The
|
|
2
|
+
gaps between letters vary a little (and grow after a comma or full stop) so it feels like a
|
|
3
|
+
person, not a machine. With several phrases it types one, waits, deletes it (backspacing letter
|
|
4
|
+
by letter, or selecting it all and clearing it at once) and types the next; it can loop forever
|
|
5
|
+
or stop on the last phrase. The caret blinks while it waits and stays solid while it types.
|
|
6
|
+
Letters are counted the way people see them, so Thai vowels and tone marks arrive together with
|
|
7
|
+
their consonant instead of on their own. Screen readers never hear single letters: they get the
|
|
8
|
+
whole text (every phrase, as a list) from a hidden copy. It pauses while it is off screen. With
|
|
9
|
+
reduced motion each phrase appears whole and the next one replaces it without typing. */
|
|
10
|
+
import { g, RM, $$ } from './core.js';
|
|
11
|
+
|
|
12
|
+
const state = new WeakMap();
|
|
13
|
+
const CARETS = new Set(['bar', 'block', 'underscore']);
|
|
14
|
+
|
|
15
|
+
/** Split text into the letters people see (a Thai consonant keeps its vowel and tone marks). */
|
|
16
|
+
const graphemes = (s, locale) => {
|
|
17
|
+
if (typeof Intl !== 'undefined' && Intl.Segmenter) return [...new Intl.Segmenter(locale, { granularity: 'grapheme' }).segment(s)].map((x) => x.segment);
|
|
18
|
+
return [...s];
|
|
19
|
+
};
|
|
20
|
+
/** "a, b and c" in the page language (what screen readers hear for several phrases). */
|
|
21
|
+
export const listText = (items, locale) => {
|
|
22
|
+
if (items.length < 2) return items[0] ?? '';
|
|
23
|
+
try { return new Intl.ListFormat(locale, { type: 'conjunction' }).format(items); } catch { return items.join(', '); }
|
|
24
|
+
};
|
|
25
|
+
const langOf = (el, locale) => locale || el.closest('[lang]')?.lang || undefined;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* const tw = typewriter(el, {
|
|
29
|
+
* phrases: ['websites', 'mobile apps', 'online shops'], // default: data-words="a|b|c", or the element's own text
|
|
30
|
+
* speed: 0.06, // seconds per letter while typing
|
|
31
|
+
* jitter: 0.6, // 0 = even rhythm, 1 = very uneven (more human)
|
|
32
|
+
* eraseSpeed: 0.03, // seconds per letter while deleting
|
|
33
|
+
* erase: 'backspace' | 'select', // delete letter by letter, or select the phrase and clear it at once
|
|
34
|
+
* hold: 1.6, // seconds a finished phrase stays before it is deleted
|
|
35
|
+
* gap: 0.35, // seconds of empty caret before the next phrase
|
|
36
|
+
* delay: 0.3, // seconds before the first letter
|
|
37
|
+
* loop: true, // false: stop on the last phrase (it stays)
|
|
38
|
+
* caret: 'bar' | 'block' | 'underscore' | false,
|
|
39
|
+
* whenVisible: true, // pause while the element is off screen
|
|
40
|
+
* locale: 'th', // for letter splitting and the screen-reader list (default: the page's lang)
|
|
41
|
+
* label: '…', // what screen readers hear instead of the phrase list
|
|
42
|
+
* onPhrase(i, text), // a phrase has been typed out in full
|
|
43
|
+
* })
|
|
44
|
+
* <p>We design <span id="what"></span></p> → typewriter('#what', { phrases: [...] })
|
|
45
|
+
* tw.play() · tw.pause() · tw.restart() · tw.set(phrases) · tw.destroy() · tw.phrase (index)
|
|
46
|
+
* Speed follows speed() from this package (it runs on the GSAP clock).
|
|
47
|
+
*/
|
|
48
|
+
export const typewriter = (el, opts = {}) => {
|
|
49
|
+
el = $$(el)[0];
|
|
50
|
+
if (!el) return null;
|
|
51
|
+
state.get(el)?.destroy();
|
|
52
|
+
const o = { speed: 0.06, jitter: 0.6, eraseSpeed: 0.03, erase: 'backspace', hold: 1.6, gap: 0.35, delay: 0.3, loop: true, caret: 'bar', whenVisible: true, locale: undefined, label: undefined, onPhrase: null, ...opts };
|
|
53
|
+
const original = el.innerHTML;
|
|
54
|
+
const lang = langOf(el, o.locale);
|
|
55
|
+
const clean = (list) => list.map((t) => String(t).trim()).filter(Boolean);
|
|
56
|
+
let phrases = clean(o.phrases ?? (el.dataset.words ? el.dataset.words.split('|') : [el.textContent]));
|
|
57
|
+
if (!phrases.length) phrases = [''];
|
|
58
|
+
|
|
59
|
+
el.classList.add('tw', `tw--${CARETS.has(o.caret) ? o.caret : 'none'}`);
|
|
60
|
+
el.innerHTML = '<span class="tw__sr"></span><span class="tw__vis" aria-hidden="true"><span class="tw__text"></span><i class="tw__caret"></i></span>';
|
|
61
|
+
const sr = el.firstChild, text = el.querySelector('.tw__text');
|
|
62
|
+
const label = () => { sr.textContent = o.label ?? listText(phrases, lang); };
|
|
63
|
+
label();
|
|
64
|
+
|
|
65
|
+
let p = 0, chars = graphemes(phrases[0], lang), n = 0, mode = 'type', call = null, visible = !o.whenVisible, paused = false, dead = false;
|
|
66
|
+
const typing = (on) => el.classList.toggle('is-typing', on);
|
|
67
|
+
const show = () => { text.textContent = chars.slice(0, n).join(''); };
|
|
68
|
+
const later = (sec, fn) => { call?.kill(); call = g.delayedCall(Math.max(0, sec), fn); if (!visible || paused) call.pause(); };
|
|
69
|
+
const nextPhrase = () => { p = (p + 1) % phrases.length; chars = graphemes(phrases[p], lang); n = 0; };
|
|
70
|
+
const done = () => o.onPhrase?.(p, phrases[p]);
|
|
71
|
+
const last = () => !o.loop && p === phrases.length - 1;
|
|
72
|
+
|
|
73
|
+
const step = () => {
|
|
74
|
+
if (dead) return;
|
|
75
|
+
if (RM()) { // whole phrases, no typing
|
|
76
|
+
typing(false); el.classList.add('tw--still'); text.classList.remove('is-sel');
|
|
77
|
+
if (mode !== 'type') nextPhrase();
|
|
78
|
+
n = chars.length; show(); done();
|
|
79
|
+
mode = 'hold';
|
|
80
|
+
if (phrases.length > 1 && !last()) later(o.hold + o.gap, step);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
el.classList.remove('tw--still');
|
|
84
|
+
if (mode === 'type') {
|
|
85
|
+
typing(true);
|
|
86
|
+
n++; show();
|
|
87
|
+
if (n >= chars.length) {
|
|
88
|
+
typing(false); done();
|
|
89
|
+
if (last()) { mode = 'end'; return; }
|
|
90
|
+
mode = 'erase'; later(o.hold, step); return;
|
|
91
|
+
}
|
|
92
|
+
const ch = chars[n - 1], base = o.speed * (1 - o.jitter / 2 + Math.random() * o.jitter);
|
|
93
|
+
later(base * (/[,;:]/.test(ch) ? 4 : /[.!?…]/.test(ch) ? 6 : ch === ' ' ? 1.4 : 1), step);
|
|
94
|
+
} else if (mode === 'erase') {
|
|
95
|
+
if (o.erase === 'select') {
|
|
96
|
+
if (!text.classList.contains('is-sel')) { text.classList.add('is-sel'); later(0.45, step); return; }
|
|
97
|
+
text.classList.remove('is-sel'); n = 0; show();
|
|
98
|
+
} else { typing(true); n--; show(); }
|
|
99
|
+
if (n > 0) { later(o.eraseSpeed, step); return; }
|
|
100
|
+
typing(false);
|
|
101
|
+
nextPhrase(); mode = 'type'; later(o.gap, step);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const io = o.whenVisible && typeof IntersectionObserver === 'function' ? new IntersectionObserver(([e]) => {
|
|
106
|
+
visible = e.isIntersecting;
|
|
107
|
+
if (visible && !paused) call?.resume(); else call?.pause();
|
|
108
|
+
}) : null;
|
|
109
|
+
if (io) io.observe(el); else visible = true;
|
|
110
|
+
|
|
111
|
+
const start = () => { call?.kill(); p = 0; chars = graphemes(phrases[0], lang); n = 0; mode = 'type'; text.classList.remove('is-sel'); show(); later(RM() ? 0 : o.delay, step); };
|
|
112
|
+
start();
|
|
113
|
+
|
|
114
|
+
const h = {
|
|
115
|
+
el,
|
|
116
|
+
get phrase() { return p; },
|
|
117
|
+
play() { paused = false; if (visible) call?.resume(); return h; },
|
|
118
|
+
pause() { paused = true; call?.pause(); return h; },
|
|
119
|
+
restart() { start(); return h; },
|
|
120
|
+
/** New phrases: the current one is deleted (as usual) and the first new one is typed. */
|
|
121
|
+
set(list) {
|
|
122
|
+
phrases = clean(list); if (!phrases.length) phrases = [''];
|
|
123
|
+
label();
|
|
124
|
+
p = phrases.length - 1; // so the next phrase is the first new one
|
|
125
|
+
if (RM() || n === 0) { nextPhrase(); mode = 'type'; later(0, step); }
|
|
126
|
+
else { mode = 'erase'; later(0.2, step); }
|
|
127
|
+
return h;
|
|
128
|
+
},
|
|
129
|
+
destroy() {
|
|
130
|
+
dead = true; call?.kill(); io?.disconnect();
|
|
131
|
+
el.classList.remove('tw', 'tw--bar', 'tw--block', 'tw--underscore', 'tw--none', 'tw--still', 'is-typing');
|
|
132
|
+
el.innerHTML = original; state.delete(el);
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
state.set(el, h);
|
|
136
|
+
return h;
|
|
137
|
+
};
|