Skip to content

Commit ed20075

Browse files
authored
Fix treeview glitches (#109)
* Fix first level of tree items not properly tagged as nested * Fix react tree view first level items not always being flagged as nested * Fix positioning and spacing for the collapse button in tree items * Scale margin between collapser and content * Update ref snapshots --------- Co-authored-by: Frédéric Collonval <[email protected]>
1 parent ce33451 commit ed20075

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

packages/components/src/tree-item/tree-item.styles.ts

+33-13
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,25 @@ import {
4444
heightNumber
4545
} from '../styles/index.js';
4646

47+
/**
48+
* Tree item expand collapse button size CSS Partial
49+
* @public
50+
*/
51+
export const expandCollapseButtonSize = cssPartial`(((${baseHeightMultiplier} + ${density}) * 0.5 + 2) * ${designUnit})`;
52+
4753
const ltr = css`
4854
.expand-collapse-glyph {
4955
transform: rotate(0deg);
5056
}
5157
:host(.nested) .expand-collapse-button {
5258
left: var(
5359
--expand-collapse-button-nested-width,
54-
calc(${heightNumber} * -1px)
60+
calc(
61+
(
62+
${expandCollapseButtonSize} +
63+
((${baseHeightMultiplier} + ${density}) * 1.25)
64+
) * -1px
65+
)
5566
);
5667
}
5768
:host([selected])::after {
@@ -69,7 +80,12 @@ const rtl = css`
6980
:host(.nested) .expand-collapse-button {
7081
right: var(
7182
--expand-collapse-button-nested-width,
72-
calc(${heightNumber} * -1px)
83+
calc(
84+
(
85+
${expandCollapseButtonSize} +
86+
((${baseHeightMultiplier} + ${density}) * 1.25)
87+
) * -1px
88+
)
7389
);
7490
}
7591
:host([selected])::after {
@@ -80,12 +96,6 @@ const rtl = css`
8096
}
8197
`;
8298

83-
/**
84-
* Tree item expand collapse button size CSS Partial
85-
* @public
86-
*/
87-
export const expandCollapseButtonSize = cssPartial`((${baseHeightMultiplier} / 2) * ${designUnit}) + ((${designUnit} * ${density}) / 2)`;
88-
8999
const expandCollapseHoverBehavior = DesignToken.create<Swatch>(
90100
'tree-item-expand-collapse-hover'
91101
).withDefault((target: HTMLElement) => {
@@ -132,7 +142,6 @@ export const treeItemStyles: FoundationElementTemplate<
132142
background: ${neutralFillStealthRest};
133143
cursor: pointer;
134144
font-family: ${bodyFont};
135-
--expand-collapse-button-size: calc(${heightNumber} * 1px);
136145
--tree-item-nested-width: 0;
137146
}
138147
@@ -199,8 +208,8 @@ export const treeItemStyles: FoundationElementTemplate<
199208
border: none;
200209
outline: none;
201210
/* TODO: adaptive typography https://github.com/microsoft/fast/issues/2432 */
202-
width: calc((${expandCollapseButtonSize} + (${designUnit} * 2)) * 1px);
203-
height: calc((${expandCollapseButtonSize} + (${designUnit} * 2)) * 1px);
211+
width: calc(${expandCollapseButtonSize} * 1px);
212+
height: calc(${expandCollapseButtonSize} * 1px);
204213
padding: 0;
205214
display: flex;
206215
justify-content: center;
@@ -261,7 +270,13 @@ export const treeItemStyles: FoundationElementTemplate<
261270
262271
:host(.nested) .content-region {
263272
position: relative;
264-
margin-inline-start: var(--expand-collapse-button-size);
273+
/* Add left margin to collapse button size */
274+
margin-inline-start: calc(
275+
(
276+
${expandCollapseButtonSize} +
277+
((${baseHeightMultiplier} + ${density}) * 1.25)
278+
) * 1px
279+
);
265280
}
266281
267282
:host(.nested) .expand-collapse-button {
@@ -304,7 +319,12 @@ export const treeItemStyles: FoundationElementTemplate<
304319
305320
::slotted(${context.tagFor(TreeItem)}) {
306321
--tree-item-nested-width: 1em;
307-
--expand-collapse-button-nested-width: calc(${heightNumber} * -1px);
322+
--expand-collapse-button-nested-width: calc(
323+
(
324+
${expandCollapseButtonSize} +
325+
((${baseHeightMultiplier} + ${density}) * 1.25)
326+
) * -1px
327+
);
308328
}
309329
`.withBehaviors(
310330
new DirectionalStyleSheetBehavior(ltr, rtl),
Loading

packages/react-components/lib/TreeView.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import {
22
jpTreeView,
33
provideJupyterDesignSystem
44
} from '@jupyter/web-components';
5-
import React, { forwardRef, useImperativeHandle, useRef } from 'react';
5+
import React, {
6+
forwardRef,
7+
useLayoutEffect,
8+
useImperativeHandle,
9+
useRef
10+
} from 'react';
611
import { useProperties } from './react-utils.js';
712

813
provideJupyterDesignSystem().register(jpTreeView());
@@ -12,6 +17,12 @@ export const TreeView = forwardRef((props, forwardedRef) => {
1217
const { className, renderCollapsedNodes, currentSelected, ...filteredProps } =
1318
props;
1419

20+
useLayoutEffect(() => {
21+
// Fix using private API to force refresh of nested flag on
22+
// first level of tree items.
23+
ref.current?.setItems();
24+
}, [ref.current]);
25+
1526
/** Properties - run whenever a property has changed */
1627
useProperties(ref, 'currentSelected', props.currentSelected);
1728

0 commit comments

Comments
 (0)