UNPKG

3.44 kBTypeScriptView Raw
1import * as React from 'react';
2import { ClassValue } from 'clsx';
3export type WithCommonProps<T> = T & {
4 className?: string;
5 style?: React.CSSProperties;
6 ref?: React.Ref<any>;
7};
8type EventHandlers = Record<string, React.EventHandler<any>>;
9type ExtractComponentProps<P> = P extends infer T | ((ownerState: any) => infer T) ? T : never;
10/**
11 * An internal function to create a Material UI slot.
12 *
13 * This is an advanced version of Base UI `useSlotProps` because Material UI allows leaf component to be customized via `component` prop
14 * while Base UI does not need to support leaf component customization.
15 *
16 * @param {string} name: name of the slot
17 * @param {object} parameters
18 * @returns {[Slot, slotProps]} The slot's React component and the slot's props
19 *
20 * Note: the returned slot's props
21 * - will never contain `component` prop.
22 * - might contain `as` prop.
23 */
24export default function useSlot<T extends string, ElementType extends React.ElementType, SlotProps, OwnerState extends {}, ExternalSlotProps extends {
25 component?: React.ElementType;
26 ref?: React.Ref<any>;
27}, ExternalForwardedProps extends {
28 component?: React.ElementType;
29 slots?: {
30 [k in T]?: React.ElementType;
31 };
32 slotProps?: {
33 [k in T]?: ExternalSlotProps | ((ownerState: OwnerState) => ExternalSlotProps);
34 };
35}, AdditionalProps, SlotOwnerState extends {}>(
36/**
37 * The slot's name. All Material UI components should have `root` slot.
38 *
39 * If the name is `root`, the logic behaves differently from other slots,
40 * e.g. the `externalForwardedProps` are spread to `root` slot but not other slots.
41 */
42name: T, parameters: (T extends 'root' ? {
43 ref: React.ForwardedRef<any>;
44} : {
45 ref?: React.ForwardedRef<any>;
46}) & {
47 /**
48 * The slot's className
49 */
50 className: ClassValue | ClassValue[];
51 /**
52 * The slot's default styled-component
53 */
54 elementType: ElementType;
55 /**
56 * The component's ownerState
57 */
58 ownerState: OwnerState;
59 /**
60 * The `other` props from the consumer. It has to contain `component`, `slots`, and `slotProps`.
61 * The function will use those props to calculate the final rendered element and the returned props.
62 *
63 * If the slot is not `root`, the rest of the `externalForwardedProps` are neglected.
64 */
65 externalForwardedProps: ExternalForwardedProps;
66 getSlotProps?: (other: EventHandlers) => WithCommonProps<SlotProps>;
67 additionalProps?: WithCommonProps<AdditionalProps>;
68 /**
69 * For overriding the component's ownerState for the slot.
70 * This is required for some components that need styling via `ownerState`.
71 *
72 * It is a function because `slotProps.{slot}` can be a function which has to be resolved first.
73 */
74 getSlotOwnerState?: (mergedProps: AdditionalProps & SlotProps & ExternalSlotProps & ExtractComponentProps<Exclude<Exclude<ExternalForwardedProps['slotProps'], undefined>[T], undefined>>) => SlotOwnerState;
75 /**
76 * props forward to `T` only if the `slotProps.*.component` is not provided.
77 * e.g. Autocomplete's listbox uses Popper + StyledComponent
78 */
79 internalForwardedProps?: any;
80}): [ElementType, {
81 className: string;
82 ownerState: OwnerState & SlotOwnerState;
83} & AdditionalProps & SlotProps & ExternalSlotProps & ExtractComponentProps<Exclude<Exclude<ExternalForwardedProps["slotProps"], undefined>[T], undefined>>];
84export {};
85
\No newline at end of file