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

Update script to support current tailwindui.com #11

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

wzulfikar
Copy link
Contributor

@wzulfikar wzulfikar commented Nov 1, 2022

What
Update scraper to make it work for the new tailwindui.com site. This PR also tries to improve the codebase so it's easier to maintain.

Checklist:

  • Add .env.sample as a template for environment variables required by the script
  • Add npm scripts for entry point (eg. yarn start react, yarn create-mdx react)
  • Add screenshots in docs/hints for future guidance
  • Extract codes into util/ and lib/
  • Add guest mode (run the script site without login info) to scrape free components
  • Update playwright
  • Update README

Why
The previous code no longer work due to changes in tailwindui.com.

Test plan

  • Copy .env.sample into .env
  • Add your TailwindUI login info to the .env file
  • Run yarn start react and it should create a json file at ./output/tailwindui.react.json
  • Run yarn create-mdx react and it should create the Storybook stories at ./output/tailwindui-react/

Preview:

twui-storybook.mp4.mp4

I also tested the generated code in Stackblitz to ensure that it works with current latest Storybook. Screenshot:

@@ -0,0 +1,10 @@
const fs = require("fs");

function writeToFile(outputDir, componentType, sections, error = false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Borrowed from #7

@@ -0,0 +1,12 @@
[
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sample output of getSections.js (added here for reference)

Comment on lines +20 to +22
if (!toggleCode) {
return
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allow the script to keep going when the code block is locked (user has no access to it).

Example component ("Get the code" indicates the code block is locked):

image

// Change the type of code snippet to HTML/React/Vue (hint: ../docs/hints/toggle-code-format.png)
const toggleSnippetType = el.querySelector('select.form-select')
toggleSnippetType.value = type;
toggleSnippetType.dispatchEvent(new Event('change', { bubbles: true }));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

bubbles: true is needed otherwise the onChange event won't be propagated to React and it won't rerender (SO).

@wzulfikar wzulfikar changed the title Update code for new site Update script to support current tailwindui.com Nov 2, 2022
@@ -52,12 +47,15 @@ async function run() {
components.forEach((component) => {
const componentName = pascalCase(component.title);

// Add `Component_` prefix for import name if component name starts with number
const importName = componentName.match(/^\d/) ? `Component_${componentName}` : componentName;
Copy link
Contributor Author

@wzulfikar wzulfikar Nov 2, 2022

Choose a reason for hiding this comment

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

JS variable can't start with number and so is JSX tag. So I added custom prefix Component_ when the imported component starts with number.

Before / after:

image

The component will still show up as "4X2GridOnBrand" in storybook:

image

@zawarudo
Copy link

zawarudo commented Jan 2, 2023

@wzulfikar This is cool! Worked for me.
Nice job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants