|
| 1 | +import { useCollapsible, Collapsible } from '@docusaurus/theme-common'; |
| 2 | + |
| 3 | +import clsx from 'clsx'; |
| 4 | +import React from 'react'; |
| 5 | +import { useRef, useState } from 'react'; |
| 6 | + |
| 7 | +import styles from './styles.module.css'; |
| 8 | +import useIsBrowser from '@docusaurus/useIsBrowser'; |
| 9 | +import ThemedImage from '@theme/ThemedImage'; |
| 10 | +import useBaseUrl from '@docusaurus/useBaseUrl'; |
| 11 | + |
| 12 | +const DetailsStyling = ({ summary, children, ...props }): JSX.Element => { |
| 13 | + const isBrowser = useIsBrowser(); |
| 14 | + const { collapsed, setCollapsed } = useCollapsible({ |
| 15 | + initialState: !props.open, |
| 16 | + }); |
| 17 | + |
| 18 | + const arrowIcon = { |
| 19 | + light: useBaseUrl('/img/Arrow.svg'), |
| 20 | + dark: useBaseUrl('img/Arrow-dark.svg'), |
| 21 | + }; |
| 22 | + |
| 23 | + const detailsRef = useRef<HTMLDetailsElement>(null); |
| 24 | + const [open, setOpen] = useState(props.open); |
| 25 | + |
| 26 | + // As we need to modify our own summary, we need to extract original content of summary |
| 27 | + const extractedSummaryElement = summary.props.children; |
| 28 | + |
| 29 | + return ( |
| 30 | + <details |
| 31 | + {...props} |
| 32 | + ref={detailsRef} |
| 33 | + open={open} |
| 34 | + data-collapsed={collapsed} |
| 35 | + className={clsx( |
| 36 | + styles.details, |
| 37 | + isBrowser && styles.isBrowser, |
| 38 | + props.className |
| 39 | + )} |
| 40 | + onMouseDown={(e) => { |
| 41 | + const target = e.target as HTMLElement; |
| 42 | + // Prevent a double-click to highlight summary text |
| 43 | + if (isInSummary(target) && e.detail > 1) { |
| 44 | + e.preventDefault(); |
| 45 | + } |
| 46 | + }} |
| 47 | + onClick={(e) => { |
| 48 | + e.stopPropagation(); // For isolation of multiple nested details/summary |
| 49 | + const target = e.target as HTMLElement; |
| 50 | + const shouldToggle = |
| 51 | + isInSummary(target) && hasParent(target, detailsRef.current!); |
| 52 | + if (!shouldToggle) { |
| 53 | + return; |
| 54 | + } |
| 55 | + e.preventDefault(); |
| 56 | + if (collapsed) { |
| 57 | + setCollapsed(false); |
| 58 | + setOpen(true); |
| 59 | + } else { |
| 60 | + setCollapsed(true); |
| 61 | + // Don't do this, it breaks close animation! |
| 62 | + // setOpen(false); |
| 63 | + } |
| 64 | + }} |
| 65 | + > |
| 66 | + <summary> |
| 67 | + <ThemedImage sources={arrowIcon} className={styles.arrow} /> |
| 68 | + |
| 69 | + <p>{extractedSummaryElement}</p> |
| 70 | + </summary> |
| 71 | + |
| 72 | + <Collapsible |
| 73 | + lazy={false} |
| 74 | + collapsed={collapsed} |
| 75 | + disableSSRStyle |
| 76 | + onCollapseTransitionEnd={(newCollapsed) => { |
| 77 | + setCollapsed(newCollapsed); |
| 78 | + setOpen(!newCollapsed); |
| 79 | + }} |
| 80 | + > |
| 81 | + <div className={styles.collapsibleContent}>{children}</div> |
| 82 | + </Collapsible> |
| 83 | + </details> |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +function isInSummary(node: HTMLElement | null): boolean { |
| 88 | + if (!node) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + return node.tagName === 'SUMMARY' || isInSummary(node.parentElement); |
| 92 | +} |
| 93 | + |
| 94 | +function hasParent(node: HTMLElement | null, parent: HTMLElement): boolean { |
| 95 | + if (!node) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + return node === parent || hasParent(node.parentElement, parent); |
| 99 | +} |
| 100 | + |
| 101 | +export default DetailsStyling; |
0 commit comments