Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 135 additions & 108 deletions src/components/MaintainersMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
useMemo,
useRef,
useState,
type ComponentProps,
} from 'react'
import { Popover } from '@base-ui/react/popover'
import { ClientOnly } from '@tanstack/react-router'
import { useSuspenseQuery } from '@tanstack/react-query'
import { geoDistance, select, zoom, zoomIdentity, type D3ZoomEvent } from 'd3'
Expand Down Expand Up @@ -63,14 +65,13 @@ type ProfileViewport = {
bottom: number
contentTop: number
contentBottom: number
helpTop: number
helpBottom: number
}

type ProfileSize = { width: number; height: number }
type ProfileSizes = { portraits: ProfileSize; count: ProfileSize }
type OffscreenEdge = 'top' | 'bottom'
type MarkerVector = [x: number, y: number, z: number]
type ProfileSelection = { groupId: string; trigger: HTMLButtonElement }

function markerVector(latitude: number, longitude: number): MarkerVector {
const lat = (latitude * Math.PI) / 180
Expand Down Expand Up @@ -234,12 +235,6 @@ function placeProfiles(
top: bounds.height - zoomControls.height,
bottom: bounds.height,
},
{
left: bounds.contentInset,
right: contentRight,
top: viewport.helpTop,
bottom: viewport.helpBottom,
},
]
const labels = nodes.flatMap(
({ id, card, line, pin, sizes, pendingImages }) => {
Expand Down Expand Up @@ -641,10 +636,6 @@ type MaintainersMapProps = {
maintainers: Maintainer[]
}

function Key({ children }: { children: React.ReactNode }) {
return <kbd className="font-sans font-medium">{children}</kbd>
}

function GlobeAvatar({
member,
color,
Expand Down Expand Up @@ -690,13 +681,15 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
const [reducedMotion, setReducedMotion] = useState(false)
const [scale, setScale] = useState(initialView.scale)
const [maxZoom, setMaxZoom] = useState(desktopMaxZoom)
const [selectedId, setSelectedId] = useState('')
const [selection, setSelection] = useState<ProfileSelection | null>(null)
const [paused, setPaused] = useState(false)
const [behavior] = useState(() => zoom<HTMLDivElement, unknown>())
const [profilePositions] = useState(
(): Map<string, ProfilePosition> => new Map(),
)
const profileNodes = useRef<ReturnType<typeof readProfileNodes>>(undefined)
const openProfileTrigger = useRef<HTMLButtonElement>(null)
const profilePopupId = `${prefix}-nearby-maintainers`
const [startingView] = useState(() => initialMaintainersView(initialLocation))
const view = useRef({ ...startingView })
const target = useRef({ ...startingView })
Expand Down Expand Up @@ -765,9 +758,14 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
}
return result
}, [maintainers, prefix, scale])
const selected = groups.find((group) => group.id === selectedId)
// Read by the frame loop, so zooming doesn't restart it.
const groupsRef = useRef(groups)
const selected = groups.find((group) => group.id === selection?.groupId)

useLayoutEffect(() => {
openProfileTrigger.current = selection?.trigger ?? null
wakeGlobe.current?.()
}, [selection])

useLayoutEffect(() => {
if (!host || !surface) return
Expand Down Expand Up @@ -858,7 +856,6 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
const container = host
const instance = globe
const section = container.closest('section')
const help = section?.querySelector('#maintainers-globe-help')
const intro = section?.parentElement?.querySelector(':scope > header')
const navbarHeight =
Number.parseFloat(
Expand All @@ -882,14 +879,10 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
const { top, height } = container.getBoundingClientRect()
const contentTop =
(intro?.getBoundingClientRect().bottom ?? top) - top + viewportGutter
const helpBounds = help?.getBoundingClientRect()
// Keep floating profiles clear of the next section.
const nextContent =
help?.nextElementSibling ?? section?.nextElementSibling
const nextContent = section?.nextElementSibling
const contentBottom =
(nextContent?.getBoundingClientRect().top ??
helpBounds?.bottom ??
top + height) -
(nextContent?.getBoundingClientRect().top ?? top + height) -
top -
viewportGutter
const screenTop = visualViewport?.offsetTop ?? 0
Expand All @@ -900,8 +893,6 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
bottom: screenTop + screenHeight - viewportGutter - top,
contentTop,
contentBottom,
helpTop: (helpBounds?.top ?? 0) - top,
helpBottom: (helpBounds?.bottom ?? 0) - top,
}
viewportChanged = true
requestFrame()
Expand Down Expand Up @@ -970,9 +961,15 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
const readingProfile = container.querySelector(
'[data-globe-profile]:hover, [data-globe-profile]:focus-within',
)
const idle = !readingProfile && !interacting.current
const idle =
!readingProfile &&
!openProfileTrigger.current &&
!interacting.current
const autoRotate =
globeVisible && !reducedMotion && !pausedRef.current
globeVisible &&
!reducedMotion &&
!pausedRef.current &&
!openProfileTrigger.current
if (autoRotate && idle) {
target.current.phi += Math.min(elapsed, 100) * 0.00015
}
Expand Down Expand Up @@ -1014,6 +1011,18 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
viewport,
globeVisible,
) || !profileNodes.current
const trigger = openProfileTrigger.current
if (trigger) {
const profile = trigger.closest('[data-globe-profile]')
if (
!trigger.isConnected ||
profile?.getAttribute('data-shown') !== 'true' ||
profile.hasAttribute('data-offscreen') !==
trigger.hasAttribute('data-globe-count')
) {
setSelection(null)
}
}
viewportChanged = false
}
wasIdle = idle
Expand Down Expand Up @@ -1100,18 +1109,25 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
}
}, [host, behavior, invalidate, pauseRotation])

