pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+94
@@ -0,0 +1,94 @@
|
||||
"use strict";
|
||||
'use client';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.CompoundComponentContext = void 0;
|
||||
exports.useCompoundParent = useCompoundParent;
|
||||
var React = _interopRequireWildcard(require("react"));
|
||||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||||
const CompoundComponentContext = exports.CompoundComponentContext = /*#__PURE__*/React.createContext(null);
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
CompoundComponentContext.displayName = 'CompoundComponentContext';
|
||||
}
|
||||
/**
|
||||
* Sorts the subitems by their position in the DOM.
|
||||
*/
|
||||
function sortSubitems(subitems) {
|
||||
const subitemsArray = Array.from(subitems.keys()).map(key => {
|
||||
const subitem = subitems.get(key);
|
||||
return {
|
||||
key,
|
||||
subitem
|
||||
};
|
||||
});
|
||||
subitemsArray.sort((a, b) => {
|
||||
const aNode = a.subitem.ref.current;
|
||||
const bNode = b.subitem.ref.current;
|
||||
if (aNode === null || bNode === null || aNode === bNode) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-bitwise
|
||||
return aNode.compareDocumentPosition(bNode) & Node.DOCUMENT_POSITION_PRECEDING ? 1 : -1;
|
||||
});
|
||||
return new Map(subitemsArray.map(item => [item.key, item.subitem]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a way for a component to know about its children.
|
||||
*
|
||||
* Child components register themselves with the `useCompoundItem` hook, passing in arbitrary metadata to the parent.
|
||||
*
|
||||
* This is a more powerful altervantive to `children` traversal, as child components don't have to be placed
|
||||
* directly inside the parent component. They can be anywhere in the tree (and even rendered by other components).
|
||||
*
|
||||
* The downside is that this doesn't work with SSR as it relies on the useEffect hook.
|
||||
*
|
||||
* @ignore - internal hook.
|
||||
*/
|
||||
function useCompoundParent() {
|
||||
const [subitems, setSubitems] = React.useState(new Map());
|
||||
const subitemKeys = React.useRef(new Set());
|
||||
const deregisterItem = React.useCallback(function deregisterItem(id) {
|
||||
subitemKeys.current.delete(id);
|
||||
setSubitems(previousState => {
|
||||
const newState = new Map(previousState);
|
||||
newState.delete(id);
|
||||
return newState;
|
||||
});
|
||||
}, []);
|
||||
const registerItem = React.useCallback(function registerItem(id, item) {
|
||||
let providedOrGeneratedId;
|
||||
if (typeof id === 'function') {
|
||||
providedOrGeneratedId = id(subitemKeys.current);
|
||||
} else {
|
||||
providedOrGeneratedId = id;
|
||||
}
|
||||
subitemKeys.current.add(providedOrGeneratedId);
|
||||
setSubitems(previousState => {
|
||||
const newState = new Map(previousState);
|
||||
newState.set(providedOrGeneratedId, item);
|
||||
return newState;
|
||||
});
|
||||
return {
|
||||
id: providedOrGeneratedId,
|
||||
deregister: () => deregisterItem(providedOrGeneratedId)
|
||||
};
|
||||
}, [deregisterItem]);
|
||||
const sortedSubitems = React.useMemo(() => sortSubitems(subitems), [subitems]);
|
||||
const getItemIndex = React.useCallback(function getItemIndex(id) {
|
||||
return Array.from(sortedSubitems.keys()).indexOf(id);
|
||||
}, [sortedSubitems]);
|
||||
const contextValue = React.useMemo(() => ({
|
||||
getItemIndex,
|
||||
registerItem,
|
||||
totalSubitemCount: subitems.size
|
||||
}), [getItemIndex, registerItem, subitems.size]);
|
||||
return {
|
||||
contextValue,
|
||||
subitems: sortedSubitems
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user