@@ -115,6 +115,7 @@ export const extractColors = styleString => {
115115const 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