function focus(group: MaintainerGroup) {
pauseRotation()
const phi = (-group.location[1] * Math.PI) / 180 - Math.PI / 2
target.current.phi =
view.current.phi +
Math.atan2(
Math.sin(phi - view.current.phi),
Math.cos(phi - view.current.phi),
)
target.current.theta = (group.location[0] * Math.PI) / 180
setSelectedId(group.id)
invalidate()
function profileTriggerProps(
groupId: string,
suffix: string,
): ComponentProps<'button'> {
const id = `${groupId}-${suffix}`
const expanded = selection?.trigger.id === id
return {
id,
type: 'button',
'aria-haspopup': 'dialog',
'aria-expanded': expanded,
'aria-controls': expanded ? profilePopupId : undefined,
onClick(event) {
const trigger = event.currentTarget
setSelection((current) =>
current?.trigger === trigger ? null : { groupId, trigger },
)
},
}
}

// The composite globe needs a focus target for its keyboard rotation controls.
Expand All @@ -1136,10 +1152,6 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
aria-describedby="maintainers-globe-help"
tabIndex={0}
onKeyDown={(event) => {
if (event.key === 'Escape') {
setSelectedId('')
return
}
if (event.target !== event.currentTarget || !event.shiftKey) return
const directions: Record<string, [number, number]> = {
ArrowLeft: [-0.15, 0],
Expand Down Expand Up @@ -1218,11 +1230,12 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
>
<div
data-globe-portraits
className="pointer-events-auto left-0 top-0 flex w-max items-center -space-x-1 rounded-full border border-border-default dark:border-white/20 bg-background-elevated/95 p-1 shadow-[0_6px_16px_#00000024] group-data-[offscreen]/profile:pointer-events-none group-data-[offscreen]/profile:invisible group-data-[offscreen]/profile:absolute sm:-space-x-1.5 sm:p-1.5"
className="pointer-events-auto left-0 top-0 flex w-max items-center rounded-full border border-border-default dark:border-white/20 bg-background-elevated/95 p-1 shadow-[0_6px_16px_#00000024] group-data-[offscreen]/profile:pointer-events-none group-data-[offscreen]/profile:invisible group-data-[offscreen]/profile:absolute sm:p-1.5 [&>button~button]:-ms-1 sm:[&>button~button]:-ms-1.5"
>
{group.members.map((member) => (
<Tooltip
key={member.github}
className="max-sm:hidden pointer-coarse:hidden"
content={
<div className="max-w-64 space-y-1">
<div className="font-semibold">{member.name}</div>
Expand All @@ -1233,10 +1246,11 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
}
>
<button
type="button"
onClick={() => focus(group)}
{...profileTriggerProps(
group.id,
`portrait-${member.github}`,
)}
aria-label={`Show ${member.name}`}
aria-expanded={selectedId === group.id}
className="relative shrink-0 rounded-full bg-background-elevated hover:z-10 focus-visible:z-10 focus-visible:outline-2 focus-visible:outline-offset-2"
>
<GlobeAvatar member={member} color={group.color} />
Expand All @@ -1245,11 +1259,9 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
))}
</div>
<button
type="button"
{...profileTriggerProps(group.id, 'count')}
data-globe-count
onClick={() => focus(group)}
aria-label={`Show ${group.members.length} ${group.members.length === 1 ? 'maintainer' : 'maintainers'} near ${group.members[0]?.location?.name ?? 'this location'}`}
aria-expanded={selectedId === group.id}
className="invisible pointer-events-auto absolute left-0 top-0 inline-flex h-7 w-max items-center gap-1 rounded-full border border-border-default dark:border-white/20 bg-background-elevated/95 px-2 text-xs font-semibold tabular-nums shadow-sm group-data-[offscreen]/profile:visible group-data-[offscreen]/profile:static"
>
<span aria-hidden="true" className="flex -space-x-0.5">
Expand Down Expand Up @@ -1332,71 +1344,86 @@ export function MaintainersMap({ maintainers }: MaintainersMapProps) {
</button>
</div>
</div>
<p
id="maintainers-globe-help"
className="relative z-20 text-center text-xs leading-relaxed text-text-muted opacity-80"
<Popover.Root
open={Boolean(selected)}
onOpenChange={(open, details) => {
if (open) return
if (
details.reason === 'outside-press' &&
details.event.target instanceof Element &&
details.event.target.closest('[data-globe-profile] button')
) {
details.cancel()
return
}
setSelection(null)
}}
>
<span className="hidden sm:inline sm:pointer-coarse:hidden">
Select to see nearby maintainers, <Key>Esc</Key> to close.
</span>
<span className="sm:hidden sm:pointer-coarse:inline">
Tap a profile to see nearby maintainers.
</span>
</p>
{selected && (
<div
role="region"
aria-label="Selected maintainers"
className="relative z-20 rounded-xl border border-border-default bg-background-subtle/50 p-4 dark:border-white/20"
>
<div className="mb-4 flex items-start justify-between gap-4">
<div>
<h3 className="font-semibold">
{`${selected.members.length} maintainer${selected.members.length === 1 ? '' : 's'} nearby`}
</h3>
<p className="mt-1 text-xs text-text-muted">
Select a profile to open their GitHub.
</p>
</div>
<button
type="button"
aria-label="Close selected cluster"
onClick={() => {
setSelectedId('')
}}
className="rounded p-1 hover:bg-surface-state-hover"
{selection && selected && (
<Popover.Portal>
<Popover.Positioner
anchor={selection.trigger}
positionMethod="fixed"
sideOffset={8}
collisionPadding={12}
collisionAvoidance={{ align: 'shift' }}
className="z-[var(--z-overlay)] data-[anchor-hidden]:invisible"
>
<XIcon />
</button>
</div>
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
{selected.members.map((member) => (
<a
key={member.github}
href={`https://github.com/${member.github}`}
target="_blank"
rel="noreferrer"
className="flex min-w-0 items-center gap-3 rounded-lg p-2 hover:bg-surface-state-hover"
<Popover.Popup
id={profilePopupId}
className="flex max-h-(--available-height) w-72 max-w-(--available-width) flex-col rounded-xl border border-border-default bg-background-elevated p-2 shadow-lg outline-none [&_:focus-visible]:outline-2 [&_:focus-visible]:-outline-offset-2 [&_:focus-visible]:outline-border-strong dark:border-white/20"
>
<span className="relative block size-9 shrink-0 overflow-hidden rounded-full">
<MaintainerAvatarImage
src={member.avatar}
alt=""
width={36}
height={36}
/>
</span>
<div className="min-w-0">
<div className="text-sm font-semibold">{member.name}</div>
<div className="text-xs text-text-muted">
{member.location?.name}
<div className="flex shrink-0 items-start justify-between gap-2 px-2 py-1">
<div>
<Popover.Title className="text-sm font-semibold">
{selected.members.length === 1
? '1 maintainer nearby'
: `${selected.members.length} maintainers nearby`}
</Popover.Title>
<Popover.Description className="mt-1 text-xs text-text-muted">
Select a profile to open their GitHub.
</Popover.Description>
</div>
<Popover.Close
aria-label="Close maintainer profiles"
className="grid size-7 shrink-0 place-items-center rounded-md hover:bg-surface-state-hover"
>
<XIcon aria-hidden="true" className="size-4" />
</Popover.Close>
</div>
</a>
))}
</div>
</div>
)}
<div className="min-h-0 overflow-y-auto overscroll-contain">
{selected.members.map((member) => (
<a
key={member.github}
href={`https://github.com/${member.github}`}
target="_blank"
rel="noreferrer"
className="flex items-center gap-3 rounded-lg p-2 hover:bg-surface-state-hover"
>
<span className="relative block size-9 shrink-0 overflow-hidden rounded-full">
<MaintainerAvatarImage
src={member.avatar}
alt=""
width={36}
height={36}
/>
</span>
<div className="min-w-0">
<div className="text-sm font-semibold">
{member.name}
</div>
<div className="text-xs text-text-muted">
{member.location?.name}
</div>
</div>
</a>
))}
</div>
</Popover.Popup>
</Popover.Positioner>
</Popover.Portal>
)}
</Popover.Root>
{!groups.length && (
<p role="status" className="text-sm text-text-secondary">
No shared locations for the selected maintainers and contributors.
Expand Down
Loading
Loading