Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
useAnchorRef: return a VirtualElement instead of a range
  • Loading branch information
ciampo committed Sep 6, 2022
commit 1fdf043698a62d0a4e4f60b4320638d14d31143a
6 changes: 3 additions & 3 deletions packages/rich-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ _Returns_
### useAnchorRef

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.

_Parameters_

Expand Down
21 changes: 16 additions & 5 deletions packages/rich-text/src/component/use-anchor-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

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 to undefined? (I'm guessing the potential null comes from the element.closest().)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm guessing the potential null comes from the element.closest()

Correct, that's why I had added null as a potential return value.

Your suggestion definitely makes sense to me, implemented in 7cea8ce

Copy link
Contributor Author

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 anchor to be null — this was in response to some feedback received on a related PR, where it was noted than allowing null as a possible value of the anchor would result in a better devX (ie. the Popover can handle the null case easily, instead of requiring its consumers to pass undefined instead of null, which is a typical value that refs can assume in React)

With that in mind, I've also pushed a change to this PR, restoring useAnchorRef's return value to include null too 1bc3b66

*/
export function useAnchorRef( { ref, value, settings = {} } ) {
const { tagName, className, name } = settings;
Expand All @@ -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();
},
};
Comment on lines +53 to +58
Copy link
Contributor Author

@ciampo ciampo Aug 30, 2022

Choose a reason for hiding this comment

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

This is the main change of the PR: this hook now returns a VirtualElement instead of a Range.

A VirtualElement is an element with the ownerDocument and getBoundingClientRect() properties, and is used by floating-ui to determine an anchor when there's not an actual DOM Element available.

Couple of notes:

}

let element = range.startContainer;
Expand Down