-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Fix incorrect useAnchor positioning when switching from virtual to rich text elements #58900
Merged
Merged
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { usePrevious } from '@wordpress/compose'; | ||
import { useState, useLayoutEffect } from '@wordpress/element'; | ||
|
||
/** @typedef {import('../register-format-type').WPFormat} WPFormat */ | ||
|
@@ -137,44 +138,31 @@ function getAnchor( editableContentElement, tagName, className ) { | |
* @return {Element|VirtualAnchorElement|undefined|null} The active element or selection range. | ||
*/ | ||
export function useAnchor( { editableContentElement, settings = {} } ) { | ||
const { tagName, className } = settings; | ||
const { tagName, className, isActive } = settings; | ||
const [ anchor, setAnchor ] = useState( () => | ||
getAnchor( editableContentElement, tagName, className ) | ||
); | ||
const wasActive = usePrevious( isActive ); | ||
|
||
useLayoutEffect( () => { | ||
if ( ! editableContentElement ) return; | ||
|
||
const { ownerDocument } = editableContentElement; | ||
|
||
function callback() { | ||
if ( | ||
editableContentElement === ownerDocument.activeElement || | ||
// When a link is created, we need to attach the popover to the newly created anchor. | ||
( ! wasActive && isActive ) || | ||
// Sometimes we're _removing_ an active anchor, such as the inline color popover. | ||
// When we add the color, it switches from a virtual anchor to a `<mark>` element. | ||
// When we _remove_ the color, it switches from a `<mark>` element to a virtual anchor. | ||
( wasActive && ! isActive ) | ||
) { | ||
setAnchor( | ||
getAnchor( editableContentElement, tagName, className ) | ||
); | ||
} | ||
|
||
function attach() { | ||
ownerDocument.addEventListener( 'selectionchange', callback ); | ||
} | ||
|
||
function detach() { | ||
ownerDocument.removeEventListener( 'selectionchange', callback ); | ||
} | ||
|
||
if ( editableContentElement === ownerDocument.activeElement ) { | ||
attach(); | ||
} | ||
|
||
editableContentElement.addEventListener( 'focusin', attach ); | ||
editableContentElement.addEventListener( 'focusout', detach ); | ||
|
||
return () => { | ||
detach(); | ||
|
||
editableContentElement.removeEventListener( 'focusin', attach ); | ||
editableContentElement.removeEventListener( 'focusout', detach ); | ||
}; | ||
Comment on lines
-156
to
-176
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe all of these were used to show/hide the link control popover when the caret was placed inside the link boundaries. This is no longer the UX as of #57986 |
||
}, [ editableContentElement, tagName, className ] ); | ||
}, [ editableContentElement, tagName, className, isActive, wasActive ] ); | ||
|
||
return anchor; | ||
} |
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.
Is it better to add an
isActive
property instead of shoving it in thesettings
array? Probably... But I didn't want to have to update the docs to always support it if so. If that isn't a concern, I think we should addisActive
as its own prop onuseAnchor
.