-
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathBox.tsx
More file actions
62 lines (54 loc) · 1.88 KB
/
Box.tsx
File metadata and controls
62 lines (54 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type {ElementType} from '@solid-aria/types';
import clsx from 'clsx';
import type {JSX, JSXElement, ParentProps, Ref} from 'solid-js';
import {omitProps, pickProps} from 'solid-use/props';
import type {Sprinkles} from '../../theme';
import {sprinkles} from '../../theme';
import {styled} from '../../utils';
import {boxBase} from './Box.css';
type UnboxIntrinsicElements<T> =
T extends JSX.HTMLAttributes<infer U> ? U : never;
type UnboxComponentProp<U> = U extends {ref: infer X} ? X : never;
export type DynamicNode<T extends ValidConstructor> = T extends ValidElements
? UnboxIntrinsicElements<JSX.IntrinsicElements[T]>
: T extends ValidComponent<infer U>
? UnboxComponentProp<U>
: never;
export type ValidElements = keyof JSX.IntrinsicElements;
export type ValidComponent<P> = (props: P) => JSX.Element;
export type ValidConstructor =
| ValidElements
// oxlint-disable-next-line typescript/no-explicit-any
| ValidComponent<any>
| (string & {});
export type DynamicProps<T extends ValidConstructor> = T extends ValidElements
? JSX.IntrinsicElements[T]
: T extends ValidComponent<infer U>
? U
: Record<string, unknown>;
type BoxParameters = {
// Fallback to support both index number and string (ex. 2 | "2")
[key in keyof Sprinkles]: Sprinkles[key] | `${Sprinkles[key] & number}`;
};
export type BoxProps<T extends ElementType = 'div'> = Partial<BoxParameters> & {
as?: T;
ref?: Ref<DynamicNode<T>>;
} & Omit<DynamicProps<T>, 'as' | 'disabled' | 'ref'>;
export function Box<T extends ElementType = 'div'>(
props: ParentProps<BoxProps<T>>,
): JSXElement {
return (
<styled.div
{...omitProps(props, ['as', 'ref'])}
as={props.as ?? 'div'}
ref={props.ref}
class={clsx(
boxBase,
props.class,
sprinkles(pickProps(props, [...sprinkles.properties.keys()])),
)}
>
{props.children}
</styled.div>
);
}