Newer
Older
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
'use client';
import React from 'react';
import { cn, withProps, withRef } from '@udecode/cn';
import { useElement } from '@udecode/plate-common/react';
import { useBlockSelected } from '@udecode/plate-selection/react';
import {
TableRowPlugin,
useTableCellElement,
useTableCellElementResizable,
useTableCellElementResizableState,
useTableCellElementState,
} from '@udecode/plate-table/react';
import { blockSelectionVariants } from './block-selection';
import { PlateElement } from './plate-element';
import { ResizeHandle } from './resizable';
export const TableCellElement = withRef<
typeof PlateElement,
{
hideBorder?: boolean;
isHeader?: boolean;
}
>(({ children, className, hideBorder, isHeader, style, ...props }, ref) => {
const { element } = props;
const rowElement = useElement(TableRowPlugin.key);
const isSelectingRow = useBlockSelected(rowElement.id as string);
const {
borders,
colIndex,
colSpan,
hovered,
hoveredLeft,
isSelectingCell,
readOnly,
rowIndex,
rowSize,
selected,
} = useTableCellElementState();
const { props: cellProps } = useTableCellElement({ element: props.element });
const resizableState = useTableCellElementResizableState({
colIndex,
colSpan,
rowIndex,
});
const { bottomProps, hiddenLeft, leftProps, rightProps } =
useTableCellElementResizable(resizableState);
return (
<PlateElement
ref={ref}
as={isHeader ? 'th' : 'td'}
className={cn(
className,
'h-full overflow-visible border-none bg-white p-0 dark:bg-zinc-950',
hideBorder && 'before:border-none',
element.background ? 'bg-[--cellBackground]' : 'bg-white dark:bg-zinc-950',
!hideBorder &&
cn(
isHeader && 'text-left [&_>_*]:m-0',
'before:size-full',
selected && 'before:z-10 before:bg-zinc-100 dark:before:bg-zinc-800',
'before:absolute before:box-border before:select-none before:content-[""]',
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
borders &&
cn(
borders.bottom?.size &&
`before:border-b before:border-b-border`,
borders.right?.size && `before:border-r before:border-r-border`,
borders.left?.size && `before:border-l before:border-l-border`,
borders.top?.size && `before:border-t before:border-t-border`
)
)
)}
{...cellProps}
{...props}
style={
{
'--cellBackground': element.background,
...style,
} as React.CSSProperties
}
>
<div
className='relative z-20 box-border h-full px-4 py-2'
style={{
minHeight: rowSize,
}}
>
{children}
</div>
{!isSelectingCell && (
<div
className='group absolute top-0 size-full select-none'
contentEditable={false}
suppressContentEditableWarning={true}
>
{!readOnly && (
<>
<ResizeHandle
{...rightProps}
className='-top-3 right-[-5px] w-[10px]'
/>
<ResizeHandle
{...bottomProps}
className='bottom-[-5px] h-[10px]'
/>
{!hiddenLeft && (
<ResizeHandle
{...leftProps}
className='-top-3 left-[-5px] w-[10px]'
/>
)}
{hovered && (
<div
className={cn(
'absolute -top-3 z-30 h-[calc(100%_+_12px)] w-1 bg-zinc-950 dark:bg-zinc-300',
'right-[-1.5px]'
)}
/>
)}
{hoveredLeft && (
<div
className={cn(
'absolute -top-3 z-30 h-[calc(100%_+_12px)] w-1 bg-zinc-950 dark:bg-zinc-300',
'left-[-1.5px]'
)}
/>
)}
</>
)}
</div>
)}
{isSelectingRow && (
<div className={blockSelectionVariants()} contentEditable={false} />
)}
</PlateElement>
);
});
TableCellElement.displayName = 'TableCellElement';
export const TableCellHeaderElement = withProps(TableCellElement, {
isHeader: true,
});