@onesy/ui-react
Version:
UI for React
243 lines (242 loc) • 8.71 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
const _excluded = ["children", "style"],
_excluded2 = ["ref", "in", "value", "prefix", "run", "append", "add", "enter", "exit", "enterOnAdd", "exitOnAdd", "noAbruption", "removeOnExited", "delay", "duration", "timing_function", "addTransition", "onTransition", "onAppended", "onAdd", "onAdding", "onAdded", "onEnter", "onEntering", "onEntered", "onExit", "onExiting", "onExited", "onRemoved", "expandSize", "orientation", "WrapperProps", "className", "style", "children"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { c as _c } from "react/compiler-runtime";
import React from 'react';
import { is, isEnvironment } from '@onesy/utils';
import { useOnesyTheme } from '@onesy/style-react';
import { Transition } from '..';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const Wrapper = props => {
const $ = _c(10);
let children;
let other;
let style;
if ($[0] !== props) {
var _props = props;
({
children,
style
} = _props);
other = _objectWithoutProperties(_props, _excluded);
_props;
$[0] = props;
$[1] = children;
$[2] = other;
$[3] = style;
} else {
children = $[1];
other = $[2];
style = $[3];
}
let t0;
if ($[4] !== style) {
t0 = _objectSpread({
width: "100%"
}, style);
$[4] = style;
$[5] = t0;
} else {
t0 = $[5];
}
let t1;
if ($[6] !== children || $[7] !== other || $[8] !== t0) {
t1 = /*#__PURE__*/_jsx("div", _objectSpread(_objectSpread({}, other), {}, {
style: t0,
children: children
}));
$[6] = children;
$[7] = other;
$[8] = t0;
$[9] = t1;
} else {
t1 = $[9];
}
return t1;
};
const Expand = props_ => {
const theme = useOnesyTheme();
const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyExpand?.props?.default), props_);
const {
ref,
in: inProp,
value: valueProvided,
prefix,
run,
append,
add,
enter,
exit,
enterOnAdd,
exitOnAdd,
noAbruption,
removeOnExited = true,
delay,
duration: duration_,
timing_function,
addTransition,
onTransition,
onAppended,
onAdd,
onAdding,
onAdded,
onEnter,
onEntering,
onEntered,
onExit,
onExiting,
onExited,
onRemoved,
expandSize,
orientation,
WrapperProps,
className,
style,
children
} = props,
other = _objectWithoutProperties(props, _excluded2);
const [value, setValue] = React.useState(valueProvided !== undefined ? valueProvided : null);
const [parent, setParent] = React.useState();
const refs = {
root: React.useRef(undefined),
placeholder: React.useRef(undefined),
parent: React.useRef(undefined),
element: React.useRef(undefined),
value: React.useRef(0),
previousWidth: React.useRef(isEnvironment('browser') && window.innerWidth)
};
refs.value.current = value;
refs.parent.current = parent;
let prop = 'height';
if (orientation === 'horizontal') prop = 'width';
const isTransition = item => {
const values = ['Transition', 'Fade', 'Grow', 'Slide', 'Zoom'];
return values.some(item_ => item?.includes(item_));
};
const childrenWithTransition = isTransition(children?.type?.displayName);
React.useEffect(() => {
const method = () => {
const newWidth = window.innerWidth;
if (refs.previousWidth.current === newWidth) return;
setValue(null);
setParent(null);
refs.previousWidth.current = newWidth;
};
// on resize
// recalculate width, value
window.addEventListener('resize', method);
return () => {
window.removeEventListener('resize', method);
};
}, []);
const styles = status => {
const styles_ = refs.root.current && window.getComputedStyle(refs.root?.current) || {};
const allStyles = {
appended: {
position: 'fixed',
left: '1114%',
visibility: 'hidden'
},
enter: {
transition: 'none',
[prop]: 0,
overflow: 'hidden'
},
entering: {
[prop]: `${valueProvided !== undefined ? valueProvided : refs.value.current}px`,
overflow: 'hidden'
},
entered: {
[prop]: 'auto'
},
exit: {
[prop]: styles_[prop],
overflow: 'hidden'
},
exiting: {
[prop]: expandSize !== undefined ? expandSize : '0',
overflow: 'hidden'
},
exited: {
[prop]: expandSize !== undefined ? expandSize : '0',
overflow: 'hidden'
}
};
return allStyles[status];
};
const duration = (status_0, property = 'opacity') => {
const properties = {
[prop]: theme.transitions.duration.rg
};
return `${(is('simple', duration) ? duration : duration[status_0]) || properties[property]}ms`;
};
const timingFunction = status_1 => (is('simple', timing_function) ? timing_function : timing_function[status_1]) || theme.transitions.timing_function.standard;
const children_ = /*#__PURE__*/_jsx(Wrapper, _objectSpread(_objectSpread({}, WrapperProps), {}, {
children: children
}));
return /*#__PURE__*/_jsxs(_Fragment, {
children: [value === null && /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/_jsx("div", {
ref: item_0 => {
if (!item_0) return;
if (!parent) setParent(item_0.parentElement);
}
}), parent && /*#__PURE__*/_jsx(Wrapper, _objectSpread(_objectSpread({}, WrapperProps), {}, {
ref: item_1 => {
if (!item_1) return;
if (WrapperProps?.ref) {
if (is('function', WrapperProps.ref)) WrapperProps?.ref(item_1);else WrapperProps.ref.current = item_1;
}
if (refs.value.current === null) {
setValue(item_1.getBoundingClientRect()[prop] || 0);
}
},
style: _objectSpread({
position: 'fixed',
left: '1114%',
visibility: 'hidden',
width: parent.offsetWidth
}, WrapperProps?.style),
children: /*#__PURE__*/React.cloneElement(children, _objectSpread({}, childrenWithTransition && {
in: true
}))
}))]
}), value !== null && /*#__PURE__*/_jsx(Transition, _objectSpread(_objectSpread({
removeOnExited: true
}, props), {}, {
children: (status_2, ref_) => {
// If children update
// & value is updated
if (['appending', 'appended', 'adding', 'added', 'entering', 'entered'].includes(status_2)) {
const value_ = (refs.root.current?.getBoundingClientRect() || {})[prop];
if (value_ > 0 && value_ !== refs.value.current) setValue(value_);
}
return /*#__PURE__*/React.cloneElement(children_, _objectSpread(_objectSpread({}, other), {}, {
className,
ref: item_2 => {
refs.root.current = item_2;
if (ref) {
if (is('function', ref)) ref(item_2);else ref.current = item_2;
}
if (ref_) {
if (is('function', ref_)) ref_(item_2);else ref_.current = item_2;
}
if (children_.ref) {
if (is('function', children_.ref)) children_.ref(item_2);else children_.ref.current = item_2;
}
},
style: _objectSpread(_objectSpread(_objectSpread({
position: 'relative',
transition: `${prop} ${duration(status_2, prop)} ${timingFunction(status_2)} ${addTransition ? `, ${addTransition}` : ''}`,
visibility: status_2 === 'exited' && !inProp && expandSize === undefined ? 'hidden' : undefined
}, styles(status_2)), style), children_?.style)
}));
}
}))]
});
};
Expand.displayName = 'onesy-Expand';
export default Expand;