DEV: Plugin api for adding extra header icons (#9964)

DEV: Plugin api for adding extra header icons (#9964)

diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index 101c97d..879eeac 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -28,7 +28,10 @@ import { addTagsHtmlCallback } from "discourse/lib/render-tags";
 import { addUserMenuGlyph } from "discourse/widgets/user-menu";
 import { addPostClassesCallback } from "discourse/widgets/post";
 import { addPostTransformCallback } from "discourse/widgets/post-stream";
-import { attachAdditionalPanel } from "discourse/widgets/header";
+import {
+  attachAdditionalPanel,
+  addToHeaderIcons
+} from "discourse/widgets/header";
 import {
   registerIconRenderer,
   replaceIcon
@@ -1141,6 +1144,19 @@ class PluginApi {
   addCategoryLinkIcon(renderer) {
     addExtraIconRenderer(renderer);
   }
+  /**
+   * Adds a widget to the header-icon ul. The widget must already be created. You can create new widgets
+   * in a theme or plugin via an initializer prior to calling this function.
+   *
+   * `‍``
+   * api.addToHeaderIcons(
+   *  createWidget('some-widget')
+   * `‍``
+   *
+   **/
+  addToHeaderIcons(icon) {
+    addToHeaderIcons(icon);
+  }
 }
 
 let _pluginv01;
diff --git a/app/assets/javascripts/discourse/app/widgets/header.js b/app/assets/javascripts/discourse/app/widgets/header.js
index 2580fff..4699a58 100644
--- a/app/assets/javascripts/discourse/app/widgets/header.js
+++ b/app/assets/javascripts/discourse/app/widgets/header.js
@@ -12,6 +12,12 @@ import { addExtraUserClasses } from "discourse/helpers/user-avatar";
 import { scrollTop } from "discourse/mixins/scroll-top";
 import { h } from "virtual-dom";
 
+const _extraHeaderIcons = [];
+
+export function addToHeaderIcons(icon) {
+  _extraHeaderIcons.push(icon);
+}
+
 const dropdown = {
   buildClasses(attrs) {
     if (attrs.active) {
@@ -226,6 +232,12 @@ createWidget("header-icons", {
       );
     }
 
+    if (_extraHeaderIcons) {
+      _extraHeaderIcons.forEach(icon => {
+        icons.push(this.attach(icon));
+      });
+    }
+
     return icons;
   }
 });

GitHub sha: b3c8d364

1 Like

This commit appears in #9964 which was approved by eviltrout. It was merged by markvanlan.