Skip to content

Commit

Permalink
#182 Only log missing context warning in public API
Browse files Browse the repository at this point in the history
  • Loading branch information
leroykorterink committed Oct 9, 2023
1 parent 28e11e1 commit 4fcc059
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { createContext, useContext, type RefObject } from 'react';
import { createContext, type RefObject } from 'react';
import type { BeforeUnmountCallback } from '../useBeforeUnmount/useBeforeUnmount.js';

export type TransitionPresenceContextType = Set<RefObject<BeforeUnmountCallback>> | undefined;

export const TransitionPresenceContext = createContext<TransitionPresenceContextType>(undefined);

export function useTransitionPresence(): TransitionPresenceContextType {
const context = useContext(TransitionPresenceContext);

if (context === undefined) {
// eslint-disable-next-line no-console
console.warn('Component is not rendered in the context of a TransitionPresence');
}

return context;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { childrenAreEqual } from '../_utils/childrenAreEqual.js';
import { tick } from '../_utils/tick.js';
import {
useBeforeUnmount,
useTransitionPresenceBeforeUnmount,
type BeforeUnmountCallback,
} from '../useBeforeUnmount/useBeforeUnmount.js';
import { TransitionPresenceContext } from './TransitionPresence.context.js';
Expand Down Expand Up @@ -104,7 +104,7 @@ export function TransitionPresence({
]);

// Apply same effect when TransitionPresence in tree updates
useBeforeUnmount(beforeUnmountPreviousChildren);
useTransitionPresenceBeforeUnmount(beforeUnmountPreviousChildren);

return (
<TransitionPresenceContext.Provider value={beforeUnmountCallbacks}>
Expand Down
9 changes: 6 additions & 3 deletions packages/react-transition-presence/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from './TransitionPresence/TransitionPresence.js';
export * from './TransitionPresence/TransitionPresence.context.js';
export * from './useBeforeUnmount/useBeforeUnmount.js';
export { TransitionPresenceContext } from './TransitionPresence/TransitionPresence.context.js';
export { TransitionPresence } from './TransitionPresence/TransitionPresence.js';
export {
type BeforeUnmountCallback,
useBeforeUnmount,
} from './useBeforeUnmount/useBeforeUnmount.js';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRefValue } from '@mediamonks/react-hooks';
import { useEffect } from 'react';
import { useTransitionPresence } from '../TransitionPresence/TransitionPresence.context.js';
import { useContext, useEffect } from 'react';
import { TransitionPresenceContext } from '../index.js';

export type BeforeUnmountCallback = (
abortSignal: AbortSignal,
Expand All @@ -12,7 +12,31 @@ PromiseLike<unknown> | void;
* TransitionPresence boundary
*/
export function useBeforeUnmount(callback: BeforeUnmountCallback): void {
const transitionPresence = useTransitionPresence();
const transitionPresence = useContext(TransitionPresenceContext);
const callbackRef = useRefValue(callback);

if (transitionPresence === undefined) {
// eslint-disable-next-line no-console
console.warn('Component is not rendered in the context of a TransitionPresence');
}

useEffect(() => {
queueMicrotask(() => {
transitionPresence?.add(callbackRef);
});

return () => {
transitionPresence?.delete(callbackRef);
};
}, [transitionPresence, callbackRef]);
}

/**
* useBeforeUnmount without the warning, this should only be used within the
* <TransitionPresence> component in this package.
*/
export function useTransitionPresenceBeforeUnmount(callback: BeforeUnmountCallback): void {
const transitionPresence = useContext(TransitionPresenceContext);
const callbackRef = useRefValue(callback);

useEffect(() => {
Expand Down

0 comments on commit 4fcc059

Please sign in to comment.