Skip to content

Commit df207e0

Browse files
committed
refactor: name magic numbers and rename el in landing handlebars
1 parent dc59569 commit df207e0

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

‎apps/web/src/components/landing/handlebars.tsx‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import {
1010

1111
type HandlebarsProps = PropsWithChildren;
1212

13+
const MIN_HANDLE_SEPARATION_PX = 60;
14+
const MASK_GRADIENT_EDGE_PADDING_PX = 10;
15+
const HANDLEBARS_ROTATE_DEG = 2.76;
16+
const RIGHT_HANDLE_LEFT_OFFSET_PX = -30;
17+
1318
export function Handlebars({ children }: HandlebarsProps) {
1419
const containerRef = useRef<HTMLDivElement>(null);
1520
const leftHandleRef = useRef<HTMLDivElement>(null);
@@ -38,17 +43,17 @@ export function Handlebars({ children }: HandlebarsProps) {
3843
});
3944

4045
useLayoutEffect(() => {
41-
const el = containerRef.current;
42-
if (!el) return;
46+
const container = containerRef.current;
47+
if (!container) return;
4348

4449
const updateWidth = () => {
45-
const newWidth = el.offsetWidth;
50+
const newWidth = container.offsetWidth;
4651
setWidth(newWidth);
4752
setRightHandle(newWidth);
4853
};
4954

5055
const observer = new ResizeObserver(updateWidth);
51-
observer.observe(el);
56+
observer.observe(container);
5257
updateWidth();
5358

5459
return () => observer.disconnect();
@@ -72,7 +77,10 @@ export function Handlebars({ children }: HandlebarsProps) {
7277
const deltaX = event.clientX - startX;
7378

7479
if (side === "left") {
75-
const maxLeft = Math.max(0, rightHandlePositionRef.current - 60);
80+
const maxLeft = Math.max(
81+
0,
82+
rightHandlePositionRef.current - MIN_HANDLE_SEPARATION_PX,
83+
);
7684
const nextLeftHandle = Math.max(
7785
0,
7886
Math.min(maxLeft, initialPosition + deltaX),
@@ -83,7 +91,7 @@ export function Handlebars({ children }: HandlebarsProps) {
8391

8492
const minRight = Math.min(
8593
widthRef.current,
86-
leftHandlePositionRef.current + 60,
94+
leftHandlePositionRef.current + MIN_HANDLE_SEPARATION_PX,
8795
);
8896
const nextRightHandle = Math.max(
8997
minRight,
@@ -114,10 +122,10 @@ export function Handlebars({ children }: HandlebarsProps) {
114122

115123
const hasMeasuredWidth = width > 0;
116124
const leftGradientPercent = hasMeasuredWidth
117-
? (leftHandle / (width - 10)) * 100
125+
? (leftHandle / (width - MASK_GRADIENT_EDGE_PADDING_PX)) * 100
118126
: 0;
119127
const rightGradientPercent = hasMeasuredWidth
120-
? (rightHandle / (width + 10)) * 100
128+
? (rightHandle / (width + MASK_GRADIENT_EDGE_PADDING_PX)) * 100
121129
: 100;
122130
const textMask = hasMeasuredWidth
123131
? `linear-gradient(90deg,
@@ -131,7 +139,11 @@ export function Handlebars({ children }: HandlebarsProps) {
131139

132140
return (
133141
<div className="flex justify-center gap-4 leading-16">
134-
<div ref={containerRef} className="relative mt-0.5 -rotate-[2.76deg]">
142+
<div
143+
ref={containerRef}
144+
className="relative mt-0.5"
145+
style={{ transform: `rotate(-${HANDLEBARS_ROTATE_DEG}deg)` }}
146+
>
135147
<div className="absolute inset-0 z-10 flex size-full justify-between rounded-2xl border border-yellow-500">
136148
<div
137149
ref={leftHandleRef}
@@ -156,7 +168,9 @@ export function Handlebars({ children }: HandlebarsProps) {
156168
ref={rightHandleRef}
157169
className="bg-background absolute z-20 flex h-full w-7 cursor-ew-resize touch-none items-center justify-center rounded-full border border-yellow-500 select-none"
158170
style={{
159-
left: hasMeasuredWidth ? "-30px" : undefined,
171+
left: hasMeasuredWidth
172+
? `${RIGHT_HANDLE_LEFT_OFFSET_PX}px`
173+
: undefined,
160174
right: hasMeasuredWidth ? undefined : "0px",
161175
translate: hasMeasuredWidth ? `${rightHandle}px 0` : undefined,
162176
}}

0 commit comments

Comments
 (0)