Top Menu Logout Button Theme Component

I would not recommend using a widget; it will eventually disappear as part of the core modernization.

The same is true for api.addToHeaderIcons; it’s now deprecated in favor of api.headerIcons.add.

Here are some suggestions:

  • I encourage you to use the GitHub - discourse/discourse-theme-skeleton: Template for Discourse themes · GitHub as a reference when creating a theme component. It contains the latest configuration files. It’s helpful for linting/prettifying code and a good practice to have a standard structure.

  • When using the <template> syntax, rename the file with .gjs (glimmer JS); otherwise, it may not work.

  • In my example, you could move the template in its file, for example:
    /javascripts/discourse/components/logout-header-button.gjs

import DButton from "discourse/components/d-button";
import routeAction from "discourse/helpers/route-action";

const LogoutHeaderButton = <template>
  <li class="logout">
    <DButton
      @action={{routeAction "logout"}}
      class="btn-flat icon"
      @icon="sign-out-alt"
      @title="user.log_out"
      @label="user.log_out"
    />
  </li>
</template>;

export default LogoutHeaderButton;

Then, in the initializer, you import the component and use it as a parameter in headerIcons:

import LogoutHeaderButton from "../components/logout-header-button";

export default apiInitializer("1.28.0", (api) => {
  if (api.getCurrentUser()) {
    api.headerIcons.add("logout", LogoutHeaderButton, { before: "auth" });
  }
});

  • The location of the button can be adjusted. You can read the documentation:

https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/lib/plugin-api.gjs#L1838-L1886

Let me know if you have questions. I can always PR later if you need an example. :+1: