mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-23 19:48:54 +02:00
refactor(theme): split admonitions, make swizzle easier, better retrocompatibility (#7945)
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
This commit is contained in:
parent
f1415525c0
commit
6f63ffe0a3
32 changed files with 914 additions and 236 deletions
|
@ -78,3 +78,5 @@ export {duplicates, uniq} from './utils/jsUtils';
|
|||
export {usePrismTheme} from './hooks/usePrismTheme';
|
||||
|
||||
export {useDocsPreferredVersion} from './contexts/docsPreferredVersion';
|
||||
|
||||
export {processAdmonitionProps} from './utils/admonitionUtils';
|
||||
|
|
|
@ -40,8 +40,7 @@ export const ThemeClassNames = {
|
|||
backToTopButton: 'theme-back-to-top-button',
|
||||
codeBlock: 'theme-code-block',
|
||||
admonition: 'theme-admonition',
|
||||
admonitionType: (type: 'note' | 'tip' | 'danger' | 'info' | 'caution') =>
|
||||
`theme-admonition-${type}`,
|
||||
admonitionType: (type: string) => `theme-admonition-${type}`,
|
||||
},
|
||||
layout: {
|
||||
// TODO add other stable classNames here
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import React, {type ReactNode} from 'react';
|
||||
|
||||
// Workaround because it's difficult in MDX v1 to provide a MDX title as props
|
||||
// See https://github.com/facebook/docusaurus/pull/7152#issuecomment-1145779682
|
||||
function extractMDXAdmonitionTitle(children: ReactNode): {
|
||||
mdxAdmonitionTitle: ReactNode | undefined;
|
||||
rest: ReactNode;
|
||||
} {
|
||||
const items = React.Children.toArray(children);
|
||||
const mdxAdmonitionTitle = items.find(
|
||||
(item) =>
|
||||
React.isValidElement(item) &&
|
||||
(item.props as {mdxType: string} | null)?.mdxType ===
|
||||
'mdxAdmonitionTitle',
|
||||
);
|
||||
const rest = <>{items.filter((item) => item !== mdxAdmonitionTitle)}</>;
|
||||
return {
|
||||
mdxAdmonitionTitle,
|
||||
rest,
|
||||
};
|
||||
}
|
||||
|
||||
export function processAdmonitionProps<
|
||||
Props extends {readonly children: ReactNode; readonly title?: ReactNode},
|
||||
>(props: Props): Props {
|
||||
const {mdxAdmonitionTitle, rest} = extractMDXAdmonitionTitle(props.children);
|
||||
const title = props.title ?? mdxAdmonitionTitle;
|
||||
return {
|
||||
...props,
|
||||
// Do not return "title: undefined" prop
|
||||
// this might create unwanted props overrides when merging props
|
||||
// For example: {...default,...props}
|
||||
...(title && {title}),
|
||||
children: rest,
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue