Skip to content

Commit a9c0442

Browse files
committed
refactor(utils): split type utils into modules
1 parent 4a73b4a commit a9c0442

21 files changed

+183
-164
lines changed

packages/lumx-react/src/utils/type.ts

Lines changed: 0 additions & 164 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Callback function type alias (use for readability)
3+
*/
4+
export type Callback = () => void;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { ReactElement, Ref } from 'react';
2+
3+
/** LumX Component Type. */
4+
export type Comp<P, T = HTMLElement> = {
5+
(props: P & { ref?: Ref<T> }): ReactElement | null;
6+
/** React component type. */
7+
readonly $$typeof: symbol;
8+
/** Component default props. */
9+
defaultProps?: Partial<P>;
10+
/** Component name. */
11+
displayName?: string;
12+
/** Component base class name. */
13+
className?: string;
14+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type React from 'react';
2+
import type { Comp } from './Comp';
3+
4+
/**
5+
* Extract ref type for a component or JSX intrinsic element
6+
*
7+
* @example ComponentRef<'div'> => React.Ref<HTMLDivElement>
8+
* @example ComponentRef<Button> => React.Ref<HTMLButtonElement
9+
*/
10+
export type ComponentRef<C> = C extends keyof JSX.IntrinsicElements
11+
? JSX.IntrinsicElements[C]['ref']
12+
: C extends Comp<any, infer T>
13+
? React.Ref<T>
14+
: C extends React.JSXElementConstructor<{ ref?: infer R }>
15+
? R
16+
: never;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* JS falsy values.
3+
* (excluding `NaN` as it can't be distinguished from `number`)
4+
*/
5+
export type Falsy = false | undefined | null | 0 | '';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { HasClassName } from './HasClassName';
2+
3+
/**
4+
* Define a generic props types.
5+
*/
6+
export interface GenericProps extends HasClassName {
7+
/**
8+
* Any prop (particularly any supported prop for a HTML element).
9+
*/
10+
[propName: string]: any;
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Require either `aria-label` or `arial-labelledby` prop.
3+
* If none are set, the order will prioritize `aria-labelledby` over `aria-label` as it
4+
* needs a visible element.
5+
*/
6+
export type HasAriaLabelOrLabelledBy<T = string | undefined> = T extends string
7+
? {
8+
/**
9+
* The id of the element to use as title of the dialog. Can be within or out of the dialog.
10+
* Although it is not recommended, aria-label can be used instead if no visible element is available.
11+
*/
12+
'aria-labelledby': T;
13+
/** The label of the dialog. */
14+
'aria-label'?: undefined;
15+
}
16+
: {
17+
'aria-label': string;
18+
'aria-labelledby'?: undefined;
19+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface HasClassName {
2+
/**
3+
* Class name forwarded to the root element of the component.
4+
*/
5+
className?: string;
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface HasCloseMode {
2+
/**
3+
* Choose how the children are hidden when closed
4+
* ('hide' keeps the children in DOM but hide them, 'unmount' remove the children from the DOM).
5+
*/
6+
closeMode?: 'hide' | 'unmount';
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
3+
export type HasPolymorphicAs<E extends React.ElementType> = React.ComponentPropsWithoutRef<E> & {
4+
/**
5+
* Customize the rendered component.
6+
*/
7+
as?: E;
8+
};

0 commit comments

Comments
 (0)