Skip to content

Commit 65fd060

Browse files
committed
Add responsive button compaction with icons and make shared hook for consistent UI behavior
1 parent b49465d commit 65fd060

9 files changed

Lines changed: 168 additions & 89 deletions

File tree

‎packages/shared/src/hooks/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './use-is-narrow-viewport'
22
export * from './use-is-mac'
3+
export * from './use-compacting'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useState, useEffect, useRef, useCallback } from 'react'
2+
3+
export const use_compacting = (max_compact_step = 4) => {
4+
const container_ref = useRef<HTMLDivElement>(null)
5+
const [compact_step, set_compact_step] = useState(0)
6+
const [thresholds, set_thresholds] = useState<Record<number, number>>({})
7+
8+
const report_width = useCallback((width: number, step: number) => {
9+
set_thresholds((prev) => {
10+
if (prev[step] === width) return prev
11+
return { ...prev, [step]: width }
12+
})
13+
}, [])
14+
15+
useEffect(() => {
16+
const observer = new ResizeObserver((entries) => {
17+
const width = entries[0].contentRect.width
18+
19+
// Try to expand (reduce compaction)
20+
for (let i = 0; i < compact_step; i++) {
21+
if (thresholds[i] !== undefined && width >= thresholds[i]) {
22+
set_compact_step(i)
23+
return
24+
}
25+
}
26+
27+
// Try to shrink (increase compaction)
28+
if (thresholds[0] !== undefined && width < thresholds[0]) {
29+
if (compact_step === 0) {
30+
set_compact_step(1)
31+
} else if (compact_step < max_compact_step) {
32+
if (
33+
thresholds[compact_step] !== undefined &&
34+
width < thresholds[compact_step]
35+
) {
36+
set_compact_step(compact_step + 1)
37+
}
38+
}
39+
}
40+
})
41+
42+
if (container_ref.current) observer.observe(container_ref.current)
43+
return () => observer.disconnect()
44+
}, [thresholds, compact_step, max_compact_step])
45+
46+
return {
47+
container_ref,
48+
compact_step,
49+
report_width
50+
}
51+
}

‎packages/ui/src/components/editor/panel/ResponsePreviewFooter/ResponsePreviewFooter.module.scss‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,23 @@
66
flex: 1;
77
}
88
}
9+
10+
.button-content {
11+
display: flex;
12+
align-items: center;
13+
justify-content: center;
14+
gap: 4px;
15+
}
16+
17+
.text {
18+
&--hidden {
19+
display: none;
20+
}
21+
}
22+
23+
.icon {
24+
display: none;
25+
&--visible {
26+
display: inline-block;
27+
}
28+
}

‎packages/ui/src/components/editor/panel/ResponsePreviewFooter/ResponsePreviewFooter.tsx‎

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { useRef, useLayoutEffect } from 'react'
2+
import cn from 'classnames'
3+
import { use_compacting } from '@shared/hooks'
14
import { Button } from '../../common/Button'
25
import styles from './ResponsePreviewFooter.module.scss'
36

