Skip to content

Commit d824790

Browse files
fix(unity-bootstrap-theme): fix long accordion scroll issue
# Conflicts: # packages/shared/utils/scroll-helper.d.ts # packages/shared/utils/scroll-helper.js # packages/unity-bootstrap-theme/src/scss/extends/_cards.scss # packages/unity-bootstrap-theme/stories/atoms/accordion/accordion.examples.stories.js # packages/unity-bootstrap-theme/stories/atoms/accordion/accordion.templates.stories.js # packages/unity-bootstrap-theme/stories/atoms/accordion/accordion.test.js # shared/utils/index.js
1 parent 861e2c1 commit d824790

File tree

11 files changed

+58
-749
lines changed

11 files changed

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