Subchapter 2.46
references/dashboard-page/DYNAMIC_PARAMETERS.mdMarkdown10 KBView on GitHub
Complete guide for managing dynamic parameters for embedded scripts in dashboard pages.
This dashboard page manages dynamic parameters for an embedded script. The parameters are configurable values that site owners can set through this dashboard interface, and they will be passed to the embedded script as template variables.
IMPORTANT: Only implement UI for parameters that are relevant to your current use case. Ignore parameters that don’t apply to the functionality you’re building. It’s perfectly fine to not use all parameters if they’re not applicable.
import { embeddedScripts } from '@wix/app-management';export type MyScriptOptions = {
headline: string;
text: string;
imageUrl: string;
activationMode: 'active' | 'timed' | 'disabled';
startDate?: string;
endDate?: string;
};const [options, setOptions] = useState<MyScriptOptions>(defaultOptions);
const [isLoading, setIsLoading] = useState(true);
const [isSaving, setIsSaving] = useState(false);
useEffect(() => {
const loadSettings = async () => {
try {
const embeddedScript = await embeddedScripts.getEmbeddedScript();
const data = embeddedScript.parameters as Partial<Record<keyof MyScriptOptions, string>> || {};
setOptions((prev) => ({
...prev,
textField: data?.textField || prev.textField,
booleanField: data?.booleanField === 'true' ? true : data?.booleanField === 'false' ? false : prev.booleanField,
numberField: Number(data?.numberField) || prev.numberField,
}));
} catch (error) {
console.error('Failed to load settings:', error);
} finally {
setIsLoading(false);
}
};
loadSettings();
}, []);{isLoading ? (
<Box align="center" verticalAlign="middle" height="50vh">
<Loader text="Loading..." />
</Box>
) : (
// ... form content
)}See the generated site-popup example for a complete reference implementation:
Key implementation patterns from the example:
When dynamic parameters are present, you MUST generate these files:
The withProviders.tsx is NOT optional - it must always be generated when there are dynamic parameters.
You MUST generate the following file: src/extensions/dashboard/withProviders.tsx
This file is REQUIRED to wrap dashboard components with the Wix Design System provider.
import React from 'react';
import { WixDesignSystemProvider } from '@wix/design-system';
import { i18n } from '@wix/essentials';
export default function withProviders<P extends {} = {}>(Component: React.FC<P>) {
return function DashboardProviders(props: P) {
const locale = i18n.getLocale();
return (
<WixDesignSystemProvider locale={locale} features={{ newColorsBranding: true }}>
<Component {...props} />
</WixDesignSystemProvider>
);
};
}
// Also export as named export for backwards compatibility
export { withProviders };This file must be included in your generated files output.
In your dashboard page component (page.tsx):
import withProviders from '../../withProviders';export default withProviders(MyComponent);Example structure:
import { useEffect, useState, type FC } from 'react';
import { dashboard } from '@wix/dashboard';
import { embeddedScripts } from '@wix/app-management';
import { Page, Card, Button, ... } from '@wix/design-system';
import '@wix/design-system/styles.global.css';
import withProviders from '../../withProviders';
const MyDashboardPage: FC = () => {
const [options, setOptions] = useState<MyScriptOptions>(defaultOptions);
const [isLoading, setIsLoading] = useState(true);
const [isSaving, setIsSaving] = useState(false);
useEffect(() => {
const loadSettings = async () => {
try {
const embeddedScript = await embeddedScripts.getEmbeddedScript();
const data = embeddedScript.parameters || {};
// ... update options with data
} catch (error) {
console.error('Failed to load settings:', error);
} finally {
setIsLoading(false);
}
};
loadSettings();
}, []);
const handleSave = async () => {
setIsSaving(true);
try {
await embeddedScripts.embedScript({ parameters: { /* ... */ } });
dashboard.showToast({ message: 'Saved!', type: 'success' });
} catch (error) {
console.error('Failed to save:', error);
dashboard.showToast({ message: 'Failed to save', type: 'error' });
} finally {
setIsSaving(false);
}
};
return (
<Page height="100vh">
{/* Page content - NO WixDesignSystemProvider here */}
</Page>
);
};
export default withProviders(MyDashboardPage);