Skip to content

Commit d31254f

Browse files
Verma-Punitajitbohra
authored andcommitted
refactor: remove unused useEffect in Edit component and clean up InlineUI component
1 parent d5e3793 commit d31254f

File tree

2 files changed

+61
-60
lines changed

2 files changed

+61
-60
lines changed

src/infotip/edit.js

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { __ } from '@wordpress/i18n';
55
import { RichTextToolbarButton } from '@wordpress/block-editor';
6-
import { useState, useEffect } from '@wordpress/element';
6+
import { useState } from '@wordpress/element';
77

88
/**
99
* Internal dependencies
@@ -25,47 +25,35 @@ import InfotipIcon from '../../assets/images/infotip';
2525
* @param {Object} props.activeAttributes - The currently active attributes.
2626
* @return {JSX.Element} - The rendered infotip button.
2727
*/
28-
export function Edit( {
28+
export function Edit({
2929
value,
3030
onChange,
3131
onFocus,
3232
isActive,
3333
contentRef,
3434
activeAttributes,
35-
} ) {
36-
const [ isSettingOpen, setIsSettingOpen ] = useState( false );
37-
38-
useEffect( () => {
39-
return () => {
40-
const { ownerDocument } = contentRef.current;
41-
const infotips = ownerDocument.querySelectorAll( 'tatva-infotip' );
42-
infotips.forEach( ( infotip ) => {
43-
if ( infotip && typeof infotip.hideTooltip === 'function' ) {
44-
infotip.hideTooltip();
45-
}
46-
} );
47-
};
48-
}, [ contentRef ] );
35+
}) {
36+
const [isSettingOpen, setIsSettingOpen] = useState(false);
4937

5038
return (
5139
<>
5240
<RichTextToolbarButton
53-
icon={ <InfotipIcon /> }
54-
title={ __( 'Infotip', 'blablablocks-formats' ) }
55-
onClick={ () => setIsSettingOpen( true ) }
56-
isActive={ isActive }
41+
icon={<InfotipIcon />}
42+
title={__('Infotip', 'blablablocks-formats')}
43+
onClick={() => setIsSettingOpen(true)}
44+
isActive={isActive}
5745
/>
58-
{ isSettingOpen && (
46+
{isSettingOpen && (
5947
<InlineUI
60-
activeAttributes={ activeAttributes }
61-
onClose={ () => setIsSettingOpen( false ) }
62-
contentRef={ contentRef.current }
63-
isActive={ isActive }
64-
value={ value }
65-
onChange={ onChange }
66-
onFocus={ onFocus }
48+
activeAttributes={activeAttributes}
49+
onClose={() => setIsSettingOpen(false)}
50+
contentRef={contentRef.current}
51+
isActive={isActive}
52+
value={value}
53+
onChange={onChange}
54+
onFocus={onFocus}
6755
/>
68-
) }
56+
)}
6957
</>
7058
);
7159
}

src/infotip/inline-ui.js

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import { __ } from '@wordpress/i18n';
55
import { useAnchor } from '@wordpress/rich-text';
6+
import { useEffect } from '@wordpress/element';
67
import { Popover, TabPanel } from '@wordpress/components';
78

89
/**
@@ -23,85 +24,97 @@ import { createFormatHelpers } from '../utils';
2324
* @param {boolean} props.isActive - Indicates if the format is currently active.
2425
* @return {JSX.Element} - The rendered InlineUI component.
2526
*/
26-
function InlineUI( {
27+
function InlineUI({
2728
activeAttributes,
2829
value,
2930
onChange,
3031
onClose,
3132
contentRef,
3233
isActive,
33-
} ) {
34+
}) {
3435
const name = 'blablablocks/infotip'; // Format type name
3536

36-
const { update, remove } = createFormatHelpers( {
37+
const { update, remove } = createFormatHelpers({
3738
value,
3839
onChange,
3940
formatType: name,
4041
activeAttributes,
41-
} );
42+
});
4243

43-
const anchor = useAnchor( {
44+
const anchor = useAnchor({
4445
editableContentElement: contentRef,
4546
settings: { isActive },
46-
} );
47+
});
48+
49+
useEffect(() => {
50+
return () => {
51+
const { ownerDocument } = contentRef.current;
52+
const infotips = ownerDocument.querySelectorAll('tatva-infotip');
53+
infotips.forEach((infotip) => {
54+
if (infotip && typeof infotip.hideTooltip === 'function') {
55+
infotip.hideTooltip();
56+
}
57+
});
58+
};
59+
}, [contentRef]);
4760

4861
return (
4962
<Popover
50-
anchor={ anchor }
63+
anchor={anchor}
5164
className="block-editor-format-toolbar__blablablocks-infotip-popover"
5265
position="middle center"
53-
onClose={ onClose }
54-
offset={ 30 }
66+
onClose={onClose}
67+
offset={30}
5568
shift
5669
__unstableSlotName="__unstable-block-tools-after"
5770
>
5871
<TabPanel
5972
className="block-editor-format-toolbar__blablablocks-infotip-tab-panel"
60-
tabs={ [
73+
tabs={[
6174
{
6275
name: 'text',
63-
title: __( 'Text', 'blablablocks-formats' ),
76+
title: __('Text', 'blablablocks-formats'),
6477
content: (
6578
<TextTab
66-
activeAttributes={ activeAttributes }
67-
name={ name }
68-
onChange={ onChange }
69-
onClose={ onClose }
70-
removeAttributes={ remove }
71-
updateAttributes={ update }
72-
value={ value }
79+
activeAttributes={activeAttributes}
80+
name={name}
81+
onChange={onChange}
82+
onClose={onClose}
83+
removeAttributes={remove}
84+
updateAttributes={update}
85+
value={value}
7386
/>
7487
),
7588
},
7689
{
7790
name: 'overlay',
78-
title: __( 'Overlay', 'blablablocks-formats' ),
91+
title: __('Overlay', 'blablablocks-formats'),
7992
content: (
8093
<OverlayTab
81-
activeAttributes={ activeAttributes }
82-
updateAttributes={ update }
83-
removeAttributes={ remove }
94+
activeAttributes={activeAttributes}
95+
updateAttributes={update}
96+
removeAttributes={remove}
8497
/>
8598
),
86-
disabled: ! activeAttributes.content,
99+
disabled: !activeAttributes.content,
87100
},
88101
{
89102
name: 'icon',
90-
title: __( 'Icon', 'blablablocks-formats' ),
103+
title: __('Icon', 'blablablocks-formats'),
91104
content: (
92105
<IconTab
93-
activeAttributes={ activeAttributes }
94-
updateAttributes={ update }
95-
removeAttributes={ remove }
106+
activeAttributes={activeAttributes}
107+
updateAttributes={update}
108+
removeAttributes={remove}
96109
/>
97110
),
98111
disabled:
99-
! activeAttributes.content &&
100-
! activeAttributes[ 'icon-enabled' ],
112+
!activeAttributes.content &&
113+
!activeAttributes['icon-enabled'],
101114
},
102-
] }
115+
]}
103116
>
104-
{ ( tab ) => tab.content }
117+
{(tab) => tab.content}
105118
</TabPanel>
106119
</Popover>
107120
);

0 commit comments

Comments
 (0)