Skip to content

Commit cb0a386

Browse files
authored
style: refactor sticky table styles (#294)
* style: refactor sticky table styles Signed-off-by: yazhou <yazhouhu@yunify.com> * ci: Add changeset Signed-off-by: yazhou <yazhouhu@yunify.com> * style: refactor sticky table styles Signed-off-by: yazhou <yazhouhu@yunify.com> --------- Signed-off-by: yazhou <yazhouhu@yunify.com>
1 parent 2dd9d29 commit cb0a386

File tree

6 files changed

+64
-17
lines changed

6 files changed

+64
-17
lines changed

.changeset/ten-days-sell.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@kubed/code-editor': patch
3+
'@kubed/diff-viewer': patch
4+
'@kubed/components': patch
5+
'@kubed/log-viewer': patch
6+
'@kubed/charts': patch
7+
'@kubed/hooks': patch
8+
'@kubed/icons': patch
9+
'@kubed/tests': patch
10+
'kubed-documents': patch
11+
---
12+
13+
style: Refactor with-sticky table styles

packages/components/src/Table/BaseTable/TableCell.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ const TableCellRoot = styled.td<{
109109
top: 0,
110110
zIndex: fixed ? 3 : 2,
111111
}),
112+
'&.with-sticky': {
113+
position: 'sticky',
114+
top: 0,
115+
zIndex: 2,
116+
},
112117
...(fixedLastLeft && {
113118
'&:after': {
114119
content: '""',
@@ -183,7 +188,13 @@ export const TableCell = React.forwardRef<
183188
return (
184189
<TableCellRoot
185190
as={component as any}
186-
className={cx('table-cell', className)}
191+
className={cx(
192+
'table-cell',
193+
{
194+
'with-sticky': !!(variant === 'head' && table && table.stickyHeader),
195+
},
196+
className
197+
)}
187198
$ownerState={cellProps}
188199
ref={ref}
189200
{...other}

packages/components/src/Table/BaseTable/TableHead.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ interface TableHeadProps {
1313
className?: string;
1414
style?: React.CSSProperties;
1515
hasBorder?: boolean;
16+
hasBorderTop?: boolean;
1617
}
1718

1819
const TableHeadRoot = styled.thead<{
1920
$ownerState: {
2021
stickyHeader?: boolean;
22+
hasBorderTop?: boolean;
2123
};
22-
}>(({ $ownerState: { stickyHeader }, theme }) => ({
24+
}>(({ $ownerState: { stickyHeader, hasBorderTop }, theme }) => ({
2325
display: 'table-header-group',
2426
backgroundColor: theme.palette.background,
2527

@@ -28,12 +30,20 @@ const TableHeadRoot = styled.thead<{
2830
top: 0,
2931
zIndex: 2,
3032
}),
33+
...(hasBorderTop && {
34+
borderTop: `1px solid ${theme.palette.accents_1}`,
35+
}),
36+
'&.with-sticky': {
37+
position: 'sticky',
38+
top: 0,
39+
zIndex: 2,
40+
},
3141
}));
3242

3343
export const TableHead = React.forwardRef<
3444
HTMLTableSectionElement,
3545
React.PropsWithChildren<TableHeadProps>
36-
>(({ className, hasBorder = tableLv.hasBorder, ...rest }, ref) => {
46+
>(({ className, hasBorderTop, hasBorder = tableLv.hasBorder, ...rest }, ref) => {
3747
const lv = React.useMemo(() => ({ ...tableLv, hasBorder }), [hasBorder]);
3848
const table = useTableContext();
3949

@@ -43,9 +53,16 @@ export const TableHead = React.forwardRef<
4353
{...rest}
4454
$ownerState={{
4555
stickyHeader: table?.stickyHeader,
56+
hasBorderTop,
4657
}}
4758
ref={ref}
48-
className={cx('kube-table-head', className)}
59+
className={cx(
60+
'kube-table-head',
61+
{
62+
'with-sticky': table?.stickyHeader,
63+
},
64+
className
65+
)}
4966
/>
5067
</TableLvContext.Provider>
5168
);

packages/components/src/Table/DataTable/BaseTable.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function BaseDataTable<T>({ table }: BaseDataTableProps<T>) {
1515
loading,
1616
meta: {
1717
refetch,
18+
enable: { toolbar } = {},
1819
enableDefault: { tr: enableTr = true } = {},
1920
getProps: {
2021
table: getTableProps,
@@ -31,7 +32,7 @@ export function BaseDataTable<T>({ table }: BaseDataTableProps<T>) {
3132
return (
3233
<>
3334
<BaseTable.Table {...tableProps}>
34-
<BaseTable.TableHead>
35+
<BaseTable.TableHead hasBorderTop={!!toolbar}>
3536
{table.getHeaderGroups().map((headerGroup) => (
3637
<BaseTable.TableRow key={headerGroup.id}>
3738
{headerGroup.headers.map((header) => (

packages/components/src/Table/DataTable/Table.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import {
1414
StorageStateOptions,
1515
} from './interfaces';
1616

17-
const ToolbarWithBorder = styled(Toolbar)<any>`
18-
border-bottom: 1px solid ${({ theme }) => theme.palette.accents_1};
19-
`;
2017
declare module '@tanstack/react-table' {
2118
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2219
interface TableOptionsResolved<TData extends RowData>
@@ -93,7 +90,7 @@ export function DataTable<T>({ className, table }: DataTableRootProps<T>) {
9390
const { options: { meta: { enable: { pagination, toolbar } = {} } = {} } = {} } = table;
9491
return (
9592
<div className={cx('table-container', className)}>
96-
{!!toolbar && <ToolbarWithBorder table={table} />}
93+
{!!toolbar && <Toolbar table={table} />}
9794
<BaseDataTable table={table} />
9895
{!!pagination && (
9996
<TableFooter>

packages/components/src/Table/Table.story.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Menu, MenuItem } from '../Menu/Menu';
1717
import { Select } from '../Select/Select';
1818
import { StateHandler, Status2StorageFeature, getDefaultTableOptions, useTable } from './DataTable';
1919
import { Center } from '../Center/Center';
20+
import styled from 'styled-components';
2021

2122
const { Table, TableBody, TableCell, TableHead, TableRow, Pagination, Toolbar } = BaseTable;
2223
export default {
@@ -109,6 +110,18 @@ export const basicTableWithSelected = () => {
109110
};
110111

111112
export const basicTableWithFixedHeader = () => {
113+
const Wrapper = styled.div`
114+
padding: 20px;
115+
height: 300px;
116+
overflow-y: auto;
117+
background-color: #f5f5f5;
118+
& {
119+
.kube-table-head.with-sticky,
120+
.table-cell.with-sticky {
121+
top: -20px;
122+
}
123+
}
124+
`;
112125
const data = React.useMemo(() => {
113126
return [...Array(100).fill(1)].map((_, i) => ({
114127
col1: i,
@@ -117,14 +130,9 @@ export const basicTableWithFixedHeader = () => {
117130
}));
118131
}, []);
119132
return (
120-
<div
121-
style={{
122-
height: '300px',
123-
overflowY: 'auto',
124-
}}
125-
>
133+
<Wrapper>
126134
<Table stickyHeader>
127-
<TableHead hasBorder>
135+
<TableHead hasBorder hasBorderTop>
128136
<TableRow>
129137
<TableCell colSpan={2}>Header 1</TableCell>
130138
<TableCell colSpan={1}>Header 2</TableCell>
@@ -145,7 +153,7 @@ export const basicTableWithFixedHeader = () => {
145153
))}
146154
</TableBody>
147155
</Table>
148-
</div>
156+
</Wrapper>
149157
);
150158
};
151159

0 commit comments

Comments
 (0)