UNPKG

seti-ramesesv1

Version:

Reusable components and context for Next.js apps

83 lines (79 loc) 2.83 kB
import resolveComponentProps from '../../../utils/esm/resolveComponentProps/resolveComponentProps.js'; import mergeSlotProps from '../../../utils/esm/mergeSlotProps/mergeSlotProps.js'; import useForkRef from '../../../utils/esm/useForkRef/useForkRef.js'; import appendOwnerState from '../../../utils/esm/appendOwnerState/appendOwnerState.js'; /** * An internal function to create a Material UI slot. * * This is an advanced version of Base UI `useSlotProps` because Material UI allows leaf component to be customized via `component` prop * while Base UI does not need to support leaf component customization. * * @param {string} name: name of the slot * @param {object} parameters * @returns {[Slot, slotProps]} The slot's React component and the slot's props * * Note: the returned slot's props * - will never contain `component` prop. * - might contain `as` prop. */ function useSlot( /** * The slot's name. All Material UI components should have `root` slot. * * If the name is `root`, the logic behaves differently from other slots, * e.g. the `externalForwardedProps` are spread to `root` slot but not other slots. */ name, parameters) { const { className, elementType: initialElementType, ownerState, externalForwardedProps, internalForwardedProps, shouldForwardComponentProp = false, ...useSlotPropsParams } = parameters; const { component: rootComponent, slots = { [name]: undefined }, slotProps = { [name]: undefined }, ...other } = externalForwardedProps; const elementType = slots[name] || initialElementType; // `slotProps[name]` can be a callback that receives the component's ownerState. // `resolvedComponentsProps` is always a plain object. const resolvedComponentsProps = resolveComponentProps(slotProps[name], ownerState); const { props: { component: slotComponent, ...mergedProps }, internalRef } = mergeSlotProps({ className, ...useSlotPropsParams, externalForwardedProps: name === 'root' ? other : undefined, externalSlotProps: resolvedComponentsProps }); const ref = useForkRef(internalRef, resolvedComponentsProps?.ref, parameters.ref); const LeafComponent = name === 'root' ? slotComponent || rootComponent : slotComponent; const props = appendOwnerState(elementType, { ...(name === 'root' && !rootComponent && !slots[name] && internalForwardedProps), ...(name !== 'root' && !slots[name] && internalForwardedProps), ...mergedProps, ...(LeafComponent && !shouldForwardComponentProp && { as: LeafComponent }), ...(LeafComponent && shouldForwardComponentProp && { component: LeafComponent }), ref }, ownerState); return [elementType, props]; } export { useSlot as default }; //# sourceMappingURL=useSlot.js.map