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

fix: type button and refactor SectionWrapper #518

Merged
Merged
Changes from all commits
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
47 changes: 36 additions & 11 deletions react/src/Sidebar/SidebarList.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { type PropsWithChildren, useCallback, useMemo } from 'react'
import { FC, type PropsWithChildren, useCallback, useMemo } from 'react'
import {
Box,
chakra,
Collapse,
forwardRef,
HTMLChakraProps,
Icon,
useDisclosure,
type UseDisclosureReturn,
Expand Down Expand Up @@ -54,6 +55,37 @@ export interface SidebarListProps extends BaseSidebarItemProps {
onClick?: () => void
}

type SectionWrapperProps = HTMLChakraProps<'button'> &
HTMLChakraProps<'div'> & { onlyCaretToggle: boolean }

const SectionWrapper: FC<PropsWithChildren & SectionWrapperProps> = ({
children,
onlyCaretToggle,
...props
}) => {
if (onlyCaretToggle) return <chakra.div {...props}>{children}</chakra.div>

return (
<chakra.button type="button" {...props}>
{children}
</chakra.button>
)
}
const ToggleChevronWrapper: FC<PropsWithChildren & SectionWrapperProps> = ({
children,
onlyCaretToggle,
...props
}) => {
if (onlyCaretToggle)
return (
<chakra.button type="button" {...props}>
{children}
</chakra.button>
)

return <chakra.div {...props}>{children}</chakra.div>
}

export const SidebarList = forwardRef<
PropsWithChildren<SidebarListProps>,
'li'
Expand Down Expand Up @@ -104,16 +136,6 @@ export const SidebarList = forwardRef<
return merge({}, mergedStyles, { cursor: 'pointer' })
}, [onlyCaretToggle, styles.item, styles.parent])

const SectionWrapper = useMemo(() => {
if (onlyCaretToggle) return chakra.div
return chakra.button
}, [onlyCaretToggle])

const ToggleChevronWrapper = useMemo(() => {
if (onlyCaretToggle) return chakra.button
return chakra.div
}, [onlyCaretToggle])

return (
<chakra.li __css={styles.list} pl={0} ref={ref} {...props}>
<Box>
Expand All @@ -122,19 +144,22 @@ export const SidebarList = forwardRef<
data-expanded={dataAttr(isOpen)}
data-active={dataAttr(dataActive)}
onClick={handleExpandSection}
onlyCaretToggle={onlyCaretToggle}
>
<chakra.span flex={1} __css={styles.label}>
{icon ? (
<Icon as={icon} __css={styles.icon} {...iconProps} />
) : null}
{label}
</chakra.span>

<ToggleChevronWrapper
layerStyle="focusRing.default"
aria-label={onlyCaretToggle ? 'Toggle section' : undefined}
onClick={onToggle}
display="flex"
outline="none"
onlyCaretToggle={onlyCaretToggle}
>
<ToggleChevron
reduceMotion={reduceMotion}
Expand Down