Skip to content

Commit

Permalink
[New docs] Global site and docusaurus theme improvements (#8093)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkajtoch authored Oct 23, 2024
1 parent 4b54564 commit 4a36358
Show file tree
Hide file tree
Showing 30 changed files with 359 additions and 113 deletions.
2 changes: 1 addition & 1 deletion packages/docusaurus-theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@docusaurus/theme-common": "^3.5.2",
"@docusaurus/utils-validation": "^3.5.2",
"@elastic/datemath": "^5.0.3",
"@elastic/eui": "94.5.0",
"@elastic/eui": "97.2.0",
"@elastic/eui-docgen": "workspace:^",
"@emotion/css": "^11.11.2",
"@emotion/react": "^11.11.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import {
EuiButton,
EuiButtonIcon,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { DemoActionsBar, type DemoActionsBarProps } from './actions_bar';
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

const IMPORT_REGEX = /^import [^'"]* from ['"]([^.'"\n ][^'"\n ]*)['"];?/gm;
const DEFAULT_EXPORT_REGEX = /export default /;
const COMPONENT_ONLY_REGEX = /^\(?</;
Expand Down
8 changes: 8 additions & 0 deletions packages/docusaurus-theme/src/components/demo/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { createContext, useContext } from 'react';
import { DemoSourceMeta } from './demo';

Expand Down
17 changes: 17 additions & 0 deletions packages/docusaurus-theme/src/components/demo/create_demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { Demo, DemoProps } from './demo';

/**
* Creates a custom <Demo /> component with predefined props.
*/
export const createDemo =
(defaultProps: Partial<DemoProps>): typeof Demo =>
(props: DemoProps) =>
<Demo {...defaultProps} {...props} />;
30 changes: 23 additions & 7 deletions packages/docusaurus-theme/src/components/demo/demo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import {
Children,
PropsWithChildren,
Expand Down Expand Up @@ -42,6 +50,7 @@ export interface DemoProps extends PropsWithChildren {
*/
scope?: Record<string, unknown>;
previewPadding?: DemoPreviewProps['padding'];
previewWrapper?: DemoPreviewProps['wrapperComponent'];
}

const getDemoStyles = (euiTheme: UseEuiTheme) => ({
Expand All @@ -60,6 +69,7 @@ export const Demo = ({
scope,
isSourceOpen: _isSourceOpen = false,
previewPadding,
previewWrapper,
}: DemoProps) => {
const styles = useEuiMemoizedStyles(getDemoStyles);
const [sources, setSources] = useState<DemoSourceMeta[]>([]);
Expand All @@ -69,16 +79,19 @@ export const Demo = ({
// liveProviderKey restarts the demo to its initial state
const [liveProviderKey, setLiveProviderKey] = useState<number>(0);

const finalScope = useMemo(() => ({
...demoDefaultScope,
...scope,
}), [scope]);
const finalScope = useMemo(
() => ({
...demoDefaultScope,
...scope,
}),
[scope]
);

const addSource = useCallback<DemoContextObject['addSource']>(
(source: DemoSourceMeta) => {
setSources((sources) => ([...sources, source]));
setSources((sources) => [...sources, source]);
},
[],
[]
);

const onClickCopyToClipboard = useCallback(() => {
Expand All @@ -104,7 +117,10 @@ export const Demo = ({
theme={prismThemes.dracula}
scope={finalScope}
>
<DemoPreview padding={previewPadding} />
<DemoPreview
padding={previewPadding}
wrapperComponent={previewWrapper}
/>
<DemoActionsBar
isSourceOpen={isSourceOpen}
setSourceOpen={setIsSourceOpen}
Expand Down
8 changes: 8 additions & 0 deletions packages/docusaurus-theme/src/components/demo/editor/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { DemoEditor } from './editor';
9 changes: 9 additions & 0 deletions packages/docusaurus-theme/src/components/demo/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { Demo, type DemoProps } from './demo';
export { DemoSource } from './source';
export { createDemo } from './create_demo';
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { DemoPreview } from './preview';
42 changes: 34 additions & 8 deletions packages/docusaurus-theme/src/components/demo/preview/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { css } from '@emotion/react';
import { LivePreview } from 'react-live';
import {
ComponentType,
CSSProperties,
Fragment,
PropsWithChildren,
} from 'react';
import BrowserOnly from '@docusaurus/BrowserOnly';
import ErrorBoundary from '@docusaurus/ErrorBoundary';
import { ErrorBoundaryErrorMessageFallback } from '@docusaurus/theme-common';
import { UseEuiTheme, useEuiTheme, EuiPaddingSize, euiPaddingSize } from '@elastic/eui';
import { CSSProperties } from 'react';
import {
UseEuiTheme,
useEuiTheme,
EuiPaddingSize,
euiPaddingSize,
} from '@elastic/eui';

export interface DemoPreviewProps {
padding?: EuiPaddingSize;
wrapperComponent?: ComponentType<PropsWithChildren>;
}

const getPreviewStyles = (euiTheme: UseEuiTheme) => ({
Expand All @@ -22,11 +41,12 @@ const getPreviewStyles = (euiTheme: UseEuiTheme) => ({
* of the live component preview component.
* Due to the limitations of react-live the demo is only rendered client-side.
*/
const PreviewLoader = () => (
<div>Loading...</div>
);
const PreviewLoader = () => <div>Loading...</div>;

export const DemoPreview = ({ padding = 'l' }: DemoPreviewProps) => {
export const DemoPreview = ({
padding = 'l',
wrapperComponent: WrapperComponent = Fragment,
}: DemoPreviewProps) => {
const euiTheme = useEuiTheme();
const styles = getPreviewStyles(euiTheme);
const paddingSize = euiPaddingSize(euiTheme, padding);
Expand All @@ -39,9 +59,15 @@ export const DemoPreview = ({ padding = 'l' }: DemoPreviewProps) => {
<BrowserOnly fallback={<PreviewLoader />}>
{() => (
<>
<ErrorBoundary fallback={(params: any) => <ErrorBoundaryErrorMessageFallback {...params} />}>
<ErrorBoundary
fallback={(params: any) => (
<ErrorBoundaryErrorMessageFallback {...params} />
)}
>
<div css={styles.previewWrapper} style={style}>
<LivePreview />
<WrapperComponent>
<LivePreview />
</WrapperComponent>
</div>
</ErrorBoundary>
</>
Expand Down
8 changes: 8 additions & 0 deletions packages/docusaurus-theme/src/components/demo/scope.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import * as EUI from '@elastic/eui';
import * as EmotionReact from '@emotion/react';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { Children, ReactElement, ReactNode } from 'react';
import { isElement } from 'react-is';

Expand Down
8 changes: 8 additions & 0 deletions packages/docusaurus-theme/src/components/demo/source/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { DemoSource, type DemoSourceProps } from './source';
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { PropsWithChildren, useEffect } from 'react';
import { useDemoContext } from '../context';
import { getSourceFromChildren } from './get_source_from_children';
Expand Down
96 changes: 96 additions & 0 deletions packages/docusaurus-theme/src/components/guideline/guideline.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { PropsWithChildren, ReactNode, useMemo } from 'react';
import {
EuiFlexItem,
EuiSplitPanel,
EuiPanelProps,
useEuiMemoizedStyles,
UseEuiTheme,
} from '@elastic/eui';
import { css } from '@emotion/react';
import { GuidelineType } from './types';
import { GuidelineText } from './guideline_text';

export interface GuidelineProps extends PropsWithChildren {
type: GuidelineType;
text: string | ReactNode;
panelPadding?: EuiPanelProps['paddingSize'];
panelStyle?: EuiPanelProps['style'];
}

const getGuidelineStyles = ({ euiTheme }: UseEuiTheme) => ({
root: css`
margin-block: var(--eui-theme-content-vertical-spacing);
`,
wrapper: css`
border-block-end: 2px solid ${euiTheme.colors.lightShade};
`,
wrapperDo: css`
border-color: ${euiTheme.colors.success};
`,
wrapperDont: css`
border-color: ${euiTheme.colors.danger};
`,
textWrapper: css`
margin-block-start: var(--eui-size-xs);
`,
});

export const Guideline = ({
children,
text,
type = 'default',
panelPadding = 'm',
panelStyle,
}: GuidelineProps) => {
const styles = useEuiMemoizedStyles(getGuidelineStyles);

const panelColor = useMemo((): EuiPanelProps['color'] => {
if (type === 'do') {
return 'success';
}

if (type === 'dont') {
return 'danger';
}

return 'subdued';
}, [type]);

const textElement = useMemo(() => {
if (typeof text === 'string') {
return <GuidelineText type={type}>{text}</GuidelineText>;
}

return text;
}, [text]);

const wrapperStyles = [
styles.wrapper,
type === 'do' && styles.wrapperDo,
type === 'dont' && styles.wrapperDont,
];

return (
<EuiFlexItem css={styles.root} style={{ flexBasis: 300 }}>
<EuiSplitPanel.Outer
color="transparent"
hasBorder={false}
hasShadow={false}
>
<figure>
<EuiSplitPanel.Inner
css={wrapperStyles}
color={panelColor}
paddingSize={panelPadding}
style={panelStyle}
>
{children}
</EuiSplitPanel.Inner>
<EuiSplitPanel.Inner paddingSize="none" css={styles.textWrapper}>
{textElement}
</EuiSplitPanel.Inner>
</figure>
</EuiSplitPanel.Outer>
</EuiFlexItem>
);
};
Loading

0 comments on commit 4a36358

Please sign in to comment.