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

IBX-6718: Missing scroll in language dropdown #934

Merged
merged 1 commit into from
Oct 17, 2023
Merged
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
46 changes: 34 additions & 12 deletions src/bundle/ui-dev/src/modules/common/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import PropTypes from 'prop-types';
import { createCssClassNames } from '../../common/helpers/css.class.names';
import Icon from '../../common/icon/icon';

const { Translator } = window;
const { Translator, document } = window;
const MIN_SEARCH_ITEMS_DEFAULT = 5;
const MIN_ITEMS_LIST_HEIGHT = 150;
const ITEMS_LIST_WIDGET_MARGIN = 8;
const ITEMS_LIST_SITE_MARGIN = ITEMS_LIST_WIDGET_MARGIN + 4;

const Dropdown = ({ dropdownListRef, value, options, onChange, small, single, extraClasses, renderSelectedItem, minSearchItems }) => {
const containerRef = useRef();
const containerItemsRef = useRef();
const [isExpanded, setIsExpanded] = useState(false);
const [filterText, setFilterText] = useState('');
const [itemsListStyles, setItemsListStyles] = useState({});
const selectedItem = options.find((option) => option.value === value);
const dropdownClassName = createCssClassNames({
'ibexa-dropdown': true,
Expand All @@ -22,6 +26,7 @@ const Dropdown = ({ dropdownListRef, value, options, onChange, small, single, ex
[extraClasses]: true,
});
const toggleExpanded = () => {
calculateAndSetItemsListStyles();
setIsExpanded((prevState) => !prevState);
};
const updateFilterValue = (event) => setFilterText(event.target.value);
Expand Down Expand Up @@ -59,24 +64,36 @@ const Dropdown = ({ dropdownListRef, value, options, onChange, small, single, ex
</li>
);
};
const renderItemsList = () => {
const calculateAndSetItemsListStyles = () => {
const itemsStyles = {};
const { width, left, top, height, bottom } = containerRef.current.getBoundingClientRect();

itemsStyles.width = width;
itemsStyles.left = left;

if (window.innerHeight - bottom > MIN_ITEMS_LIST_HEIGHT) {
itemsStyles.top = top + height + ITEMS_LIST_WIDGET_MARGIN;
itemsStyles.maxHeight = window.innerHeight - bottom - ITEMS_LIST_SITE_MARGIN;
} else {
const headerContainer = document.querySelector('.ibexa-main-header');
Copy link
Contributor

@tischsoic tischsoic Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we were wondering in last few days why ibexa-main-header offsetHeight was introduced so if you also wonder, you can find the explanation in this comment: #1183 (comment)

const headerHeight = headerContainer.offsetHeight;

itemsStyles.top = top - ITEMS_LIST_WIDGET_MARGIN;
itemsStyles.maxHeight = top - headerHeight - ITEMS_LIST_SITE_MARGIN;
itemsStyles.transform = 'translateY(-100%)';
}

setItemsListStyles(itemsStyles);
};
const renderItemsList = () => {
const placeholder = Translator.trans(/*@Desc("Search...")*/ 'dropdown.placeholder', {}, 'universal_discovery_widget');
const itemsContainerClass = createCssClassNames({
'ibexa-dropdown__items': true,
'ibexa-dropdown__items--search-hidden': options.length < minSearchItems,
});

if (containerRef.current) {
const { width, left, top, height } = containerRef.current.getBoundingClientRect();

itemsStyles.width = width;
itemsStyles.left = left;
itemsStyles.top = top + height + 8;
}

return (
<div className={itemsContainerClass} style={itemsStyles} ref={containerItemsRef}>
<div className={itemsContainerClass} style={itemsListStyles} ref={containerItemsRef}>
<div className="ibexa-input-text-wrapper">
<input
type="text"
Expand Down Expand Up @@ -113,6 +130,7 @@ const Dropdown = ({ dropdownListRef, value, options, onChange, small, single, ex
return;
}

const scrollContainer = document.querySelector('.ibexa-main-container__content-column');
const onInteractionOutside = (event) => {
if (containerRef.current.contains(event.target) || containerItemsRef.current?.contains(event.target)) {
return;
Expand All @@ -122,9 +140,13 @@ const Dropdown = ({ dropdownListRef, value, options, onChange, small, single, ex
};

document.body.addEventListener('click', onInteractionOutside, false);
scrollContainer.addEventListener('scroll', calculateAndSetItemsListStyles, false);

return () => {
document.body.removeEventListener('click', onInteractionOutside, false);
document.body.removeEventListener('click', onInteractionOutside);
document.body.removeEventListener('click', calculateAndSetItemsListStyles);

setItemsListStyles({});
};
}, [isExpanded]);

Expand Down