@amaui/ui-react
Version:
UI for React
109 lines (108 loc) • 5.28 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const react_router_dom_1 = require("react-router-dom");
const utils_1 = require("@amaui/utils");
const style_react_1 = require("@amaui/style-react");
const Line_1 = __importDefault(require("../Line"));
const utils_2 = require("../utils");
const useStyle = (0, style_react_1.style)(theme => ({
'@keyframes fadeIn': {
from: {
opacity: 0
},
to: {
opacity: 1
}
},
'@keyframes fadeOut': {
from: {
opacity: 1
},
to: {
opacity: 0
}
},
root: {},
in: {
animation: '$fadeIn 0.24s forwards'
},
out: {
animation: '$fadeOut 0.24s forwards'
}
}), { name: 'amaui-PageTransition' });
const PageTransition = react_1.default.forwardRef((props_, ref) => {
const theme = (0, style_react_1.useAmauiTheme)();
const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiPageTransition) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]);
const { add, classNames: classNames_, duration = 254, Component = 'div', className, children } = props, other = __rest(props, ["add", "classNames", "duration", "Component", "className", "children"]);
const { classes } = useStyle();
const location = (0, react_router_dom_1.useLocation)();
const [locationUsed, setLocationUsed] = react_1.default.useState(location);
const [transition, setTransition] = react_1.default.useState(add ? 'in' : undefined);
const refs = {
location: react_1.default.useRef(location),
transition: react_1.default.useRef(transition),
inProgress: react_1.default.useRef(false),
timeout: react_1.default.useRef(undefined),
duration: react_1.default.useRef(duration)
};
refs.location.current = location;
refs.transition.current = transition;
refs.duration.current = duration;
const classNamesToUse = {
in: (classNames_ === null || classNames_ === void 0 ? void 0 : classNames_.in) || classes.in,
out: (classNames_ === null || classNames_ === void 0 ? void 0 : classNames_.out) || classes.out
};
const hashLocation = react_1.default.useCallback((value) => {
return (0, utils_1.hash)([
value.hash,
value.pathname,
value.search
]);
}, []);
const onUpdate = react_1.default.useCallback(async () => {
if (refs.inProgress.current)
return;
if (hashLocation(location) !== hashLocation(locationUsed)) {
setTransition('out');
clearTimeout(refs.timeout.current);
refs.timeout.current = setTimeout(() => {
setLocationUsed(refs.location.current);
setTransition('in');
refs.inProgress.current = false;
}, refs.duration.current);
}
}, [location, locationUsed]);
react_1.default.useEffect(() => {
onUpdate();
}, [location, locationUsed]);
return ((0, jsx_runtime_1.jsx)(Line, Object.assign({ ref: ref, direction: 'column', align: 'unset', justify: 'unset', flex: true, fullWidth: true, Component: Component, className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('PageTransition', theme) && [
`amaui-PageTransition-root`,
`amaui-PageTransition-transition-${transition}`
],
className,
classes.root,
classNamesToUse[transition]
]) }, other, { children: react_1.default.cloneElement(children, {
location: locationUsed
}) })));
});
PageTransition.displayName = 'amaui-PageTransition';
exports.default = PageTransition;
;