-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Focus control in widgets customizer #31308
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0b017cf
Handle focus control
kevin940726 11cfee4
Handle moving to sidebar
kevin940726 bff84ee
Use single quote
kevin940726 a2b0bf5
Fix focusing on the same element twice
kevin940726 5bf83c5
Fix focusing when moving to sidebar
kevin940726 b154652
Rename the hooks
kevin940726 304003e
Fix focusing on the back button
kevin940726 ae67922
Add tests and upgrade puppeteer-testing-library
kevin940726 35ec116
Try to fix test
kevin940726 76cf4e8
Move utils to widgets package
kevin940726 3d52629
Normalize MoveToWidgetArea
kevin940726 7a00a02
Import from getWidgetIdFromBlock
kevin940726 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
packages/customize-widgets/src/components/customize-widgets/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useEffect, createPortal } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import SidebarBlockEditor from '../sidebar-block-editor'; | ||
import FocusControl from '../focus-control'; | ||
import SidebarControls from '../sidebar-controls'; | ||
|
||
export default function CustomizeWidgets( { | ||
api, | ||
sidebarControls, | ||
blockEditorSettings, | ||
} ) { | ||
const [ activeSidebarControl, setActiveSidebarControl ] = useState( null ); | ||
|
||
useEffect( () => { | ||
const unsubscribers = sidebarControls.map( ( sidebarControl ) => | ||
sidebarControl.subscribe( ( expanded ) => { | ||
if ( expanded ) { | ||
setActiveSidebarControl( sidebarControl ); | ||
} | ||
} ) | ||
); | ||
|
||
return () => { | ||
unsubscribers.forEach( ( unsubscriber ) => unsubscriber() ); | ||
}; | ||
}, [ sidebarControls ] ); | ||
|
||
const activeSidebar = | ||
activeSidebarControl && | ||
createPortal( | ||
<SidebarBlockEditor | ||
key={ activeSidebarControl.id } | ||
blockEditorSettings={ blockEditorSettings } | ||
sidebar={ activeSidebarControl.sidebarAdapter } | ||
inserter={ activeSidebarControl.inserter } | ||
inspector={ activeSidebarControl.inspector } | ||
/>, | ||
activeSidebarControl.container[ 0 ] | ||
); | ||
|
||
return ( | ||
<SidebarControls | ||
sidebarControls={ sidebarControls } | ||
activeSidebarControl={ activeSidebarControl } | ||
> | ||
<FocusControl api={ api } sidebarControls={ sidebarControls }> | ||
{ activeSidebar } | ||
</FocusControl> | ||
</SidebarControls> | ||
); | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/customize-widgets/src/components/focus-control/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
createContext, | ||
useState, | ||
useEffect, | ||
useContext, | ||
useCallback, | ||
useMemo, | ||
} from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { settingIdToWidgetId } from '../../utils'; | ||
|
||
const FocusControlContext = createContext(); | ||
|
||
export default function FocusControl( { api, sidebarControls, children } ) { | ||
const [ focusedWidgetIdRef, setFocusedWidgetIdRef ] = useState( { | ||
current: null, | ||
} ); | ||
|
||
const focusWidget = useCallback( | ||
( widgetId ) => { | ||
for ( const sidebarControl of sidebarControls ) { | ||
const widgets = sidebarControl.setting.get(); | ||
|
||
if ( widgets.includes( widgetId ) ) { | ||
sidebarControl.sectionInstance.expand( { | ||
// Schedule it after the complete callback so that | ||
// it won't be overridden by the "Back" button focus. | ||
completeCallback() { | ||
// Create a "ref-like" object every time to ensure | ||
// the same widget id can also triggers the focus control. | ||
setFocusedWidgetIdRef( { current: widgetId } ); | ||
}, | ||
} ); | ||
|
||
break; | ||
} | ||
} | ||
}, | ||
[ sidebarControls ] | ||
); | ||
|
||
useEffect( () => { | ||
function handleFocus( settingId ) { | ||
const widgetId = settingIdToWidgetId( settingId ); | ||
|
||
focusWidget( widgetId ); | ||
} | ||
|
||
function handleReady() { | ||
api.previewer.preview.bind( | ||
'focus-control-for-setting', | ||
handleFocus | ||
); | ||
} | ||
|
||
api.previewer.bind( 'ready', handleReady ); | ||
|
||
return () => { | ||
api.previewer.unbind( 'ready', handleReady ); | ||
api.previewer.preview.unbind( | ||
'focus-control-for-setting', | ||
handleFocus | ||
); | ||
}; | ||
}, [ api, focusWidget ] ); | ||
|
||
const context = useMemo( () => [ focusedWidgetIdRef, focusWidget ], [ | ||
focusedWidgetIdRef, | ||
focusWidget, | ||
] ); | ||
|
||
return ( | ||
<FocusControlContext.Provider value={ context }> | ||
{ children } | ||
</FocusControlContext.Provider> | ||
); | ||
} | ||
|
||
export const useFocusControl = () => useContext( FocusControlContext ); |
43 changes: 43 additions & 0 deletions
43
packages/customize-widgets/src/components/focus-control/use-blocks-focus-control.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef, useEffect } from '@wordpress/element'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
import { getWidgetIdFromBlock } from '@wordpress/widgets'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useFocusControl } from '.'; | ||
|
||
export default function useBlocksFocusControl( blocks ) { | ||
const { selectBlock } = useDispatch( blockEditorStore ); | ||
const [ focusedWidgetIdRef ] = useFocusControl(); | ||
|
||
const blocksRef = useRef( blocks ); | ||
|
||
useEffect( () => { | ||
blocksRef.current = blocks; | ||
}, [ blocks ] ); | ||
|
||
useEffect( () => { | ||
if ( focusedWidgetIdRef.current ) { | ||
const focusedBlock = blocksRef.current.find( | ||
( block ) => | ||
getWidgetIdFromBlock( block ) === focusedWidgetIdRef.current | ||
); | ||
|
||
if ( focusedBlock ) { | ||
selectBlock( focusedBlock.clientId ); | ||
// If the block is already being selected, the DOM node won't | ||
// get focused again automatically. | ||
// We select the DOM and focus it manually here. | ||
const blockNode = document.querySelector( | ||
`[data-block="${ focusedBlock.clientId }"]` | ||
); | ||
blockNode?.focus(); | ||
} | ||
} | ||
}, [ focusedWidgetIdRef, selectBlock ] ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to constructor so that it can still listen to the changes even when it's not active yet.