-
Notifications
You must be signed in to change notification settings - Fork 98
/
WindowMockup.tsx
90 lines (75 loc) 路 2.52 KB
/
WindowMockup.tsx
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { forwardRef } from 'react'
import clsx from 'clsx'
import { twMerge } from 'tailwind-merge'
import { IComponentBaseProps } from '../types'
import { bgColors, brandColors, componentStatuses } from '../constants'
export const windowMockupColors = [
...bgColors,
...brandColors,
...componentStatuses,
] as const
type WindowMockupColors = typeof windowMockupColors[number]
export type WindowMockupProps = React.HTMLAttributes<HTMLDivElement> &
IComponentBaseProps & {
frameColor?: WindowMockupColors
backgroundColor?: WindowMockupColors
border?: boolean
borderColor?: WindowMockupColors
}
const WindowMockup = forwardRef<HTMLDivElement, WindowMockupProps>(
(
{
border,
borderColor,
backgroundColor,
frameColor,
dataTheme,
className,
children,
...props
},
ref
): JSX.Element => {
// Set border color to framecolor or 'bg-base-300', if border color is not defined
const borderColorValue = borderColor
? borderColor
: frameColor || 'base-300'
const classes = twMerge(
'mockup-window',
border && `border border-${borderColorValue}`,
clsx({
[`border-${borderColorValue}`]: borderColorValue,
[`bg-${frameColor}`]: frameColor,
}),
className
)
console.log('classes', classes)
// If border is true, then we need to add the border-t and padding classes to the children
// if more than one child is passed in, or the single child is not a valid element, then we need to wrap the child/children in a div
const numChildren = React.Children.count(children)
const firstChild = numChildren > 0 && React.Children.toArray(children)[0]
// List of classes that child element will have
const innerClasses = clsx(
backgroundColor && `bg-${backgroundColor}`,
border && `border-t border-${borderColorValue}`,
'p-4'
)
console.log('innerClasses', innerClasses)
// Add the innerClasses to the child element, merging classNames if existing, or wrapping with div and adding innerClasses
const innerEl =
firstChild && React.isValidElement(firstChild) ? (
React.cloneElement(firstChild, {
className: twMerge(innerClasses, firstChild.props.className),
})
) : (
<div className={innerClasses}>{children}</div>
)
return (
<div {...props} data-theme={dataTheme} className={classes} ref={ref}>
{innerEl}
</div>
)
}
)
WindowMockup.displayName = 'WindowMockup'
export default WindowMockup