pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+20
@@ -0,0 +1,20 @@
|
||||
import * as React from 'react';
|
||||
export type TransitionContextValue = {
|
||||
/**
|
||||
* `true`, if the current element was requested to appear.
|
||||
*/
|
||||
requestedEnter: boolean;
|
||||
/**
|
||||
* Callback to be called when the element has completely exited.
|
||||
*/
|
||||
onExited: () => void;
|
||||
/**
|
||||
* Registers a child transition.
|
||||
* This is used to notify the parent `useTransitionTrigger` hook that a child transition exists
|
||||
* and state changes must be triggered by the context's callbck functions.
|
||||
*
|
||||
* Returns a function to unregister the child transition.
|
||||
*/
|
||||
registerTransition: () => () => void;
|
||||
};
|
||||
export declare const TransitionContext: React.Context<TransitionContextValue | null>;
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import * as React from 'react';
|
||||
export const TransitionContext = /*#__PURE__*/React.createContext(null);
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
TransitionContext.displayName = 'TransitionContext';
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export * from './useTransitionStateManager';
|
||||
export * from './useTransitionTrigger';
|
||||
export * from './TransitionContext';
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export * from './useTransitionStateManager';
|
||||
export * from './useTransitionTrigger';
|
||||
export * from './TransitionContext';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/useTransition/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
export type UseTransitionStateManagerReturnValue = {
|
||||
/**
|
||||
* `true`, if the current element should be visible.
|
||||
*/
|
||||
requestedEnter: boolean;
|
||||
/**
|
||||
* Callback to be called when the element has completely exited.
|
||||
*/
|
||||
onExited: () => void;
|
||||
};
|
||||
/**
|
||||
* Allows an element to be transitioned in and out.
|
||||
* The transition is triggerred by a `TransitionContext` placed above in the component tree.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Transitions](https://mui.com/base-ui/react-transitions/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useTransitionStateManager API](https://mui.com/base-ui/react-transitions/hooks-api/#use-transition-state-manager)
|
||||
*/
|
||||
export declare function useTransitionStateManager(): UseTransitionStateManagerReturnValue;
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
import { TransitionContext } from './TransitionContext';
|
||||
/**
|
||||
* Allows an element to be transitioned in and out.
|
||||
* The transition is triggerred by a `TransitionContext` placed above in the component tree.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Transitions](https://mui.com/base-ui/react-transitions/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useTransitionStateManager API](https://mui.com/base-ui/react-transitions/hooks-api/#use-transition-state-manager)
|
||||
*/
|
||||
export function useTransitionStateManager() {
|
||||
const transitionContext = React.useContext(TransitionContext);
|
||||
if (!transitionContext) {
|
||||
throw new Error('Missing transition context');
|
||||
}
|
||||
const {
|
||||
registerTransition,
|
||||
requestedEnter,
|
||||
onExited
|
||||
} = transitionContext;
|
||||
React.useEffect(() => {
|
||||
return registerTransition();
|
||||
}, [registerTransition]);
|
||||
return {
|
||||
onExited,
|
||||
requestedEnter
|
||||
};
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
import { TransitionContextValue } from './TransitionContext';
|
||||
/**
|
||||
* Allows child elements to be transitioned in and out.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Transitions](https://mui.com/base-ui/react-transitions/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useTransitionTrigger API](https://mui.com/base-ui/react-transitions/hooks-api/#use-transition-trigger)
|
||||
*/
|
||||
export declare function useTransitionTrigger(requestEnter: boolean): UseTransitionTriggerReturnValue;
|
||||
export type UseTransitionTriggerReturnValue = {
|
||||
/**
|
||||
* The value of a `TransitionContext` to be placed around children that will be transitioned.
|
||||
*/
|
||||
contextValue: TransitionContextValue;
|
||||
/**
|
||||
* `true`, if the transitioned element has exited completely (or not entered yet).
|
||||
*/
|
||||
hasExited: boolean;
|
||||
};
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
/**
|
||||
* Allows child elements to be transitioned in and out.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Transitions](https://mui.com/base-ui/react-transitions/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useTransitionTrigger API](https://mui.com/base-ui/react-transitions/hooks-api/#use-transition-trigger)
|
||||
*/
|
||||
export function useTransitionTrigger(requestEnter) {
|
||||
const [exitTransitionFinished, setExitTransitionFinished] = React.useState(true);
|
||||
const hasPendingExitTransition = React.useRef(false);
|
||||
const registeredTransitions = React.useRef(0);
|
||||
const [hasTransition, setHasTransition] = React.useState(false);
|
||||
const previousRequestEnter = React.useRef(requestEnter);
|
||||
React.useEffect(() => {
|
||||
if (!requestEnter &&
|
||||
// checking registeredTransitions.current instead of hasTransition to avoid this effect re-firing whenever hasTransition changes
|
||||
registeredTransitions.current > 0 &&
|
||||
// prevents waiting for a pending transition right after mounting
|
||||
previousRequestEnter.current !== requestEnter) {
|
||||
hasPendingExitTransition.current = true;
|
||||
setExitTransitionFinished(false);
|
||||
}
|
||||
previousRequestEnter.current = requestEnter;
|
||||
}, [requestEnter]);
|
||||
const handleExited = React.useCallback(() => {
|
||||
hasPendingExitTransition.current = false;
|
||||
setExitTransitionFinished(true);
|
||||
}, []);
|
||||
const registerTransition = React.useCallback(() => {
|
||||
registeredTransitions.current += 1;
|
||||
setHasTransition(true);
|
||||
return () => {
|
||||
registeredTransitions.current -= 1;
|
||||
if (registeredTransitions.current === 0) {
|
||||
setHasTransition(false);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
let hasExited;
|
||||
if (!hasTransition) {
|
||||
// If there are no transitions registered, the `exited` state is opposite of `requestEnter` immediately.
|
||||
hasExited = !requestEnter;
|
||||
} else if (requestEnter) {
|
||||
hasExited = false;
|
||||
} else {
|
||||
hasExited = !hasPendingExitTransition.current && exitTransitionFinished;
|
||||
}
|
||||
const contextValue = React.useMemo(() => ({
|
||||
requestedEnter: requestEnter,
|
||||
onExited: handleExited,
|
||||
registerTransition,
|
||||
hasExited
|
||||
}), [handleExited, requestEnter, registerTransition, hasExited]);
|
||||
return {
|
||||
contextValue,
|
||||
hasExited
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user