Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DraftHeroProjectHeading.tsx

60 lines2.4 KB

1import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
2​
3import { observeResize } from "~/lib/observeResize";
4​
5export function DraftHeroProjectHeading({
6 children,
7 projectName,
8}: {
9 children: ReactNode;
10 projectName: string;
11}) {
12 const containerRef = useRef<HTMLDivElement>(null);
13 const inlineRef = useRef<HTMLSpanElement>(null);
14 const [fitsInline, setFitsInline] = useState(false);
15​
16 useLayoutEffect(() => {
17 const container = containerRef.current;
18 const inline = inlineRef.current;
19 if (!container || !inline) return;
20​
21 // Measure the full inline heading independently of its current layout, so
22 // moving the name below it cannot make the fit check oscillate.
23 const update = () =>
24 setFitsInline(
25 inline.getBoundingClientRect().width <= container.getBoundingClientRect().width,
26 );
27 update();
28 return observeResize([container, inline], update);
29 }, []);
30​
31 return (
32 <div ref={containerRef} className="group/draft-hero relative w-full" data-inline={fitsInline}>
33 <span
34 aria-hidden="true"
35 className="pointer-events-none invisible absolute inset-x-0 top-0 h-0 overflow-hidden"
36 >
37 <span
38 ref={inlineRef}
39 className="flex w-max items-baseline gap-2 whitespace-nowrap font-normal text-3xl tracking-tight"
40 >
41 <span>What should we build in</span>
42 <span>
43 <span className="font-medium">{projectName}</span>?
44 </span>
45 </span>
46 </span>
47 <h1 className="flex w-full flex-col items-center justify-center gap-2 text-center font-normal text-2xl text-foreground tracking-tight sm:text-3xl lg:group-data-[inline=true]/draft-hero:flex-row">
48 <span className="max-w-full lg:group-data-[inline=true]/draft-hero:shrink-0">
49 What should we build
50 <span className="lg:group-data-[inline=true]/draft-hero:hidden">?</span>
51 <span className="hidden lg:group-data-[inline=true]/draft-hero:inline"> in</span>
52 </span>
53 <span className="flex min-w-0 max-w-full items-baseline justify-center text-base tracking-normal lg:group-data-[inline=true]/draft-hero:text-3xl lg:group-data-[inline=true]/draft-hero:tracking-tight">
54 <span className="min-w-0">{children}</span>
55 <span className="hidden shrink-0 lg:group-data-[inline=true]/draft-hero:inline">?</span>
56 </span>
57 </h1>
58 </div>
59 );