Set variable value
This page is for Designer API.
variable.set(value, options?)
Set the value of the selected variable.
Syntax
variable.set(value: string | number | SizeValue | Variable | CustomValue,options?: {mode: VariableMode,}): Promise<null>;
Parameters
| Parameter | Description | Type |
|---|---|---|
| value | The value to set for the variable. Must match the variable’s type. You can also pass a Variable object to create an alias, a CustomValue for custom values, or null to reset a mode-specific value to its default. | string | number | SizeValue | Variable | CustomValue | null |
| options | An optional parameter to set the mode value of the variable. | {mode: VariableMode} |
Resetting a mode-specific value
To reset a mode-specific value back to the default base value, pass null
when calling variable.set() along with the mode you want to reset. Variables
without a mode-specific value won’t be affected.
Variable values by type
Color
Size
Number & Percentage
FontFamily
collection.createColorVariable(name: string, value: string | ColorVariable | CustomValue): Promise<ColorVariable>
| Accepted Formats for Value | Examples |
|---|---|
| Color name | collection.createColorVariable("primary", "red"); |
| RGB Hex | collection.createColorVariable("primary", "#ffcc11"); |
| RGBA Hex | collection.createColorVariable("primary", "#fffcc11"); |
collection.createSizeVariable(name: string, value: SizeValue | SizeVariable | CustomValue): Promise<SizeVariable>
| Format | Unit | Examples |
|---|---|---|
| Pixels | px | { unit: "px", value: 50 }); |
| Root EM | rem | { unit: "rem", value: 2 }); |
| EM Units | em | { unit: "em", value: 1.5 }); |
| Viewport Width | vw | { unit: "vw", value: 100 }); |
| Viewport Height | vh | { unit: "vh", value: 100 }); |
| Small Viewport Height | svh | { unit: "svh", value: 100 }); |
| Small Viewport Width | svw | { unit: "svw", value: 100 }); |
| Character Units | ch | { unit: "ch", value: 80 }); |
collection.createNumberVariable(name: string, value: number | NumberVariable | CustomValue): Promise<NumberVariable>
| Accepted Formats for Value | Examples |
|---|---|
| Number value between 0 and 100 | collection.createPercentageVariable("opacity", 50); |
collection.createFontFamilyVariable(name: string, value: string | FontFamilyVariable | CustomValue): Promise<FontFamilyVariable>
| Accepted Formats for Value | Examples |
|---|---|
| String of the font family name (e.g., “Verdana”) | collection.createFontFamilyVariable("defaultFont", "Verdana"); |
Returns
Promise<null>
A Promise that resolves to null
Examples
Color
Size
Number & Percentage
Font family
Alias
Custom value
// Get Collectionconst collection = await webflow.getDefaultVariableCollection();// Get Variableconst variable = await collection?.getVariableByName("MyColorVariable");// Check Variable type and set colorif (variable?.type === "Color") await variable.set("#fffcc11");
// Get Collectionconst collection = await webflow.getDefaultVariableCollection();// Get Variableconst variable = await collection?.getVariableByName("MySizeVariable");// Check Variable type and set sizeif (variable?.type === "Size") await variable.set({ value: 16, unit: "px" });
// Get Collectionconst collection = await webflow.getDefaultVariableCollection();// Get Variableconst variable = await collection?.getVariableByName("MyNumberVariable");// Check Variable type and set numberif (variable?.type === "Number") await variable.set(100);
// Get Collectionconst collection = await webflow.getDefaultVariableCollection();// Get Variableconst variable = await collection?.getVariableByName("MyFontVariable");// Check Variable type and set font familyif (variable?.type === "FontFamily") await variable.set("Verdana");
// Get Collectionconst collection = await webflow.getDefaultVariableCollection();// Get Variablesconst variableToSet = await collection?.getVariableByName("MyAliasVariable");const variableToAlias = await collection?.getVariableByName("MyColorVariable");// Check that Variable types are compatible and set aliasif (variableToSet?.type === "Color" && variableToAlias) {await variableToSet.set(variableToAlias);}
// Get Collectionconst collection = await webflow.getDefaultVariableCollection();// Get Variableconst variable = await collection?.getVariableByName("MyCustomVariable");// Check Variable type and set custom valueif (variable?.type === "Color") {await variable.set({type: "custom",value: "color-mix(in srgb, var(--blue-500), #fff 60%)"});}
Designer ability
| Designer Ability | Locale | Branch | Workflow | Sitemode |
|---|---|---|---|---|
| canModifyVariables | Any | Main | Canvas | Design |