Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(next-release/main): use Amplify UI components for Accordion #6079

Merged
merged 10 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
initial commit
  • Loading branch information
hbuchel committed Oct 24, 2023
commit fa7a518c9c97d5e6e5760b7e790d140c7abdbb7a
2 changes: 1 addition & 1 deletion mdx-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ExportedImage from 'next-image-export-optimizer';

import InlineFilter from './src/components/InlineFilter';
import { YoutubeEmbed } from './src/components/YoutubeEmbed';
import Accordion from './src/components/Accordion';
import { Accordion } from './src/components/Accordion';
import { Block, BlockSwitcher } from './src/components/BlockSwitcher';
import { Callout } from './src/components/Callout';
import Fragments from './src/components/Fragments';
Expand Down
160 changes: 160 additions & 0 deletions src/components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { useRef, useState, createElement, useEffect } from 'react';
import { trackExpanderOpen } from '../../utils/track';
import { IconExpand, IconChevron } from '@/components/Icons';
import {
View,
Flex,
Button,
Link,
Text,
VisuallyHidden
} from '@aws-amplify/ui-react';
import { useRouter } from 'next/router';

type AccordionProps = {
title?: string;
headingLevel?: '2' | '3' | '4' | '5' | '6';
eyebrow?: string;
children?: React.ReactNode;
};

export const Accordion: React.FC<AccordionProps> = ({
title,
headingLevel,
eyebrow,
children
}) => {
const [initialHeight, setInitialHeight] = useState<number>(0);
const [expandedHeight, setExpandedHeight] = useState<number>(0);
const detailsRef = useRef<HTMLDetailsElement>(null);
const summaryRef = useRef<HTMLElement>(null);
const path = useRouter().asPath;
const headingId = title?.replace(/\s+/g, '-').toLowerCase();
const headingHref = path + headingId;
const headingEl: React.ElementType = headingLevel
? `h${headingLevel}`
: 'div';
const isLinkableHeading = ['h2', 'h3'].includes(headingEl);

useEffect(() => {
const details = detailsRef.current;
const summary = summaryRef.current;
if (summary && details) {
const initHeight = summary.offsetHeight;
const expHeight = getHiddenHeight(details);

setInitialHeight(initHeight);
setExpandedHeight(expHeight);
}
}, [detailsRef, summaryRef, initialHeight, expandedHeight]);

function getHiddenHeight(el) {
if (!el?.cloneNode) {
return null;
}

const clone = el.cloneNode(true);
clone.setAttribute('open', '');
el.after(clone);
const height = clone.offsetHeight;
clone.remove();
return height;
}

const collapse = [
{
maxHeight: expandedHeight + 'px'
},
{ maxHeight: initialHeight + 'px' }
];

const expand = [
{ maxHeight: initialHeight + 'px' },
{
maxHeight: expandedHeight + 'px'
}
];

const animationTiming = {
duration: 700,
iterations: 1
};

const closeAccordion = () => {
const expander = detailsRef.current;
if (expander) {
const scrollToLoc = expander.offsetTop - 48 - 70 - 10; // account for nav heights and 10px buffer

expander.animate(collapse, animationTiming);
window.scrollTo({
left: 0,
top: scrollToLoc,
behavior: 'smooth'
});
setTimeout(function () {
expander.removeAttribute('open');
}, 700);
}
};

const toggleAccordion = (e) => {
e.preventDefault();

const expander = detailsRef.current;
// Close accordion
if (expander?.hasAttribute('open')) {
expander?.animate(collapse, animationTiming);
setTimeout(function () {
expander.removeAttribute('open');
}, 700);
} else {
// Open accordion
trackExpanderOpen(expander?.id.replace('-acc', ''));
expander?.setAttribute('open', '');
expander?.animate(expand, animationTiming);
}
};

return (
<View
as="details"
id={headingId + '-acc'}
className="accordion"
ref={detailsRef}
>
<Flex
as="summary"
id="accordion__summary"
className="accordion__summary"
ref={summaryRef}
onClick={toggleAccordion}
>
<Flex className="accordion__summary__inner">
<Flex className="accordion__eyebrow">
<IconExpand />
<Text as="span">{eyebrow}</Text>
</Flex>
<View as={headingEl}>
{isLinkableHeading ? (
<Link href={headingHref}>{title}</Link>
) : (
title
)}
</View>
</Flex>
<IconChevron className="accordion__chevron" />
</Flex>
<View id="accordion__body" className="accordion__body">
{children}
</View>

<Button
id="accordion__body__button"
className="accordion__body__button"
onClick={closeAccordion}
>
<IconChevron />
</Button>
</View>
);
};
18 changes: 0 additions & 18 deletions src/components/Accordion/icons.tsx

This file was deleted.

1 change: 1 addition & 0 deletions src/components/Accordion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Accordion } from './Accordion';
164 changes: 0 additions & 164 deletions src/components/Accordion/index.tsx

This file was deleted.

Loading