phase 0.1.0 → 0.3.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/dist/react.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { clamp01, easeOutCubic } from "./ease.js";
3
- import { C as missingContextError, _ as subscribeMediaQuery, a as createPointer, c as prefersReducedMotion, d as subscribeDpr, f as createRenderState, g as readMediaQuery, h as createLifecycle, i as observeResize, l as whenIdle, m as createLoop, n as createThrottle, o as createMutation, p as createScrollProgress, r as createScroll, s as REDUCED_MOTION_QUERY, t as createDebounce, u as readDpr, v as createSight, x as invalidDurationError } from "./debounce-BX3NrBak.js";
3
+ import { S as invalidDurationError, _ as subscribeMediaQuery, a as createPointer, c as prefersReducedMotion, d as subscribeDpr, f as createRenderState, g as readMediaQuery, h as createLifecycle, i as observeResize, l as whenIdle, m as createLoop, n as createThrottle, o as createMutation, p as createScrollProgress, r as createScroll, s as REDUCED_MOTION_QUERY, t as createDebounce, u as readDpr, v as createSight, w as missingContextError, x as conflictingTargetError } from "./debounce-BA-WszUy.js";
4
4
  import { createContext, createElement, use, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
5
5
  import { jsx } from "react/jsx-runtime";
6
6
  //#region src/react/use-synced-ref/index.ts
@@ -71,20 +71,21 @@ const INITIAL_STATE$6 = {
71
71
  * return <div ref={ref} />;
72
72
  */
73
73
  function useLoop(options) {
74
- const { fps, enabled = true, reducedMotion, degraded, degradedFps, intersectionOptions } = options;
74
+ const { target, fps, enabled = true, reducedMotion, degraded, degradedFps, intersectionOptions } = options;
75
75
  const onTickRef = useSyncedRef(options.onTick);
76
76
  const internalRef = useRef(null);
77
77
  const ref = options.ref ?? internalRef;
78
78
  const [state, setState] = useState(INITIAL_STATE$6);
79
79
  const loopRef = useRef(null);
80
80
  useEffect(() => {
81
- const element = ref.current;
82
- if (!element || !enabled) {
81
+ if (target && options.ref) conflictingTargetError("useLoop");
82
+ const anchor = target === "page" ? document : ref.current;
83
+ if (!anchor || !enabled) {
83
84
  setState(INITIAL_STATE$6);
84
85
  return;
85
86
  }
86
87
  const loop = createLoop({
87
- target: element,
88
+ target: anchor,
88
89
  onTick: (frame) => onTickRef.current(frame),
89
90
  fps,
90
91
  reducedMotion,
@@ -110,7 +111,8 @@ function useLoop(options) {
110
111
  fps,
111
112
  reducedMotion,
112
113
  degraded,
113
- degradedFps
114
+ degradedFps,
115
+ target
114
116
  ]);
115
117
  return {
116
118
  ref,
@@ -143,7 +145,7 @@ const INITIAL_STATE$5 = {
143
145
  * return <canvas ref={ref} />;
144
146
  */
145
147
  function useLifecycle(options) {
146
- const { reducedMotion, intersectionOptions, enabled = true } = options ?? {};
148
+ const { target, reducedMotion, intersectionOptions, enabled = true } = options ?? {};
147
149
  const paused = options?.paused ?? false;
148
150
  const onPhaseChangeRef = useSyncedRef(options?.onPhaseChange);
149
151
  const internalRef = useRef(null);
@@ -151,13 +153,14 @@ function useLifecycle(options) {
151
153
  const [state, setState] = useState(INITIAL_STATE$5);
152
154
  const lifecycleRef = useRef(null);
153
155
  useEffect(() => {
154
- const element = ref.current;
155
- if (!element || !enabled) {
156
+ if (target && options?.ref) conflictingTargetError("useLifecycle");
157
+ const anchor = target === "page" ? document : ref.current;
158
+ if (!anchor || !enabled) {
156
159
  setState(INITIAL_STATE$5);
157
160
  return;
158
161
  }
159
162
  const lifecycle = createLifecycle({
160
- target: element,
163
+ target: anchor,
161
164
  reducedMotion,
162
165
  intersectionOptions,
163
166
  onPhaseChange: (phase, phaseReason) => {
@@ -174,7 +177,11 @@ function useLifecycle(options) {
174
177
  lifecycle.stop();
175
178
  lifecycleRef.current = null;
176
179
  };
177
- }, [enabled, reducedMotion]);
180
+ }, [
181
+ enabled,
182
+ reducedMotion,
183
+ target
184
+ ]);
178
185
  useEffect(() => {
179
186
  const lifecycle = lifecycleRef.current;
180
187
  if (!lifecycle) return;
@@ -245,14 +252,17 @@ function useSight(options) {
245
252
  const phaseRef = useRef("unknown");
246
253
  const phaseReasonRef = useRef("initial");
247
254
  const onVisibilityChangeRef = useSyncedRef(options?.onVisibilityChange);
255
+ const target = options?.target;
248
256
  const internalRef = useRef(null);
249
257
  const ref = options?.ref ?? internalRef;
250
258
  useEffect(() => {
251
- const element = ref.current;
252
- if (!element) return;
259
+ if (target && options?.ref) conflictingTargetError("useSight");
260
+ const anchor = target === "page" ? document : ref.current;
261
+ if (!anchor) return;
253
262
  let frozen = false;
263
+ let instance = null;
254
264
  const sight = createSight({
255
- target: element,
265
+ target: anchor,
256
266
  intersectionOptions: {
257
267
  root: options?.root,
258
268
  rootMargin: options?.rootMargin,
@@ -269,12 +279,14 @@ function useSight(options) {
269
279
  });
270
280
  if (observe === "once" && phase === "visible") {
271
281
  frozen = true;
272
- sight.stop();
282
+ instance?.stop();
273
283
  }
274
284
  }
275
285
  });
286
+ instance = sight;
287
+ if (frozen) sight.stop();
276
288
  return () => sight.stop();
277
- }, [observe]);
289
+ }, [observe, target]);
278
290
  return {
279
291
  ref,
280
292
  ...state,
@@ -767,7 +779,7 @@ function initialState() {
767
779
  */
768
780
  function useScroll(options) {
769
781
  const [state, setState] = useState(INITIAL_STATE);
770
- const { visibility = "pause", enabled = true, intersectionOptions } = options;
782
+ const { target, visibility = "pause", enabled = true, intersectionOptions } = options;
771
783
  const phaseRef = useRef("paused");
772
784
  const phaseReasonRef = useRef("initial");
773
785
  const stateRef = useRef(initialState());
@@ -779,8 +791,9 @@ function useScroll(options) {
779
791
  instanceRef.current?.measure();
780
792
  }, []);
781
793
  useEffect(() => {
782
- const element = ref.current;
783
- if (!element || !enabled) {
794
+ if (target && options.ref) conflictingTargetError("useScroll");
795
+ const anchor = target === "page" ? document : ref.current;
796
+ if (!anchor || !enabled) {
784
797
  instanceRef.current = null;
785
798
  setState(INITIAL_STATE);
786
799
  phaseRef.current = "paused";
@@ -789,7 +802,7 @@ function useScroll(options) {
789
802
  return;
790
803
  }
791
804
  const instance = createScroll({
792
- target: element,
805
+ target: anchor,
793
806
  onScroll: (scrollState) => {
794
807
  stateRef.current = scrollState;
795
808
  onScrollRef.current(scrollState);
@@ -810,7 +823,11 @@ function useScroll(options) {
810
823
  instance.stop();
811
824
  instanceRef.current = null;
812
825
  };
813
- }, [enabled, visibility]);
826
+ }, [
827
+ enabled,
828
+ visibility,
829
+ target
830
+ ]);
814
831
  return {
815
832
  ref,
816
833
  ...state,