惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
雷峰网
雷峰网
The Register - Security
The Register - Security
The Cloudflare Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
I
InfoQ
博客园 - 三生石上(FineUI控件)
H
Help Net Security
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recorded Future
Recorded Future
小众软件
小众软件
Martin Fowler
Martin Fowler
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
V
Visual Studio Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
V
V2EX
Blog — PlanetScale
Blog — PlanetScale

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
Handling The Indentation of a Treeview Component
Ahmad Shadeed · 2024-04-30 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

Back in the school days, the math teacher said that what he likes about math is that you can have many solutions for the same problem.

Some solutions are easier or harder than others. I think the same about solving UI problems with CSS.

In a nutshell, the problem is to build a UI that contains nested components with an increasing indentation for each one. The problem is common on the web, but I will highlight the ones from Github, Figma, Adobe and more.

Let’s dive in.

What we are going to disassemble

In the following figure, we have a list of three components. The difference between them is that the deeper the nesting, the more spacing the component has.

Here is another version with spacing highlighted:

How would you solve that in CSS? Well, that’s the topic of the article. I will show you how GitHub, Figma, and Adobe solved this problem.

A look at the similar UIs

This UI pattern is very common on the web. Here are examples of the same concept with different UIs:

Now that you have an idea about what each UI looks like, let’s explore how each of them built it.

GitHub component

In GitHub, the component consists of the following:

  • Spacer
  • Toggle
  • Content wrapper
    • Icon
    • Label

Here is the HTML:

<div class="TreeView-item">
  <div class="spacer"></div>
  <div class="toggle"></div>
  <div class="content">
    <div class="TreeView-item-visual"></div>
    <span class="TreeView-item-text">ReactART-test.js.snap</span>
  </div>
</div>

In CSS, the team used CSS grid to handle the layout.

.TreeView-item {
  --toggle-width: 1rem;
  --spacer-col: 1rem; /* will go into this later */
  display: grid;
  grid-template-columns: var(--spacer-col) var(--toggle-width) 1fr;
  grid-template-areas: "spacer toggle content";
}

.spacer {
  grid-area: spacer;
}

.toggle {
  grid-area: toggle;
}

.content {
  grid-area: content;
}

Here is a closer look at the UI:

The usage of CSS grid for the UI is useful. Here are a few reasons:

  • By using the grid-area, we can assign the column for each item in the component.
  • The last nested layer doesn’t have a toggle. With CSS grid, its space will be reserved and won’t collapse the UI.
  • Depending on the depth level, the width of the first column will change based on a CSS variable on the component.

The spacer column

In the CSS, the first column is for the spacer. See the following:

.TreeView-item {
  --toggle-width: 1rem;
  --spacer-col: 1rem; /* will go into this later */
  display: grid;
  grid-template-columns: var(--spacer-col) var(--toggle-width) 1fr;
  grid-template-areas: "spacer toggle content";
}

To calculate the spacer column width, the CSS in GitHub uses the toggle width and the depth level.

.TreeView-item {
  --spacer-col: calc(
    calc(var(--level) - 1) * (var(--toggle-width) / 2)
  );
}

The minimum spacing is 8px. While this works, I’m thinking about the reason to use the --toggle-width variable. I found no reason except for having a minimum spacing of 8px.

Adobe way

In the web version of Photoshop, the layers UI has a similar structure to the tree view in GitHub.

Here is the HTML

<psw-tree-view-item indent="0" layer-visible can-open dir="ltr" open>
  <div id="link">
    <span id="first-column"></span>
    <span id="second-column"></span>
    <span id="label"></span>
  </div>
</psw-tree-view-item>

And a closer look at the tree view item:

The layout is built with CSS flexbox. For the nested items, the spacing is managed via padding-right on the first column.

:host([dir="ltr"][indent="1"]) #first-column {
  padding-right: var(--spectrum-global-dimension-size-200);
}

:host([dir="ltr"][indent="2"]) #first-column {
  padding-right: calc(2 * var(--spectrum-global-dimension-size-200));
}

/* and so on */

While this works, it’s not the best solution for me. I can make it a bit better and use CSS logical properties.

:host([indent="2"]) #first-column {
  padding-inline-end: calc(
    2 * var(--spectrum-global-dimension-size-200)
  );
}

I’m not a fan of using hardcoded values in CSS.

Figma way

Figma’s solution is different from Adobe and GitHub. Here is the UI:

<div class="object_row">
  <span class="object_row--indents">
    <span class="object_row--indent"></span>
    <!-- The more nesting, the more indent items.. -->
    <span class="svg-container object_row--expandCaret"></span>
  </span>
  <span class="object_row--layerIcon"></span>
  <span class="object_row--rowText"></span>
  <span class="object_row--rowActions"></span>
</div>

The layout is built with Flexbox, similar to Photoshop Web. Here are a few differences:

  • The toggle arrow is placed inside the idents group (it has position: absolute) to take it out of the flow.
  • The spacing is managed via Javascript. The more nesting, the more spacer elements.

In the following figure, notice how four spans represent the spacing.

Not my favorite solution.

Carbon design system

The tree view items in Carbon design system don’t use depth but instead have more padding from the left side.

The reason you see a negative margin in the screenshot above is that the selected item should be clickable. Without the negative margin, the clickable area will only be at the start of the text till the end of the element.

While it works, I would prefer to flatten all lists and keep the depth per item level.

Adobe Spectrum design system

Each tree view component is indented based on the nesting level. However, to make an item fully clickable, they used a pseudo-element that fills the entire space.

See the following video:

For me, using a pseudo-element is much better than dealing with negative margins (Like in Carbon design system).

My favorite

I like how GitHub solved this. Here is why:

  • The indentation is being handled per item basis. No need for complex relationships between child and parents.
  • If there is no toggle, its space will still be reserved.
  • Works great for left-to-right (LTR) and right-to-left (RTL) layouts.

We can do the same by using CSS max() function.

.TreeView-item {
  --spacer-col: max(8px, var(--level) * 8px);
}

This is much cleaner and easier to understand for me. The minimum value is 8px and the maximum value depends on the nesting depth.

The final thing to mention is using content-visibility on each treeview item. I spotted this:

.PRIVATE_TreeView-item-container {
  content-visibility: auto;
  contain-intrinsic-size: auto 2rem;
}

This is very useful for performance. Imagine browsing a tree view with thousands of sub items.

According to MDN:

Size containment allows a user agent to lay out an element as though it had a fixed size, preventing unnecessary reflows by avoiding the re-rendering of child elements to determine the actual size (thereby improving user experience).

Outro

That was a fun exploration. The interesting thing is that it’s only about the indentation part. There are many more areas to cover, but this is what caught my eye. I hope you enjoyed it and thank you for reading.