Skip to content

Commit

Permalink
RichText: show boundary only with editable element focus (#15466)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed May 15, 2019
1 parent f004741 commit 9ea31a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
15 changes: 11 additions & 4 deletions packages/block-editor/src/components/rich-text/editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ function applyInternetExplorerInputFix( editorNode ) {
}

const IS_PLACEHOLDER_VISIBLE_ATTR_NAME = 'data-is-placeholder-visible';
const CLASS_NAME = 'editor-rich-text__editable block-editor-rich-text__editable';

const oldClassName = 'editor-rich-text__editable';

export const className = 'block-editor-rich-text__editable';

/**
* Whether or not the user agent is Internet Explorer.
Expand Down Expand Up @@ -116,7 +119,11 @@ export default class Editable extends Component {
}

if ( ! isEqual( this.props.className, nextProps.className ) ) {
this.editorNode.className = classnames( nextProps.className, CLASS_NAME );
this.editorNode.className = classnames(
className,
oldClassName,
nextProps.className
);
}

const { removedKeys, updatedKeys } = diffAriaProps( this.props, nextProps );
Expand Down Expand Up @@ -156,7 +163,7 @@ export default class Editable extends Component {
style,
record,
valueToEditableHTML,
className,
className: additionalClassName,
isPlaceholderVisible,
...remainingProps
} = this.props;
Expand All @@ -166,7 +173,7 @@ export default class Editable extends Component {
return createElement( tagName, {
role: 'textbox',
'aria-multiline': true,
className: classnames( className, CLASS_NAME ),
className: classnames( className, oldClassName, additionalClassName ),
contentEditable: true,
[ IS_PLACEHOLDER_VISIBLE_ATTR_NAME ]: isPlaceholderVisible,
ref: this.bindEditorNode,
Expand Down
21 changes: 12 additions & 9 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import Autocomplete from '../autocomplete';
import BlockFormatControls from '../block-format-controls';
import FormatEdit from './format-edit';
import FormatToolbar from './format-toolbar';
import Editable from './editable';
import Editable, { className as editableClassName } from './editable';
import { pickAriaProps } from './aria';
import { getPatterns } from './patterns';
import { withBlockEditContext } from '../block-edit/context';
Expand Down Expand Up @@ -472,15 +472,18 @@ export class RichText extends Component {
const boundarySelector = '*[data-rich-text-format-boundary]';
const element = this.editableRef.querySelector( boundarySelector );

if ( element ) {
const computedStyle = getComputedStyle( element );
const newColor = computedStyle.color
.replace( ')', ', 0.2)' )
.replace( 'rgb', 'rgba' );

globalStyle.innerHTML =
`*:focus ${ boundarySelector }{background-color: ${ newColor }}`;
if ( ! element ) {
return;
}

const computedStyle = getComputedStyle( element );
const newColor = computedStyle.color
.replace( ')', ', 0.2)' )
.replace( 'rgb', 'rgba' );
const selector = `.${ editableClassName }:focus ${ boundarySelector }`;
const rule = `background-color: ${ newColor }`;

globalStyle.innerHTML = `${ selector } {${ rule }}`;
}

/**
Expand Down

0 comments on commit 9ea31a7

Please sign in to comment.