Skip to content

Commit

Permalink
DEV: Refactor icon links to use Glimmer templates
Browse files Browse the repository at this point in the history
The site header was refactored to a Glimmer component, which does not support the previous decorateWidget API. Refactor icon links to use Glimmer templates and the headerIcons.add API instead.
  • Loading branch information
kvognar committed Mar 10, 2024
1 parent 7625191 commit 94b1999
Showing 1 changed file with 24 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { withPluginApi } from "discourse/lib/plugin-api";
import { iconNode } from "discourse-common/lib/icon-library";
import { iconHTML } from "discourse-common/lib/icon-library";
import { dasherize } from "@ember/string";
import isValidUrl from "../lib/isValidUrl";
import { h } from "virtual-dom";
import { htmlSafe } from "@ember/template";

function buildIcon(icon) {
if (isValidUrl(icon)) {
return h(
"img",
{
attributes: {
src: icon,
},
},
""
);
function buildIcon(iconNameOrImageUrl) {
if (isValidUrl(iconNameOrImageUrl)) {
return <template>
<img src="{{iconNameOrImageUrl}}"/>
</template>
} else {
return iconNode(icon.toLowerCase());
return htmlSafe(iconHTML(iconNameOrImageUrl.toLowerCase()));
}
}

Expand All @@ -38,24 +32,23 @@ export default {
const rel = target ? "noopener" : "";
const isLastLink =
link === links[links.length - 1] ? ".last-custom-icon" : "";
const selector = `li.custom-header-icon-link.${className}.${viewClass}${isLastLink}`;

api.decorateWidget("header-icons:before", (helper) => {
return helper.h(selector, [
helper.h(
"a.icon.btn-flat",
{
href,
title,
target,
attributes: {
rel,
},
},
icon
),
]);
});
const iconComponent = <template>
<li class="custom-header-icon-link {{className}} {{viewClass}} {{isLastLink}}">
<a class="icon btn-flat"
href="{{href}}"
title="{{title}}"
target="{{target}}"
rel="{{rel}}"
>
{{icon}}
</a>
</li>
</template>

const beforeIcon = ['chat', 'search', 'hamburger', 'user-menu']

api.headerIcons.add(title, iconComponent, { before: beforeIcon })
});
} catch (error) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 94b1999

Please sign in to comment.