Skip to content

Commit 4aad2a3

Browse files
Merge pull request #1562 from ASU/uds-1316a
Uds 1316: fix long accordion scroll issue
2 parents d776141 + b8ecdf2 commit 4aad2a3

File tree

16 files changed

+257
-818
lines changed

16 files changed

+257
-818
lines changed

packages/shared/utils/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from "./numbers";
66
export * from "./script-utils";
77
export * from "./timers";
88
export * from "./deep-cloner";
9+
export * from "./scroll-helper";

packages/shared/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from "./numbers";
66
export * from "./script-utils";
77
export * from "./timers";
88
export * from "./deep-cloner";
9+
export * from "./scroll-helper";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function adjustShrinkingElementIfAboveViewport(element: any): void;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
export function adjustShrinkingElementIfAboveViewport(element) {
3+
if (element && typeof element.getBoundingClientRect === "function") {
4+
const { bottom: inViewportHeight, top } = element.getBoundingClientRect();
5+
const { offsetTop } = element;
6+
if (top < 0 && inViewportHeight > 0) {
7+
const savedStyle = element.style;
8+
element.style = `${savedStyle};transition: height 0s !important; overflow:hidden; height:${inViewportHeight}px!important;max-height:${inViewportHeight}px!important`;
9+
window.scrollTo({ behavior: "instant", top: offsetTop });
10+
setTimeout(() => {
11+
element.style = savedStyle;
12+
window.scrollTo({
13+
behavior: "smooth",
14+
top: element.offsetTop - inViewportHeight / 2,
15+
});
16+
}, 50);
17+
}
18+
}
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { adjustShrinkingElementIfAboveViewport } from "@asu/shared";
2+
3+
import { EventHandler } from "./bootstrap-helper";
4+
5+
function initCollapse() {
6+
function accordionClick({ target }) {
7+
const targetHref = target.getAttribute("href");
8+
if (
9+
target.getAttribute("data-bs-toggle") !== "collapse" ||
10+
!targetHref ||
11+
!targetHref.includes("#")
12+
) {
13+
return; // Exit the function - not a collapse link
14+
}
15+
16+
/**
17+
* Find the first accordion that is currently collapsing or expanding
18+
* Function will determine if scroll adjustment is needed
19+
*/
20+
adjustShrinkingElementIfAboveViewport(
21+
document.querySelector(".collapsing")
22+
);
23+
}
24+
EventHandler.on(document, "click.uds.collapse", accordionClick);
25+
}
26+
27+
EventHandler.on(window, "load.uds.collapse", initCollapse);
28+
29+
export { initCollapse };

packages/unity-bootstrap-theme/src/js/unity-bootstrap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { initCalendar } from "./calendar.js";
55
import { initCardBodies } from "./card-bodies.js";
66
import { initRankingCard } from "./card-ranking.js";
77
import { initChart } from "./charts-and-graphs.js";
8+
import { initCollapse } from "./collapse.js";
89
import { initDataLayer } from "./data-layer.js";
910
import { initGlobalHeader } from "./global-header.js";
1011
import { initHeroesVideo } from "./heroes-video.js";
@@ -19,6 +20,7 @@ const unityBootstrap = {
1920
initAnchorMenu,
2021
initBlockquoteAnimation,
2122
initCalendar,
23+
initCollapse,
2224
initChart,
2325
initDataLayer,
2426
initFixedTable,

packages/unity-bootstrap-theme/src/scss/extends/_cards.scss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,13 @@ Cards - Table of Contents
696696
7. Accordion
697697
--------------------------------------------------------------------*/
698698
.accordion {
699-
699+
&:focus {
700+
outline: none!important;
701+
box-shadow: 0 0 0 1px $uds-color-base-gray-7 !important;
702+
border: 1px solid $uds-color-base-gray-7 !important;
703+
margin: 4px;
704+
width: -webkit-fill-available;
705+
}
700706
.accordion-item {
701707
border-bottom: 1px solid $uds-color-base-gray-3;
702708
border-top: 1px solid $uds-color-base-gray-3;

0 commit comments

Comments
 (0)