Skip to content

Commit

Permalink
refactor(portable-text-editor): rename var to be more explicit
Browse files Browse the repository at this point in the history
'hasFocus' here really means: does it has focus inside the editor itself?
As opposed focus on some object inside the editor content (object blocks etc).
  • Loading branch information
skogsmaskin committed Oct 23, 2023
1 parent e2484bd commit aa5eb94
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
13 changes: 8 additions & 5 deletions packages/sanity/src/core/form/inputs/PortableText/Compositor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {useHotkeys} from './hooks/useHotKeys'
import {useTrackFocusPath} from './hooks/useTrackFocusPath'

interface InputProps extends ArrayOfObjectsInputProps<PortableTextBlock> {
hasFocus: boolean
hasFocusWithin: boolean
hotkeys?: HotkeyOptions
isActive: boolean
isFullscreen: boolean
Expand All @@ -48,7 +48,7 @@ export function Compositor(props: Omit<InputProps, 'schemaType' | 'arrayFunction
changed,
focused,
focusPath = EMPTY_ARRAY,
hasFocus,
hasFocusWithin,
hotkeys,
isActive,
isFullscreen,
Expand Down Expand Up @@ -385,7 +385,7 @@ export function Compositor(props: Omit<InputProps, 'schemaType' | 'arrayFunction
<Editor
ariaDescribedBy={ariaDescribedBy}
initialSelection={initialSelection}
hasFocus={hasFocus}
hasFocus={hasFocusWithin}
hotkeys={editorHotkeys}
isActive={isActive}
isFullscreen={isFullscreen}
Expand All @@ -408,7 +408,7 @@ export function Compositor(props: Omit<InputProps, 'schemaType' | 'arrayFunction
[
ariaDescribedBy,
initialSelection,
hasFocus,
hasFocusWithin,
editorHotkeys,
isActive,
isFullscreen,
Expand Down Expand Up @@ -453,7 +453,10 @@ export function Compositor(props: Omit<InputProps, 'schemaType' | 'arrayFunction
isChanged={changed}
path={path}
>
<Root data-focused={hasFocus ? '' : undefined} data-read-only={readOnly ? '' : undefined}>
<Root
data-focused={hasFocusWithin ? '' : undefined}
data-read-only={readOnly ? '' : undefined}
>
<Box data-wrapper="" ref={setWrapperElement}>
<Portal __unstable_name={isFullscreen ? 'expanded' : 'collapsed'}>
{isFullscreen ? <ExpandedLayer>{editorNode}</ExpandedLayer> : editorNode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,20 @@ export function PortableTextInput(props: PortableTextInputProps) {
return items
}, [members, props])

const hasFocus =
// Is true if something inside the editor itself has focus,
// as opposed to focus on a form field inside an
// annotation or object block's editing interface.
const hasFocusWithin =
focused ||
(focusPath.length > 0 &&
portableTextMemberItems.some((m) => m.member.open && m.kind === 'textBlock'))

// Set active if focused
// Set active if focused within the editor
useEffect(() => {
if (hasFocus) {
if (hasFocusWithin) {
setIsActive(true)
}
}, [hasFocus])
}, [hasFocusWithin])

// Handle editor changes
const handleEditorChange = useCallback(
Expand Down Expand Up @@ -334,7 +337,7 @@ export function PortableTextInput(props: PortableTextInputProps) {
>
<Compositor
{...props}
hasFocus={hasFocus}
hasFocusWithin={hasFocusWithin}
hotkeys={hotkeys}
isActive={isActive}
isFullscreen={isFullscreen}
Expand Down

0 comments on commit aa5eb94

Please sign in to comment.