Set variable mode on a style

style.setVariableMode(collection, mode, options?)

Apply a variable mode to a style. Each variable mode belongs to a variable collection. To apply a variable mode, you must provide the variable collection that belongs to the variable mode.


Syntax

style.setVariableMode(
collection: VariableCollection,
mode: VariableMode,
options?: {
breakpointId?: BreakpointId,
pseudoStateKey?: PseudoStateKey
}
): Promise<null>;

Parameters

  • collection: VariableCollection - The variable collection that belongs to the variable mode.

  • mode: VariableMode - The variable mode to set.

  • options?: BreakpointAndPseudo - An object that lets you filter properties by breakpoint and/or pseudo-state. (Optional)

    • BreakpointId: Identifies the responsive breakpoint to get styles for.

      type BreakpointId = "xxl" | "xl" | "large" | "main" | "medium" | "small" | "tiny"
    • PseudoStateKey: Specifies a CSS pseudo-class to get styles for.

      type PseudoStateKey = "noPseudo" | "nth-child(odd)" | "nth-child(even)" |
      "first-child" | "last-child" | "hover" | "active" |
      "visited" | "focus" | "focus-visible" | "focus-within" |
      "placeholder" | "empty" | "before" | "after"

Returns

Promise<null>

A Promise that resolves to null.

Example

// Get Selected Element
const selectedElement = await webflow.getSelectedElement()
if (selectedElement?.styles) {
// Get Styles
const styles = await selectedElement.getStyles()
const primaryStyle = styles[0] // Get the primary style
// Get Variable Collection
const variableCollection = await webflow.getDefaultVariableCollection()
const variableModes = await variableCollection?.getAllVariableModes()
const variableMode = variableModes[0]
// Set Variable Mode
if (primaryStyle && variableCollection) {
await primaryStyle.setVariableMode(variableCollection, variableMode)
console.log('Variable mode set successfully')
}
}

Designer ability

Designer AbilityLocaleBranchWorkflowSitemode
canModifyStyleBlocksAnyAnyCanvasDesign