Skip to content

Commit 6848c15

Browse files
authored
DEV: Refactor icon links to use Glimmer templates (#21)
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.
1 parent 7625191 commit 6848c15

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

javascripts/discourse/initializers/initialize-for-header-icon-links.js renamed to javascripts/discourse/initializers/initialize-for-header-icon-links.gjs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
import { withPluginApi } from "discourse/lib/plugin-api";
2-
import { iconNode } from "discourse-common/lib/icon-library";
2+
import icon from "discourse-common/helpers/d-icon";
33
import { dasherize } from "@ember/string";
44
import isValidUrl from "../lib/isValidUrl";
5-
import { h } from "virtual-dom";
65

7-
function buildIcon(icon) {
8-
if (isValidUrl(icon)) {
9-
return h(
10-
"img",
11-
{
12-
attributes: {
13-
src: icon,
14-
},
15-
},
16-
""
17-
);
6+
function buildIcon(iconNameOrImageUrl, title) {
7+
if (isValidUrl(iconNameOrImageUrl)) {
8+
return <template>
9+
<img src="{{iconNameOrImageUrl}}" aria-hidden="true"/>
10+
<span class="sr-only">{{title}}</span>
11+
</template>
1812
} else {
19-
return iconNode(icon.toLowerCase());
13+
return <template>{{icon iconNameOrImageUrl label=title}}</template>
2014
}
2115
}
2216

@@ -30,32 +24,31 @@ export default {
3024
splitLinks.forEach((link, index, links) => {
3125
const fragments = link.split(",").map((fragment) => fragment.trim());
3226
const title = fragments[0];
33-
const icon = buildIcon(fragments[1]);
27+
const iconTemplate = buildIcon(fragments[1], title);
3428
const href = fragments[2];
3529
const className = `header-icon-${dasherize(fragments[0])}`;
3630
const viewClass = fragments[3].toLowerCase();
3731
const target = fragments[4].toLowerCase() === "blank" ? "_blank" : "";
3832
const rel = target ? "noopener" : "";
3933
const isLastLink =
40-
link === links[links.length - 1] ? ".last-custom-icon" : "";
41-
const selector = `li.custom-header-icon-link.${className}.${viewClass}${isLastLink}`;
34+
link === links[links.length - 1] ? "last-custom-icon" : "";
4235

43-
api.decorateWidget("header-icons:before", (helper) => {
44-
return helper.h(selector, [
45-
helper.h(
46-
"a.icon.btn-flat",
47-
{
48-
href,
49-
title,
50-
target,
51-
attributes: {
52-
rel,
53-
},
54-
},
55-
icon
56-
),
57-
]);
58-
});
36+
const iconComponent = <template>
37+
<li class="custom-header-icon-link {{className}} {{viewClass}} {{isLastLink}}">
38+
<a class="icon btn-flat"
39+
href={{href}}
40+
title={{title}}
41+
target={{target}}
42+
rel={{rel}}
43+
>
44+
{{iconTemplate}}
45+
</a>
46+
</li>
47+
</template>
48+
49+
const beforeIcon = ['chat', 'search', 'hamburger', 'user-menu']
50+
51+
api.headerIcons.add(title, iconComponent, { before: beforeIcon })
5952
});
6053
} catch (error) {
6154
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)