Skip to content

Commit

Permalink
fix(active-desendant): item scroll when not in view (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
saiponnada authored Jan 28, 2025
1 parent ba4cca1 commit 64355ba
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 18 deletions.
9 changes: 7 additions & 2 deletions docs/core/makeup-active-descendant/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/core/makeup-active-descendant/index.min.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions docs/ui/makeup-combobox/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/ui/makeup-combobox/index.min.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions docs/ui/makeup-listbox-button/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/ui/makeup-listbox-button/index.min.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions docs/ui/makeup-listbox/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/ui/makeup-listbox/index.min.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions packages/core/makeup-active-descendant/dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const defaultOptions = {
axis: "both",
wrap: false
};
function isElementInView(el) {
const bounding = el.getBoundingClientRect();
return bounding.top >= 0 && bounding.left >= 0 && bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) && bounding.right <= (window.innerWidth || document.documentElement.clientWidth);
}
function onModelInit(e) {
const {
items,
Expand Down Expand Up @@ -44,9 +48,10 @@ function onModelChange(e) {
if (toItem) {
toItem.classList.add(this._options.activeDescendantClassName);
this._focusEl.setAttribute("aria-activedescendant", toItem.id);
if (this._options.autoScroll && this._itemContainerEl) {
if (this._options.autoScroll && this._itemContainerEl && !isElementInView(toItem)) {
toItem.scrollIntoView({
block: "center"
behavior: "smooth",
block: "nearest"
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/core/makeup-active-descendant/dist/mjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const defaultOptions = {
axis: "both",
wrap: false
};
function isElementInView(el) {
const bounding = el.getBoundingClientRect();
return bounding.top >= 0 && bounding.left >= 0 && bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) && bounding.right <= (window.innerWidth || document.documentElement.clientWidth);
}
function onModelInit(e) {
const { items, toIndex } = e.detail;
const itemEl = items[toIndex];
Expand All @@ -27,8 +31,8 @@ function onModelChange(e) {
if (toItem) {
toItem.classList.add(this._options.activeDescendantClassName);
this._focusEl.setAttribute("aria-activedescendant", toItem.id);
if (this._options.autoScroll && this._itemContainerEl) {
toItem.scrollIntoView({ block: "center" });
if (this._options.autoScroll && this._itemContainerEl && !isElementInView(toItem)) {
toItem.scrollIntoView({ behavior: "smooth", block: "nearest" });
}
}
this._el.dispatchEvent(new CustomEvent("activeDescendantChange", { detail: e.detail }));
Expand Down
14 changes: 12 additions & 2 deletions packages/core/makeup-active-descendant/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ const defaultOptions = {
wrap: false,
};

function isElementInView(el) {
const bounding = el.getBoundingClientRect();
return (
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}

function onModelInit(e) {
const { items, toIndex } = e.detail;
const itemEl = items[toIndex];
Expand All @@ -37,8 +47,8 @@ function onModelChange(e) {
toItem.classList.add(this._options.activeDescendantClassName);
this._focusEl.setAttribute("aria-activedescendant", toItem.id);

if (this._options.autoScroll && this._itemContainerEl) {
toItem.scrollIntoView({ block: "center" });
if (this._options.autoScroll && this._itemContainerEl && !isElementInView(toItem)) {
toItem.scrollIntoView({ behavior: "smooth", block: "nearest" });
}
}

Expand Down

0 comments on commit 64355ba

Please sign in to comment.