@webflow/react
v1.2.2
Published
The core React integration package for building Webflow code components. This package provides the essential tools for declaring components, rendering them on both client and server, and accessing Webflow-specific context.
Downloads
64,459
Maintainers
Keywords
Readme
@webflow/react
The core React integration package for building Webflow code components. This package provides the essential tools for declaring components, rendering them on both client and server, and accessing Webflow-specific context.
Installation
npm i @webflow/reactPeer Dependencies
This package requires the following peer dependencies:
npm i react react-domUsage
Declaring Components
Use declareComponent to create a Webflow code component definition. This should be the default export from your *.webflow.tsx file.
Basic Example
import { declareComponent } from "@webflow/react";
import { props } from "@webflow/data-types";
function Button({ text, link }) {
return (
<a href={link?.href} target={link?.target}>
{text}
</a>
);
}
export default declareComponent(Button, {
name: "Button",
description: "A customizable button component",
props: {
text: props.Text({ name: "Text", defaultValue: "Click me" }),
link: props.Link({ name: "Link" }),
},
});With Decorators
Decorators allow you to wrap your component with additional functionality, such as CSS-in-JS providers:
import { declareComponent } from "@webflow/react";
import { emotionShadowDomDecorator } from "@webflow/emotion-utils";
export default declareComponent(MyComponent, {
name: "My Component",
decorators: [emotionShadowDomDecorator],
});With Options
export default declareComponent(MyComponent, {
name: "My Component",
props: {
// ... your props
},
options: {
applyTagSelectors: true, // Provide styles targeting tag selectors (default: false)
ssr: true, // Enable server-side rendering (default: true)
},
});Using Webflow Context
Access Webflow-specific context data in your components using the useWebflowContext hook:
import { useWebflowContext } from "@webflow/react";
function MyComponent() {
const { mode, interactive, locale } = useWebflowContext();
return (
<div>
<p>Mode: {mode}</p>
<p>Interactive: {interactive ? "Yes" : "No"}</p>
<p>Locale: {locale}</p>
</div>
);
}Context Properties:
mode- The current mode ("design"or"preview")interactive- Whether the component is in an interactive statelocale- The current locale (e.g.,"en-US")
Server-Side Rendering
This package provides a server-side renderer for React components. The ServerRenderer provides:
- Server-side rendering with
renderToStringandrenderToStream - Support for creating slot elements with
createSlot - Automatic handling of Webflow context during SSR
Note: For CSS-in-JS libraries like Emotion or styled-components, use their respective server renderers instead:
@webflow/emotion-utils/server@webflow/styled-components-utils/server
Configure the server renderer in your webflow.json file:
{
"library": {
"renderer": {
"server": "@webflow/emotion-utils/server"
}
}
}API Reference
declareComponent
Creates a Webflow code component definition.
Type:
<P extends {}>(
Component: React.ComponentType<P>,
data: ComponentData<P, React.ReactNode, React.ComponentType<P>>,
) => ComponentDefinition<React.ComponentType<P>, React.ReactNode, P>;Parameters:
Component- The React component to renderdata- Component metadata and configurationdata.name- The display name of the componentdata.description(optional) - Description of the componentdata.group(optional) - Group for organizing componentsdata.props(optional) - Component props configurationdata.options(optional) - Additional optionsdata.options.applyTagSelectors(optional) - Provide tag selector styles (default:false)data.options.ssr(optional) - Enable server-side rendering (default:true)
data.decorators(optional) - Array of decorator functions
Returns: A Webflow code component definition
useWebflowContext
Hook to access Webflow context data.
Type:
() => WebflowContextType;Returns:
{
mode: "design" | "preview";
interactive: boolean;
locale: string;
}ClientRenderer
A factory that creates a client-side renderer for a React component.
Type:
ComponentClientRendererFactory<
React.ComponentType<ComponentRuntimeProps<React.ReactNode>>,
ReactDOM.Root,
React.ReactNode
>;Methods:
mount(domNode)- Creates a React root on the provided DOM nodehydrate(domNode, props?, options?)- Hydrates a server-rendered componentrender(root, props?, options?)- Renders the component to an existing rootcreateSlot(name)- Creates a named slot element for component composition
ServerRenderer
A factory that creates a server-side renderer for a React component.
Type:
ComponentServerRendererFactory<
React.ComponentType<ComponentRuntimeProps<React.ReactNode>>,
PipeableStream,
ReactDOMServer.RenderToPipeableStreamOptions,
React.ReactNode,
ReactDOMServer.ServerOptions
>;Methods:
renderToStream(props?, options?, streamOptions?)- Renders component to a pipeable streamrenderToString(props?, options?, stringOptions?)- Renders component to a stringcreateElement(props?, options?)- Creates a React element with the componentcreateSlot(name)- Creates a named slot element for component composition
applyDecorators
Utility function to apply an array of decorators to a component.
Type:
<P extends {}>(
Component: React.ComponentType<P>,
decorators: Array<
(Component: React.ComponentType<P>) => React.ComponentType<P>
>,
) => React.ComponentType<P>;License
MIT
