Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit a0a8aa0

Browse files
committed
Remove use of CSS variable
1 parent 6cba5f1 commit a0a8aa0

File tree

4 files changed

+43
-78
lines changed

4 files changed

+43
-78
lines changed

packages/devtools-components/src/tests/tree.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ describe("Tree", () => {
368368
expect(formatTree(wrapper)).toMatchSnapshot();
369369

370370
getTreeNodes(wrapper).forEach(n => {
371-
if ("ABECDMN".split("").includes(n.text())) {
371+
if ("ABECDMN".split("").includes(getSanitizedNodeText(n))) {
372372
expect(n.find(".arrow.expanded").exists()).toBe(true);
373373
} else {
374374
expect(n.find(".arrow").exists()).toBe(false);
@@ -446,14 +446,18 @@ function formatTree(wrapper) {
446446
let arrowStr = " ";
447447
if (arrow.exists()) {
448448
arrowStr = arrow.hasClass("expanded") ? "▼ " : "▶︎ ";
449-
} else {
450-
arrowStr = " ";
451449
}
452-
return `${indentStr}${arrowStr}${node.text()}`;
450+
451+
return `${indentStr}${arrowStr}${getSanitizedNodeText(node)}`;
453452
})
454453
.join("\n");
455454
}
456455

456+
function getSanitizedNodeText(node) {
457+
// Stripping off the invisible space used in the indent.
458+
return node.text().replace(/^\u200B+/, "");
459+
}
460+
457461
// Encoding of the following tree/forest:
458462
//
459463
// A

packages/devtools-components/src/tree.css

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
.tree {
6-
--arrow-width: 10px;
7-
--arrow-single-margin: 5px;
8-
--arrow-total-width: calc(var(--arrow-width) + var(--arrow-single-margin));
9-
--arrow-fill-color: var(--theme-splitter-color, #9B9B9B);
10-
--tree-indent-width: 1em;
11-
--tree-indent-border-color: #A2D1FF;
12-
--tree-indent-border-width: 1px;
13-
--tree-node-hover-background-color: #F0F9FE;
14-
--tree-node-focus-color: white;
15-
--tree-node-focus-background-color: var(--theme-selection-background, #0a84ff);
166
overflow: auto;
177
}
188

@@ -36,25 +26,35 @@
3626
display: block;
3727
}
3828

29+
.tree-indent {
30+
display: inline-block;
31+
width: 12px;
32+
margin-inline-start: 5px;
33+
border-inline-start: 1px solid #A2D1FF;
34+
}
35+
3936
.tree .tree-node[data-expandable="true"] {
4037
cursor: default;
4138
}
4239

4340
.tree .tree-node:not(.focused):hover {
44-
background-color: var(--tree-node-hover-background-color);
41+
background-color: #F0F9FE;
4542
}
4643

4744
.tree .tree-node.focused {
48-
color: var(--tree-node-focus-color);
49-
background-color: var(--tree-node-focus-background-color);
50-
--arrow-fill-color: currentColor;
45+
color: white;
46+
background-color: var(--theme-selection-background, #0a84ff);
47+
}
48+
49+
.tree-node.focused .arrow svg {
50+
fill: currentColor;
5151
}
5252

5353
.arrow svg {
54-
fill: var(--arrow-fill-color);
54+
fill: var(--theme-splitter-color, #9B9B9B);
5555
transition: transform 0.125s ease;
56-
width: var(--arrow-width);
57-
margin-inline-end: var(--arrow-single-margin);
56+
width: 10px;
57+
margin-inline-end: 5px;
5858
transform: rotate(-90deg);
5959
}
6060

packages/devtools-components/src/tree.js

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class ArrowExpander extends Component {
4444
}
4545
}
4646

47+
const treeIndent = dom.span({className: "tree-indent"}, "\u200B");
48+
4749
class TreeNode extends Component {
4850
static get propTypes() {
4951
return {
@@ -83,38 +85,6 @@ class TreeNode extends Component {
8385
})
8486
: null;
8587

86-
const treeIndentWidthVar = "var(--tree-indent-width)";
87-
const treeBorderColorVar = "var(--tree-indent-border-color, black)";
88-
const treeBorderWidthVar = "var(--tree-indent-border-width, 1px)";
89-
90-
const paddingInlineStart = `calc(
91-
(${treeIndentWidthVar} * ${depth})
92-
${(isExpandable ? "" : "+ var(--arrow-total-width)")}
93-
)`;
94-
95-
// This is the computed border that will mimic a border on tree nodes.
96-
// This allow us to have as many "borders" as we need without adding
97-
// specific elements for that purpose only.
98-
// it's a gradient with "hard stops" which will give us as much plain
99-
// lines as we need given the depth of the node.
100-
// The gradient uses CSS custom properties so everything is customizable
101-
// by consumers if needed.
102-
const backgroundBorder = depth === 0
103-
? null
104-
: "linear-gradient(90deg, " +
105-
Array.from({length: depth}).map((_, i) => {
106-
const indentWidth = `(${i} * ${treeIndentWidthVar})`;
107-
const alignIndent = `(var(--arrow-width) / 2)`;
108-
const start = `calc(${indentWidth} + ${alignIndent})`;
109-
const end = `calc(${indentWidth} + ${alignIndent} + ${treeBorderWidthVar})`;
110-
111-
return `transparent ${start},
112-
${treeBorderColorVar} ${start},
113-
${treeBorderColorVar} ${end},
114-
transparent ${end}`;
115-
}).join(",") +
116-
")";
117-
11888
let ariaExpanded;
11989
if (this.props.isExpandable) {
12090
ariaExpanded = false;
@@ -123,21 +93,20 @@ class TreeNode extends Component {
12393
ariaExpanded = true;
12494
}
12595

96+
const indents = Array.from({length: depth}).fill(treeIndent);
97+
let items = indents.concat(renderItem(item, depth, focused, arrow, expanded));
98+
12699
return dom.div(
127100
{
128101
id,
129102
className: "tree-node" + (focused ? " focused" : ""),
130-
style: {
131-
paddingInlineStart,
132-
backgroundImage: backgroundBorder,
133-
},
134103
onClick: this.props.onClick,
135104
role: "treeitem",
136105
"aria-level": depth,
137106
"aria-expanded": ariaExpanded,
138-
"data-expandable": this.props.isExpandable,
107+
"data-expandable": this.props.isExpandable
139108
},
140-
renderItem(item, depth, focused, arrow, expanded)
109+
...items
141110
);
142111
}
143112
}
@@ -189,19 +158,7 @@ function oncePerAnimationFrame(fn) {
189158
* restrict you to only one certain kind of tree.
190159
*
191160
* The tree comes with basic styling for the indent, the arrow, as well as hovered
192-
* and focused styles.
193-
* All of this can be customize on the customer end, by overriding the following
194-
* CSS custom properties :
195-
* --arrow-width: the width of the arrow.
196-
* --arrow-single-margin: the end margin between the arrow and the item that follows.
197-
* --arrow-fill-color: the fill-color of the arrow.
198-
* --tree-indent-width: the width of a 1-level-deep item.
199-
* --tree-indent-border-color: the color of the indent border.
200-
* --tree-indent-border-width: the width of the indent border.
201-
* --tree-node-hover-background-color: the background color of a hovered node.
202-
* --tree-node-focus-color: the color of a focused node.
203-
* --tree-node-focus-background-color: the background color of a focused node.
204-
*
161+
* and focused styles which can be override in CSS.
205162
*
206163
* ### Example Usage
207164
*

packages/devtools-components/stories/tree.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import React from "react";
66
const { Component, createFactory, createElement } = React;
7-
import dom from "react-dom-factories";
87

98
import Components from "../index";
109
const Tree = createFactory(Components.Tree);
@@ -85,6 +84,15 @@ storiesOf("Tree", module)
8584
getRoots: () => nodes,
8685
}, {});
8786
})
87+
.add("1000 items tree", () => {
88+
const nodes = Array.from({length: 1000}).map((_, i) => `item-${i + 1}`);
89+
return renderTree({
90+
getRoots: () => ["ROOT"],
91+
expanded: new Set()
92+
}, {
93+
children: {"ROOT": nodes}
94+
});
95+
})
8896
.add("30,000 items tree", () => {
8997
const nodes = Array.from({length: 1000}).map((_, i) => `item-${i + 1}`);
9098
return renderTree({
@@ -185,13 +193,9 @@ function createTreeElement(props, context, tree) {
185193
getChildren: x => tree.children && tree.children[x]
186194
? tree.children[x]
187195
: [],
188-
renderItem: (x, depth, focused, arrow, expanded) => dom.div({},
189-
arrow,
190-
x,
191-
),
196+
renderItem: (x, depth, focused, arrow, expanded) => [arrow, x],
192197
getRoots: () => ["A"],
193198
getKey: x => "key-" + x,
194-
itemHeight: 1,
195199
onFocus: x => {
196200
context.setState(previousState => {
197201
return {focused: x};

0 commit comments

Comments
 (0)