@enigmax/primitives 0.1.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/LICENSE +202 -0
- package/README.md +111 -0
- package/dist/chunk-5Q5TAW2N.js +267 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +1 -0
- package/dist/react/index.d.ts +45 -0
- package/dist/react/index.js +55 -0
- package/package.json +71 -0
- package/registry.json +46 -0
- package/src/core/marquee.ts +400 -0
- package/src/index.ts +1 -0
- package/src/react/index.ts +2 -0
- package/src/react/use-marquee.ts +105 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# @enigmax/primitives
|
|
2
|
+
|
|
3
|
+
Headless interaction primitives: the behaviour, the timing and the accessibility, with no styles of their own.
|
|
4
|
+
|
|
5
|
+
**The marquee takes a speed in pixels per second, never a duration.** A lap of a looping row is as long as its content, so a duration makes the speed `content / duration` and the row runs faster every time an item is added to it. Two testimonial rows split from an odd-length array, same `--duration: 20s`, measured 53.2 px/s and 66.5 px/s and were meant to look like a pair. A provider rail on a fixed 50s duration measured 45, 67 and 87 px/s at 10, 15 and 20 items, because nobody edits the duration when they add a provider. Take a speed and derive the duration: the same rail then measured 67.0 px/s at all three counts.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @enigmax/primitives
|
|
11
|
+
# or, to copy the source in and own it:
|
|
12
|
+
enigma add marquee --copy
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Use it
|
|
16
|
+
|
|
17
|
+
React:
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { useMarquee } from "@enigmax/primitives/react";
|
|
21
|
+
|
|
22
|
+
function LogoWall({ logos }) {
|
|
23
|
+
const { laneRef, trackRef, copies, dragging } = useMarquee({ speed: 80, hoverScale: 0.15 });
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div ref={laneRef} style={{ cursor: dragging ? "grabbing" : "grab" }}>
|
|
27
|
+
<div ref={trackRef} style={{ display: "flex" }}>
|
|
28
|
+
{Array.from({ length: copies }, (_, copy) => (
|
|
29
|
+
<div key={copy} aria-hidden={copy > 0} style={{ display: "flex", gap: 32 }}>
|
|
30
|
+
{logos.map(logo => <a key={logo.id} href={logo.href}>{logo.name}</a>)}
|
|
31
|
+
</div>
|
|
32
|
+
))}
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Vanilla, Astro, or anything else that hands you two elements:
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import { createMarquee } from "@enigmax/primitives";
|
|
43
|
+
|
|
44
|
+
const marquee = createMarquee(document.querySelector("[data-lane]"), document.querySelector("[data-track]"), {
|
|
45
|
+
speed: 80,
|
|
46
|
+
hoverScale: 0.15
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The core clones the track's first child to fill the lane. In React the hook reports a `copies` count instead, so React keeps ownership of the DOM.
|
|
51
|
+
|
|
52
|
+
## Options
|
|
53
|
+
|
|
54
|
+
| Option | Default | Meaning |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| `speed` | `60` | Pixels per second. Not a duration. |
|
|
57
|
+
| `reverse` | `false` | Scroll towards the start. |
|
|
58
|
+
| `vertical` | `false` | Scroll on the Y axis. |
|
|
59
|
+
| `draggable` | `true` | Grab and throw the row. |
|
|
60
|
+
| `hoverScale` | `1` | Speed multiplier while a **mouse** rests on the row. `0` pauses. |
|
|
61
|
+
| `decay` | `0.12` | Fraction of the remaining velocity gap left after one second. `0.35` is draggy. |
|
|
62
|
+
| `manageStyles` | `true` | Apply the styles the behaviour needs. Never theme styles. |
|
|
63
|
+
|
|
64
|
+
## Why the obvious implementation is wrong
|
|
65
|
+
|
|
66
|
+
Three of these cost a production bug each. They are the reason this package exists rather than a snippet.
|
|
67
|
+
|
|
68
|
+
**`setPointerCapture` silently breaks every link in the row.** A pointer capture retargets the compatibility mouse events too, so the `click` of a plain press arrives on the lane instead of on the card that was pressed, and the card's link never opens. A nine-logo carousel quietly stops being nine links and nothing in the source looks wrong. This package binds `pointermove` / `pointerup` / `pointercancel` to `window`, which is all the capture was ever for - a mousedown already captures the mouse at the OS level, so a release outside the browser window still arrives.
|
|
69
|
+
|
|
70
|
+
**`touch-action` decides whether a phone can scroll your page.** Without it the browser owns the horizontal swipe and the drag handlers never see it. With `none`, a visitor can no longer scroll the page by starting the gesture on the row, which on a phone is most of the width of the screen. The lane gets `pan-y` (or `pan-x` when vertical).
|
|
71
|
+
|
|
72
|
+
**A hover effect that ignores `pointerType` sticks forever on touch.** A tap fires `pointerenter` too, and on touch nothing ever fires the `pointerleave` that undoes it, so a "slow down on hover" leaves the row at 15% speed for good after the first tap. Every hover path here is gated to `pointerType === "mouse"`.
|
|
73
|
+
|
|
74
|
+
The rest, in short: the lap is measured from the step between two copies rather than computed from item widths; the transform is driven by one `requestAnimationFrame` loop rather than CSS keyframes, which restart on every re-author; one integrator carries cruise, hover and momentum so none of them can arrive as a cut; a drag past ~6px cancels the click in the capture phase; a right click never starts a drag; a pointer held still for ~90ms before release throws nothing; and reduced motion drops the autoplay while keeping the drag, because a drag is an answer to the pointer and not motion of the page's own accord.
|
|
75
|
+
|
|
76
|
+
## Styling
|
|
77
|
+
|
|
78
|
+
The primitive ships no visual styles. It sets only what the behaviour requires (`overflow`, `touch-action`, `user-select`, `will-change`, `transform`) and exposes state as data attributes for a theme to hook:
|
|
79
|
+
|
|
80
|
+
```css
|
|
81
|
+
[data-enigma-marquee] { cursor: grab; }
|
|
82
|
+
[data-enigma-marquee][data-dragging] { cursor: grabbing; }
|
|
83
|
+
[data-enigma-marquee][data-hovering] { --logo-opacity: 1; }
|
|
84
|
+
[data-enigma-marquee][data-reduced-motion="true"] { /* autoplay is off */ }
|
|
85
|
+
[data-enigma-marquee-track] { display: flex; }
|
|
86
|
+
[data-enigma-marquee-copy] { display: flex; gap: 2rem; }
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Tests
|
|
90
|
+
|
|
91
|
+
The suite drives a real browser and samples `new DOMMatrixReadOnly(getComputedStyle(track).transform).m41` on every animation frame, rather than asking the engine what it believes.
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
npm run test:install # once, fetches Chromium
|
|
95
|
+
npm test
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
| Check | Pass condition |
|
|
99
|
+
|---|---|
|
|
100
|
+
| Same speed at 2, 9, 11, 20 items | measured px/s equal within 1% |
|
|
101
|
+
| Drag follows the pointer | a 200px swipe moves the row 200.0px |
|
|
102
|
+
| Release | decays monotonically onto cruise, no sign flip |
|
|
103
|
+
| Four viewport resizes | no frame moves more than one frame of cruising |
|
|
104
|
+
| Drag ending on a link | 0 navigations |
|
|
105
|
+
| Plain click and tap on a link | 1 navigation each |
|
|
106
|
+
| Tap on touch | row does not stick at the hover speed |
|
|
107
|
+
| Vertical swipe on a phone | the page still scrolls |
|
|
108
|
+
| `prefers-reduced-motion` | 0.00px idle drift, drag still works |
|
|
109
|
+
| Held still, then release | no fling |
|
|
110
|
+
|
|
111
|
+
Two ways to fool yourself, both of which produced a false alarm while this was being written: do not measure a resize as before/after, because `setViewportSize` takes several hundred milliseconds during which the row legitimately keeps moving - sample every frame and look for a single anomalous one. And discard the first few samples, because the first rAF delta after installing the sampler is near zero and produces a meaningless px/s spike.
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// src/core/marquee.ts
|
|
2
|
+
var DEFAULT_DECAY = 0.12;
|
|
3
|
+
var DRAG_THRESHOLD = 6;
|
|
4
|
+
var STILL_MS = 90;
|
|
5
|
+
var MAX_DT = 0.05;
|
|
6
|
+
var VELOCITY_SMOOTHING = 0.6;
|
|
7
|
+
var IDLE_EPSILON = 0.01;
|
|
8
|
+
var DEFAULTS = {
|
|
9
|
+
speed: 60,
|
|
10
|
+
reverse: false,
|
|
11
|
+
vertical: false,
|
|
12
|
+
draggable: true,
|
|
13
|
+
hoverScale: 1,
|
|
14
|
+
decay: DEFAULT_DECAY,
|
|
15
|
+
manageStyles: true,
|
|
16
|
+
copies: "clone"
|
|
17
|
+
};
|
|
18
|
+
function createMarquee(lane, track, options = {}) {
|
|
19
|
+
let opts = { ...DEFAULTS, ...options };
|
|
20
|
+
let period = 0;
|
|
21
|
+
let copyCount = Math.max(2, track.children.length);
|
|
22
|
+
let offset = 0;
|
|
23
|
+
let velocity = 0;
|
|
24
|
+
let running = false;
|
|
25
|
+
let paused = false;
|
|
26
|
+
let destroyed = false;
|
|
27
|
+
let frame = 0;
|
|
28
|
+
let lastTime = 0;
|
|
29
|
+
let dragging = false;
|
|
30
|
+
let pointerId = null;
|
|
31
|
+
let lastPointer = 0;
|
|
32
|
+
let startPointer = 0;
|
|
33
|
+
let lastMoveTime = 0;
|
|
34
|
+
let maxTravel = 0;
|
|
35
|
+
let hovering = false;
|
|
36
|
+
const motionQuery = typeof window.matchMedia === "function" ? window.matchMedia("(prefers-reduced-motion: reduce)") : null;
|
|
37
|
+
let reducedMotion = motionQuery?.matches ?? false;
|
|
38
|
+
function target() {
|
|
39
|
+
if (reducedMotion || paused) return 0;
|
|
40
|
+
const cruise = opts.reverse ? -opts.speed : opts.speed;
|
|
41
|
+
return hovering ? cruise * opts.hoverScale : cruise;
|
|
42
|
+
}
|
|
43
|
+
function axisSize(element) {
|
|
44
|
+
return opts.vertical ? element.offsetHeight : element.offsetWidth;
|
|
45
|
+
}
|
|
46
|
+
function axisStart(element) {
|
|
47
|
+
return opts.vertical ? element.offsetTop : element.offsetLeft;
|
|
48
|
+
}
|
|
49
|
+
function applyStyles() {
|
|
50
|
+
if (!opts.manageStyles) return;
|
|
51
|
+
lane.style.overflow = "hidden";
|
|
52
|
+
lane.style.touchAction = opts.vertical ? "pan-x" : "pan-y";
|
|
53
|
+
lane.style.userSelect = "none";
|
|
54
|
+
lane.style.webkitUserSelect = "none";
|
|
55
|
+
track.style.willChange = "transform";
|
|
56
|
+
}
|
|
57
|
+
function render() {
|
|
58
|
+
const x = opts.vertical ? 0 : -offset;
|
|
59
|
+
const y = opts.vertical ? -offset : 0;
|
|
60
|
+
track.style.transform = `translate3d(${x}px, ${y}px, 0)`;
|
|
61
|
+
}
|
|
62
|
+
function readPeriod() {
|
|
63
|
+
const first = track.children[0];
|
|
64
|
+
const second = track.children[1];
|
|
65
|
+
if (!first) return 0;
|
|
66
|
+
if (second) {
|
|
67
|
+
const step = axisStart(second) - axisStart(first);
|
|
68
|
+
if (step > 0) return step;
|
|
69
|
+
}
|
|
70
|
+
return axisSize(first);
|
|
71
|
+
}
|
|
72
|
+
function requiredCopies(measured) {
|
|
73
|
+
if (measured <= 0) return 2;
|
|
74
|
+
return Math.max(2, Math.ceil(axisSize(lane) / measured) + 1);
|
|
75
|
+
}
|
|
76
|
+
function syncClones(count) {
|
|
77
|
+
const source = track.children[0];
|
|
78
|
+
if (!source) return;
|
|
79
|
+
while (track.children.length > count) track.removeChild(track.lastElementChild);
|
|
80
|
+
while (track.children.length < count) {
|
|
81
|
+
const clone = source.cloneNode(true);
|
|
82
|
+
clone.setAttribute("aria-hidden", "true");
|
|
83
|
+
clone.dataset.enigmaMarqueeCopy = String(track.children.length);
|
|
84
|
+
track.appendChild(clone);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function measure() {
|
|
88
|
+
if (destroyed) return;
|
|
89
|
+
const measured = readPeriod();
|
|
90
|
+
const needed = requiredCopies(measured);
|
|
91
|
+
const changed = measured !== period || needed !== copyCount;
|
|
92
|
+
period = measured;
|
|
93
|
+
if (needed !== copyCount) {
|
|
94
|
+
copyCount = needed;
|
|
95
|
+
if (opts.copies === "clone") syncClones(needed);
|
|
96
|
+
opts.onCopyCountChange?.(needed);
|
|
97
|
+
}
|
|
98
|
+
if (period > 0) offset = wrap(offset);
|
|
99
|
+
render();
|
|
100
|
+
if (changed) opts.onMeasure?.(period, copyCount);
|
|
101
|
+
start();
|
|
102
|
+
}
|
|
103
|
+
function wrap(value) {
|
|
104
|
+
if (period <= 0) return value;
|
|
105
|
+
return (value % period + period) % period;
|
|
106
|
+
}
|
|
107
|
+
function tick(now) {
|
|
108
|
+
frame = 0;
|
|
109
|
+
if (destroyed) return;
|
|
110
|
+
const dt = Math.min((now - lastTime) / 1e3, MAX_DT);
|
|
111
|
+
lastTime = now;
|
|
112
|
+
if (!dragging) {
|
|
113
|
+
velocity += (target() - velocity) * (1 - Math.pow(opts.decay, dt));
|
|
114
|
+
offset = wrap(offset + velocity * dt);
|
|
115
|
+
}
|
|
116
|
+
render();
|
|
117
|
+
const idle = !dragging && Math.abs(velocity) < IDLE_EPSILON && Math.abs(target()) < IDLE_EPSILON;
|
|
118
|
+
if (idle) {
|
|
119
|
+
velocity = 0;
|
|
120
|
+
running = false;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
frame = requestAnimationFrame(tick);
|
|
124
|
+
}
|
|
125
|
+
function start() {
|
|
126
|
+
if (destroyed || running) return;
|
|
127
|
+
running = true;
|
|
128
|
+
lastTime = performance.now();
|
|
129
|
+
frame = requestAnimationFrame(tick);
|
|
130
|
+
}
|
|
131
|
+
function pointerDelta(event) {
|
|
132
|
+
return opts.vertical ? event.clientY : event.clientX;
|
|
133
|
+
}
|
|
134
|
+
function onPointerDown(event) {
|
|
135
|
+
if (!opts.draggable || event.button > 0 || pointerId !== null) return;
|
|
136
|
+
pointerId = event.pointerId;
|
|
137
|
+
dragging = true;
|
|
138
|
+
lastPointer = pointerDelta(event);
|
|
139
|
+
startPointer = lastPointer;
|
|
140
|
+
lastMoveTime = event.timeStamp;
|
|
141
|
+
maxTravel = 0;
|
|
142
|
+
lane.dataset.dragging = "true";
|
|
143
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
144
|
+
window.addEventListener("pointerup", onPointerUp);
|
|
145
|
+
window.addEventListener("pointercancel", onPointerUp);
|
|
146
|
+
start();
|
|
147
|
+
}
|
|
148
|
+
function onPointerMove(event) {
|
|
149
|
+
if (event.pointerId !== pointerId) return;
|
|
150
|
+
const position = pointerDelta(event);
|
|
151
|
+
const delta = position - lastPointer;
|
|
152
|
+
lastPointer = position;
|
|
153
|
+
const dt = Math.max((event.timeStamp - lastMoveTime) / 1e3, 1e-3);
|
|
154
|
+
lastMoveTime = event.timeStamp;
|
|
155
|
+
maxTravel = Math.max(maxTravel, Math.abs(position - startPointer));
|
|
156
|
+
offset = wrap(offset - delta);
|
|
157
|
+
const instant = -delta / dt;
|
|
158
|
+
velocity = velocity * VELOCITY_SMOOTHING + instant * (1 - VELOCITY_SMOOTHING);
|
|
159
|
+
}
|
|
160
|
+
function onPointerUp(event) {
|
|
161
|
+
if (event.pointerId !== pointerId) return;
|
|
162
|
+
pointerId = null;
|
|
163
|
+
dragging = false;
|
|
164
|
+
delete lane.dataset.dragging;
|
|
165
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
166
|
+
window.removeEventListener("pointerup", onPointerUp);
|
|
167
|
+
window.removeEventListener("pointercancel", onPointerUp);
|
|
168
|
+
if (event.timeStamp - lastMoveTime > STILL_MS) velocity = 0;
|
|
169
|
+
start();
|
|
170
|
+
}
|
|
171
|
+
function onClickCapture(event) {
|
|
172
|
+
if (maxTravel <= DRAG_THRESHOLD) return;
|
|
173
|
+
event.preventDefault();
|
|
174
|
+
event.stopPropagation();
|
|
175
|
+
}
|
|
176
|
+
function onClick(event) {
|
|
177
|
+
if (maxTravel > DRAG_THRESHOLD) return;
|
|
178
|
+
opts.onItemClick?.(event);
|
|
179
|
+
}
|
|
180
|
+
function onPointerEnter(event) {
|
|
181
|
+
if (event.pointerType !== "mouse") return;
|
|
182
|
+
hovering = true;
|
|
183
|
+
lane.dataset.hovering = "true";
|
|
184
|
+
start();
|
|
185
|
+
}
|
|
186
|
+
function onPointerLeave(event) {
|
|
187
|
+
if (event.pointerType !== "mouse") return;
|
|
188
|
+
hovering = false;
|
|
189
|
+
delete lane.dataset.hovering;
|
|
190
|
+
start();
|
|
191
|
+
}
|
|
192
|
+
function onMotionChange(event) {
|
|
193
|
+
reducedMotion = event.matches;
|
|
194
|
+
lane.dataset.reducedMotion = String(reducedMotion);
|
|
195
|
+
start();
|
|
196
|
+
}
|
|
197
|
+
const resizeObserver = typeof ResizeObserver === "function" ? new ResizeObserver(() => measure()) : null;
|
|
198
|
+
function init() {
|
|
199
|
+
applyStyles();
|
|
200
|
+
lane.dataset.enigmaMarquee = "";
|
|
201
|
+
lane.dataset.reducedMotion = String(reducedMotion);
|
|
202
|
+
track.dataset.enigmaMarqueeTrack = "";
|
|
203
|
+
for (const child of Array.from(track.children)) {
|
|
204
|
+
child.dataset.enigmaMarqueeCopy ??= "0";
|
|
205
|
+
}
|
|
206
|
+
lane.addEventListener("pointerdown", onPointerDown);
|
|
207
|
+
lane.addEventListener("pointerenter", onPointerEnter);
|
|
208
|
+
lane.addEventListener("pointerleave", onPointerLeave);
|
|
209
|
+
lane.addEventListener("click", onClickCapture, true);
|
|
210
|
+
lane.addEventListener("click", onClick);
|
|
211
|
+
motionQuery?.addEventListener("change", onMotionChange);
|
|
212
|
+
resizeObserver?.observe(lane);
|
|
213
|
+
const first = track.children[0];
|
|
214
|
+
if (first) resizeObserver?.observe(first);
|
|
215
|
+
measure();
|
|
216
|
+
void document.fonts?.ready.then(() => measure());
|
|
217
|
+
}
|
|
218
|
+
init();
|
|
219
|
+
return {
|
|
220
|
+
get offset() {
|
|
221
|
+
return offset;
|
|
222
|
+
},
|
|
223
|
+
get period() {
|
|
224
|
+
return period;
|
|
225
|
+
},
|
|
226
|
+
get copyCount() {
|
|
227
|
+
return copyCount;
|
|
228
|
+
},
|
|
229
|
+
get dragging() {
|
|
230
|
+
return dragging;
|
|
231
|
+
},
|
|
232
|
+
get reducedMotion() {
|
|
233
|
+
return reducedMotion;
|
|
234
|
+
},
|
|
235
|
+
update(next) {
|
|
236
|
+
const styleKeys = next.vertical !== void 0 || next.manageStyles !== void 0;
|
|
237
|
+
opts = { ...opts, ...next };
|
|
238
|
+
if (styleKeys) applyStyles();
|
|
239
|
+
measure();
|
|
240
|
+
},
|
|
241
|
+
measure,
|
|
242
|
+
pause() {
|
|
243
|
+
paused = true;
|
|
244
|
+
start();
|
|
245
|
+
},
|
|
246
|
+
resume() {
|
|
247
|
+
paused = false;
|
|
248
|
+
start();
|
|
249
|
+
},
|
|
250
|
+
destroy() {
|
|
251
|
+
destroyed = true;
|
|
252
|
+
if (frame) cancelAnimationFrame(frame);
|
|
253
|
+
resizeObserver?.disconnect();
|
|
254
|
+
motionQuery?.removeEventListener("change", onMotionChange);
|
|
255
|
+
lane.removeEventListener("pointerdown", onPointerDown);
|
|
256
|
+
lane.removeEventListener("pointerenter", onPointerEnter);
|
|
257
|
+
lane.removeEventListener("pointerleave", onPointerLeave);
|
|
258
|
+
lane.removeEventListener("click", onClickCapture, true);
|
|
259
|
+
lane.removeEventListener("click", onClick);
|
|
260
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
261
|
+
window.removeEventListener("pointerup", onPointerUp);
|
|
262
|
+
window.removeEventListener("pointercancel", onPointerUp);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export { createMarquee };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Draggable infinite marquee: the behaviour engine, with no styling of its own.
|
|
3
|
+
*
|
|
4
|
+
* The contract is a SPEED in pixels per second, never a duration. A lap of a
|
|
5
|
+
* looping row is as long as its content, so a duration makes the row run faster
|
|
6
|
+
* every time an item is added to it. See the package README.
|
|
7
|
+
*
|
|
8
|
+
* Every rule encoded here was measured in a browser, not reasoned about. The
|
|
9
|
+
* comments marked "non-negotiable" name the bug that the obvious implementation
|
|
10
|
+
* shipped with.
|
|
11
|
+
*/
|
|
12
|
+
interface MarqueeOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Pixels per second. NOT a duration: a duration ties the speed to the item
|
|
15
|
+
* count and the row silently accelerates as content grows.
|
|
16
|
+
*/
|
|
17
|
+
speed?: number;
|
|
18
|
+
/** Scroll towards the start instead of the end. */
|
|
19
|
+
reverse?: boolean;
|
|
20
|
+
/** Scroll on the Y axis instead of the X axis. */
|
|
21
|
+
vertical?: boolean;
|
|
22
|
+
/** Allow grabbing and throwing the row. */
|
|
23
|
+
draggable?: boolean;
|
|
24
|
+
/** Speed multiplier while a MOUSE rests on the row. 0 pauses it. */
|
|
25
|
+
hoverScale?: number;
|
|
26
|
+
/** Fraction of the remaining velocity gap left after one second. */
|
|
27
|
+
decay?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Apply the styles the behaviour needs (overflow, touch-action, user-select,
|
|
30
|
+
* will-change, transform). Never theme styles. Turn off to own them yourself.
|
|
31
|
+
*/
|
|
32
|
+
manageStyles?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* How the repeated copies get into the DOM.
|
|
35
|
+
* - "clone": the engine clones the first child of the track (vanilla, Astro).
|
|
36
|
+
* - "external": the consumer renders them and is told the count (React, Vue).
|
|
37
|
+
*/
|
|
38
|
+
copies?: "clone" | "external";
|
|
39
|
+
/** Called when the required copy count changes. Required for "external". */
|
|
40
|
+
onCopyCountChange?: (count: number) => void;
|
|
41
|
+
/** Called on a click that was not the end of a drag. */
|
|
42
|
+
onItemClick?: (event: MouseEvent) => void;
|
|
43
|
+
/** Called whenever the measured lap period changes. */
|
|
44
|
+
onMeasure?: (period: number, copyCount: number) => void;
|
|
45
|
+
}
|
|
46
|
+
interface MarqueeInstance {
|
|
47
|
+
/** Current offset in px, normalized to [0, period). */
|
|
48
|
+
readonly offset: number;
|
|
49
|
+
/** Measured distance between two copies of the content, in px. */
|
|
50
|
+
readonly period: number;
|
|
51
|
+
/** Copies currently required to cover the lane plus one whole period. */
|
|
52
|
+
readonly copyCount: number;
|
|
53
|
+
/** True between pointerdown and pointerup while the row is being grabbed. */
|
|
54
|
+
readonly dragging: boolean;
|
|
55
|
+
/** True while the OS asks for reduced motion. Autoplay stops, drag does not. */
|
|
56
|
+
readonly reducedMotion: boolean;
|
|
57
|
+
update(options: Partial<MarqueeOptions>): void;
|
|
58
|
+
/** Re-read the lap from the DOM. Called for you on resize and on font load. */
|
|
59
|
+
measure(): void;
|
|
60
|
+
pause(): void;
|
|
61
|
+
resume(): void;
|
|
62
|
+
destroy(): void;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Wire a lane and its track into a draggable infinite marquee.
|
|
66
|
+
*
|
|
67
|
+
* @param lane The clipping viewport. Receives the pointer and the click guard.
|
|
68
|
+
* @param track The moved element. Its children are the repeated copies; child 0
|
|
69
|
+
* is the source content and must already be rendered.
|
|
70
|
+
*/
|
|
71
|
+
declare function createMarquee(lane: HTMLElement, track: HTMLElement, options?: MarqueeOptions): MarqueeInstance;
|
|
72
|
+
|
|
73
|
+
export { type MarqueeInstance, type MarqueeOptions, createMarquee };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createMarquee } from './chunk-5Q5TAW2N.js';
|