Skip to content

Commit

Permalink
fix(on-this-page): close on this page when user clicks a heading
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoAnjos16 committed Aug 2, 2022
1 parent d229bd3 commit 8f5bc78
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/components/on-this-page/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { useState } from 'react'
import { useContext } from 'react'
import { Box, Flex, Text } from '@vtex/brand-ui'
import AnimateHeight from 'react-animate-height'

import { APIGuideContext } from 'utils/contexts/api-guide'

import MenuIcon from 'components/icons/menu-icon'
import CloseIcon from 'components/icons/close-icon'

import styles from './styles'
import TableOfContents from 'components/table-of-contents'

import styles from './styles'

const OnThisPage = () => {
const [open, setOpen] = useState(false)
const { onThisPageOpenStatus, setOnThisPageOpenStatus } =
useContext(APIGuideContext)

return (
<Flex sx={styles.container}>
<AnimateHeight duration={300} height={open ? 'auto' : 0}>
<AnimateHeight duration={300} height={onThisPageOpenStatus ? 'auto' : 0}>
<Box sx={styles.contentContainer}>
<Text sx={styles.onThisPageTitle}>On this page</Text>
<Box>
Expand All @@ -24,11 +28,15 @@ const OnThisPage = () => {

<Flex
sx={styles.buttonContainer}
onClick={() => setOpen((open) => !open)}
onClick={() => setOnThisPageOpenStatus((open) => !open)}
>
<Text sx={styles.title(open)}>On this page</Text>
<Text sx={styles.title(onThisPageOpenStatus)}>On this page</Text>
<Box sx={styles.iconContainer}>
{!open ? <MenuIcon size={32} /> : <CloseIcon size={32} />}
{!onThisPageOpenStatus ? (
<MenuIcon size={32} />
) : (
<CloseIcon size={32} />
)}
</Box>
</Flex>
</Flex>
Expand Down
4 changes: 3 additions & 1 deletion src/components/table-of-contents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export interface Item extends SubItem {
}

const TableOfContents = () => {
const { headings, activeItem, setActiveItem } = useContext(APIGuideContext)
const { headings, activeItem, setActiveItem, setOnThisPageOpenStatus } =
useContext(APIGuideContext)

const Item = ({
title,
Expand All @@ -34,6 +35,7 @@ const TableOfContents = () => {
<Link href={`#${slug}`}>
<a
onClick={() => {
setOnThisPageOpenStatus(false)
setActiveItem(({ item }) => ({
item: level === 1 ? slug : item,
subItem: level === 1 ? '' : slug,
Expand Down
7 changes: 7 additions & 0 deletions src/utils/contexts/api-guide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type ActiveItem = {
type ContextType = {
headings: Item[]
activeItem: ActiveItem
onThisPageOpenStatus: boolean
setActiveItem: Dispatch<SetStateAction<ActiveItem>>
setOnThisPageOpenStatus: Dispatch<SetStateAction<boolean>>
goToPreviousItem: () => void
goToPreviousSubItem: () => void
}
Expand All @@ -22,7 +24,9 @@ export const APIGuideContext = createContext<ContextType>({
item: '',
subItem: '',
},
onThisPageOpenStatus: false,
setActiveItem: () => undefined,
setOnThisPageOpenStatus: () => undefined,
goToPreviousItem: () => undefined,
goToPreviousSubItem: () => undefined,
})
Expand All @@ -32,6 +36,7 @@ interface Props {
}

const APIGuideContextProvider: React.FC<Props> = ({ children, headings }) => {
const [onThisPageOpenStatus, setOnThisPageOpenStatus] = useState(false)
const [activeItem, setActiveItem] = useState<ActiveItem>({
item: '',
subItem: '',
Expand Down Expand Up @@ -76,7 +81,9 @@ const APIGuideContextProvider: React.FC<Props> = ({ children, headings }) => {
value={{
headings,
activeItem,
onThisPageOpenStatus,
setActiveItem,
setOnThisPageOpenStatus,
goToPreviousItem,
goToPreviousSubItem,
}}
Expand Down

0 comments on commit 8f5bc78

Please sign in to comment.