Skip to content

Commit

Permalink
feat(theme-common): allow passing a string for details summary (#8656)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
  • Loading branch information
pReya and Josh-Cena committed Feb 16, 2023
1 parent 5edd594 commit 533777c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/docusaurus-theme-common/src/components/Details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ function hasParent(node: HTMLElement | null, parent: HTMLElement): boolean {
}

export type DetailsProps = {
/** Summary is provided as props, including the wrapping `<summary>` tag */
summary?: ReactElement;
/**
* Summary is provided as props, optionally including the wrapping
* `<summary>` tag
*/
summary?: ReactElement | string;
} & ComponentProps<'details'>;

/**
Expand All @@ -54,6 +57,12 @@ export function Details({
// only after animation completes, otherwise close animations won't work
const [open, setOpen] = useState(props.open);

const summaryElement = React.isValidElement(summary) ? (
summary
) : (
<summary>{summary ?? 'Details'}</summary>
);

return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-noninteractive-element-interactions
<details
Expand Down Expand Up @@ -91,8 +100,7 @@ export function Details({
// setOpen(false);
}
}}>
{/* eslint-disable-next-line @docusaurus/no-untranslated-text */}
{summary ?? <summary>Details</summary>}
{summaryElement}

<Collapsible
lazy={false} // Content might matter for SEO in this case
Expand Down
5 changes: 5 additions & 0 deletions website/_dogfooding/_pages tests/markdownPageTests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ Details without a summary

</details>

import Details from '@theme/Details';

<Details summary={'My Headline'}>Some Text</Details>
<Details>Some Text</Details>

This is a fragment:

<>Hello</>
Expand Down

0 comments on commit 533777c

Please sign in to comment.