@engie-group/fluid-design-system-react
Version:
Fluid Design System React
62 lines (61 loc) • 3.39 kB
TypeScript
import type { ComponentPropsWithoutRef, ElementType, PropsWithChildren } from 'react';
export type WithHTMLAttributes<ComponentProps, Element extends ElementType> = HTMLAttributesWithoutComponentProps<Element, ComponentProps> & ComponentProps;
export type HTMLAttributesWithoutComponentProps<Element extends ElementType, ComponentProps> = Omit<ComponentPropsWithoutRef<Element>, keyof ComponentProps>;
/**
* Represents a component where its action element can be rendered as a child or as a default component.
*
* When `actionAsChild` is `false`, the default action component is used, and its properties can be passed trough the `action` property.
* When `actionAsChild` is truthy, the child is used as the action element. The `action` property does not exist, and the action properties are passed directly to the child.
*/
export type ActionAsChild<ActionDefaultProps = Record<string, unknown>> = ({
actionAsChild?: false;
} & {
action?: ActionDefaultProps;
}) | ({
actionAsChild: true;
} & PropsWithChildren);
/**
* Type guard to check if we're rendering the default component for the action. (`actionAsChild` is false)
* @param props - The props to check, they should be of type {@link ActionAsChild}.
* @returns whether or not we are rendering the default action component.
*/
export declare function isDefaultAction<ActionDefaultProps>(props: ActionAsChild<ActionDefaultProps>): props is {
actionAsChild?: false;
} & {
action: ActionDefaultProps;
};
/**
* Represents a component that can be rendered as passed child or as a default component.
*
* When `asChild` is `false`, the default component is used, and its properties can be passed directly to the host component.
* When `asChild` is truthy, the child is used as the component. The host component no longer accepts the default props, and the component properties are passed directly to the child.
*/
export type AsChild<DefaultProps = Record<string, unknown>> = ({
asChild?: false;
} & DefaultProps) | ({
asChild: true;
} & PropsWithChildren);
/**
* Type guard to check if we are using the default component (not asChild or actionAsChild).
* @param discriminant Used to discriminate between default and child rendering (mostly asChild props).
* @param props The rest of the destructured props intersection. All common props must be extracted from this object.
* @returns Wether or not we are using the default component.
*/
export declare function isDefaultComponent<T extends Record<string, any>>(discriminant: boolean | undefined, props: {} | T): props is T;
/**
* Distributive Omit type that omits keys K from each member of a union type T.
* For example, DistributiveOmit<{a: number; b: string} | {a: number; c: boolean}, 'a'> results in {b: string} | {c: boolean}.
*/
export type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
/**
* Distributive Pick type that picks keys K from each member of a union type T.
* For example, DistributivePick<{a: number; b: string} | {a: number; c: boolean}, 'a'> results in {a: number} | {a: number}.
*/
export type DistributivePick<T, K extends keyof T> = T extends any ? Pick<T, K> : never;
/**
* Prettify a type by removing intersections and unions where possible.
* This makes complex types easier to read in IDEs and error messages.
*/
export type Prettify<T> = {
[K in keyof T]: T[K];
} & {};