seitu 0.2.1 → 0.2.3

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.
@@ -76,16 +76,16 @@ export interface UseSubscriptionOptions<S extends Subscribable<any> & Readable<a
76
76
  * 'use client'
77
77
  *
78
78
  * import * as React from 'react'
79
- * import { scrollState } from 'seitu/web'
79
+ * import { createScrollState } from 'seitu/web'
80
80
  * import { useSubscription } from 'seitu/react'
81
81
  *
82
82
  * export default function Page() {
83
83
  * const ref = React.useRef<HTMLDivElement>(null)
84
- * const state = useSubscription(() => scrollState({ element: () => ref.current, direction: 'vertical' }))
84
+ * const state = useSubscription(() => createScrollState({ element: () => ref.current, direction: 'vertical' }))
85
85
  *
86
86
  * return (
87
87
  * <div ref={ref}>
88
- * {String(state.top.value)}
88
+ * {String(state.top.reached)}
89
89
  * </div>
90
90
  * )
91
91
  * }
@@ -33,10 +33,10 @@ export interface MediaQueryOptions<T extends string> {
33
33
  *
34
34
  * @example Vanilla
35
35
  * ```ts twoslash title="media-query.ts"
36
- * import { mediaQuery } from 'seitu/web'
36
+ * import { createMediaQuery } from 'seitu/web'
37
37
  * import { useSubscription } from 'seitu/react'
38
38
  *
39
- * const isDesktop = mediaQuery({ query: '(min-width: 768px)' })
39
+ * const isDesktop = createMediaQuery({ query: '(min-width: 768px)' })
40
40
  *
41
41
  * // Usage with subscribe
42
42
  * isDesktop.subscribe(matches => {
@@ -49,10 +49,10 @@ export interface MediaQueryOptions<T extends string> {
49
49
  *
50
50
  * @example React
51
51
  * ```tsx twoslash title="page.tsx"
52
- * import { mediaQuery } from 'seitu/web'
52
+ * import { createMediaQuery } from 'seitu/web'
53
53
  * import { useSubscription } from 'seitu/react'
54
54
  *
55
- * const isDesktop = mediaQuery({ query: '(min-width: 768px)' })
55
+ * const isDesktop = createMediaQuery({ query: '(min-width: 768px)' })
56
56
  *
57
57
  * // Usage with some function component
58
58
  * function Layout() {
@@ -63,15 +63,15 @@ export interface MediaQueryOptions<T extends string> {
63
63
  *
64
64
  * @example Errors
65
65
  * ```tsx twoslash
66
- * import { mediaQuery } from 'seitu/web'
66
+ * import { createMediaQuery } from 'seitu/web'
67
67
  * import { useSubscription } from 'seitu/react'
68
68
  *
69
69
  * // @errors: 2362 2322 1109
70
- * mediaQuery({ query: '(min-width: ' })
70
+ * createMediaQuery({ query: '(min-width: ' })
71
71
  *
72
72
  * // @errors: 2362 2322 2820
73
- * mediaQuery({ query: '(min-width: 768' })
73
+ * createMediaQuery({ query: '(min-width: 768' })
74
74
  * ```
75
75
  */
76
- export declare function mediaQuery<T extends string>(options: MediaQueryOptions<T>): MediaQuery;
76
+ export declare function createMediaQuery<T extends string>(options: MediaQueryOptions<T>): MediaQuery;
77
77
  export {};
@@ -1,7 +1,7 @@
1
1
  import type { Readable, Subscribable } from '../core/index';
2
2
  export type ScrollDirection = 'vertical' | 'horizontal' | 'both';
3
3
  export interface ScrollStateEdge {
4
- value: boolean;
4
+ reached: boolean;
5
5
  remaining: number;
6
6
  }
7
7
  export interface ScrollStateValue {
@@ -10,6 +10,12 @@ export interface ScrollStateValue {
10
10
  left: ScrollStateEdge;
11
11
  right: ScrollStateEdge;
12
12
  }
13
+ export interface ScrollStateThreshold {
14
+ top?: number;
15
+ bottom?: number;
16
+ left?: number;
17
+ right?: number;
18
+ }
13
19
  export interface ScrollState extends Subscribable<ScrollStateValue>, Readable<ScrollStateValue> {
14
20
  }
15
21
  export interface ScrollStateOptions {
@@ -25,28 +31,30 @@ export interface ScrollStateOptions {
25
31
  */
26
32
  direction?: ScrollDirection;
27
33
  /**
28
- * Number of pixels from each edge before it counts as "scrolled".
34
+ * Number of pixels from each edge before it counts as "reached".
35
+ * Pass a single number to apply the same threshold to all edges,
36
+ * or an object with per-side values.
29
37
  * @default 0
30
38
  */
31
- threshold?: number;
39
+ threshold?: number | ScrollStateThreshold;
32
40
  }
33
41
  /**
34
42
  * Creates a reactive handle that tracks scroll position of an element relative to each edge.
35
43
  *
36
44
  * @example Vanilla
37
45
  * ```ts twoslash
38
- * import { scrollState } from 'seitu/web'
46
+ * import { createScrollState } from 'seitu/web'
39
47
  *
40
- * const scroll = scrollState({
48
+ * const scroll = createScrollState({
41
49
  * element: document.querySelector('.container'),
42
50
  * direction: 'vertical',
43
51
  * threshold: 10,
44
52
  * })
45
53
  *
46
54
  * scroll.subscribe(state => {
47
- * console.log(state.top.value)
55
+ * console.log(state.top.reached)
48
56
  * console.log(state.top.remaining)
49
- * console.log(state.bottom.value)
57
+ * console.log(state.bottom.reached)
50
58
  * console.log(state.bottom.remaining)
51
59
  * })
52
60
  *
@@ -59,19 +67,19 @@ export interface ScrollStateOptions {
59
67
  * 'use client'
60
68
  *
61
69
  * import * as React from 'react'
62
- * import { scrollState } from 'seitu/web'
70
+ * import { createScrollState } from 'seitu/web'
63
71
  * import { useSubscription } from 'seitu/react'
64
72
  *
65
73
  * function Layout() {
66
74
  * const ref = React.useRef<HTMLDivElement>(null)
67
- * const state = useSubscription(() => scrollState({
75
+ * const state = useSubscription(() => createScrollState({
68
76
  * element: () => ref.current,
69
77
  * threshold: 10,
70
78
  * }))
71
79
  *
72
80
  * return (
73
81
  * <div ref={ref}>
74
- * {state.top.value ? 'at the top' : 'scrolled'}
82
+ * {state.top.reached ? 'at the top' : 'scrolled'}
75
83
  * </div>
76
84
  * )
77
85
  * }
@@ -82,22 +90,22 @@ export interface ScrollStateOptions {
82
90
  * 'use client'
83
91
  *
84
92
  * import * as React from 'react'
85
- * import { scrollState } from 'seitu/web'
93
+ * import { createScrollState } from 'seitu/web'
86
94
  * import { useSubscription } from 'seitu/react'
87
95
  *
88
96
  * function Layout() {
89
97
  * const [ref, setRef] = React.useState<HTMLDivElement | null>(null)
90
- * const state = useSubscription(() => scrollState({
98
+ * const state = useSubscription(() => createScrollState({
91
99
  * element: ref,
92
100
  * threshold: 10,
93
101
  * }), { deps: [ref] })
94
102
  *
95
103
  * return (
96
104
  * <div ref={setRef}>
97
- * {state.top.value ? 'at the top' : 'scrolled'}
105
+ * {state.top.reached ? 'at the top' : 'scrolled'}
98
106
  * </div>
99
107
  * )
100
108
  * }
101
109
  * ```
102
110
  */
103
- export declare function scrollState(options: ScrollStateOptions): ScrollState;
111
+ export declare function createScrollState(options: ScrollStateOptions): ScrollState;
package/dist/web.js CHANGED
@@ -110,13 +110,23 @@ function s(e) {
110
110
  };
111
111
  }
112
112
  var c = {
113
- value: !1,
113
+ reached: !1,
114
114
  remaining: 0
115
115
  };
116
116
  function l(e) {
117
- let { direction: t = "both", threshold: r = 0 } = e, { subscribe: i, notify: a } = n(), o = () => typeof e.element == "function" ? e.element() : e.element, s = () => {
118
- let e = o(), n = (e, t) => ({
119
- value: e,
117
+ let { direction: t = "both", threshold: r = 0 } = e, { subscribe: i, notify: a } = n(), o = typeof r == "number" ? {
118
+ top: r,
119
+ bottom: r,
120
+ left: r,
121
+ right: r
122
+ } : {
123
+ top: r.top ?? 0,
124
+ bottom: r.bottom ?? 0,
125
+ left: r.left ?? 0,
126
+ right: r.right ?? 0
127
+ }, s = () => typeof e.element == "function" ? e.element() : e.element, l = () => {
128
+ let e = s(), n = (e, t) => ({
129
+ reached: e,
120
130
  remaining: Math.max(0, t)
121
131
  });
122
132
  if (!e) return {
@@ -125,20 +135,20 @@ function l(e) {
125
135
  left: c,
126
136
  right: c
127
137
  };
128
- let i = e.scrollTop, a = e.scrollHeight - e.scrollTop - e.clientHeight, s = e.scrollLeft, l = e.scrollWidth - e.scrollLeft - e.clientWidth;
138
+ let r = e.scrollTop, i = e.scrollHeight - e.scrollTop - e.clientHeight, a = e.scrollLeft, l = e.scrollWidth - e.scrollLeft - e.clientWidth;
129
139
  return {
130
- top: t === "horizontal" ? c : n(i <= r, i),
131
- bottom: t === "horizontal" ? c : n(a <= r, a),
132
- left: t === "vertical" ? c : n(s <= r, s),
133
- right: t === "vertical" ? c : n(l <= r, l)
140
+ top: t === "horizontal" ? c : n(r <= o.top, r),
141
+ bottom: t === "horizontal" ? c : n(i <= o.bottom, i),
142
+ left: t === "vertical" ? c : n(a <= o.left, a),
143
+ right: t === "vertical" ? c : n(l <= o.right, l)
134
144
  };
135
145
  };
136
146
  return {
137
- get: s,
147
+ get: l,
138
148
  subscribe: (e) => {
139
- let t = o();
140
- if (!t) return e(s()), () => {};
141
- let n = i(() => e(s())), r = () => e(s());
149
+ let t = s();
150
+ if (!t) return e(l()), () => {};
151
+ let n = i(() => e(l())), r = () => e(l());
142
152
  return t.addEventListener("scroll", r, { passive: !0 }), () => {
143
153
  n(), t.removeEventListener("scroll", r);
144
154
  };
@@ -161,4 +171,4 @@ function d(e) {
161
171
  kind: "sessionStorage"
162
172
  });
163
173
  }
164
- export { i as createLocalStorage, o as createLocalStorageValue, u as createSessionStorage, d as createSessionStorageValue, s as mediaQuery, l as scrollState };
174
+ export { i as createLocalStorage, o as createLocalStorageValue, s as createMediaQuery, l as createScrollState, u as createSessionStorage, d as createSessionStorageValue };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seitu",
3
3
  "displayName": "Seitu",
4
4
  "type": "module",
5
- "version": "0.2.1",
5
+ "version": "0.2.3",
6
6
  "private": false,
7
7
  "author": "Valerii Strilets",
8
8
  "license": "MIT",