transition
Monitor component lifecycles and control transitions.
Feature
-
No style
-
Provider Component and composable hooks.
-
Provides Vue-like, className-based transitions.
Because they are infinitely smaller and higher performing, they can replace className-based 3rd party modules such as headlessui/transition.
Example
import { Transition } from "https://deno.land/x/atomic_ui_react@$VERSION/mod.ts";
import { useState } from "react";
export default () => {
const [isShow, setIsShow] = useState(false);
return (
<Transition
isShow={isShow}
enter="transition duration-300"
enterFrom="opacity-0"
leave="transition"
leaved="opacity-0"
>
root
</Transition>
);
};
CopyAPI
Transition
The transition component lets you add enter/leave transitions.
Generics
As extends keyof JSX.IntrinsicElements
Props
| Name | Required / Default | Description |
|---|---|---|
| isShow | ✅ | booleanWhether the component should be shown or hidden. |
| as | div | AsThe default tag. |
| immediate | false | booleanWhether do transitions immediately(on first mount) or not. - true - do transition on first mount.- false - do not transition on first mount. |
| unmount | true | booleanWhether unmount(remove DOM) at transition phase is leaved or not. |
| enterFrom | - | stringClasses before the enter phase starts. |
| enter | - | stringClasses during the entire enter phase. |
| enterTo | - | stringClasses immediately after the enter phase starts. |
| entered | - | stringClasses the enter phase is ended. |
| leaveFrom | - | stringClasses during the entire leave phase. |
| leave | - | stringClasses during the entire leave phase. |
| leaveTo | - | stringClasses before the leave phase starts. |
| leaved | - | stringClasses the leave phase is ended. |
| ref | - | Ref<Element> |
| ...allAttributes | - | AllHTMLAttributes<Element>All HTML attributes. |
Return
JSX.Element
WithTransition
Component to automatically adapt transitions to the root child.
Example
import { WithTransition } from "https://deno.land/x/atomic_ui_react@$VERSION/mod.ts";
export default () => {
return (
<WithTransition
enter="transition duration-300"
enterFrom="opacity-0"
leaved="text-red-500"
isShow
>
{(attrs, { isShowable }) => (isShowable ? <div {...attrs}></div> : <></>)}
</WithTransition>
);
};
CopyProps
| Name | Required / Default | Description |
|---|---|---|
| isShow | ✅ | booleanWhether the component should be shown or hidden. |
| children | ✅ | (attributes: Attributes, contexts: Contexts) => ReactElementRender children with props. |
| immediate | false | booleanWhether do transitions immediately(on first mount) or not. - true - do transition on first mount.- false - do not transition on first mount. |
| enterFrom | - | stringClasses before the enter phase starts. |
| enter | - | stringClasses during the entire enter phase. |
| enterTo | - | stringClasses immediately after the enter phase starts. |
| entered | - | stringClasses the enter phase is ended. |
| leaveFrom | - | stringClasses during the entire leave phase. |
| leave | - | stringClasses during the entire leave phase. |
| leaveTo | - | stringClasses before the leave phase starts. |
| leaved | - | stringClasses the leave phase is ended. |
| ref | - | Ref<Element> |
Render props
| N | Name | Description |
|---|---|---|
| 1 | attributes | |
| className | string | undefined | |
| ref | Ref<Element> | |
| 2 | contexts | |
| classNames | string[]The className tokens adapted currently transition. | |
| cleanTransitions | Partial<Transitions>Non-duplicated token and space transition props. It guarantee that there is no empty string or spaces only characters. | |
| isCompleted | booleanWhether transition lifecycle is completed or not. | |
| currentTransitions | TransitionName[]List of currently adapted transition. | |
| status | TransitionStatusCurrent transition lifecycle. | |
| isShowable | booleanWhether transition is completed and isShow state is false or not. | |
| isActivated | booleanWhether transitions are activated or not. | |
| lifecycle | TransitionLifecycleNamed transition lifecycle.
| |
| mode | "enter" | "leave" | undefinedCurrent transition mode. If it is not activated, return undefined. |
useTransition
Monitors the mount lifecycle and returns the appropriate transition status.
Example
import { useRef, useState } from "react";
import { useTransition } from "https://deno.land/x/atomic_ui_react@$VERSION/mod.ts";
export default () => {
const [isEnter] = useState(true);
const ref = useRef<HTMLDivElement>(null);
const [{ className }] = useTransition(
{ isEnter, duration: ref },
{
enter: "transition duration-300",
enterFrom: "opacity-0",
}
);
return <div ref={ref} className={className}></div>;
};
CopyuseTransitionLifecycle
Reactive state that records the current status of the transaction lifecycle
Types
import { DependencyList } from "react";
import {
TransitionLifecycle,
Useable,
} from "https://deno.land/x/atomic_ui_react@$VERSION/mod.ts";
declare function useTransitionLifecycle(
param: {
/** Specifies the transition duration */
duration: number | (() => number);
} & Partial<Useable>,
/** Effect will only activate if the values in the list change.
* Must be specified to monitor component lifecycle. Otherwise, loops may occur.
*/
deps: DependencyList
): TransitionLifecycle;
CopyExample
import {
getDuration,
useTransitionLifecycle,
} from "https://deno.land/x/atomic_ui_react@$VERSION/mod.ts";
export default () => {
const [isActivated, lifecycle] = useTransitionLifecycle(
{
duration: () => {
const el = globalThis.document.getElementById("target");
return el ? getDuration(el) : 0;
},
},
[]
);
};
CopyLooking for Atomic CSS?
How about mapcss?