Skip to content

Commit

Permalink
Styles for list box: position and scroll blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
SweetDealer committed Aug 13, 2024
1 parent 54658e2 commit fcd54d5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elsci-io/ui-essential",
"version": "1.0.65",
"version": "1.0.66",
"description": "Material Design components created for products built by elsci.io",
"main": "src/index.js",
"type": "module",
Expand Down
31 changes: 28 additions & 3 deletions src/ListBox/ListBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class ListBox extends HTMLElement {
#callbacks = {
onOptionClick: []
}
#preventScrollFunction;

/**
* @returns {{displayName:string}[]}
Expand Down Expand Up @@ -55,12 +56,14 @@ export default class ListBox extends HTMLElement {
}

show() {
this.style.display = 'block';
document.addEventListener("wheel", this.#preventScrollFunction, {capture: true, passive: false});
this.toggleAttribute('open', true)
this.#updatePosition();
}

hide() {
this.style.display = 'none';
document.removeEventListener("wheel", this.#preventScrollFunction, {capture: true, passive: false});
this.toggleAttribute('open', false)
this.#resetCurrentSelection();
}

Expand Down Expand Up @@ -102,7 +105,7 @@ export default class ListBox extends HTMLElement {
}

isVisible() {
return window.getComputedStyle(this).display !== 'none'
return this.hasAttribute('open')
}

onOptionClick(cb) {
Expand All @@ -113,6 +116,26 @@ export default class ListBox extends HTMLElement {
this.innerHTML = this.#htmlTemplate();
this.#listElement = this.querySelector("ul");
this.#addListeners();
// This function is needed to stop scrolling all page, except of list-box
// Otherwise select and typeahead inputs can be scrolled, but list-box fixed in the page
this.#preventScrollFunction = (evt)=> {
if (!this.contains(evt.target)) {
// When scrolling outside the list-box
evt.stopPropagation();
evt.preventDefault();
} else {
const isScrollingUp = evt.deltaY < 0;
const isScrollingDown = evt.deltaY > 0;
const isListScrolledToItsTop = this.#listElement.scrollTop === 0;
const isListScrolledToItsBottom = this.#listElement.scrollTop + this.#listElement.clientHeight >= this.#listElement.scrollHeight;
const nothingToScrollUp = isScrollingUp & isListScrolledToItsTop;
const nothingToScrollDown = isScrollingDown & isListScrolledToItsBottom;
if (nothingToScrollUp || nothingToScrollDown){
evt.stopPropagation();
evt.preventDefault();
}
}
};
}

/**
Expand Down Expand Up @@ -167,6 +190,8 @@ export default class ListBox extends HTMLElement {
this.#listElement.style.right = null;
}
this.#listElement.style.width = `${Math.min(widthToBe, document.documentElement.clientWidth)}px`;
this.style.width = `${Math.min(widthToBe, document.documentElement.clientWidth)}px`;
this.style.top = `${parentClientRect.top}px`;
}

#getSelectedElement() {
Expand Down
11 changes: 9 additions & 2 deletions src/SelectInput/SelectInput.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ select-input {

select-input list-box {
display: none;
margin-top: -0.875rem;
position: relative;
margin-top: 3rem;
// We use a fixed position because we want to specify the location ourselves (related to the browser window,
// not closest positioned relative)
position: fixed;
z-index: 9;
}

select-input list-box[open] {
display: block;
}

select-input text-input {
Expand Down
11 changes: 9 additions & 2 deletions src/TypeAheadInput/TypeAheadInput.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ typeahead-input {

typeahead-input list-box {
display: none;
margin-top: -0.825rem;
position: relative;
margin-top: 3rem;
// We use a fixed position because we want to specify the location ourselves (related to the browser window,
// not closest positioned relative)
position: fixed;
z-index: 9;
}

typeahead-input list-box[open] {
display: block;
}

typeahead-input.dropdown-select text-input .text-input__input {
Expand Down

0 comments on commit fcd54d5

Please sign in to comment.