-
Notifications
You must be signed in to change notification settings - Fork 392
/
ReferencedDocHeading.tsx
141 lines (128 loc) · 4.25 KB
/
ReferencedDocHeading.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, {ReactNode, useCallback, useEffect, useRef, useState} from 'react'
import {Popover, Box, Text, Heading, Flex, Button, Grid} from '@sanity/ui'
import {LinkIcon} from '@sanity/icons'
import {TitleText, LinkCircle, HorizontalLine, StyledBox} from '../pane/PaneHeader.styles'
import {useDeskToolSetting} from '../../settings'
import {ReferencedDocTooltip} from './ReferencedDocTooltip'
interface ReferenceDocHeadingProps {
title?: ReactNode
totalReferenceCount: number
}
interface InnerPopoverProps {
onClose: () => void
totalReferenceCount: number
}
export function ReferencedDocHeading(props: ReferenceDocHeadingProps) {
const {title, totalReferenceCount} = props
const documentTitleRef = useRef(null)
const [titleBoxSize, setTitleBoxSize] = useState(0)
const [hidePopover, setHidePopover] = useDeskToolSetting(
'desk-tool',
'referenced-doc-has-confirmed-dialog',
false
)
const handleHidePopover = useCallback(() => setHidePopover(true), [setHidePopover])
// When the title container changes size (a sibling pane is opened/resized, sidebar opens),
// a resize observer fires this event. In order to force the info popover to reposition,
// we set the size of the title box to a state variable and use it as a key on the popover.
// Ideally, this would automatically be handled by `@sanity/ui`.
const handleResize = useCallback(
(e: ResizeObserverEntry[]) => {
if (!hidePopover) {
setTitleBoxSize(Math.floor(e[0].borderBoxSize[0].inlineSize))
}
},
[hidePopover, setTitleBoxSize]
)
if (hidePopover) {
return (
<Flex>
<TitleText tabIndex={0} textOverflow="ellipsis" weight="semibold">
{title}
</TitleText>
<ReferencedDocTooltip totalReferenceCount={totalReferenceCount} />
</Flex>
)
}
return (
<>
<Popover
key={`popover-${titleBoxSize}`}
content={
<InnerPopover onClose={handleHidePopover} totalReferenceCount={totalReferenceCount} />
}
placement="bottom-start"
referenceElement={documentTitleRef.current}
tone="default"
portal
open
/>
<ObserveElementResize callback={handleResize}>
<Flex>
{/* This empty div is added to prevent a jump of the popover pointer once the title is rendered */}
<div ref={documentTitleRef}>{''}</div>
<TitleText tabIndex={0} textOverflow="ellipsis" weight="semibold">
<Box marginBottom={2}>{title}</Box>
</TitleText>
<ReferencedDocTooltip totalReferenceCount={totalReferenceCount} />
</Flex>
</ObserveElementResize>
</>
)
}
function InnerPopover(props: InnerPopoverProps) {
const {onClose, totalReferenceCount} = props
return (
<StyledBox as="div">
<Box margin={4}>
<Flex marginBottom={4} align="center">
<Box marginRight={2}>
<LinkCircle>
<LinkIcon fontSize="24" />
</LinkCircle>
</Box>
<Heading size={1}>This is a referenced document</Heading>
</Flex>
<Text>
This document is referenced by {totalReferenceCount} other
{totalReferenceCount === 1 ? ' document' : ' documents'}. Changes made here will be
reflected anywhere content from this document is used.
</Text>
</Box>
<HorizontalLine />
<Box marginX={1} padding={3}>
<Grid columns={2} gap={3}>
<Button
as="a"
target="_blank"
mode="ghost"
href="https://www.sanity.io/docs/connected-content"
text="Learn more"
/>
<Button onClick={onClose} tone="primary" text="Got it" autoFocus />
</Grid>
</Box>
</StyledBox>
)
}
function ObserveElementResize(props: {
children: React.ReactElement
callback: ResizeObserverCallback
}) {
const {callback, children, ...rest} = props
const [el, setEl] = useState<HTMLDivElement | null>(null)
useEffect(() => {
if (!el || typeof ResizeObserver !== 'function') return undefined
const io = new ResizeObserver(callback)
io.observe(el)
return () => {
io.unobserve(el)
io.disconnect()
}
}, [el, callback])
return (
<div ref={setEl} {...rest}>
{children}
</div>
)
}