Skip to content

Commit f306d04

Browse files
committed
Adds top actions documentation
1 parent ae804d5 commit f306d04

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
title: Adding support for Top Actions
3+
description: Top Actions is a SharePoint Framework feature that allows web part developers to add commands to a web part's toolbar
4+
ms.date: 11/15/2022
5+
---
6+
7+
# Adding support for Top Actions
8+
9+
Today, users need to be aware of the web part property panels to find out the additional options each web part provides. This is a common piece of feedback where users want actions surfacing in context of where they are without having to rely on opening something to get to those options. Therefore, we are now surfacing most common configurations from a web part's property panel directly on to the web part's toolbar. These common configurations are referred to as the web part's Top Actions.
10+
11+
12+
> At the time of writing this article, Top Actions only supports rendering a drop down and button command.
13+
14+
## Getting started
15+
16+
> [!TIP]
17+
> These instructions assume you know [how to create a hello world web part](../get-started/build-a-hello-world-web-part.md).
18+
19+
### 1. Define your Top Action configurations
20+
21+
In the example below we are defining the callback function that will be used to pull the configurations for our Top Action commands.
22+
23+
> Note: `getTopActionsConfiguration` must be defined as public on your web part's class.
24+
25+
```typescript
26+
import { ITopActions } from '@microsoft/sp-top-actions';
27+
28+
public getTopActionsConfiguration(): ITopActions | undefined {
29+
return {
30+
topActions: [],
31+
onExecute: (actionName: string, newValue: any) => {}
32+
};
33+
}
34+
```
35+
36+
### 3. Define your toolbar's user interface
37+
38+
The `topActions` array is an ordered list of controls to render in the web part toolbar. In the example below we are defining one top action as a button interface.
39+
40+
```typescript
41+
import { PropertyPaneFieldType } from '@microsoft/sp-property-pane';
42+
43+
return {
44+
topActions: [
45+
{
46+
targetProperty: 'reset',
47+
properties: {
48+
icon: 'Reset'
49+
},
50+
type: PropertyPaneFieldType.Button
51+
}
52+
]
53+
...
54+
}
55+
```
56+
57+
### 4. Execute the command when the user interacts
58+
The previous step demonstrated how to get a button to display in the web part's toolbar. Now we will perform an action when the user clicks the button. Note that `actionName` was defined as `targetProperty` in the last step and since this is a button we can ignore the `newValue` that comes in.
59+
60+
```typescript
61+
return {
62+
...
63+
onExecute: (actionName: string, newValue: any) => {
64+
if (actionName === 'reset') {
65+
// user defined logic to reset the web part
66+
this.reset();
67+
}
68+
}
69+
}
70+
```
71+
> Common pitfalls when implementing the `onExecute` command, is not syncing the new state with the web part properties and/or not refreshing or re-rendering the web part.
72+
73+
## Code Snippets
74+
### Button command
75+
The type interace for a button is similar to the property panel's button (`IPropertyPaneButtonProps`).
76+
77+
```typescript
78+
import { ITopActions } from '@microsoft/sp-top-actions';
79+
import { PropertyPaneFieldType } from '@microsoft/sp-property-pane';
80+
...
81+
public getTopActionsConfiguration(): ITopActions | undefined {
82+
return {
83+
topActions: [
84+
{
85+
targetProperty: 'reset',
86+
type: PropertyPaneFieldType.Button,
87+
properties: {
88+
icon: 'Reset'
89+
}
90+
}
91+
],
92+
onExecute: (actionName: string, newValue: any) => {
93+
if (actionName === 'reset') {
94+
// user defined logic to reset the web part
95+
this.reset();
96+
}
97+
}
98+
};
99+
}
100+
```
101+
102+
### Drop down command
103+
The type interace for a drop down is similar to the property panel's choice group (`IPropertyPaneChoiceGroupOption`).
104+
105+
```typescript
106+
import { ITopActions } from '@microsoft/sp-top-actions';
107+
import { PropertyPaneFieldType } from '@microsoft/sp-property-pane';
108+
...
109+
public getTopActionsConfiguration(): ITopActions | undefined {
110+
return {
111+
topActions: [{
112+
targetProperty: 'layout',
113+
type: PropertyPaneFieldType.ChoiceGroup,
114+
properties: {
115+
options: [
116+
{
117+
// key maps to newValue in onExecute
118+
key: 'card',
119+
text: 'Card Layout',
120+
imageSize: { width: 32, height: 32 },
121+
iconProps: { officeFabricIconFontName: 'ArticlesIcon' },
122+
checked: this.state.layout === 'card'
123+
},
124+
{
125+
key: 'list',
126+
text: 'List Layout',
127+
imageSize: { width: 32, height: 32 },
128+
// you can use iconProps, icon to define icons
129+
icon: 'List',
130+
checked: this.state.alignment === 'list'
131+
}
132+
]
133+
}
134+
}],
135+
// for ChoiceGroup drop down, the newValue tells us which option's key was selected
136+
onExecute: (actionName: string, newValue: any) => {
137+
if (actionName === 'layout') {
138+
this.setLayout(newValue);
139+
this.render();
140+
}
141+
}
142+
};
143+
}
144+
```
145+
146+
## Advanced configurations
147+
For advanced configurations of your top action commands, checkout the type definitions from `@microsoft/sp-property-pane`. Currently, the two supported topActions, button and drop down, can be defined using the same structure as `IPropertyPaneChoiceGroupOption` and `IPropertyPaneButtonProps`. From here you can see how to use features like icons, aria labels, disable state, and more.
148+
149+
```typescript
150+
import { IPropertyPaneButtonProps, IPropertyPaneChoiceGroupOption } from '@microsoft/sp-property-pane'
151+
```

0 commit comments

Comments
 (0)