Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for simplified interactive demo syntax using code blocks #7907

Merged
merged 10 commits into from
Jul 26, 2024

Conversation

tkajtoch
Copy link
Member

Summary

Resolves #7899.

This PR adds support for creating interactive component examples without the need to wrap code blocks with <Demo> in MDX. Now we can define simple interactive examples like this (ignore // at the beginning of each line):

// ```tsx interactive
// <EuiButton>Button</EuiButton>
// ```

An added benefit to that is greatly increased Github preview readability

QA

  • Open EUI+ button docs and confirm the interactive examples render correctly

@tkajtoch tkajtoch self-assigned this Jul 23, 2024
@tkajtoch tkajtoch requested a review from a team as a code owner July 23, 2024 20:14
function transform(opts = {}) {
const transforms = Array.isArray(opts.transforms) ? opts.transforms.filter(Boolean) : defaultTransforms;
- return (code) => (0, import_sucrase.transform)(code, { transforms }).code;
+ return (code) => (0, import_sucrase.transform)(code, { transforms, jsxPragma: 'jsx' }).code;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed temporarily because react-live doesn't support passing arguments to sucrase (the client-side JS/TS transpiler). I'm planning to add this functionality to react-live in a PR after we finish this milestone.

Defining jsxPragma here configures sucrase to emit jsx([...]) instead of the regular React.createElement([...]) and control what the jsx is - in our case it's the Emotion jsx wrapper. We inject the custom jsx here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So awesome this patching functionality!

Comment on lines +34 to +36
const style = {
'--docs-demo-preview-padding': paddingSize,
} as CSSProperties;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing this with a CSS variable is technically more performant than recomputing and reapplying the whole style with Emotion. Not that it matters here, but I was experimenting with the approaches of passing dynamic data to CSS without Emotion and really liked this solution.

There are many optimizations browsers could do with this to reduce style recalculations when the value changes. It would be faster to define the property using element.style.setProperty but it's more painful to maintain, so yeah, inline styles it is.

Copy link
Contributor

@mgadewoll mgadewoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Should we add an example for the simplified code block usage in this PR to test and verify?

packages/docusaurus-theme/src/components/demo/scope.ts Outdated Show resolved Hide resolved
function transform(opts = {}) {
const transforms = Array.isArray(opts.transforms) ? opts.transforms.filter(Boolean) : defaultTransforms;
- return (code) => (0, import_sucrase.transform)(code, { transforms }).code;
+ return (code) => (0, import_sucrase.transform)(code, { transforms, jsxPragma: 'jsx' }).code;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So awesome this patching functionality!

packages/docusaurus-theme/src/theme/MDXComponents/Code.tsx Outdated Show resolved Hide resolved
@@ -118,6 +118,31 @@ declare module '@theme-original/EditThisPage' {
export default function EditThisPage(props: Props): JSX.Element;
}

// original: https://github.com/facebook/docusaurus/blob/fa743c81defd24e22eae45c81bd79eb8ec2c4ef0/packages/docusaurus-theme-classic/src/theme-classic.d.ts#L364
declare module '@theme/CodeBlock' {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ So far I've used the @theme-original alias to align with other imports as we import everything else from @theme-original when swizzled. Do you think that makes sense and should we update this here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I specifically used @theme/CodeBlock to access our swizzled CodeBlock component or any other higher-level swizzled CodeBlock in an additional theme package or in website's src/theme directory. This makes it more reusable and customizable for other users.

Here's the documentation on differences between @theme-init, @theme-original, and @theme - https://docusaurus.io/docs/advanced/client

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for the types we could update the other cases that I added with @theme-original to @theme too eventually as they are just adding the types that are not available when swizzled.
But I think it there is no hurry for this.

@tkajtoch tkajtoch force-pushed the feat/demo-codeblock branch from ce2dbb8 to 1d4b1e7 Compare July 26, 2024 14:14
@tkajtoch
Copy link
Member Author

@mgadewoll I included a usage example here https://github.com/elastic/eui/pull/7907/files#diff-aeb591005eb563a650b20cef4214d958b31c62e91d92718beb0f318995084c77

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

cc @tkajtoch

Copy link
Contributor

@mgadewoll mgadewoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢 🐈‍⬛ Thanks for the changes! LGTM!

@tkajtoch tkajtoch merged commit 36ffc1e into elastic:main Jul 26, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[New docs] Integrate <Demo /> component with code blocks
3 participants