Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/PermissionsMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export const permissionsMap: PermissionsMap = {
registerComponent: { permissions: ['canCreateComponents'] },
unregisterComponent: { permissions: ['canCreateComponents'] },
getAllComponents: { permissions: ['canAccessCanvas'] },
getVariants: { permissions: ['canAccessCanvas'] },
getSelectedVariant: { permissions: ['canAccessCanvas'] },
enterComponent: { permissions: ['canModifyComponents'] },
exitComponent: { permissions: ['canAccessCanvas'] },
},
Expand Down
1 change: 1 addition & 0 deletions src/designer-extension-typings/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ interface WebflowApi {
name: string,
root: AnyElement | ElementPreset<AnyElement> | Component
): Promise<Component>;
getComponentByName(string): Promise<null | Component>;
/**
* Delete a component from the Designer. If there are any instances of the Component within the site, they will
* be converted to regular Elements.
Expand Down
45 changes: 45 additions & 0 deletions src/designer-extension-typings/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,52 @@ interface Component {
* ```
*/
setName(name: string): Promise<null>;
/**
* Retrieve all variants of a component.
* @returns A Promise resolving to an array containing all variants of the component
* @example
* ```ts
* const component = (await webflow.getAllComponents())[0];
* const variants = await component.getVariants();
* console.log(variants);
* // [
* // { id: 'base', name: 'Primary', isSelected: true },
* // { id: 'xxxx', name: 'Secondary', isSelected: false },
* // ]
* // Find which variant the user is currently editing
* const activeVariant = variants.find(v => v.isSelected);
* console.log(`Currently editing: ${activeVariant.name}`);
* ```
*/
getVariants(): Promise<Array<Variant>>;
/**
* Retrieves the selected variant of a component.
* @returns A Promise resolving to a variant
* @example
* ```ts
* const selectedVariant = await heroComponent.getSelectedVariant();
* // {
* // id: 'variant-123',
* // name: 'Secondary Hero',
* // isSelected: true,
* // }
* // // When no variant is explicitly selected, returns base
* // const base = await heroComponent.getSelectedVariant();
* // {
* // id: 'base',
* // name: 'Primary',
* // isSelected: true,
* // }
* ```
*/
getSelectedVariant(): Promise<Variant>;
getRootElement(): Promise<null | AnyElement>;
}

type ComponentId = string;

interface Variant {
id: string;
name: string;
isSelected: boolean;
}
27 changes: 27 additions & 0 deletions src/examples/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@ export const Components = {
}
},

getVariants: async () => {
const component = (await webflow.getAllComponents())[0]
const variants = await component.getVariants()
console.log(variants)
// [
// { id: 'base', name: 'Primary', isSelected: true },
// { id: 'xxxx', name: 'Secondary', isSelected: false },
// ]
// Find which variant the user is currently editing
const activeVariant = variants.find(v => v.isSelected)
console.log(`Currently editing: ${activeVariant?.name}`)
},

getSelectedVariant: async () => {
const heroComponent = webflow.getComponentByName('hero')
// When no variant is explicitly selected, returns base
const base = await heroComponent.getSelectedVariant()
console.log(JSON.stringify(base))
/*
{
id: 'base',
name: 'Primary',
isSelected: true,
}
*/
},

createComponent: async () => {
// Get selected element
const rootElement = await webflow.getSelectedElement()
Expand Down