Skip to content

Commit 27bf0cb

Browse files
committed
feat: enhance menu contribution provider to support icon registration
AdminForth/1686/we-need-here-same-looking-gif-
1 parent 37f768d commit 27bf0cb

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎adminforth/documentation/docs/tutorial/03-Customization/10-menuConfiguration.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,14 @@ async modifyResourceConfig(adminforth, resourceConfig) {
154154
placement: { position: 'first' },
155155
},
156156
];
157+
}, {
158+
icons: ['flowbite:chart-pie-solid'],
157159
});
158160
}
159161
```
160162

163+
AdminForth bundles menu icons when it builds the SPA. Providers are called during the build too, but without `adminUser`, so items they return only at runtime are not visible there. List every icon the provider can return in the `icons` option, otherwise it renders empty.
164+
161165
After the plugin changes the state used by the provider, call `refreshMenu` on the backend:
162166

163167
```ts
@@ -528,10 +532,14 @@ async modifyResourceConfig(adminforth, resourceConfig) {
528532
placement: { position: 'first' },
529533
},
530534
];
535+
}, {
536+
icons: ['flowbite:chart-pie-solid'],
531537
});
532538
}
533539
```
534540

541+
AdminForth bundles menu icons when it builds the SPA. Providers are called during the build too, but without `adminUser`, so items they return only at runtime are not visible there. List every icon the provider can return in the `icons` option, otherwise it renders empty.
542+
535543
After the plugin changes the state used by the provider, call `refreshMenu` on the backend:
536544

537545
```ts

‎adminforth/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class AdminForth implements IAdminForth {
117117
pluginsById: Record<string, AdminForthPlugin> = {};
118118
private menuContributions: AdminForthMenuContribution[] = [];
119119
private menuContributionProviders: AdminForthMenuContributionProvider[] = [];
120+
menuContributionProviderIcons: string[] = [];
120121
configValidator: IConfigValidator;
121122
restApi: AdminForthRestAPI;
122123

@@ -126,8 +127,9 @@ class AdminForth implements IAdminForth {
126127
this.menuContributions.push(contribution);
127128
}
128129

129-
registerMenuContributionProvider(provider: AdminForthMenuContributionProvider): void {
130+
registerMenuContributionProvider(provider: AdminForthMenuContributionProvider, { icons = [] }: { icons?: string[] } = {}): void {
130131
this.menuContributionProviders.push(provider);
132+
this.menuContributionProviderIcons.push(...icons);
131133
}
132134

133135
getMenuContributions(): AdminForthMenuContribution[] {

‎adminforth/modules/codeInjector.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ class CodeInjector implements ICodeInjector {
766766
registerCustomPages(this.adminforth.config);
767767
const menuWithContributions = await this.adminforth.getMenuWithContributions();
768768
collectAssetsFromMenu(menuWithContributions);
769+
// providers get no adminUser here, so icons they return only at runtime are declared on registration
770+
icons.push(...this.adminforth.menuContributionProviderIcons);
769771
registerSettingPages(this.adminforth.config.auth.userMenuSettingsPages);
770772
const spaDir = this.getSpaDir();
771773

‎adminforth/types/Back.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,13 @@ export interface IAdminForth {
701701
getPluginById<T>(id: string): T;
702702

703703
registerMenuContribution(contribution: AdminForthMenuContribution): void;
704-
registerMenuContributionProvider(provider: AdminForthMenuContributionProvider): void;
704+
/**
705+
* Registers a provider which is called every time the menu is fetched.
706+
*
707+
* AdminForth bundles menu icons when it builds the SPA, and providers are called there without `adminUser`,
708+
* so list in `options.icons` every icon the provider can return, otherwise it renders empty.
709+
*/
710+
registerMenuContributionProvider(provider: AdminForthMenuContributionProvider, options?: { icons?: string[] }): void;
705711
getMenuContributions(): AdminForthMenuContribution[];
706712
getMenuWithContributions(adminUser?: AdminUser, menu?: AdminForthConfigMenuItem[]): Promise<AdminForthConfigMenuItem[]>;
707713

0 commit comments

Comments
 (0)