Thanks for the replay @nicke920 !
I have looked through what you sent me. But still not exactly sure what I am doing wrong after a few attempts to pass a color prop from the custom Section component to it’s Child component and use it as a variant.
See code example below:
This below a CustomSection component I have where a user can select a color
via a Enum.
Once the user sets that color
. I want to be able to pass that color value down to whatever child element is rendered within that to be able to change the colors on that as well.
import { Builder, withChildren } from '@builder.io/react'; // import withChildren
export function CustomSection(props) {
const colorVariants = {
red: "bg-red-500",
blue: "bg-blue-500",
green: "bg-green-500",
};
return (
<div className={`py-16 md:py-30 ${colorVariants[props.color]}`}>
{/* I want to pass props.color to the child elements */}
{props.children}
</div>
);
}
export const CustomSectionWithBuilderChildren = withChildren(CustomSection)
export default CustomSection;
Builder.registerComponent(CustomSectionWithBuilderChildren, {
name: 'Section',
defaultChildren: [
{
'@type': '@builder.io/sdk:Element',
component: { name: 'Text', options: { text: 'I am child text block!' } }
}
],
inputs: [
{
name: 'color',
type: 'enum',
enum: ['red', 'blue', 'green'],
},
]
})
The Child Component here should be using a props.colorPassedFromParent
or whatever named prop to return the color from the parent component (in this case, the CustomSection
)
import { Builder } from '@builder.io/react'; // import withChildren
export function CustomChild(props) {
const colorVariants = {
red: "text-grey-900",
blue: "text-white",
green: "text-white",
};
return (
<div className='py-16 md:py-30'>
{/* I want props.color available here, passed from the parent. */}
<p className={colorVariants[props.colorPassedFromParent]}>{props.title}</p>
</div>
);
}
export default CustomChild;
Builder.registerComponent(CustomChild, {
name: 'CustomChild',
inputs: [
{ name: 'title', type: 'richText', defaultValue: 'I am a title', required: true },
]
})
I guess my question is, is this possible with the {props.children}
method of rendering child components? Or would I have to make a Section or Data model or something like that?