Skip to content

Commit

Permalink
feat(website): add Example utility component
Browse files Browse the repository at this point in the history
  • Loading branch information
tkajtoch committed Oct 23, 2024
1 parent 432dd02 commit 4055901
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
110 changes: 110 additions & 0 deletions packages/website/src/components/example/example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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, ReactNode, useMemo, Children, ReactElement } from 'react';
import { css } from '@emotion/react';
import {
EuiSplitPanel,
UseEuiTheme,
useEuiTheme,
useEuiMemoizedStyles,
euiFontSize,
} from '@elastic/eui';
import { isElement } from 'react-is';

export interface ExampleProps extends PropsWithChildren {}

const getExampleStyles = (theme: UseEuiTheme) => {
const { euiTheme } = theme;

return {
wrappingSplitPanel: css`
gap: ${euiTheme.size.l};
`,
childrenWrapper: css`
// Enforce a static font size for inline code within headings
h1 code,
h2 code,
h3 code,
h4 code,
h5 code,
h6 code {
${euiFontSize(theme, 'm')};
}
`,
};
};

const ExampleDescription = ({ children }: PropsWithChildren) => children;

const ExampleSnippet = ({ children }: PropsWithChildren) => children;

const ExamplePreview = ({ children }: PropsWithChildren) => children;

export const Example = ({ children }: ExampleProps) => {
const styles = useEuiMemoizedStyles(getExampleStyles);
const { euiTheme } = useEuiTheme();
const { description, snippet, preview } = useMemo(() => {
let description: ReactNode;
let snippet: ReactNode;
let preview: ReactNode;
Children.toArray(children).forEach((child) => {
if (!isElement(child)) {
return;
}

switch ((child as ReactElement).type) {
case ExampleDescription:
description = child;
break;
case ExampleSnippet:
snippet = child;
break;
case ExamplePreview:
preview = child;
break;
}
});

return { description, snippet, preview };
}, [children]);

return (
<EuiSplitPanel.Outer
color="transparent"
direction="column"
css={styles.wrappingSplitPanel}
>
<EuiSplitPanel.Inner paddingSize="none" css={styles.childrenWrapper}>
{description}
</EuiSplitPanel.Inner>
<EuiSplitPanel.Inner paddingSize="none">
<EuiSplitPanel.Outer
direction="column"
hasBorder
hasShadow={false}
style={{ overflow: 'hidden' }}
>
<EuiSplitPanel.Inner>{preview}</EuiSplitPanel.Inner>
<EuiSplitPanel.Inner
paddingSize="none"
css={css`
background: ${euiTheme.colors.lightestShade};
`}
>
{snippet}
</EuiSplitPanel.Inner>
</EuiSplitPanel.Outer>
</EuiSplitPanel.Inner>
</EuiSplitPanel.Outer>
);
};

Example.Description = ExampleDescription;
Example.Preview = ExamplePreview;
Example.Snippet = ExampleSnippet;
9 changes: 9 additions & 0 deletions packages/website/src/components/example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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 { Example, type ExampleProps } from './example';
9 changes: 9 additions & 0 deletions packages/website/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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 * from './example';

0 comments on commit 4055901

Please sign in to comment.