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 code transformer to <Demo> component #7894

Merged
merged 5 commits into from
Jul 19, 2024

Conversation

tkajtoch
Copy link
Member

@tkajtoch tkajtoch commented Jul 18, 2024

Summary

Resolves #7866.

This adds a custom code transformer to our <Demo> component that makes the existing component examples work with react-live. The major factor was that react-live only supports code that's just a JSX component or render function declaration. There's no way to define variables outside of the render function or have import/export statements.

The new code transformer removes any import statements because all externals should always come from the scope, and wraps the source code in an IIFE to support defining variables and functions outside of the render function as we often do in our examples.

QA

  • Open built PR preview and confirm that the EuiButton examples work as intended

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

@tkajtoch tkajtoch self-assigned this Jul 18, 2024
@tkajtoch tkajtoch marked this pull request as ready for review July 18, 2024 16:56
@tkajtoch tkajtoch requested a review from a team as a code owner July 18, 2024 16:56
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.

🚢 🐈‍⬛ Works like a charm! Awesome and straight forward code transformer! 🤖

}

// If the demo is more than just JSX wrap in an immediately invoked function expression
return `(() => { ${code} })()`;
Copy link
Contributor

Choose a reason for hiding this comment

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

🧠

const COMPONENT_ONLY_REGEX = /^\(?</;

/**
* Transforms input JS/TS source code to a react-live compatible syntax.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the great description, that's really helpful for understanding! 🙏

@tkajtoch tkajtoch merged commit e738c32 into elastic:main Jul 19, 2024
8 of 9 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] Add code transformer to the interactive code component to support existing EUI code examples
3 participants