-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Refactor useAnchorRef and related components to work with the new Popover anchor prop
#43713
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
Changes from 1 commit
1fdf043
82cfb4c
4717042
8b57ffc
05c5486
b179e5b
1bc3b66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,19 +12,25 @@ import { getActiveFormat } from '../get-active-format'; | |
| /** @typedef {import('../register-format-type').RichTextFormatType} RichTextFormatType */ | ||
| /** @typedef {import('../create').RichTextValue} RichTextValue */ | ||
|
|
||
| /** | ||
| * @typedef {Object} VirtualAnchorElement | ||
| * @property {Function} getBoundingClientRect A function returning a DOMRect | ||
| * @property {Document} ownerDocument The element's ownerDocument | ||
| */ | ||
|
|
||
| /** | ||
| * This hook, to be used in a format type's Edit component, returns the active | ||
| * element that is formatted, or the selection range if no format is active. | ||
| * The returned value is meant to be used for positioning UI, e.g. by passing it | ||
| * to the `Popover` component. | ||
| * element that is formatted, or a virtual element for the selection range if | ||
| * no format is active. The returned value is meant to be used for positioning | ||
| * UI, e.g. by passing it to the `Popover` component. | ||
| * | ||
| * @param {Object} $1 Named parameters. | ||
| * @param {RefObject<HTMLElement>} $1.ref React ref of the element | ||
| * containing the editable content. | ||
| * @param {RichTextValue} $1.value Value to check for selection. | ||
| * @param {RichTextFormatType} $1.settings The format type's settings. | ||
| * | ||
| * @return {Element|Range} The active element or selection range. | ||
| * @return {Element|VirtualAnchorElement|null|undefined} The active element or selection range. | ||
| */ | ||
| export function useAnchorRef( { ref, value, settings = {} } ) { | ||
| const { tagName, className, name } = settings; | ||
|
|
@@ -44,7 +50,12 @@ export function useAnchorRef( { ref, value, settings = {} } ) { | |
| const range = selection.getRangeAt( 0 ); | ||
|
|
||
| if ( ! activeFormat ) { | ||
| return range; | ||
| return { | ||
| ownerDocument: range.startContainer.ownerDocument, | ||
| getBoundingClientRect() { | ||
| return range.getBoundingClientRect(); | ||
| }, | ||
| }; | ||
|
||
| } | ||
|
|
||
| let element = range.startContainer; | ||
|
|
||
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.
Just checking — is it safe for this to return
null? Should it be normalized toundefined? (I'm guessing the potentialnullcomes from theelement.closest().)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.
Correct, that's why I had added
nullas a potential return value.Your suggestion definitely makes sense to me, implemented in 7cea8ce
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.
Just flagging that, in the base PR, I've made a change to allow
anchorto benull— this was in response to some feedback received on a related PR, where it was noted than allowingnullas a possible value of theanchorwould result in a better devX (ie. the Popover can handle thenullcase easily, instead of requiring its consumers to passundefinedinstead ofnull, which is a typical value thatrefs can assume in React)With that in mind, I've also pushed a change to this PR, restoring
useAnchorRef's return value to includenulltoo 1bc3b66