-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from JohnsonMao/feature/toc-and-collapse
Feature/toc and collapse
- Loading branch information
Showing
12 changed files
with
260 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use client'; | ||
|
||
import { useRef, ReactNode, HTMLAttributes } from 'react'; | ||
import { useSpring, animated } from '@react-spring/web'; | ||
|
||
type CollapseProps = { | ||
isOpen: boolean; | ||
children: ReactNode; | ||
} & HTMLAttributes<HTMLElement>; | ||
|
||
export default function Collapse({ | ||
isOpen, | ||
children, | ||
...props | ||
}: CollapseProps) { | ||
const childrenRef = useRef<HTMLDivElement>(null); | ||
const styles = useSpring({ | ||
to: { | ||
height: isOpen ? childrenRef.current?.clientHeight : 0, | ||
opacity: isOpen ? 1 : 0.6, | ||
}, | ||
}); | ||
|
||
return ( | ||
<animated.div className="overflow-hidden" style={styles} {...props}> | ||
<div ref={childrenRef}>{children}</div> | ||
</animated.div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import { Globals } from '@react-spring/web'; | ||
|
||
import Collapse from '.'; | ||
|
||
describe('Collapse component', () => { | ||
beforeAll(() => { | ||
Globals.assign({ | ||
skipAnimation: true, | ||
}); | ||
}); | ||
|
||
it('should render correct element', async () => { | ||
render( | ||
<Collapse isOpen data-testid="collapse"> | ||
Test | ||
</Collapse> | ||
); | ||
const collapse = screen.getByTestId('collapse'); | ||
|
||
expect(collapse).toBeInTheDocument(); | ||
expect(collapse).toHaveTextContent('Test'); | ||
}); | ||
|
||
it('should update Collapse component styles on re-render', async () => { | ||
const { rerender } = render( | ||
<Collapse isOpen={false} data-testid="collapse"> | ||
Test | ||
</Collapse> | ||
); | ||
const collapse = screen.getByTestId('collapse'); | ||
|
||
await waitFor(() => expect(collapse).toHaveStyle('opacity: 0.6')); | ||
|
||
rerender( | ||
<Collapse isOpen={true} data-testid="collapse"> | ||
Test | ||
</Collapse> | ||
); | ||
|
||
await waitFor(() => expect(collapse).toHaveStyle('opacity: 1')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Collapse from './Collapse'; | ||
|
||
export default Collapse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.