Skip to content

Commit 2249c29

Browse files
authored
fix (wp5.9): text formatting popup closes when clearing highlight color (#2131)
* Fixed the clear highlight button. * fix: use mousedown instead of click to prevent closing of popover * fix: aligned formatting toolbar color indicator. fixes #2082 Co-authored-by: Benjamin Intal <[email protected]> fixes #2083
1 parent 48bc5b9 commit 2249c29

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/format-types/highlight/editor.scss

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
.stackable-components-toolbar {
2-
border-top: none !important;
3-
border-bottom: none !important;
4-
border-left: 1px solid #000 !important;
5-
border-right: 1px solid #000 !important;
6-
.components-icon-button {
7-
justify-content: center;
8-
svg {
9-
margin: 0 !important;
10-
}
1+
.block-editor-block-toolbar .stk-toolbar-button {
2+
padding: 6px 12px !important;
3+
&::before {
4+
left: 8px !important;
5+
right: 8px !important;
116
}
127
}
138
.components-stackable-highlight__popover .components-popover__content {
@@ -26,10 +21,11 @@
2621
background: #000;
2722
height: 3px;
2823
width: 20px;
29-
left: auto;
24+
left: 50%;
3025
right: auto;
3126
top: 50%;
3227
margin: 9px 0 0;
28+
transform: translateX(-50%);
3329
}
3430
.ugb-advanced-toolbar-control .components-base-control__field {
3531
margin-bottom: 24px;

src/format-types/highlight/index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export const extractColors = styleString => {
115115
const HighlightButton = props => {
116116
const [ isOpen, setIsOpen ] = useState( false )
117117
const popoverEl = useRef( null )
118+
const [ colorType, setColorType ] = useState( null )
118119

119120
// Close the window if the user clicks outside.
120121
const clickOutsideListener = useCallback( event => {
@@ -127,8 +128,8 @@ const HighlightButton = props => {
127128

128129
// Assign the outside click listener.
129130
useEffect( () => {
130-
document.body.addEventListener( 'click', clickOutsideListener )
131-
return () => document.body.removeEventListener( 'click', clickOutsideListener )
131+
document.body.addEventListener( 'mousedown', clickOutsideListener )
132+
return () => document.body.removeEventListener( 'mousedown', clickOutsideListener )
132133
}, [ clickOutsideListener ] )
133134

134135
const {
@@ -141,6 +142,15 @@ const HighlightButton = props => {
141142
// Backward compatibility for ugb/highlight.
142143
let isActive = _isActive
143144
let highlightStyles = activeAttributes?.style
145+
146+
// Open the popup according to the highlight type.
147+
useEffect( () => {
148+
const {
149+
colorType = '',
150+
} = isActive ? extractColors( highlightStyles ) : {}
151+
setColorType( colorType )
152+
}, [ isOpen ] )
153+
144154
if ( value ) {
145155
( value.activeFormats || [] ).some( format => {
146156
if ( format?.type === 'ugb/highlight' ) {
@@ -156,9 +166,7 @@ const HighlightButton = props => {
156166
const {
157167
textColor = '',
158168
highlightColor = '',
159-
colorType = '',
160169
} = isActive ? extractColors( highlightStyles ) : {}
161-
162170
// If highlighted, show the highlight color, otherwise show the text color.
163171
const displayIconColor = ( colorType !== '' ? highlightColor : textColor ) || textColor
164172

@@ -167,11 +175,13 @@ const HighlightButton = props => {
167175
<Toolbar className="stackable-components-toolbar">
168176
<Button
169177
label={ __( 'Color & Highlight', i18n ) }
170-
className="components-button components-icon-button components-toolbar__control"
178+
className="components-toolbar__control stk-toolbar-button"
171179
icon="editor-textcolor"
172180
aria-haspopup="true"
173181
tooltip={ __( 'Color & Highlight', i18n ) }
174-
onClick={ () => setIsOpen( ! isOpen ) }
182+
onClick={ () => {
183+
setIsOpen( ! isOpen )
184+
} }
175185
isPressed={ isActive }
176186
>
177187
<span className="components-stackable-highlight-color__indicator" style={ { backgroundColor: displayIconColor } } />
@@ -181,7 +191,7 @@ const HighlightButton = props => {
181191
position="bottom center"
182192
className="components-stackable-highlight__popover"
183193
focusOnMount="container"
184-
useRef={ popoverEl }
194+
ref={ popoverEl }
185195
isAlternate
186196
>
187197
<div className="components-stackable-highlight__inner">
@@ -210,6 +220,7 @@ const HighlightButton = props => {
210220
: highlightColor || textColor || ''
211221

212222
onChange( createApplyFormat( value, colorType, defaultTextColor, defaultHighlightColor ), { withoutHistory: true } )
223+
setColorType( colorType )
213224
} }
214225
isSmall
215226
/>

0 commit comments

Comments
 (0)