Skip to content

Commit 76931bf

Browse files
domhhvsnowystinger
andauthored
fix(s2-docs): auto-scroll and expand section for group selected nav link (adobe#9664)
* fix(s2-docs): auto-scroll and expand section for group selected nav link * refactor(s2-docs): utilize scrollIntoViewport with added behavior and alignment options * refactor(s2-docs): remove redundant type annotation and verbose boolean comparison * fix: remove behavior option * go back to native scroll into view --------- Co-authored-by: Robert Snow <snowystinger@gmail.com>
1 parent fde2a81 commit 76931bf

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

packages/dev/s2-docs/src/Nav.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export function Nav() {
188188
);
189189
}
190190
return (
191-
<Disclosure id={name} key={name} isQuiet density="spacious" defaultExpanded={name === 'Components' || name === currentPage.exports?.section} styles={style({minWidth: 185})}>
191+
<Disclosure id={name} key={name} isQuiet density="spacious" defaultExpanded={name === 'Components' || name === currentPage.exports?.section || name === currentPage.exports?.group} styles={style({minWidth: 185})}>
192192
<DisclosureTitle>{name}</DisclosureTitle>
193193
<DisclosurePanel>
194194
<div className={style({paddingStart: space(18)})}>{nav}</div>
@@ -255,10 +255,19 @@ export function SideNavItem(props) {
255255
}
256256

257257
export function SideNavLink(props) {
258-
let linkRef = useRef(null);
258+
let linkRef = useRef<HTMLAnchorElement | null>(null);
259259
let selected = useContext(SideNavContext);
260260
let {isExternal, ...linkProps} = props;
261-
261+
262+
useEffect(() => {
263+
let link = linkRef.current;
264+
if (!link || !props.isSelected) {
265+
return;
266+
}
267+
268+
link.scrollIntoView({block: 'start', behavior: 'smooth'});
269+
}, [props.isSelected]);
270+
262271
return (
263272
<BaseLink
264273
{...linkProps}
@@ -283,7 +292,8 @@ export function SideNavLink(props) {
283292
},
284293
textDecoration: 'none',
285294
borderRadius: 'default',
286-
transition: 'default'
295+
transition: 'default',
296+
scrollMarginTop: 64
287297
})}>
288298
{(renderProps) => (<>
289299
<span

0 commit comments

Comments
 (0)