Skip to content

Commit

Permalink
Distraction Free: Avoid focus loss when enabling/disabling distractio…
Browse files Browse the repository at this point in the history
…n free mode via the more menu (#51627)

* Avoid header region unmounting when enabling distraction free mode via the more menu

* remove the conditional in framer motion state for header elements, remove block selection workaround for lost focus

---------

Co-authored-by: Andrei Draganescu <andrei.draganescu@automattic.com>
  • Loading branch information
talldan and draganescu authored Jun 20, 2023
1 parent 32d394c commit e8c6840
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
9 changes: 2 additions & 7 deletions packages/edit-post/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ function Header( { setEntitiesSavedStatesCallback } ) {
isPublishSidebarOpened,
isSaving,
showIconLabels,
isDistractionFreeMode,
} = useSelect(
( select ) => ( {
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(),
Expand All @@ -36,21 +35,17 @@ function Header( { setEntitiesSavedStatesCallback } ) {
isSaving: select( editPostStore ).isSavingMetaBoxes(),
showIconLabels:
select( editPostStore ).isFeatureActive( 'showIconLabels' ),
isDistractionFreeMode:
select( editPostStore ).isFeatureActive( 'distractionFree' ),
} ),
[]
);

const isDistractionFree = isDistractionFreeMode && isLargeViewport;

const slideY = {
hidden: isDistractionFree ? { y: '-50' } : { y: 0 },
hidden: { y: '-50px' },
hover: { y: 0, transition: { type: 'tween', delay: 0.2 } },
};

const slideX = {
hidden: isDistractionFree ? { x: '-100%' } : { x: 0 },
hidden: { x: '-100%' },
hover: { x: 0, transition: { type: 'tween', delay: 0.2 } },
};

Expand Down
10 changes: 0 additions & 10 deletions packages/edit-post/src/components/header/writing-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,16 @@ function WritingMenu() {
[]
);

const blocks = useSelect(
( select ) => select( blockEditorStore ).getBlocks(),
[]
);

const { setIsInserterOpened, setIsListViewOpened, closeGeneralSidebar } =
useDispatch( postEditorStore );
const { set: setPreference } = useDispatch( preferencesStore );

const { selectBlock } = useDispatch( blockEditorStore );

const toggleDistractionFree = () => {
registry.batch( () => {
setPreference( 'core/edit-post', 'fixedToolbar', false );
setIsInserterOpened( false );
setIsListViewOpened( false );
closeGeneralSidebar();
if ( ! isDistractionFree && !! blocks.length ) {
selectBlock( blocks[ 0 ].clientId );
}
} );
};

Expand Down
36 changes: 23 additions & 13 deletions packages/interface/src/components/interface-skeleton/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ function InterfaceSkeleton(
const mergedLabels = { ...defaultLabels, ...labels };

const headerVariants = {
hidden: isDistractionFree ? { opacity: 0 } : { opacity: 1 },
hidden: { opacity: 0 },
hover: {
opacity: 1,
transition: { type: 'tween', delay: 0.2, delayChildren: 0.2 },
},
distractionFreeInactive: { opacity: 1, transition: { delay: 0 } },
};

return (
Expand All @@ -97,23 +98,32 @@ function InterfaceSkeleton(
) }
>
<div className="interface-interface-skeleton__editor">
{ !! header && isDistractionFree && (
{ !! header && (
<NavigableRegion
as={ motion.div }
className="interface-interface-skeleton__header"
aria-label={ mergedLabels.header }
initial={ isDistractionFree ? 'hidden' : 'hover' }
whileHover="hover"
initial={
isDistractionFree
? 'hidden'
: 'distractionFreeInactive'
}
whileHover={
isDistractionFree
? 'hover'
: 'distractionFreeInactive'
}
animate={
isDistractionFree
? 'hidden'
: 'distractionFreeInactive'
}
variants={ headerVariants }
transition={ { type: 'tween', delay: 0.8 } }
>
{ header }
</NavigableRegion>
) }
{ !! header && ! isDistractionFree && (
<NavigableRegion
className="interface-interface-skeleton__header"
ariaLabel={ mergedLabels.header }
transition={
isDistractionFree
? { type: 'tween', delay: 0.8 }
: undefined
}
>
{ header }
</NavigableRegion>
Expand Down

1 comment on commit e8c6840

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in e8c6840.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5317514373
📝 Reported issues:

Please sign in to comment.