Skip to content

Commit

Permalink
fix(anchor): affichage de l'ancre au niveau de l'accordéon sur les `f…
Browse files Browse the repository at this point in the history
…iches-ministère-travail` (#5831)
  • Loading branch information
maxgfr committed May 7, 2024
1 parent 059572e commit 0bf84f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ function Fiche(props: Props): JSX.Element {
}, [sections]);

const { asPath } = useRouter();
const anchor = asPath.split("#")[1];
const [anchor, setAnchor] = React.useState<string[]>([]);

React.useEffect(() => {
const hash = asPath.split("#")[1];
if (hash) setAnchor([hash]);
}, [asPath]);

// titleless section have the page title but no anchor.
const untitledSection = sections.find((section) => !section.anchor);
Expand All @@ -79,8 +84,9 @@ function Fiche(props: Props): JSX.Element {
>
{untitledSection && <Html>{untitledSection.html}</Html>}
<Accordion
key={anchor.join(",")} // Add key prop to force rerender when anchor array changes
titleLevel={2}
preExpanded={[anchor]}
preExpanded={anchor}
items={titledSections}
/>
</StyledAnswer>
Expand Down
16 changes: 15 additions & 1 deletion packages/react-ui/src/Accordion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@ export const Accordion = ({
const AccordionItemButton = variants[variant].ItemButton;
const AccordionItemPanel = variants[variant].ItemPanel;
/* eslint-enable */

React.useEffect(() => {
if (props?.preExpanded?.length && props.preExpanded[0]?.length) {
const anchor = document?.querySelector(`#${props.preExpanded[0]}`);
if (anchor) {
anchor.scrollIntoView();
}
}
}, [props.preExpanded]);
return (
<AccordionVariant allowZeroExpanded allowMultipleExpanded {...props}>
<AccordionVariant
{...props}
allowZeroExpanded
allowMultipleExpanded
preExpanded={props.preExpanded ?? []}
>
{items.map(({ body, icon, id, title }, index) => (
<div id={id} key={`${id}-${index}`}>
<AccordionItem
Expand Down

0 comments on commit 0bf84f4

Please sign in to comment.