UNPKG

2.59 kBTypeScriptView Raw
1import * as React from 'react';
2import { ClassValue } from 'clsx';
3import { Simplify } from '@mui/types';
4import { EventHandlers } from './types';
5export type WithCommonProps<T> = T & {
6 className?: string;
7 style?: React.CSSProperties;
8 ref?: React.Ref<any>;
9};
10export interface MergeSlotPropsParameters<SlotProps, ExternalForwardedProps, ExternalSlotProps, AdditionalProps> {
11 /**
12 * A function that returns the internal props of the component.
13 * It accepts the event handlers passed into the component by the user
14 * and is responsible for calling them where appropriate.
15 */
16 getSlotProps?: (other: EventHandlers) => WithCommonProps<SlotProps>;
17 /**
18 * Props provided to the `slotProps.*` of the Base UI component.
19 */
20 externalSlotProps?: WithCommonProps<ExternalSlotProps>;
21 /**
22 * Extra props placed on the Base UI component that should be forwarded to the slot.
23 * This should usually be used only for the root slot.
24 */
25 externalForwardedProps?: WithCommonProps<ExternalForwardedProps>;
26 /**
27 * Additional props to be placed on the slot.
28 */
29 additionalProps?: WithCommonProps<AdditionalProps>;
30 /**
31 * Extra class name(s) to be placed on the slot.
32 */
33 className?: ClassValue | ClassValue[];
34}
35export type MergeSlotPropsResult<SlotProps, ExternalForwardedProps, ExternalSlotProps, AdditionalProps> = {
36 props: Simplify<SlotProps & ExternalForwardedProps & ExternalSlotProps & AdditionalProps & {
37 className?: string;
38 style?: React.CSSProperties;
39 }>;
40 internalRef: React.Ref<any> | undefined;
41};
42/**
43 * Merges the slot component internal props (usually coming from a hook)
44 * with the externally provided ones.
45 *
46 * The merge order is (the latter overrides the former):
47 * 1. The internal props (specified as a getter function to work with get*Props hook result)
48 * 2. Additional props (specified internally on a Base UI component)
49 * 3. External props specified on the owner component. These should only be used on a root slot.
50 * 4. External props specified in the `slotProps.*` prop.
51 * 5. The `className` prop - combined from all the above.
52 * @param parameters
53 * @returns
54 */
55export default function mergeSlotProps<SlotProps, ExternalForwardedProps extends Record<string, unknown>, ExternalSlotProps extends Record<string, unknown>, AdditionalProps>(parameters: MergeSlotPropsParameters<SlotProps, ExternalForwardedProps, ExternalSlotProps, AdditionalProps>): MergeSlotPropsResult<SlotProps, ExternalForwardedProps, ExternalSlotProps, AdditionalProps>;