@@ -14,16 +17,49 @@ export const ResponsePreviewFooter: React.FC<Props> = ({
1417
on_accept,
1518
is_accept_disabled
1619
}) => {
20+
const { container_ref, compact_step, report_width } = use_compacting(3)
21+
const back_ref = useRef<HTMLSpanElement>(null)
22+
const reject_ref = useRef<HTMLSpanElement>(null)
23+
const accept_ref = useRef<HTMLSpanElement>(null)
24+
25+
useLayoutEffect(() => {
26+
if (back_ref.current && reject_ref.current && accept_ref.current) {
27+
// Padding (approx 24px per button) + gap (8px * 2)
28+
const padding_and_gap = 3 * 24 + 2 * 8
29+
const width =
30+
back_ref.current.offsetWidth +
31+
reject_ref.current.offsetWidth +
32+
accept_ref.current.offsetWidth +
33+
padding_and_gap
34+
report_width(width, compact_step)
35+
}
36+
}, [compact_step, report_width])
37+
1738
return (
18-
<div className={styles.container}>
19-
<Button on_click={on_back} is_secondary>
20-
Back
39+
<div className={styles.container} ref={container_ref}>
40+
<Button on_click={on_back} is_secondary title="Back">
41+
<span className={styles['button-content']} ref={back_ref}>
42+
{compact_step < 1 && <span className={styles.text}>Back</span>}
43+
{compact_step >= 1 && (
44+
<span className={cn('codicon', 'codicon-chevron-left')} />
45+
)}
46+
</span>
2147
</Button>
22-
<Button on_click={on_reject} is_danger>
23-
Reject
48+
<Button on_click={on_reject} is_danger title="Reject">
49+
<span className={styles['button-content']} ref={reject_ref}>
50+
{compact_step < 2 && <span className={styles.text}>Reject</span>}
51+
{compact_step >= 2 && (
52+
<span className={cn('codicon', 'codicon-close')} />
53+
)}
54+
</span>
2455
</Button>
25-
<Button on_click={on_accept} disabled={is_accept_disabled}>
26-
Accept
56+
<Button on_click={on_accept} disabled={is_accept_disabled} title="Accept">
57+
<span className={styles['button-content']} ref={accept_ref}>
58+
{compact_step < 3 && <span className={styles.text}>Accept</span>}
59+
{compact_step >= 3 && (
60+
<span className={cn('codicon', 'codicon-check')} />
61+
)}
62+
</span>
2763
</Button>
2864
</div>
2965
)

‎packages/ui/src/components/editor/panel/modals/Modal/Modal.module.scss‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
text-align: center;
4242
font-weight: 500;
4343
font-size: 14px;
44-
padding: 12px 12px 0 12px;
44+
padding: 10px 12px 0 12px;
4545

4646
&--with-icon {
4747
padding-top: 6px;

‎packages/vscode/src/views/panel/frontend/components/Layout/Footer/Footer.tsx‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { useState, useEffect, useContext } from 'react'
1+
import { useState, useEffect, useContext, useRef, useLayoutEffect } from 'react'
22
import cn from 'classnames'
33
import { Icon } from '@ui/components/editor/common/Icon'
44
import styles from './Footer.module.scss'
5+
import { use_compacting } from '@shared/hooks'
56
import { LayoutContext } from '../../../contexts/LayoutContext'
6-
import { use_compacting } from './use-compacting'
77

88
type Props = {
99
on_donate_click: () => void
@@ -24,7 +24,17 @@ export const Footer: React.FC<Props> = ({ on_donate_click }) => {
2424
const [is_apply_disabled_temporarily, set_is_apply_disabled_temporarily] =
2525
useState(false)
2626

27-
const { container_ref, left_ref, right_ref, compact_step } = use_compacting()
27+
const { container_ref, compact_step, report_width } = use_compacting(4)
28+
const left_ref = useRef<HTMLDivElement>(null)
29+
const right_ref = useRef<HTMLDivElement>(null)
30+
31+
useLayoutEffect(() => {
32+
if (left_ref.current && right_ref.current) {
33+
const width =
34+
left_ref.current.offsetWidth + right_ref.current.offsetWidth + 6
35+
report_width(width, compact_step)
36+
}
37+
}, [compact_step, report_width])
2838

2939
useEffect(() => {
3040
set_is_commit_disabled_temporarily(false)

‎packages/vscode/src/views/panel/frontend/components/Layout/Footer/use-compacting.ts‎

Lines changed: 0 additions & 74 deletions
This file was deleted.

‎packages/vscode/src/views/panel/frontend/components/donations/DonationsFooter/DonationsFooter.module.scss‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@
1616
}
1717
}
1818
}
19+
20+
.button-content {
21+
display: flex;
22+
align-items: center;
23+
justify-content: center;
24+
gap: 4px;
25+
}

‎packages/vscode/src/views/panel/frontend/components/donations/DonationsFooter/DonationsFooter.tsx‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { useRef, useLayoutEffect } from 'react'
2+
import cn from 'classnames'
3+
import { use_compacting } from '@shared/hooks'
14
import { Button as UiButton } from '@ui/components/editor/common/Button'
25
import styles from './DonationsFooter.module.scss'
36

@@ -6,16 +9,41 @@ type Props = {
69
}
710

811
export const DonationsFooter: React.FC<Props> = (props) => {
12+
const { container_ref, compact_step, report_width } = use_compacting(2)
13+
const close_ref = useRef<HTMLSpanElement>(null)
14+
const coffee_ref = useRef<HTMLSpanElement>(null)
15+
16+
useLayoutEffect(() => {
17+
if (close_ref.current && coffee_ref.current) {
18+
const padding_and_gap = 2 * 24 + 8
19+
const width =
20+
close_ref.current.offsetWidth +
21+
coffee_ref.current.offsetWidth +
22+
padding_and_gap
23+
report_width(width, compact_step)
24+
}
25+
}, [compact_step, report_width])
26+
927
return (
10-
<div className={styles.container}>
28+
<div className={styles.container} ref={container_ref}>
1129
<UiButton on_click={props.on_close} is_secondary>
12-
Close
30+
<span className={styles['button-content']} ref={close_ref}>
31+
{compact_step < 1 && 'Close'}
32+
{compact_step >= 1 && (
33+
<span className={cn('codicon', 'codicon-chevron-left')} />
34+
)}
35+
</span>
1336
</UiButton>
1437
<UiButton
1538
url="https://buymeacoffee.com/robertpiosik"
16-
title="https://buymeacoffee.com/robertpiosik"
39+
title="buymeacoffee.com/robertpiosik"
1740
>
18-
Buy Me A Coffee ↗
41+
<span className={styles['button-content']} ref={coffee_ref}>
42+
{compact_step < 2 && 'Buy Me A Coffee ↗'}
43+
{compact_step >= 2 && (
44+
<span className={cn('codicon', 'codicon-coffee')} />
45+
)}
46+
</span>
1947
</UiButton>
2048
</div>
2149
)

0 commit comments

Comments
 (0)