domet 1.0.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 +21 -0
- package/README.md +187 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/useDomet.d.ts +60 -0
- package/dist/useDomet.d.ts.map +1 -0
- package/dist/useDomet.js +488 -0
- package/dist/useScrowl.d.ts +60 -0
- package/dist/useScrowl.d.ts.map +1 -0
- package/dist/useScrowl.js +488 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 blksmr
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# domet
|
|
2
|
+
|
|
3
|
+
domet is a lightweight scroll spy hook for React. Track which section is in view, highlight nav items, and build smooth scrolling experiences.
|
|
4
|
+
|
|
5
|
+
Uses `requestAnimationFrame` with throttling for 60fps performance. Hysteresis prevents jittery switching near section boundaries.
|
|
6
|
+
|
|
7
|
+
For the source code, check out the [GitHub](https://github.com/blksmr/domet).
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install domet
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { useDomet } from 'domet'
|
|
19
|
+
|
|
20
|
+
const sections = ['intro', 'features', 'api']
|
|
21
|
+
|
|
22
|
+
function Page() {
|
|
23
|
+
const { activeId, sectionProps, navProps } = useDomet(sections)
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<>
|
|
27
|
+
<nav>
|
|
28
|
+
{sections.map(id => (
|
|
29
|
+
<button key={id} {...navProps(id)}>
|
|
30
|
+
{id}
|
|
31
|
+
</button>
|
|
32
|
+
))}
|
|
33
|
+
</nav>
|
|
34
|
+
|
|
35
|
+
<section {...sectionProps('intro')}>...</section>
|
|
36
|
+
<section {...sectionProps('features')}>...</section>
|
|
37
|
+
<section {...sectionProps('api')}>...</section>
|
|
38
|
+
</>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API Reference
|
|
44
|
+
|
|
45
|
+
### Arguments
|
|
46
|
+
|
|
47
|
+
| Argument | Type | Description |
|
|
48
|
+
|----------|------|-------------|
|
|
49
|
+
| `sectionIds` | `string[]` | Array of section IDs to track |
|
|
50
|
+
| `containerRef` | `RefObject<HTMLElement> \| null` | Scrollable container (defaults to `window`) |
|
|
51
|
+
| `options` | `DometOptions` | Configuration options |
|
|
52
|
+
|
|
53
|
+
### Options
|
|
54
|
+
|
|
55
|
+
| Option | Type | Default | Description |
|
|
56
|
+
|--------|------|---------|-------------|
|
|
57
|
+
| `offset` | `number` | `0` | Trigger offset from top in pixels |
|
|
58
|
+
| `offsetRatio` | `number` | `0.08` | Viewport ratio for trigger line calculation |
|
|
59
|
+
| `debounceMs` | `number` | `10` | Throttle delay in milliseconds |
|
|
60
|
+
| `visibilityThreshold` | `number` | `0.6` | Minimum visibility ratio (0-1) for section to get priority |
|
|
61
|
+
| `hysteresisMargin` | `number` | `150` | Score margin to prevent rapid section switching |
|
|
62
|
+
| `behavior` | `'smooth' \| 'instant' \| 'auto'` | `'auto'` | Scroll behavior. `'auto'` respects `prefers-reduced-motion` |
|
|
63
|
+
|
|
64
|
+
### Callbacks
|
|
65
|
+
|
|
66
|
+
| Callback | Type | Description |
|
|
67
|
+
|----------|------|-------------|
|
|
68
|
+
| `onActiveChange` | `(id: string \| null, prevId: string \| null) => void` | Called when active section changes |
|
|
69
|
+
| `onSectionEnter` | `(id: string) => void` | Called when a section enters the viewport |
|
|
70
|
+
| `onSectionLeave` | `(id: string) => void` | Called when a section leaves the viewport |
|
|
71
|
+
| `onScrollStart` | `() => void` | Called when scrolling starts |
|
|
72
|
+
| `onScrollEnd` | `() => void` | Called when scrolling stops |
|
|
73
|
+
|
|
74
|
+
### Return Value
|
|
75
|
+
|
|
76
|
+
| Property | Type | Description |
|
|
77
|
+
|----------|------|-------------|
|
|
78
|
+
| `activeId` | `string \| null` | ID of the currently active section |
|
|
79
|
+
| `activeIndex` | `number` | Index of the active section in `sectionIds` (-1 if none) |
|
|
80
|
+
| `scroll` | `ScrollState` | Global scroll state |
|
|
81
|
+
| `sections` | `Record<string, SectionState>` | Per-section state indexed by ID |
|
|
82
|
+
| `sectionProps` | `(id: string) => SectionProps` | Props to spread on section elements |
|
|
83
|
+
| `navProps` | `(id: string) => NavProps` | Props to spread on nav items |
|
|
84
|
+
| `registerRef` | `(id: string) => (el: HTMLElement \| null) => void` | Manual ref registration |
|
|
85
|
+
| `scrollToSection` | `(id: string) => void` | Programmatically scroll to a section |
|
|
86
|
+
|
|
87
|
+
## Types
|
|
88
|
+
|
|
89
|
+
### ScrollState
|
|
90
|
+
|
|
91
|
+
Global scroll information updated on every scroll event.
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
type ScrollState = {
|
|
95
|
+
y: number // Current scroll position in pixels
|
|
96
|
+
progress: number // Overall scroll progress (0-1)
|
|
97
|
+
direction: 'up' | 'down' | null // Scroll direction
|
|
98
|
+
velocity: number // Scroll speed
|
|
99
|
+
isScrolling: boolean // True while actively scrolling
|
|
100
|
+
maxScroll: number // Maximum scroll value
|
|
101
|
+
viewportHeight: number // Viewport height in pixels
|
|
102
|
+
offset: number // Effective trigger offset
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### SectionState
|
|
107
|
+
|
|
108
|
+
Per-section state available for each tracked section.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
type SectionState = {
|
|
112
|
+
bounds: SectionBounds // Position and dimensions
|
|
113
|
+
visibility: number // Visibility ratio (0-1)
|
|
114
|
+
progress: number // Section scroll progress (0-1)
|
|
115
|
+
isInViewport: boolean // True if any part is visible
|
|
116
|
+
isActive: boolean // True if this is the active section
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
type SectionBounds = {
|
|
120
|
+
top: number
|
|
121
|
+
bottom: number
|
|
122
|
+
height: number
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Examples
|
|
127
|
+
|
|
128
|
+
### With Callbacks
|
|
129
|
+
|
|
130
|
+
```tsx
|
|
131
|
+
const { activeId } = useDomet(sections, null, {
|
|
132
|
+
onActiveChange: (id, prevId) => {
|
|
133
|
+
console.log(`Changed from ${prevId} to ${id}`)
|
|
134
|
+
},
|
|
135
|
+
onSectionEnter: (id) => {
|
|
136
|
+
console.log(`Entered: ${id}`)
|
|
137
|
+
},
|
|
138
|
+
})
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Using Scroll State
|
|
142
|
+
|
|
143
|
+
```tsx
|
|
144
|
+
const { scroll, sections } = useDomet(sectionIds)
|
|
145
|
+
|
|
146
|
+
// Global progress bar
|
|
147
|
+
<div style={{ width: `${scroll.progress * 100}%` }} />
|
|
148
|
+
|
|
149
|
+
// Per-section animations
|
|
150
|
+
{sectionIds.map(id => (
|
|
151
|
+
<div style={{ opacity: sections[id]?.visibility }} />
|
|
152
|
+
))}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Custom Container
|
|
156
|
+
|
|
157
|
+
```tsx
|
|
158
|
+
const containerRef = useRef<HTMLDivElement>(null)
|
|
159
|
+
const { activeId } = useDomet(sections, containerRef)
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<div ref={containerRef} style={{ overflow: 'auto', height: '100vh' }}>
|
|
163
|
+
{/* sections */}
|
|
164
|
+
</div>
|
|
165
|
+
)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Fine-tuning Behavior
|
|
169
|
+
|
|
170
|
+
```tsx
|
|
171
|
+
useDomet(sections, null, {
|
|
172
|
+
visibilityThreshold: 0.8, // Require 80% visibility
|
|
173
|
+
hysteresisMargin: 200, // More resistance to switching
|
|
174
|
+
})
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Why domet?
|
|
178
|
+
|
|
179
|
+
This library was born from a real need at work. I wanted a scroll-spy solution that was powerful and completely headless, but above all, extremely lightweight. No bloated dependencies, no opinionated styling, just a hook that does one thing well.
|
|
180
|
+
|
|
181
|
+
The name **domet** comes from Bosnian/Serbian/Croatian and means "reach" or "range" — the distance something can cover. Pronounced `/ˈdɔ.met/`: stress on the first syllable, open "o", and a hard "t" at the end.
|
|
182
|
+
|
|
183
|
+
## Support
|
|
184
|
+
|
|
185
|
+
For issues or feature requests, open an issue on [GitHub](https://github.com/blksmr/domet).
|
|
186
|
+
|
|
187
|
+
You can also reach out to me on [Twitter](https://x.com/blkasmir).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,YAAY,EACZ,QAAQ,EACR,cAAc,EACd,WAAW,EACX,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,cAAc,GACf,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default, useDomet } from "./useDomet";
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { RefObject } from "react";
|
|
2
|
+
export type SectionBounds = {
|
|
3
|
+
top: number;
|
|
4
|
+
bottom: number;
|
|
5
|
+
height: number;
|
|
6
|
+
};
|
|
7
|
+
export type ScrollState = {
|
|
8
|
+
y: number;
|
|
9
|
+
progress: number;
|
|
10
|
+
direction: "up" | "down" | null;
|
|
11
|
+
velocity: number;
|
|
12
|
+
isScrolling: boolean;
|
|
13
|
+
maxScroll: number;
|
|
14
|
+
viewportHeight: number;
|
|
15
|
+
offset: number;
|
|
16
|
+
};
|
|
17
|
+
export type SectionState = {
|
|
18
|
+
bounds: SectionBounds;
|
|
19
|
+
visibility: number;
|
|
20
|
+
progress: number;
|
|
21
|
+
isInViewport: boolean;
|
|
22
|
+
isActive: boolean;
|
|
23
|
+
};
|
|
24
|
+
export type ScrollBehavior = "smooth" | "instant" | "auto";
|
|
25
|
+
export type DometOptions = {
|
|
26
|
+
offset?: number;
|
|
27
|
+
offsetRatio?: number;
|
|
28
|
+
debounceMs?: number;
|
|
29
|
+
visibilityThreshold?: number;
|
|
30
|
+
hysteresisMargin?: number;
|
|
31
|
+
behavior?: ScrollBehavior;
|
|
32
|
+
onActiveChange?: (id: string | null, prevId: string | null) => void;
|
|
33
|
+
onSectionEnter?: (id: string) => void;
|
|
34
|
+
onSectionLeave?: (id: string) => void;
|
|
35
|
+
onScrollStart?: () => void;
|
|
36
|
+
onScrollEnd?: () => void;
|
|
37
|
+
};
|
|
38
|
+
export type SectionProps = {
|
|
39
|
+
id: string;
|
|
40
|
+
ref: (el: HTMLElement | null) => void;
|
|
41
|
+
"data-domet": string;
|
|
42
|
+
};
|
|
43
|
+
export type NavProps = {
|
|
44
|
+
onClick: () => void;
|
|
45
|
+
"aria-current": "page" | undefined;
|
|
46
|
+
"data-active": boolean;
|
|
47
|
+
};
|
|
48
|
+
export type UseDometReturn = {
|
|
49
|
+
activeId: string | null;
|
|
50
|
+
activeIndex: number;
|
|
51
|
+
scroll: ScrollState;
|
|
52
|
+
sections: Record<string, SectionState>;
|
|
53
|
+
registerRef: (id: string) => (el: HTMLElement | null) => void;
|
|
54
|
+
scrollToSection: (id: string) => void;
|
|
55
|
+
sectionProps: (id: string) => SectionProps;
|
|
56
|
+
navProps: (id: string) => NavProps;
|
|
57
|
+
};
|
|
58
|
+
export declare function useDomet(sectionIds: string[], containerRef?: RefObject<HTMLElement> | null, options?: DometOptions): UseDometReturn;
|
|
59
|
+
export default useDomet;
|
|
60
|
+
//# sourceMappingURL=useDomet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDomet.d.ts","sourceRoot":"","sources":["../src/useDomet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAgBvC,MAAM,MAAM,aAAa,GAAG;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,CAAC,EAAE,MAAM,CAAC;IACV,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAAC;IAChC,QAAQ,EAAE,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