|
| 1 | +import React, { Children, useState, forwardRef, type Ref, useId } from 'react'; |
| 2 | +import { Provider } from 'react-aria-components'; |
| 3 | +import { cx } from 'cva'; |
| 4 | +import { ChevronDown } from '@obosbbl/grunnmuren-icons-react'; |
| 5 | + |
| 6 | +import { useClientLayoutEffect } from '../utils/useClientLayoutEffect'; |
| 7 | +import { HeadingContext, ContentContext } from '../content'; |
| 8 | + |
| 9 | +type AccordionProps = { |
| 10 | + children: React.ReactNode; |
| 11 | + |
| 12 | + /** Additional CSS className for the element. */ |
| 13 | + className?: string; |
| 14 | + |
| 15 | + /** Additional style properties for the element. */ |
| 16 | + style?: React.CSSProperties; |
| 17 | +}; |
| 18 | + |
| 19 | +type AccordionItemProps = { |
| 20 | + children?: React.ReactNode; |
| 21 | + |
| 22 | + /** Additional CSS className for the element. */ |
| 23 | + className?: string; |
| 24 | + |
| 25 | + /** Additional style properties for the element. */ |
| 26 | + style?: React.CSSProperties; |
| 27 | + |
| 28 | + /** Whether the accordion is open (controlled) */ |
| 29 | + isOpen?: boolean; |
| 30 | + /** Whether the accordion is open by default (uncontrolled) */ |
| 31 | + defaultOpen?: boolean; |
| 32 | + /** Handler that is called when the accordion's open state changes */ |
| 33 | + onOpenChange?: (isOpen: boolean) => void; |
| 34 | +}; |
| 35 | + |
| 36 | +function Accordion(props: AccordionProps, ref: Ref<HTMLDivElement>) { |
| 37 | + const { children, ...restProps } = props; |
| 38 | + |
| 39 | + const childCount = Children.count(children); |
| 40 | + |
| 41 | + return ( |
| 42 | + <div {...restProps} ref={ref}> |
| 43 | + {Children.map(children, (child, index) => ( |
| 44 | + <> |
| 45 | + {child} |
| 46 | + {index < childCount - 1 && ( |
| 47 | + <hr className="border-gray-light" aria-hidden /> |
| 48 | + )} |
| 49 | + </> |
| 50 | + ))} |
| 51 | + </div> |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function AccordionItem(props: AccordionItemProps, ref: Ref<HTMLDivElement>) { |
| 56 | + const { |
| 57 | + className, |
| 58 | + children, |
| 59 | + defaultOpen = false, |
| 60 | + isOpen: controlledIsOpen, |
| 61 | + onOpenChange, |
| 62 | + ...restProps |
| 63 | + } = props; |
| 64 | + |
| 65 | + const contentId = useId(); |
| 66 | + const buttonId = useId(); |
| 67 | + |
| 68 | + const isControlled = controlledIsOpen != null; |
| 69 | + |
| 70 | + // This component has internal state that controls whether it is open or not, |
| 71 | + // regardless if we are controlled or uncontrolled. |
| 72 | + // If we are controlled, we use a layout effect to sync the controlled state |
| 73 | + // with the internal state. |
| 74 | + // |
| 75 | + const [isOpen, setIsOpen] = useState( |
| 76 | + // If we are controlled, use that open state, otherwise use the uncontrolled |
| 77 | + isControlled ? controlledIsOpen : defaultOpen, |
| 78 | + ); |
| 79 | + |
| 80 | + useClientLayoutEffect(() => { |
| 81 | + if (isControlled) { |
| 82 | + setIsOpen(controlledIsOpen); |
| 83 | + } |
| 84 | + }, [controlledIsOpen, isControlled]); |
| 85 | + |
| 86 | + const handleOpenChange = () => { |
| 87 | + const newOpenState = !isOpen; |
| 88 | + |
| 89 | + if (!isControlled) { |
| 90 | + setIsOpen(newOpenState); |
| 91 | + } |
| 92 | + |
| 93 | + // Always call the change handler, even if we're uncontrolled. |
| 94 | + // Easier to add stuff such as tracking etc. |
| 95 | + if (onOpenChange) { |
| 96 | + onOpenChange(newOpenState); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + return ( |
| 101 | + <div |
| 102 | + {...restProps} |
| 103 | + className={cx('group relative', className)} |
| 104 | + ref={ref} |
| 105 | + data-open={isOpen} |
| 106 | + > |
| 107 | + <Provider |
| 108 | + values={[ |
| 109 | + [ |
| 110 | + HeadingContext, |
| 111 | + { |
| 112 | + className: 'font-semibold leading-7', |
| 113 | + // Supply a default level here to make this typecheck ok. Will be overwritten with the consumers set heading level anyways |
| 114 | + level: 3, |
| 115 | + _innerWrapper: (children) => ( |
| 116 | + <button |
| 117 | + aria-controls={contentId} |
| 118 | + aria-expanded={isOpen} |
| 119 | + // the z-index is necessary for the focus ring to be drawn above the left border of the content |
| 120 | + className="relative z-10 flex min-h-[44px] w-full items-center justify-between gap-1.5 rounded-sm py-3.5 text-left focus:outline-none focus-visible:ring focus-visible:ring-black" |
| 121 | + id={buttonId} |
| 122 | + onClick={handleOpenChange} |
| 123 | + > |
| 124 | + {children} |
| 125 | + <ChevronDown |
| 126 | + className={cx( |
| 127 | + 'transition-transform duration-300 motion-reduce:transition-none', |
| 128 | + isOpen && 'rotate-180', |
| 129 | + )} |
| 130 | + /> |
| 131 | + </button> |
| 132 | + ), |
| 133 | + }, |
| 134 | + ], |
| 135 | + [ |
| 136 | + ContentContext, |
| 137 | + { |
| 138 | + className: |
| 139 | + // Uses pseudo element for vertical padding, since that doesn't affect the height when the accordion is closed |
| 140 | + 'text-sm font-light leading-6 px-3.5 relative overflow-hidden border-mint border-l-[3px] before:relative before:block before:h-1.5 after:relative after:block after:h-1.5', |
| 141 | + role: 'region', |
| 142 | + // @ts-expect-error TODO: remove this expect-error when we're on React 19 https://github.com/facebook/react/issues/17157#issuecomment-2003750544 |
| 143 | + inert: isOpen ? undefined : 'true', |
| 144 | + 'aria-labelledby': buttonId, |
| 145 | + _outerWrapper: (children) => ( |
| 146 | + <div |
| 147 | + className={cx( |
| 148 | + 'grid transition-all duration-300 after:relative after:block after:h-0 after:transition-all after:duration-300 motion-reduce:transition-none', |
| 149 | + isOpen ? 'grid-rows-[1fr] after:h-3.5' : 'grid-rows-[0fr] ', |
| 150 | + )} |
| 151 | + > |
| 152 | + {children} |
| 153 | + </div> |
| 154 | + ), |
| 155 | + }, |
| 156 | + ], |
| 157 | + ]} |
| 158 | + > |
| 159 | + {children} |
| 160 | + </Provider> |
| 161 | + </div> |
| 162 | + ); |
| 163 | +} |
| 164 | + |
| 165 | +const _Accordion = forwardRef(Accordion); |
| 166 | +const _AccordionItem = forwardRef(AccordionItem); |
| 167 | +export { |
| 168 | + _Accordion as Accordion, |
| 169 | + _AccordionItem as AccordionItem, |
| 170 | + type AccordionProps, |
| 171 | + type AccordionItemProps, |
| 172 | +}; |
0 commit comments