index.mjs 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. "use client";
  2. import { jsx } from 'react/jsx-runtime';
  3. import { useContext, useRef, useMemo } from 'react';
  4. import { LayoutGroupContext } from '../../context/LayoutGroupContext.mjs';
  5. import { DeprecatedLayoutGroupContext } from '../../context/DeprecatedLayoutGroupContext.mjs';
  6. import { useForceUpdate } from '../../utils/use-force-update.mjs';
  7. import { nodeGroup } from '../../projection/node/group.mjs';
  8. const shouldInheritGroup = (inherit) => inherit === true;
  9. const shouldInheritId = (inherit) => shouldInheritGroup(inherit === true) || inherit === "id";
  10. const LayoutGroup = ({ children, id, inherit = true }) => {
  11. const layoutGroupContext = useContext(LayoutGroupContext);
  12. const deprecatedLayoutGroupContext = useContext(DeprecatedLayoutGroupContext);
  13. const [forceRender, key] = useForceUpdate();
  14. const context = useRef(null);
  15. const upstreamId = layoutGroupContext.id || deprecatedLayoutGroupContext;
  16. if (context.current === null) {
  17. if (shouldInheritId(inherit) && upstreamId) {
  18. id = id ? upstreamId + "-" + id : upstreamId;
  19. }
  20. context.current = {
  21. id,
  22. group: shouldInheritGroup(inherit)
  23. ? layoutGroupContext.group || nodeGroup()
  24. : nodeGroup(),
  25. };
  26. }
  27. const memoizedContext = useMemo(() => ({ ...context.current, forceRender }), [key]);
  28. return (jsx(LayoutGroupContext.Provider, { value: memoizedContext, children: children }));
  29. };
  30. export { LayoutGroup };