Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added customization for avoiding overlapping of BlockChooser #421

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
### Fix

- Nei template dei feed RSS ora viene mostrata la sorgente se presente.
- Sistemata sovrapposizione della finestra di selezione blocchi con altri blocchi e con l'header del sito.

## Versione 7.22.2 (11/10/2023)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/* CUSTOMIZATIONS
- zindex for handling overlapping in small pages
*/

import React from 'react';
import { doesNodeContainClick } from 'semantic-ui-react/dist/commonjs/lib';
import addSVG from '@plone/volto/icons/circle-plus.svg';
import { blockHasValue } from '@plone/volto/helpers';
import { Icon, BlockChooser } from '@plone/volto/components';
import config from '@plone/volto/registry';
import { Button, Ref } from 'semantic-ui-react';
import { defineMessages, useIntl } from 'react-intl';
import { usePopper } from 'react-popper';
import { Portal } from 'react-portal';

const messages = defineMessages({
addBlock: {
id: 'Add block',
defaultMessage: 'Add block',
},
});

export const ButtonComponent = (props) => {
const intl = useIntl();
const {
className = `block-add-button${
config.experimental.addBlockButton.enabled ? ' new-add-block' : ''
}`,
size = '19px',
onShowBlockChooser,
} = props;

return (
<Button
icon
basic
title={intl.formatMessage(messages.addBlock)}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
onShowBlockChooser();
}}
className={className}
>
<Icon name={addSVG} className={className} size={size} />
</Button>
);
};

const BlockChooserButton = (props) => {
const {
block,
allowedBlocks,
showRestricted,
data,
onMutateBlock,
onInsertBlock,
blocksConfig,
buttonComponent,
properties,
} = props;
const { disableNewBlocks } = data;
const [addNewBlockOpened, setAddNewBlockOpened] = React.useState(false);

const blockChooserRef = React.useRef();

const handleClickOutside = React.useCallback((e) => {
if (
blockChooserRef.current &&
doesNodeContainClick(blockChooserRef.current, e)
)
return;
setAddNewBlockOpened(false);
}, []);

const Component = buttonComponent || ButtonComponent;

React.useEffect(() => {
document.addEventListener('mousedown', handleClickOutside, false);
return () => {
document.removeEventListener('mousedown', handleClickOutside, false);
};
}, [handleClickOutside]);

const [referenceElement, setReferenceElement] = React.useState(null);
const [popperElement, setPopperElement] = React.useState(null);
const { styles, attributes } = usePopper(referenceElement, popperElement, {
placement: config.experimental.addBlockButton.enabled
? 'bottom'
: 'right-start',
modifiers: [
{
name: 'offset',
options: {
offset: [-10, 5],
},
},
{
name: 'flip',
options: {
fallbackPlacements: ['right-end', 'top-start'],
},
},
],
});

return (
<>
{!disableNewBlocks &&
(config.experimental.addBlockButton.enabled ||
!blockHasValue(data)) && (
<Ref innerRef={setReferenceElement}>
<Component
{...props}
onShowBlockChooser={() => setAddNewBlockOpened(true)}
/>
</Ref>
)}
{addNewBlockOpened && (
<Portal node={document.getElementById('body')}>
<div
ref={setPopperElement}
style={{ ...styles.popper, zIndex: 1000 }}
{...attributes.popper}
>
<BlockChooser
onMutateBlock={
onMutateBlock
? (id, value) => {
setAddNewBlockOpened(false);
onMutateBlock(id, value);
}
: null
}
onInsertBlock={
onInsertBlock
? (id, value) => {
setAddNewBlockOpened(false);
onInsertBlock(id, value);
}
: null
}
currentBlock={block}
allowedBlocks={allowedBlocks}
blocksConfig={blocksConfig}
properties={properties}
showRestricted={showRestricted}
ref={blockChooserRef}
/>
</div>
</Portal>
)}
</>
);
};

export default BlockChooserButton;