Skip to content

Commit de43530

Browse files
committed
Implement automatic width calculation for the Dropdown component based on the longest option label
1 parent 0081cab commit de43530

5 files changed

Lines changed: 42 additions & 29 deletions

File tree

‎packages/ui/src/components/editor/common/Dropdown/Dropdown.module.scss‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
.dropdown {
22
position: relative;
3-
width: 100%;
43
}
54

65
.button {
76
display: flex;
87
align-items: center;
98
justify-content: space-between;
10-
gap: var(--padding-8px);
119
background: var(--vscode-input-background);
1210
color: var(--vscode-input-foreground);
1311
border: 1px solid var(--cwc-border-color);
1412
padding: var(--padding-4px) var(--padding-8px);
1513
border-radius: var(--border-radius-4px);
1614
cursor: pointer;
1715
width: 100%;
18-
text-align: left;
1916

2017
&:hover {
2118
background: var(--vscode-list-hoverBackground);
@@ -36,8 +33,3 @@
3633
.chevron_open {
3734
transform: rotate(180deg);
3835
}
39-
40-
.menu_override {
41-
left: 0;
42-
right: auto;
43-
}

‎packages/ui/src/components/editor/common/Dropdown/Dropdown.stories.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77

88
const sample_options: Dropdown.Option<string>[] = [
99
{ value: 'option1', label: 'Option 1' },
10-
{ value: 'option2', label: 'Option 2' },
10+
{ value: 'option2', label: 'This is a much longer option label' },
1111
{ value: 'option3', label: 'Option 3' }
1212
]
1313

‎packages/ui/src/components/editor/common/Dropdown/Dropdown.tsx‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useRef, useEffect } from 'react'
1+
import { useState, useRef, useEffect, useLayoutEffect, useMemo } from 'react'
22
import cn from 'classnames'
33
import { DropdownMenu } from '../DropdownMenu'
44
import styles from './Dropdown.module.scss'
@@ -14,19 +14,42 @@ export namespace Dropdown {
1414
value: T
1515
onChange: (value: T) => void
1616
className?: string
17-
max_width?: number
1817
}
1918
}
2019

2120
export const Dropdown = <T extends string>({
2221
options,
2322
value,
2423
onChange,
25-
className,
26-
max_width
24+
className
2725
}: Dropdown.Props<T>) => {
2826
const [is_open, set_is_open] = useState(false)
2927
const dropdown_ref = useRef<HTMLDivElement>(null)
28+
const button_ref = useRef<HTMLButtonElement>(null)
29+
const [width, set_width] = useState<number>()
30+
31+
const longest_label = useMemo(
32+
() =>
33+
options.reduce(
34+
(longest, option) =>
35+
option.label.length > longest.length ? option.label : longest,
36+
''
37+
),
38+
[options]
39+
)
40+
41+
useLayoutEffect(() => {
42+
if (button_ref.current && longest_label) {
43+
const canvas = document.createElement('canvas')
44+
const context = canvas.getContext('2d')
45+
if (context) {
46+
const button_style = window.getComputedStyle(button_ref.current)
47+
context.font = button_style.font
48+
const text_width = context.measureText(longest_label).width
49+
set_width(text_width + 40)
50+
}
51+
}
52+
}, [longest_label])
3053

3154
const selected_option = options.find((opt) => opt.value === value)
3255

@@ -55,7 +78,7 @@ export const Dropdown = <T extends string>({
5578
}
5679
}))
5780

58-
const style = max_width ? { maxWidth: `${max_width}px` } : undefined
81+
const style = width ? { width: `${width}px` } : undefined
5982

6083
return (
6184
<div
@@ -64,6 +87,7 @@ export const Dropdown = <T extends string>({
6487
style={style}
6588
>
6689
<button
90+
ref={button_ref}
6791
className={styles.button}
6892
onClick={() => set_is_open(!is_open)}
6993
data-is-open={is_open}
@@ -77,9 +101,7 @@ export const Dropdown = <T extends string>({
77101
)}
78102
/>
79103
</button>
80-
{is_open && (
81-
<DropdownMenu items={menu_items} className={styles.menu_override} />
82-
)}
104+
{is_open && <DropdownMenu items={menu_items} />}
83105
</div>
84106
)
85107
}

‎packages/ui/src/components/editor/common/DropdownMenu/DropdownMenu.tsx‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { useState } from 'react'
12
import styles from './DropdownMenu.module.scss'
2-
import cn from 'classnames'
33

44
export namespace DropdownMenu {
55
export type Item = {
@@ -11,24 +11,24 @@ export namespace DropdownMenu {
1111

1212
export type Props = {
1313
items: Item[]
14-
on_mouse_enter?: React.MouseEventHandler<HTMLDivElement>
15-
className?: string
1614
}
1715
}
1816

19-
export const DropdownMenu: React.FC<DropdownMenu.Props> = ({
20-
items,
21-
on_mouse_enter,
22-
className
23-
}) => {
17+
export const DropdownMenu: React.FC<DropdownMenu.Props> = (props) => {
18+
const [is_preselection_respected, set_is_preselection_respected] =
19+
useState<boolean>(true)
20+
2421
return (
25-
<div className={cn(styles.menu, className)} onMouseEnter={on_mouse_enter}>
26-
{items.map((item, index) => (
22+
<div className={styles.menu}>
23+
{props.items.map((item, index) => (
2724
<div
2825
key={index}
2926
className={styles.item}
3027
onClick={item.on_click}
31-
data-selected={item.is_selected}
28+
data-selected={item.is_selected && is_preselection_respected}
29+
onMouseEnter={() => {
30+
set_is_preselection_respected(false)
31+
}}
3232
>
3333
<span>{item.label}</span>
3434
{item.shortcut && (

‎packages/vscode/src/views/settings/frontend/Home/sections/GeneralSection.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
8989
options={CLEAR_CHECKS_OPTIONS}
9090
value={props.clear_checks_in_workspace_behavior}
9191
onChange={props.on_clear_checks_in_workspace_behavior_change}
92-
max_width={320}
9392
/>
9493
}
9594
/>

0 commit comments

Comments
 (0)