Skip to content

Commit 45f2433

Browse files
Remove Dynamically Created Inline Styles from Map Layers (#1463)
* init commit * change, add check-inline-styles script * glob search, prettier, class selectors * Apply suggestions from copilot code review Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: andremig-bentley <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent e0bd80a commit 45f2433

File tree

6 files changed

+102
-14
lines changed

6 files changed

+102
-14
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Remove Dynamically Created Inline Styles",
4+
"packageName": "@itwin/map-layers",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"change": "beachball change --no-commit",
2020
"change:dev": "beachball change --config beachball.config.dev.js --no-commit",
2121
"cspell": "cspell .",
22+
"check-inline-styles": "node scripts/check-inline-styles.js",
2223
"publish": "beachball publish --new",
2324
"bump": "beachball bump",
2425
"prettier": "prettier --check .",
@@ -31,6 +32,7 @@
3132
"beachball": "^2.54.0",
3233
"cpx2": "^7.0.2",
3334
"cspell": "^9.2.0",
35+
"glob": "^11.0.0",
3436
"lage": "^2.14.6",
3537
"prettier": "^3.6.2",
3638
"yargs": "^17.7.2"

packages/itwin/map-layers/src/ui/widget/MapLayerDroppable.scss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,18 @@
1010
.map-manager-display-none {
1111
display: none;
1212
}
13+
14+
// Hover-based visibility for settings menus
15+
.map-manager-source-item {
16+
.map-layer-settings-menu-wrapper,
17+
.map-layer-settings-sublayers-menu {
18+
visibility: hidden;
19+
}
20+
21+
&:hover {
22+
.map-layer-settings-menu-wrapper,
23+
.map-layer-settings-sublayers-menu {
24+
visibility: visible;
25+
}
26+
}
27+
}

packages/itwin/map-layers/src/ui/widget/MapLayerDroppable.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ const StrictModeDroppable = ({ children, ...props }: DroppableProps) =>{
5858
return <Droppable {...props}>{children}</Droppable>;
5959
};
6060

61-
const changeVisibilityByElementId = (element: Element | null, visible: boolean) => {
62-
if (element) {
63-
element.setAttribute("style", `visibility: ${visible ? "visible" : "hidden"}`);
64-
}
65-
};
6661

6762
/** @internal */
6863
export function MapLayerDroppable(props: MapLayerDroppableProps) {
@@ -116,11 +111,6 @@ export function MapLayerDroppable(props: MapLayerDroppableProps) {
116111
[props],
117112
);
118113

119-
const changeSettingsMenuVisibility = (event: React.MouseEvent<HTMLDivElement, MouseEvent>, visible: boolean) => {
120-
changeVisibilityByElementId(event.currentTarget.querySelector("#MapLayerSettingsMenuWrapper"), visible);
121-
changeVisibilityByElementId(event.currentTarget.querySelector("#MapLayerSettingsSubLayersMenu"), visible);
122-
};
123-
124114
const renderItem: DraggableChildrenFn = (dragProvided, _, rubric) => {
125115
assert(props.layersList !== undefined);
126116
const activeLayer = props.layersList[rubric.source.index];
@@ -133,8 +123,6 @@ export function MapLayerDroppable(props: MapLayerDroppableProps) {
133123
key={activeLayer.name}
134124
{...dragProvided.draggableProps}
135125
ref={dragProvided.innerRef}
136-
onMouseEnter={(event) => changeSettingsMenuVisibility(event, true)}
137-
onMouseLeave={(event) => changeSettingsMenuVisibility(event, false)}
138126
>
139127
{/* Checkbox */}
140128
<Checkbox
@@ -204,7 +192,7 @@ export function MapLayerDroppable(props: MapLayerDroppableProps) {
204192
</span>
205193

206194
{/* SubLayersPopupButton */}
207-
<div id="MapLayerSettingsSubLayersMenu" className="map-manager-item-sub-layer-container map-manager-hidden">
195+
<div className="map-manager-item-sub-layer-container map-layer-settings-sublayers-menu">
208196
{activeLayer.subLayers && activeLayer.subLayers.length > 1 && (
209197
<SubLayersPopupButton
210198
checkboxStyle="eye"
@@ -252,7 +240,7 @@ export function MapLayerDroppable(props: MapLayerDroppableProps) {
252240
<SvgStatusWarning />
253241
</IconButton>
254242
)}
255-
<div id="MapLayerSettingsMenuWrapper" className="map-manager-hidden">
243+
<div className="map-layer-settings-menu-wrapper">
256244
<MapLayerSettingsMenu
257245
activeViewport={props.activeViewport}
258246
mapLayerSettings={activeLayer}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/check-inline-styles.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Script to check for inline styles
5+
* This validates that the map-layers package contains no inline style attributes
6+
*/
7+
8+
const fs = require("fs");
9+
const path = require("path");
10+
const { glob } = require("glob");
11+
12+
const mapLayersPath = "packages/itwin/map-layers/**/*.{ts,tsx}";
13+
14+
async function checkForInlineStyles() {
15+
try {
16+
const files = await glob(mapLayersPath, { cwd: process.cwd() });
17+
let violations = [];
18+
19+
files.forEach((file) => {
20+
const content = fs.readFileSync(file, "utf8");
21+
const lines = content.split("\n");
22+
23+
lines.forEach((line, index) => {
24+
// Check for style={{ or style = { patterns
25+
if (/style\s*=\s*\{/.test(line)) {
26+
violations.push({
27+
file,
28+
line: index + 1,
29+
content: line.trim(),
30+
});
31+
}
32+
33+
// Check for setAttribute("style", ...) patterns
34+
if (/\.setAttribute\s*\(\s*["']style["']\s*,/.test(line)) {
35+
violations.push({
36+
file,
37+
line: index + 1,
38+
content: line.trim(),
39+
});
40+
}
41+
42+
// Check for .style.property = value patterns
43+
if (/\.style\.[a-zA-Z][a-zA-Z0-9]*\s*=/.test(line)) {
44+
violations.push({
45+
file,
46+
line: index + 1,
47+
content: line.trim(),
48+
});
49+
}
50+
});
51+
});
52+
53+
if (violations.length > 0) {
54+
console.error("\nInline styles detected in map-layers package:");
55+
violations.forEach((v) => {
56+
console.error(`In file: ${v.file}`);
57+
console.error(`On line ${v.line}: ${v.content}`);
58+
console.error("");
59+
});
60+
console.error(`Total violations: ${violations.length}`);
61+
console.error("Please convert inline styles to CSS classes.");
62+
} else {
63+
console.log("\nNo inline styles found in map-layers package");
64+
console.log(`Checked ${files.length} files`);
65+
}
66+
} catch (error) {
67+
console.error("Error checking for inline styles:", error);
68+
process.exit(1);
69+
}
70+
}
71+
72+
// Run the check
73+
checkForInlineStyles();

0 commit comments

Comments
 (0)