Skip to content

Commit 4b543e4

Browse files
authored
fix v2 drawer scrolling issues (#1908)
* pass scroll as a boolean argument on Link * set scroll={false} for NextLinks used within the drawer * always scroll to the top of the drawer on load * fix typo * pass resourceId as a parameter to LearningResourceExpandedV2
1 parent 9e47922 commit 4b543e4

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

frontends/main/src/page-components/LearningResourceDrawer/LearningResourceDrawerV2.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const DrawerContent: React.FC<{
115115
<>
116116
<LearningResourceExpandedV2
117117
imgConfig={imgConfigs.large}
118+
resourceId={resourceId}
118119
resource={resource.data}
119120
carousels={[similarResourcesCarousel]}
120121
user={user}

frontends/ol-components/src/components/LearningResourceExpanded/InfoSectionV2.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ const RunDates: React.FC<{ resource: LearningResource }> = ({ resource }) => {
196196
const showMoreLink = (
197197
<NoWrap>
198198
<ShowHideLink
199+
scroll={false}
199200
color="red"
200201
size="small"
201202
onClick={() => setShowingMore(!showingMore)}

frontends/ol-components/src/components/LearningResourceExpanded/LearningResourceExpandedV2.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const setup = (resource: LearningResource, isLearningPathEditor?: boolean) => {
3535
return render(
3636
<BrowserRouter>
3737
<LearningResourceExpandedV2
38+
resourceId={resource.id}
3839
resource={resource}
3940
user={user}
4041
shareUrl={`https://learn.mit.edu/search?resource=${resource.id}`}

frontends/ol-components/src/components/LearningResourceExpanded/LearningResourceExpandedV2.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useRef, useState } from "react"
1+
import React, { useCallback, useEffect, useRef, useState } from "react"
22
import styled from "@emotion/styled"
33
import Skeleton from "@mui/material/Skeleton"
44
import { default as NextImage } from "next/image"
@@ -292,6 +292,7 @@ const CarouselContainer = styled.div({
292292
})
293293

294294
type LearningResourceExpandedV2Props = {
295+
resourceId: number
295296
resource?: LearningResource
296297
user?: User
297298
shareUrl?: string
@@ -658,13 +659,18 @@ const ResourceDescription = ({ resource }: { resource?: LearningResource }) => {
658659
data-testid="drawer-description-text"
659660
ref={descriptionRendered}
660661
/**
661-
* Resource descriptions can contain HTML. They are santiized on the
662+
* Resource descriptions can contain HTML. They are sanitized on the
662663
* backend during ETL. This is safe to render.
663664
*/
664665
dangerouslySetInnerHTML={{ __html: resource.description || "" }}
665666
/>
666667
{(isClamped || clampedOnFirstRender.current) && (
667-
<Link color="red" size="small" onClick={() => setExpanded(!isExpanded)}>
668+
<Link
669+
scroll={false}
670+
color="red"
671+
size="small"
672+
onClick={() => setExpanded(!isExpanded)}
673+
>
668674
{isExpanded ? "Show less" : "Show more"}
669675
</Link>
670676
)}
@@ -673,6 +679,7 @@ const ResourceDescription = ({ resource }: { resource?: LearningResource }) => {
673679
}
674680

675681
const LearningResourceExpandedV2: React.FC<LearningResourceExpandedV2Props> = ({
682+
resourceId,
676683
resource,
677684
imgConfig,
678685
user,
@@ -684,8 +691,14 @@ const LearningResourceExpandedV2: React.FC<LearningResourceExpandedV2Props> = ({
684691
onAddToUserListClick,
685692
closeDrawer,
686693
}) => {
694+
const outerContainerRef = useRef<HTMLDivElement>(null)
695+
useEffect(() => {
696+
if (outerContainerRef.current && outerContainerRef.current.scrollTo) {
697+
outerContainerRef.current.scrollTo(0, 0)
698+
}
699+
}, [resourceId])
687700
return (
688-
<OuterContainer>
701+
<OuterContainer ref={outerContainerRef}>
689702
<TitleSection
690703
resource={resource}
691704
closeDrawer={closeDrawer ?? (() => {})}

frontends/ol-components/src/components/Link/Link.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,17 @@ type LinkProps = LinkStyleProps &
7171
* re-fetch API data.
7272
*/
7373
shallow?: boolean
74+
scroll?: boolean
7475
}
7576

76-
const BaseLink = ({ href, shallow, nohover, onClick, ...rest }: LinkProps) => {
77+
const BaseLink = ({
78+
href,
79+
shallow,
80+
nohover,
81+
scroll,
82+
onClick,
83+
...rest
84+
}: LinkProps) => {
7785
if (process.env.NODE_ENV === "development") {
7886
invariant(
7987
!shallow || href?.startsWith("?"),
@@ -82,6 +90,7 @@ const BaseLink = ({ href, shallow, nohover, onClick, ...rest }: LinkProps) => {
8290
}
8391
return (
8492
<NextLink
93+
scroll={scroll}
8594
href={href || ""}
8695
{...rest}
8796
onClick={

0 commit comments

Comments
 (0)