Skip to content

Commit

Permalink
[base, desk-tool] Make click on connector put focus into field
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge authored and rexxars committed Oct 6, 2020
1 parent c8dd4f2 commit 910ba3b
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as PathUtils from '@sanity/util/paths'
import React from 'react'
import {useReporter} from './tracker'
import {Path} from '@sanity/types'

import {ConnectorContext} from './ChangeIndicatorContext'
/**
* This is used to draw the bar that wraps the diff components in the changes panel
*/
Expand All @@ -13,6 +13,8 @@ export const ChangeFieldWrapper = (props: {
}) => {
const ref = React.useRef<HTMLDivElement>(null)

const {onSetFocus} = React.useContext(ConnectorContext)

const [isHover, setHover] = React.useState(false)
const onMouseEnter = React.useCallback(() => {
setHover(true)
Expand All @@ -29,7 +31,12 @@ export const ChangeFieldWrapper = (props: {
hasRevertHover: props.hasHover
}))
return (
<div ref={ref} onMouseLeave={onMouseLeave} onMouseEnter={onMouseEnter}>
<div
ref={ref}
onClick={() => onSetFocus(props.path)}
onMouseLeave={onMouseLeave}
onMouseEnter={onMouseEnter}
>
{props.children}
</div>
)
Expand Down
10 changes: 6 additions & 4 deletions packages/@sanity/base/src/change-indicators/ChangeIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function ChangeIndicatorProvider(props: {
value={{
value: props.value,
compareValue: props.compareValue,
focusPath: parentContext.focusPath ? parentContext.focusPath : props.focusPath,
focusPath: props.focusPath || [],
path: props.path,
fullPath: fullPath
}}
Expand Down Expand Up @@ -137,18 +137,20 @@ export const ChangeIndicatorWithProvidedFullPath = ({
interface ContextProvidedProps {
compareDeep?: boolean
children?: React.ReactNode
disabled?: boolean
}

export const ContextProvidedChangeIndicator = (props: ContextProvidedProps) => {
const context = React.useContext(ChangeIndicatorContext)
const {value, compareValue, path, focusPath, fullPath} = context

return (
return props.disabled ? (
<>{props.children}</>
) : (
<CoreChangeIndicator
fullPath={fullPath}
value={value}
compareValue={compareValue}
hasFocus={PathUtils.isEqual(fullPath, focusPath)}
hasFocus={PathUtils.isEqual(path, focusPath)}
compareDeep={props.compareDeep}
>
{props.children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ interface ChangeIndicatorContext {
export interface ConnectorContext {
isReviewChangesOpen: boolean
onOpenReviewChanges: () => void
onSetFocus: (nextPath: Path) => void
}
export const ConnectorContext: React.Context<ConnectorContext> = React.createContext({
isReviewChangesOpen: false as boolean,
onOpenReviewChanges: () => {}
onOpenReviewChanges: () => {},
onSetFocus: (nextPath: Path) => {}
})

const initial: ChangeIndicatorContext = {path: [], fullPath: [], focusPath: null}
const initial: ChangeIndicatorContext = {path: [], fullPath: [], focusPath: []}

export const ChangeIndicatorContext: React.Context<ChangeIndicatorContext> = React.createContext(
initial
Expand Down

0 comments on commit 910ba3b

Please sign in to comment.