"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Transition: () => Transition, TransitionGroup: () => TransitionGroup }); module.exports = __toCommonJS(src_exports); // src/transition.tsx var import_react = require("react"); var import_jsx_runtime = require("react/jsx-runtime"); var TransitionInner = ({ children, isEnter, onMounted, ...props }) => { const ref = (0, import_react.useRef)(null); const stepRef = (0, import_react.useRef)(0); const originalClass = (0, import_react.useMemo)(() => children.props.className ?? "", []); const getcls = () => `${originalClass} ${isEnter ? props.enter ?? "" : props.leave ?? ""}`; const [className, setClassName] = (0, import_react.useState)(() => getcls()); const toggle = () => { const el = ref?.current; if (!el) return; stepRef.current = isEnter ? 0 : 2; setClassName(originalClass); }; (0, import_react.useEffect)(() => { const step = stepRef.current; if (step === 0 || step === 2) { const acls = step === 0 ? props.enterActive : props.leaveActive; stepRef.current |= 1; setClassName(`${getcls()} ${acls ?? ""}`); } }, [className]); const first = (0, import_react.useRef)(true); (0, import_react.useEffect)(() => { if (first.current) { first.current = false; return; } toggle(); }, [isEnter]); (0, import_react.useLayoutEffect)(() => { if (!ref.current) return; const onEnd = () => { if (!ref.current) return; if (stepRef.current === 3) { props.onAfterLeave?.(ref.current); } else if (stepRef.current === 1) { props.onAfterEnter?.(ref.current); } }; ref.current.addEventListener("transitionend", onEnd); onMounted(ref.current); return () => { ref.current?.removeEventListener("transitionend", onEnd); }; }, []); return (0, import_react.cloneElement)(children, { className, ref }); }; var Transition = (0, import_react.forwardRef)( ({ destroyAfterLeave, appear, onAfterLeave, onAfterEnter, onEnterCancelled, onLeaveCancelled, isEnter, ...restProps }, ref) => { const [mounted, setMounted] = (0, import_react.useState)(!destroyAfterLeave || (appear ? !isEnter : isEnter)); const [realEnter, setRealEnter] = (0, import_react.useState)(appear ? !isEnter : isEnter); const state = (0, import_react.useRef)((appear ? !isEnter : isEnter) ? 1 /* Entered */ : 3 /* Leaved */); (0, import_react.useEffect)(() => { if (!destroyAfterLeave) { setRealEnter(isEnter); } else { if (isEnter) { if (state.current === 2 /* Leaving */) { onLeaveCancelled?.(); state.current = 0 /* Entering */; setRealEnter(true); } else { setMounted(true); } } else { if (state.current === 0 /* Entering */ || state.current === 1 /* Entered */) { if (state.current === 0 /* Entering */) { onEnterCancelled?.(); } state.current = 2 /* Leaving */; setRealEnter(false); } } } }, [isEnter]); return mounted ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)( TransitionInner, { isEnter: realEnter, onAfterLeave: (el) => { state.current = 3 /* Leaved */; onAfterLeave?.(el); if (destroyAfterLeave) { setMounted(false); } }, onAfterEnter: (el) => { state.current = 1 /* Entered */; onAfterEnter?.(el); }, ...restProps, onMounted: (el) => { if (isEnter) { state.current = 0 /* Entering */; setRealEnter(true); } if (typeof ref === "function") { ref(el); } else if (ref) { ref.current = el; } } } ) : null; } ); // src/transition-group.tsx var import_react2 = require("react"); function k(children, onLeave, appear = false) { return children.map((node) => { const key = node.key; if (!key) throw new Error('child of must has "key"'); const item = (0, import_react2.cloneElement)(node, { key, isEnter: true, appear, onAfterLeave() { onLeave(key); } }); return { key, node: item, isEnter: true }; }); } var TransitionGroup = ({ children, appear }) => { const initList = (0, import_react2.useMemo)(() => appear ? [] : k(children, onLeave, false), []); const refList = (0, import_react2.useRef)(initList); const [renderList, setRenderList] = (0, import_react2.useState)(initList); function onLeave(key) { const idx = refList.current.findIndex((item) => item.key === key); if (idx >= 0) { refList.current.splice(idx, 1); setRenderList(refList.current.slice()); } else { console.warn("something strange wrong"); } } const first = (0, import_react2.useRef)(true); (0, import_react2.useLayoutEffect)(() => { if (first.current) { first.current = false; if (!appear) { return; } } const newItems = k(children, onLeave, true); const newKeys = /* @__PURE__ */ new Map(); newItems.forEach((it, i) => newKeys.set(it.key.toString(), i)); const oldKeys = /* @__PURE__ */ new Set(); refList.current.forEach((old) => { oldKeys.add(old.key); const newIdx = newKeys.get(old.key); if (newIdx === void 0) { if (old.isEnter) { old.isEnter = false; old.node = (0, import_react2.cloneElement)(old.node, { isEnter: false }); } } else { old.isEnter = true; old.node = (0, import_react2.cloneElement)(newItems[newIdx].node, { isEnter: true }); } }); newItems.forEach((item) => { if (!oldKeys.has(item.key)) { refList.current.push(item); } }); setRenderList(refList.current.slice()); }, [children]); return renderList.map((item) => item.node); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Transition, TransitionGroup }); //# sourceMappingURL=index.cjs.map