Skip to content

Commit 8bb4ec7

Browse files
authored
[DEVREL-2687, DEVREL-2688]: Get component variants (#21)
1 parent 35cf0f3 commit 8bb4ec7

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/components/PermissionsMap.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export const permissionsMap: PermissionsMap = {
5454
getComponentByName: { permissions: ['canAccessCanvas'] },
5555
getComponentByNameAndGroup: { permissions: ['canAccessCanvas'] },
5656
getInstanceCount: { permissions: ['canAccessCanvas'] },
57+
getVariants: { permissions: ['canAccessCanvas'] },
58+
getSelectedVariant: { permissions: ['canAccessCanvas'] },
5759
enterComponent: { permissions: ['canModifyComponents'] },
5860
openCanvas: { permissions: ['canModifyComponents'] },
5961
selectComponent: { permissions: ['canModifyComponents'] },

src/designer-extension-typings/api.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ interface WebflowApi {
138138
name: string,
139139
root: AnyElement | ElementPreset<AnyElement> | Component
140140
): Promise<Component>;
141+
getComponentByName(string): Promise<null | Component>;
141142
/**
142143
* Delete a component from the Designer. If there are any instances of the Component within the site, they will
143144
* be converted to regular Elements.

src/designer-extension-typings/components.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,52 @@ interface Component {
3131
* ```
3232
*/
3333
setName(name: string): Promise<null>;
34+
/**
35+
* Retrieve all variants of a component.
36+
* @returns A Promise resolving to an array containing all variants of the component
37+
* @example
38+
* ```ts
39+
* const component = (await webflow.getAllComponents())[0];
40+
* const variants = await component.getVariants();
41+
* console.log(variants);
42+
* // [
43+
* // { id: 'base', name: 'Primary', isSelected: true },
44+
* // { id: 'xxxx', name: 'Secondary', isSelected: false },
45+
* // ]
46+
* // Find which variant the user is currently editing
47+
* const activeVariant = variants.find(v => v.isSelected);
48+
* console.log(`Currently editing: ${activeVariant.name}`);
49+
* ```
50+
*/
51+
getVariants(): Promise<Array<Variant>>;
52+
/**
53+
* Retrieves the selected variant of a component.
54+
* @returns A Promise resolving to a variant
55+
* @example
56+
* ```ts
57+
* const selectedVariant = await heroComponent.getSelectedVariant();
58+
* // {
59+
* // id: 'variant-123',
60+
* // name: 'Secondary Hero',
61+
* // isSelected: true,
62+
* // }
63+
* // // When no variant is explicitly selected, returns base
64+
* // const base = await heroComponent.getSelectedVariant();
65+
* // {
66+
* // id: 'base',
67+
* // name: 'Primary',
68+
* // isSelected: true,
69+
* // }
70+
* ```
71+
*/
72+
getSelectedVariant(): Promise<Variant>;
3473
getRootElement(): Promise<null | AnyElement>;
3574
}
3675

3776
type ComponentId = string;
77+
78+
interface Variant {
79+
id: string;
80+
name: string;
81+
isSelected: boolean;
82+
}

src/examples/components.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ export const Components = {
8080
} else {
8181
await webflow.unregisterComponent(hero);
8282
}
83+
getVariants: async () => {
84+
const component = (await webflow.getAllComponents())[0]
85+
const variants = await component.getVariants()
86+
console.log(variants)
87+
// [
88+
// { id: 'base', name: 'Primary', isSelected: true },
89+
// { id: 'xxxx', name: 'Secondary', isSelected: false },
90+
// ]
91+
// Find which variant the user is currently editing
92+
const activeVariant = variants.find(v => v.isSelected)
93+
console.log(`Currently editing: ${activeVariant?.name}`)
94+
},
95+
96+
getSelectedVariant: async () => {
97+
const heroComponent = webflow.getComponentByName('hero')
98+
// When no variant is explicitly selected, returns base
99+
const base = await heroComponent.getSelectedVariant()
100+
console.log(JSON.stringify(base))
101+
/*
102+
{
103+
id: 'base',
104+
name: 'Primary',
105+
isSelected: true,
106+
}
107+
*/
83108
},
84109

85110
createComponent: async () => {

0 commit comments

Comments
 (0)