pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+2
@@ -0,0 +1,2 @@
|
||||
export * from './useCompoundParent';
|
||||
export * from './useCompoundItem';
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
'use client';
|
||||
|
||||
export * from './useCompoundParent';
|
||||
export * from './useCompoundItem';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/useCompound/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import { KeyGenerator } from './useCompoundParent';
|
||||
export interface UseCompoundItemReturnValue<Key> {
|
||||
/**
|
||||
* The unique key for the child component.
|
||||
* If the id was provided to `useCompoundItem`, this will be the same value.
|
||||
* Otherwise, this will be a value generated by the `id` function.
|
||||
*/
|
||||
id: Key | undefined;
|
||||
/**
|
||||
* The 0-based index of the child component in the parent component's list of registered children.
|
||||
*/
|
||||
index: number;
|
||||
/**
|
||||
* The total number of child components registered with the parent component.
|
||||
* This value will be correct after the effect phase of the component (as children are registered with the parent during the effect phase).
|
||||
*/
|
||||
totalItemCount: number;
|
||||
}
|
||||
/**
|
||||
* Registers a child component with the parent component.
|
||||
*
|
||||
* @param id A unique key for the child component. If the `id` is `undefined`, the registration logic will not run (this can sometimes be the case during SSR).
|
||||
* This can be either a value, or a function that generates a value based on already registered siblings' ids.
|
||||
* If a function, it's called with the set of the ids of all the items that have already been registered.
|
||||
* Return `existingKeys.size` if you want to use the index of the new item as the id.
|
||||
* @param itemMetadata Arbitrary metadata to pass to the parent component. This should be a stable reference (for example a memoized object), to avoid unnecessary re-registrations.
|
||||
*
|
||||
* @ignore - internal hook.
|
||||
*/
|
||||
export declare function useCompoundItem<Key, Subitem>(id: Key | KeyGenerator<Key>, itemMetadata: Subitem): UseCompoundItemReturnValue<Key>;
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
|
||||
import { CompoundComponentContext } from './useCompoundParent';
|
||||
/**
|
||||
* Registers a child component with the parent component.
|
||||
*
|
||||
* @param id A unique key for the child component. If the `id` is `undefined`, the registration logic will not run (this can sometimes be the case during SSR).
|
||||
* This can be either a value, or a function that generates a value based on already registered siblings' ids.
|
||||
* If a function, it's called with the set of the ids of all the items that have already been registered.
|
||||
* Return `existingKeys.size` if you want to use the index of the new item as the id.
|
||||
* @param itemMetadata Arbitrary metadata to pass to the parent component. This should be a stable reference (for example a memoized object), to avoid unnecessary re-registrations.
|
||||
*
|
||||
* @ignore - internal hook.
|
||||
*/
|
||||
export function useCompoundItem(id, itemMetadata) {
|
||||
const context = React.useContext(CompoundComponentContext);
|
||||
if (context === null) {
|
||||
throw new Error('useCompoundItem must be used within a useCompoundParent');
|
||||
}
|
||||
const {
|
||||
registerItem
|
||||
} = context;
|
||||
const [registeredId, setRegisteredId] = React.useState(typeof id === 'function' ? undefined : id);
|
||||
useEnhancedEffect(() => {
|
||||
const {
|
||||
id: returnedId,
|
||||
deregister
|
||||
} = registerItem(id, itemMetadata);
|
||||
setRegisteredId(returnedId);
|
||||
return deregister;
|
||||
}, [registerItem, itemMetadata, id]);
|
||||
return {
|
||||
id: registeredId,
|
||||
index: registeredId !== undefined ? context.getItemIndex(registeredId) : -1,
|
||||
totalItemCount: context.totalSubitemCount
|
||||
};
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
import * as React from 'react';
|
||||
interface RegisterItemReturnValue<Key> {
|
||||
/**
|
||||
* The id of the item.
|
||||
*/
|
||||
id: Key;
|
||||
/**
|
||||
* A function that deregisters the item.
|
||||
*/
|
||||
deregister: () => void;
|
||||
}
|
||||
export type KeyGenerator<Key> = (existingKeys: Set<Key>) => Key;
|
||||
export type CompoundComponentContextValue<Key, Subitem> = {
|
||||
/**
|
||||
* Registers an item with the parent.
|
||||
* This should be called during the effect phase of the child component.
|
||||
* The `itemMetadata` should be a stable reference (for example a memoized object), to avoid unnecessary re-registrations.
|
||||
*
|
||||
* @param id Id of the item or A function that generates a unique id for the item.
|
||||
* It is called with the set of the ids of all the items that have already been registered.
|
||||
* Return `existingKeys.size` if you want to use the index of the new item as the id..
|
||||
* @param itemMetadata Arbitrary metadata to pass to the parent component.
|
||||
*/
|
||||
registerItem: (id: Key | KeyGenerator<Key>, item: Subitem) => RegisterItemReturnValue<Key>;
|
||||
/**
|
||||
* Returns the 0-based index of the item in the parent component's list of registered items.
|
||||
*
|
||||
* @param id id of the item.
|
||||
*/
|
||||
getItemIndex: (id: Key) => number;
|
||||
/**
|
||||
* The total number of items registered with the parent.
|
||||
*/
|
||||
totalSubitemCount: number;
|
||||
};
|
||||
export declare const CompoundComponentContext: React.Context<CompoundComponentContextValue<any, any> | null>;
|
||||
export interface UseCompoundParentReturnValue<Key, Subitem extends {
|
||||
ref: React.RefObject<Node>;
|
||||
}> {
|
||||
/**
|
||||
* The value for the CompoundComponentContext provider.
|
||||
*/
|
||||
contextValue: CompoundComponentContextValue<Key, Subitem>;
|
||||
/**
|
||||
* The subitems registered with the parent.
|
||||
* The key is the id of the subitem, and the value is the metadata passed to the `useCompoundItem` hook.
|
||||
* The order of the items is the same as the order in which they were registered.
|
||||
*/
|
||||
subitems: Map<Key, 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.
|
||||
*/
|
||||
export declare function useCompoundParent<Key, Subitem extends {
|
||||
ref: React.RefObject<Node>;
|
||||
}>(): UseCompoundParentReturnValue<Key, Subitem>;
|
||||
export {};
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
export const 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.
|
||||
*/
|
||||
export 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