Skip to content

feat[react-gen2]: support for variant containers#3828

Merged
sidmohanty11 merged 52 commits intoBuilderIO:mainfrom
sidmohanty11:feat/variant-container-gen2
Feb 3, 2025
Merged

feat[react-gen2]: support for variant containers#3828
sidmohanty11 merged 52 commits intoBuilderIO:mainfrom
sidmohanty11:feat/variant-container-gen2

Conversation

@sidmohanty11
Copy link
Copy Markdown
Contributor

@sidmohanty11 sidmohanty11 commented Jan 15, 2025

Description

Adds support for Variant Containers on Gen2 React SDK

Jira
https://builder-io.atlassian.net/browse/ENG-7676

Screenshot
If relevant, add a screenshot or two of the changes you made.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 15, 2025

🦋 Changeset detected

Latest commit: 90620c7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@builder.io/sdk-solid Patch
@builder.io/sdk-svelte Patch
@builder.io/sdk-vue Patch
@builder.io/sdk-angular Patch
@builder.io/sdk-react-native Patch
@builder.io/sdk-qwik Patch
@builder.io/sdk-react Patch
@builder.io/sdk-react-nextjs Patch
@builder.io/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Jan 15, 2025

View your CI Pipeline Execution ↗ for commit 90620c7.

Command Status Duration Result
nx test @e2e/qwik-city ✅ Succeeded 9m 13s View ↗
nx test @e2e/nextjs-sdk-next-app ✅ Succeeded 8m 26s View ↗
nx test @e2e/nuxt ✅ Succeeded 8m View ↗
nx test @e2e/svelte ✅ Succeeded 6m 55s View ↗
nx test @e2e/hydrogen ✅ Succeeded 6m 56s View ↗
nx test @e2e/react-sdk-next-14-app ✅ Succeeded 6m 35s View ↗
nx test @e2e/angular-16-ssr ✅ Succeeded 6m 47s View ↗
nx test @e2e/react-sdk-next-pages ✅ Succeeded 6m 31s View ↗
Additional runs (35) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-01-31 08:50:13 UTC

Comment on lines +111 to +120
// Only remove the function if it's not directly exported
const exportParent = fnNode.findParent((p) =>
p.isExportNamedDeclaration()
);
if (
!exportParent ||
!t.isFunctionDeclaration(exportParent.node.declaration)
) {
fnNode.remove();
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@samijaber had to update this file to allow exporting fns if they are marked exported. This was needed because we cannot convert to a fn from a string in hydrogen because of their strict policy (in the server) that we cannot use eval/Function, i.e, earlier I was trying this

export const filterWithCustomTargeting = new Function(
  `return ${FILTER_WITH_CUSTOM_TARGETING_SCRIPT}`
)();

@sidmohanty11
Copy link
Copy Markdown
Contributor Author

@samijaber thanks for the build-inline-fns idea, it simplified the logic a lot. Also have added the tests for the filterWithCustomTargeting function and have acknowledged all of the review comments, please take a look

Copy link
Copy Markdown
Contributor

@samijaber samijaber left a comment

Choose a reason for hiding this comment

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

I'm unable to drag'n'drop successfully into an empty Variant Container block

https://www.loom.com/share/6a4705e1674f41c9a9d9304059e5d38a

Comment thread .changeset/itchy-beers-marry.md Outdated
Comment thread .changeset/itchy-beers-marry.md Outdated
Comment thread .changeset/tricky-hairs-march.md Outdated
@samijaber
Copy link
Copy Markdown
Contributor

There may be an issue with how the previewingIndex is applied, unless I'm confused: https://www.loom.com/share/af8e458536574721b83b456dba695a2f

I can only see and edit variant 1 in the visual editor

Comment on lines +53 to +59
const thisPrefix = 'this.';
const pathPrefix = 'component.options.';
return props.path.startsWith(pathPrefix)
? props.path
: `${pathPrefix}${props.path || ''}`;
return props.path.startsWith(thisPrefix)
? props.path.replace(thisPrefix, '')
: props.path.startsWith(pathPrefix)
? props.path
: `${pathPrefix}${props.path || ''}`;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the catch!

added children support for <Blocks /> for this

I'm unable to drag'n'drop successfully into an empty Variant Container block

we need to render them as <Blocks /> because:

  1. we need the BlocksWrapper to render so that hydration matches with the other variants
  2. this will allow us to drag and drop - will enable empty blocks

Comment on lines +82 to +100
return {
blocks: variant.blocks,
path: `component.options.variants.${previewingIndex}.blocks`,
};
}
// Otherwise we're editing the default variant
return fallback;
}

// If not editing, check if there's a winning variant
const winningVariant = filteredVariants?.[0];
return winningVariant
? {
blocks: winningVariant.blocks,
path: `component.options.variants.${variants?.indexOf(winningVariant)}.blocks`,
}
: // else return the default variant
fallback;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There may be an issue with how the previewingIndex is applied, unless I'm confused:

Turns out this was happening because when we don't provide any targeting, filteredVariants renders a winning variant. Have fixed this

test('only default variants are ssred on the server', async ({ browser, packageName, sdk }) => {
test.skip(!isSSRFramework(packageName));
test.skip(!['react', 'oldReact'].includes(sdk));
// Cannot read properties of null (reading 'useContext')
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@samijaber we should create a ticket to move away from remix v1 for gen1 tests and use v2 that we already have in snippets. This will help us alias a single react and fix these tests

repo: https://github.com/sidmohanty11/builder-remix-gen1-example

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yeah @sidmohanty11 , I actually spent a bunch of time doing this locally but hit some cascading issues that caused most (interactivity-related?) tests to fail for remix v2, so i ended up putting a pin in it for now. But would be good to sort it out

Copy link
Copy Markdown
Contributor

@samijaber samijaber left a comment

Choose a reason for hiding this comment

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

Another bug I found: styles (probably other root-level attributes too) not applying to root VariantContainer itself: https://www.loom.com/share/8576990685a7420f8fa33cfbac9a6ce2

@sidmohanty11
Copy link
Copy Markdown
Contributor Author

@samijaber thanks for the catch again! it was for styles only, the block didn't have the builder-id in the classname so it wasn't getting applied. Have fixed this and added a test for this

Screenshot 2025-01-31 at 12 16 10 PM

Copy link
Copy Markdown
Contributor

@samijaber samijaber left a comment

Choose a reason for hiding this comment

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

🔥

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