1
- import { useClickOutside } from '@sanity/ui'
1
+ import { useClickOutside , Label , Popover , Flex } from '@sanity/ui'
2
2
import { toString } from '@sanity/util/paths'
3
- import classNames from 'classnames'
4
3
import { isKeySegment , Path } from '@sanity/types'
5
- import ChevronDownIcon from 'part:@sanity/base/chevron-down-icon'
6
- import { Popover } from 'part:@sanity/components/popover'
7
4
import React , { useCallback , useEffect , useState } from 'react'
8
5
import { ConnectorContext , useReportedValues } from '@sanity/base/lib/change-indicators'
6
+ import styled from 'styled-components'
7
+ import { ChevronDownIcon } from '@sanity/icons'
9
8
import {
10
9
ChangeList ,
11
10
DiffContext ,
@@ -16,30 +15,51 @@ import {
16
15
} from '../../../../diff'
17
16
import { PortableTextChild } from '../types'
18
17
import { isEmptyObject } from '../helpers'
19
- import styles from './Annotation.css '
18
+ import { InlineBox , InlineText , PopoverContainer , PreviewContainer } from './styledComponents '
20
19
21
20
interface AnnotationProps {
22
21
diff ?: ObjectDiff
23
22
object : PortableTextChild
24
- children : JSX . Element
25
23
schemaType ?: ObjectSchemaType
26
24
path : Path
25
+ children : React . ReactNode
27
26
}
28
27
28
+ const AnnotationWrapper = styled . div `
29
+ text-decoration: none;
30
+ display: inline;
31
+ position: relative;
32
+ border: 0;
33
+ padding: 0;
34
+ border-bottom: 2px dotted currentColor;
35
+ box-shadow: inset 0 0 0 1px var(--card-border-color);
36
+ white-space: nowrap;
37
+ align-items: center;
38
+ background-color: color(var(--card-fg-color) a(10%));
39
+
40
+ &[data-changed] {
41
+ cursor: pointer;
42
+ }
43
+
44
+ &[data-removed] {
45
+ text-decoration: line-through;
46
+ }
47
+
48
+ &:hover ${ PreviewContainer } {
49
+ opacity: 1;
50
+ }
51
+ `
52
+
29
53
export function Annotation ( {
30
54
children,
31
55
diff,
32
56
object,
33
57
schemaType,
34
58
path,
35
59
...restProps
36
- } : AnnotationProps & Omit < React . HTMLProps < HTMLSpanElement > , 'onClick' > ) {
60
+ } : AnnotationProps ) {
37
61
if ( ! schemaType ) {
38
- return (
39
- < span { ...restProps } className = { styles . root } >
40
- Unknown schema type
41
- </ span >
42
- )
62
+ return < AnnotationWrapper { ...restProps } > Unknown schema type</ AnnotationWrapper >
43
63
}
44
64
if ( diff && diff . action !== 'unchanged' ) {
45
65
return (
@@ -54,14 +74,15 @@ export function Annotation({
54
74
</ AnnnotationWithDiff >
55
75
)
56
76
}
57
- return < span className = { styles . root } > { children } </ span >
77
+ return < AnnotationWrapper > { children } </ AnnotationWrapper >
58
78
}
59
79
60
80
interface AnnnotationWithDiffProps {
61
81
diff : ObjectDiff
62
82
object : PortableTextChild
63
83
schemaType : ObjectSchemaType
64
84
path : Path
85
+ children ?: React . ReactNode
65
86
}
66
87
67
88
function AnnnotationWithDiff ( {
@@ -71,18 +92,13 @@ function AnnnotationWithDiff({
71
92
schemaType,
72
93
path,
73
94
...restProps
74
- } : AnnnotationWithDiffProps & Omit < React . HTMLProps < HTMLSpanElement > , 'onClick' > ) {
95
+ } : AnnnotationWithDiffProps ) {
75
96
const { onSetFocus} = React . useContext ( ConnectorContext )
76
97
const { path : fullPath } = React . useContext ( DiffContext )
77
98
const [ popoverElement , setPopoverElement ] = useState < HTMLDivElement | null > ( null )
78
99
const color = useDiffAnnotationColor ( diff , [ ] )
79
100
const style = color ? { background : color . background , color : color . text } : { }
80
101
const isRemoved = diff . action === 'removed'
81
- const className = classNames (
82
- styles . root ,
83
- styles . isChanged ,
84
- diff . action === 'removed' && styles . removed
85
- )
86
102
const [ open , setOpen ] = useState ( false )
87
103
const emptyObject = object && isEmptyObject ( object )
88
104
const markDefPath = [ path [ 0 ] ] . concat ( [ 'markDefs' , { _key : object . _key } ] )
@@ -129,26 +145,40 @@ function AnnnotationWithDiff({
129
145
130
146
const popoverContent = (
131
147
< DiffContext . Provider value = { { path : myPath } } >
132
- < div className = { styles . popoverContainer } >
133
- < div className = { styles . popoverContent } >
134
- { emptyObject && < span className = { styles . empty } > Empty { schemaType . title } </ span > }
148
+ < PopoverContainer padding = { 3 } >
149
+ < div >
150
+ { emptyObject && (
151
+ < Label size = { 1 } muted >
152
+ Empty { schemaType . title }
153
+ </ Label >
154
+ ) }
135
155
{ ! emptyObject && < ChangeList diff = { diff } schemaType = { schemaType } /> }
136
156
</ div >
137
- </ div >
157
+ </ PopoverContainer >
138
158
</ DiffContext . Provider >
139
159
)
140
160
return (
141
- < span { ...restProps } className = { className } onClick = { handleOpenPopup } style = { style } >
142
- < Popover content = { popoverContent } open = { open } ref = { setPopoverElement } >
143
- < span className = { styles . previewContainer } >
161
+ < AnnotationWrapper
162
+ { ...restProps }
163
+ onClick = { handleOpenPopup }
164
+ style = { style }
165
+ data-changed = ""
166
+ data-removed = { diff . action === 'removed' ? '' : undefined }
167
+ >
168
+ < Popover content = { popoverContent } open = { open } ref = { setPopoverElement } portal >
169
+ < PreviewContainer paddingLeft = { 1 } >
144
170
< DiffTooltip annotations = { annotations } description = { `${ diff . action } annotation` } >
145
- < span >
171
+ < InlineBox style = { { display : 'inline-flex' } } >
146
172
< span > { children } </ span >
147
- < ChevronDownIcon />
148
- </ span >
173
+ < Flex align = "center" paddingX = { 1 } >
174
+ < InlineText size = { 0 } >
175
+ < ChevronDownIcon />
176
+ </ InlineText >
177
+ </ Flex >
178
+ </ InlineBox >
149
179
</ DiffTooltip >
150
- </ span >
180
+ </ PreviewContainer >
151
181
</ Popover >
152
- </ span >
182
+ </ AnnotationWrapper >
153
183
)
154
184
}
0 commit comments