Skip to content

Commit

Permalink
[field] Anchor annotation tooltips to individual changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent edc1012 commit d72e8a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {DiffAnnotationTooltip} from './DiffAnnotationTooltip'
import {getAnnotationAtPath, getAnnotationColor} from './helpers'

export interface AnnotationProps {
annotation: Annotation
annotation: Annotation | undefined | null
}

export interface AnnotatedDiffProps {
Expand Down
28 changes: 21 additions & 7 deletions packages/@sanity/field/src/diff/components/Change.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import {Path} from '../../paths'
import {PreviewComponent} from '../../preview/types'
import {DiffAnnotationTooltip, DiffAnnotationCard} from '../annotations'
import {DiffAnnotationTooltip, getAnnotationAtPath, useAnnotationColor} from '../annotations'
import {Diff as DiffType, SchemaType} from '../../types'
import {ChangeLayout} from './ChangeLayout'
import styles from './Change.css'
Expand All @@ -19,37 +19,51 @@ interface ChangeProps {
export function Change({
layout = 'inline',
diff,
path,
className,
schemaType,
previewComponent: PreviewComponent
}: ChangeProps) {
const containerClassName = className ? `${styles.root} ${className}` : styles.root
const {fromValue, toValue, action} = diff
const annotation = getAnnotationAtPath(diff, path || [])
const color = useAnnotationColor(annotation)
const colorStyle = color ? {background: color.background, color: color.text} : {}

if (action === 'unchanged') {
return <PreviewComponent value={toValue} schemaType={schemaType} />
}

const from = hasValue(fromValue) ? (
<DiffAnnotationCard as="del" className={styles.remove} diff={diff}>
<DiffAnnotationTooltip
as="del"
className={styles.remove}
annotation={annotation}
style={colorStyle}
>
<PreviewComponent value={fromValue} schemaType={schemaType} />
</DiffAnnotationCard>
</DiffAnnotationTooltip>
) : (
undefined
)

const to = hasValue(toValue) ? (
<DiffAnnotationCard as="ins" className={styles.add} diff={diff}>
<DiffAnnotationTooltip
as="ins"
className={styles.add}
annotation={annotation}
style={colorStyle}
>
<PreviewComponent value={toValue} schemaType={schemaType} />
</DiffAnnotationCard>
</DiffAnnotationTooltip>
) : (
undefined
)

return (
<DiffAnnotationTooltip className={containerClassName} diff={diff}>
<div className={containerClassName}>
<ChangeLayout from={from} to={to} layout={layout} />
</DiffAnnotationTooltip>
</div>
)
}

Expand Down

0 comments on commit d72e8a2

Please sign in to comment.