Skip to content

Commit

Permalink
Keep a copy of anchor ref to preserve highlighted text selection (#36263
Browse files Browse the repository at this point in the history
)

* Keep a copy of anchor ref to preserve selection

* Update native files as well
  • Loading branch information
alshakero authored Nov 5, 2021
1 parent 8029f73 commit 787fd3e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,18 @@ _Returns_

- `Object`: Props to pass to the element to mark as a block.

### useCachedTruthy

Keeps an up-to-date copy of the passed value and returns it. If value becomes falsy, it will return the last truthy copy.

_Parameters_

- _value_ `any`:

_Returns_

- `any`: value

### useInnerBlocksProps

This hook is used to lightly mark an element as an inner blocks wrapper
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export { useCustomSides } from './dimensions';
export { getBorderClassesAndStyles, useBorderProps } from './use-border-props';
export { getColorClassesAndStyles, useColorProps } from './use-color-props';
export { getSpacingClassesAndStyles } from './use-spacing-props';
export { useCachedTruthy } from './use-cached-truthy';
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ import './font-size';
export { getBorderClassesAndStyles, useBorderProps } from './use-border-props';
export { getColorClassesAndStyles, useColorProps } from './use-color-props';
export { getSpacingClassesAndStyles } from './use-spacing-props';
export { useCachedTruthy } from './use-cached-truthy';
20 changes: 20 additions & 0 deletions packages/block-editor/src/hooks/use-cached-truthy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';

/**
* Keeps an up-to-date copy of the passed value and returns it. If value becomes falsy, it will return the last truthy copy.
*
* @param {any} value
* @return {any} value
*/
export function useCachedTruthy( value ) {
const [ cachedValue, setCachedValue ] = useState( value );
useEffect( () => {
if ( value ) {
setCachedValue( value );
}
}, [ value ] );
return cachedValue;
}
1 change: 1 addition & 0 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
useColorProps as __experimentalUseColorProps,
useCustomSides as __experimentalUseCustomSides,
getSpacingClassesAndStyles as __experimentalGetSpacingClassesAndStyles,
useCachedTruthy,
} from './hooks';
export * from './components';
export * from './utils';
Expand Down
13 changes: 12 additions & 1 deletion packages/format-library/src/text-color/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getColorObjectByColorValue,
getColorObjectByAttributeValues,
store as blockEditorStore,
useCachedTruthy,
} from '@wordpress/block-editor';
import { Popover, TabPanel } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -139,7 +140,17 @@ export default function InlineColorUI( {
onClose,
contentRef,
} ) {
const anchorRef = useAnchorRef( { ref: contentRef, value, settings } );
/*
As you change the text color by typing a HEX value into a field,
the return value of document.getSelection jumps to the field you're editing,
not the highlighted text. Given that useAnchorRef uses document.getSelection,
it will return null, since it can't find the <mark> element within the HEX input.
This caches the last truthy value of the selection anchor reference.
*/
const anchorRef = useCachedTruthy(
useAnchorRef( { ref: contentRef, value, settings } )
);

return (
<Popover
onClose={ onClose }
Expand Down

0 comments on commit 787fd3e

Please sign in to comment.