Skip to content

Commit 54db867

Browse files
Use custom grid element instead of VSCodeDataGrid
1 parent a7f8019 commit 54db867

8 files changed

Lines changed: 285 additions & 156 deletions

File tree

extensions/ql-vscode/src/stories/model-editor/MethodRow.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { Meta, StoryFn } from "@storybook/react";
55
import { MethodRow as MethodRowComponent } from "../../view/model-editor/MethodRow";
66
import { CallClassification, Method } from "../../model-editor/method";
77
import { ModeledMethod } from "../../model-editor/modeled-method";
8-
import { VSCodeDataGrid } from "@vscode/webview-ui-toolkit/react";
98
import {
109
MULTIPLE_MODELS_GRID_TEMPLATE_COLUMNS,
1110
SINGLE_MODEL_GRID_TEMPLATE_COLUMNS,
1211
} from "../../view/model-editor/ModeledMethodDataGrid";
1312
import { ModelEditorViewState } from "../../model-editor/shared/view-state";
1413
import { createMockExtensionPack } from "../../../test/factories/model-editor/extension-pack";
1514
import { Mode } from "../../model-editor/shared/mode";
15+
import { DataGrid } from "../../view/common/DataGrid";
1616

1717
export default {
1818
title: "CodeQL Model Editor/Method Row",
@@ -24,9 +24,9 @@ const Template: StoryFn<typeof MethodRowComponent> = (args) => {
2424
? MULTIPLE_MODELS_GRID_TEMPLATE_COLUMNS
2525
: SINGLE_MODEL_GRID_TEMPLATE_COLUMNS;
2626
return (
27-
<VSCodeDataGrid gridTemplateColumns={gridTemplateColumns}>
27+
<DataGrid gridTemplateColumns={gridTemplateColumns}>
2828
<MethodRowComponent {...args} />
29-
</VSCodeDataGrid>
29+
</DataGrid>
3030
);
3131
};
3232

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as React from "react";
2+
import { styled } from "styled-components";
3+
4+
/*
5+
* A drop-in replacement for the VSCodeDataGrid family of components.
6+
*
7+
* The difference is that the `display: grid` styling is applied to `DataGrid`, whereas
8+
* in the VS Code version that styling is applied to `VSCodeDataGridRow`. This gives
9+
* column alignment across rows in situation with dynamic contents. It also allows
10+
* for cells to span multiple rows and all the other features of data grids.
11+
*/
12+
13+
const StyledDataGrid = styled.div<{ $gridTemplateColumns: string | number }>`
14+
display: grid;
15+
grid-template-columns: ${(props) => props.$gridTemplateColumns};
16+
padding: calc((var(--design-unit) / 4) * 1px) 0;
17+
box-sizing: border-box;
18+
width: 100%;
19+
background: transparent;
20+
`;
21+
22+
interface DataGridProps {
23+
gridTemplateColumns: string | number;
24+
children: React.ReactNode;
25+
}
26+
27+
export function DataGrid(props: DataGridProps) {
28+
const { gridTemplateColumns, children } = props;
29+
30+
return (
31+
<StyledDataGrid
32+
aria-label="DataGrid"
33+
$gridTemplateColumns={gridTemplateColumns}
34+
>
35+
{children}
36+
</StyledDataGrid>
37+
);
38+
}
39+
40+
export const DataGridRow = styled.div<{ $focused?: boolean }>`
41+
display: contents;
42+
43+
&:hover > * {
44+
background-color: var(--list-hover-background);
45+
}
46+
47+
& > * {
48+
// Use !important to override the background color set by the hover state
49+
background-color: ${(props) =>
50+
props.$focused
51+
? "var(--vscode-editor-selectionBackground) !important"
52+
: "inherit"};
53+
}
54+
`;
55+
56+
const StyledDataGridCell = styled.div<{
57+
$gridRow: string | number;
58+
$gridColumn: string | number;
59+
}>`
60+
grid-row: ${(props) => props.$gridRow};
61+
grid-column: ${(props) => props.$gridColumn};
62+
padding: calc(var(--design-unit) * 1px) calc(var(--design-unit) * 3px);
63+
`;
64+
65+
interface DataGridCellProps {
66+
gridRow: string | number;
67+
gridColumn: string | number;
68+
children: React.ReactNode;
69+
}
70+
71+
export function DataGridCell(props: DataGridCellProps) {
72+
const { gridRow, gridColumn, children } = props;
73+
74+
return (
75+
<StyledDataGridCell $gridRow={gridRow} $gridColumn={gridColumn}>
76+
{children}
77+
</StyledDataGridCell>
78+
);
79+
}
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
1-
import {
2-
VSCodeDataGridCell,
3-
VSCodeDataGridRow,
4-
} from "@vscode/webview-ui-toolkit/react";
51
import * as React from "react";
62
import { styled } from "styled-components";
73
import { pluralize } from "../../common/word";
4+
import { DataGridCell, DataGridRow } from "../common/DataGrid";
5+
import { ModelEditorViewState } from "../../model-editor/shared/view-state";
86

9-
const HiddenMethodsCell = styled(VSCodeDataGridCell)`
7+
const HiddenMethodsText = styled.div`
108
text-align: center;
119
`;
1210

1311
interface Props {
12+
gridRow: number;
1413
numHiddenMethods: number;
1514
someMethodsAreVisible: boolean;
15+
viewState: ModelEditorViewState;
1616
}
1717

1818
export function HiddenMethodsRow({
19+
gridRow,
1920
numHiddenMethods,
2021
someMethodsAreVisible,
22+
viewState,
2123
}: Props) {
2224
if (numHiddenMethods === 0) {
2325
return null;
2426
}
2527

28+
const gridColumn = viewState.showMultipleModels ? "span 6" : "span 5";
29+
2630
return (
27-
<VSCodeDataGridRow>
28-
<HiddenMethodsCell gridColumn="span 5">
29-
{someMethodsAreVisible && "And "}
30-
{pluralize(numHiddenMethods, "method", "methods")} modeled in other
31-
CodeQL packs
32-
</HiddenMethodsCell>
33-
</VSCodeDataGridRow>
31+
<DataGridRow>
32+
<DataGridCell gridRow={gridRow} gridColumn={gridColumn}>
33+
<HiddenMethodsText>
34+
{someMethodsAreVisible && "And "}
35+
{pluralize(numHiddenMethods, "method", "methods")} modeled in other
36+
CodeQL packs
37+
</HiddenMethodsText>
38+
</DataGridCell>
39+
</DataGridRow>
3440
);
3541
}

extensions/ql-vscode/src/view/model-editor/MethodName.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Method } from "../../model-editor/method";
44

55
const Name = styled.span`
66
font-family: var(--vscode-editor-font-family);
7+
word-break: break-all;
78
`;
89

910
export const MethodName = (method: Method): JSX.Element => {

0 commit comments

Comments
 (0)