|
1 | 1 | 'use client'; |
2 | 2 |
|
| 3 | +import React from 'react'; |
3 | 4 | import type { ClientTOCPageGroup } from './encodeClientTableOfContents'; |
4 | 5 |
|
5 | 6 | import { tcls } from '@/lib/tailwind'; |
| 7 | +import { ToggleChevron } from '../primitives'; |
6 | 8 |
|
7 | 9 | import { PagesList } from './PagesList'; |
8 | 10 | import { TOCPageIcon } from './TOCPageIcon'; |
| 11 | +import { ToCButtonItemStyles } from './styles'; |
9 | 12 |
|
10 | 13 | export function PageGroupItem(props: { page: ClientTOCPageGroup; isFirst?: boolean }) { |
11 | 14 | const { page, isFirst } = props; |
| 15 | + const descendants = page.descendants ?? []; |
| 16 | + const hasDescendants = descendants.length > 0; |
| 17 | + const [isOpen, setIsOpen] = React.useState(true); |
| 18 | + const { sentinelRef, isSticking } = useIsSticking(); |
| 19 | + |
| 20 | + const handleToggle = () => { |
| 21 | + if (!hasDescendants) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + setIsOpen((prev) => !prev); |
| 26 | + }; |
12 | 27 |
|
13 | 28 | return ( |
14 | 29 | <li className="page-group-item flex flex-col"> |
| 30 | + <div ref={sentinelRef} className="h-0" aria-hidden="true" /> |
15 | 31 | <div |
16 | 32 | className={tcls( |
17 | | - '-top-4 sticky z-1 flex items-center gap-3 px-3', |
18 | | - 'font-semibold text-xs uppercase tracking-wide', |
19 | | - 'mt-2 pt-4 pb-3', // Add extra padding to make the header fade a bit nicer |
20 | | - '-mb-1.5', // Then pull the page items a bit closer, effective bottom padding is 1.5 units / 6px. |
21 | | - 'mask-[linear-gradient(rgba(0,0,0,1)_70%,rgba(0,0,0,0))]', // Fade out effect of fixed page items. We want the fade to start past the header, this is a good approximation. |
| 33 | + '-top-4 sticky z-1 after:pointer-events-none after:absolute after:inset-x-0 after:top-full after:h-4 after:bg-linear-to-b after:from-tint-base after:to-transparent after:transition-opacity', |
| 34 | + isSticking ? '' : 'after:opacity-0', |
| 35 | + 'mt-1 pt-2.5 pb-0', |
22 | 36 | 'bg-tint-base', |
| 37 | + 'sidebar-filled:after:from-tint-subtle', |
23 | 38 | 'sidebar-filled:bg-tint-subtle', |
| 39 | + 'theme-muted:after:from-tint-subtle', |
24 | 40 | 'theme-muted:bg-tint-subtle', |
25 | 41 | '[html.sidebar-filled.theme-bold.tint_&]:bg-tint-subtle', |
| 42 | + '[html.sidebar-filled.theme-bold.tint_&]:after:from-tint-subtle', |
26 | 43 | '[html.sidebar-filled.theme-muted_&]:bg-tint-base', |
| 44 | + '[html.sidebar-filled.theme-muted_&]:after:from-tint-base', |
27 | 45 | '[html.sidebar-filled.theme-bold.tint_&]:bg-tint-base', |
| 46 | + '[html.sidebar-filled.theme-bold.tint_&]:after:from-tint-base', |
28 | 47 | 'lg:[html.sidebar-default.theme-gradient_&]:bg-gradient-primary', |
| 48 | + 'lg:[html.sidebar-default.theme-gradient_&]:after:from-primary-2', |
29 | 49 | 'lg:[html.sidebar-default.theme-gradient.tint_&]:bg-gradient-tint', |
30 | | - isFirst ? '-mt-2 -top-2 rounded-t-2xl pt-2' : '' |
| 50 | + 'lg:[html.sidebar-default.theme-gradient.tint_&]:after:from-tint-subtle', |
| 51 | + isFirst ? '-mt-2 -top-2 circular-corners:rounded-t-2xl rounded-t-md pt-2' : '' |
31 | 52 | )} |
32 | 53 | > |
33 | | - <TOCPageIcon page={page} /> |
34 | | - {page.title} |
| 54 | + <button |
| 55 | + type="button" |
| 56 | + disabled={!hasDescendants} |
| 57 | + aria-expanded={hasDescendants ? isOpen : undefined} |
| 58 | + onClick={handleToggle} |
| 59 | + className={tcls( |
| 60 | + ToCButtonItemStyles, |
| 61 | + 'toc-group min-h-8 w-full border-0 text-left', |
| 62 | + 'font-semibold text-xs uppercase tracking-wide', |
| 63 | + 'appearance-none', |
| 64 | + '[&_.toc-group-chevron]:transition-opacity', |
| 65 | + 'hover:[&_.toc-group-chevron]:opacity-11', |
| 66 | + 'focus-visible:[&_.toc-group-chevron]:opacity-11', |
| 67 | + hasDescendants ? 'cursor-pointer' : '' |
| 68 | + )} |
| 69 | + > |
| 70 | + <TOCPageIcon page={page} /> |
| 71 | + <span className="min-w-0 flex-1">{page.title}</span> |
| 72 | + {hasDescendants ? ( |
| 73 | + <span |
| 74 | + className={tcls( |
| 75 | + 'toc-group-chevron ml-auto flex shrink-0 transition-opacity duration-150', |
| 76 | + isOpen |
| 77 | + ? 'pointer-events-none opacity-0 delay-75' |
| 78 | + : 'opacity-6 delay-0' |
| 79 | + )} |
| 80 | + > |
| 81 | + <ToggleChevron |
| 82 | + open={isOpen} |
| 83 | + orientation="right-to-down" |
| 84 | + className="m-0! size-3!" |
| 85 | + /> |
| 86 | + </span> |
| 87 | + ) : null} |
| 88 | + </button> |
35 | 89 | </div> |
36 | | - {page.descendants && page.descendants.length > 0 ? ( |
37 | | - <PagesList pages={page.descendants} /> |
| 90 | + {hasDescendants ? ( |
| 91 | + <div |
| 92 | + className={tcls( |
| 93 | + 'mt-px grid transition-[grid-template-rows,opacity] duration-200 ease-in-out', |
| 94 | + isOpen ? 'grid-rows-[1fr] opacity-100' : 'grid-rows-[0fr] opacity-0' |
| 95 | + )} |
| 96 | + > |
| 97 | + <div className="overflow-hidden"> |
| 98 | + <PagesList pages={descendants} /> |
| 99 | + </div> |
| 100 | + </div> |
38 | 101 | ) : null} |
39 | 102 | </li> |
40 | 103 | ); |
41 | 104 | } |
| 105 | + |
| 106 | +/** |
| 107 | + * Detect when a sticky element becomes "stuck" using an IntersectionObserver on a sentinel. |
| 108 | + * Place the sentinel ref on a 0-height element right before the sticky element. |
| 109 | + */ |
| 110 | +function useIsSticking() { |
| 111 | + const sentinelRef = React.useRef<HTMLDivElement>(null); |
| 112 | + const [isSticking, setIsSticking] = React.useState(false); |
| 113 | + |
| 114 | + React.useEffect(() => { |
| 115 | + const sentinel = sentinelRef.current; |
| 116 | + if (!sentinel) return; |
| 117 | + |
| 118 | + // Find the closest scrollable ancestor to use as IntersectionObserver root |
| 119 | + let scrollParent: Element | null = sentinel.parentElement; |
| 120 | + while (scrollParent) { |
| 121 | + const { overflowY } = getComputedStyle(scrollParent); |
| 122 | + if (overflowY === 'auto' || overflowY === 'scroll') break; |
| 123 | + scrollParent = scrollParent.parentElement; |
| 124 | + } |
| 125 | + |
| 126 | + const observer = new IntersectionObserver( |
| 127 | + ([entry]) => { |
| 128 | + if (entry) { |
| 129 | + setIsSticking(!entry.isIntersecting); |
| 130 | + } |
| 131 | + }, |
| 132 | + { root: scrollParent } |
| 133 | + ); |
| 134 | + |
| 135 | + observer.observe(sentinel); |
| 136 | + return () => observer.disconnect(); |
| 137 | + }, []); |
| 138 | + |
| 139 | + return { sentinelRef, isSticking }; |
| 140 | +} |
0 commit comments