Skip to content

Commit 95a6c3d

Browse files
authored
FIX: allows admins to set a width to the icon (#27)
It's useful when using an image as icon which can have a large width that our icons.
1 parent 03fe6e6 commit 95a6c3d

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { dasherize } from "@ember/string";
22
import concatClass from "discourse/helpers/concat-class";
33
import { withPluginApi } from "discourse/lib/plugin-api";
4+
import { escapeExpression } from "discourse/lib/utilities";
45
import icon from "discourse-common/helpers/d-icon";
56
import isValidUrl from "../lib/isValidUrl";
67

@@ -40,6 +41,11 @@ export default {
4041
const isLastLink =
4142
index === links.length - 1 ? "last-custom-icon" : "";
4243

44+
let style = "";
45+
if (link.width) {
46+
style = `width: ${escapeExpression(link.width)}px`;
47+
}
48+
4349
const iconComponent = <template>
4450
<li
4551
class={{concatClass
@@ -55,6 +61,7 @@ export default {
5561
title={{link.title}}
5662
target={{target}}
5763
rel={{rel}}
64+
style={{style}}
5865
>
5966
{{iconTemplate}}
6067
</a>

locales/en.yml

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ en:
1414
url:
1515
label: URL
1616
description: The URL for the link
17+
width:
18+
label: Width
19+
description: Allows to set the width of the icon
1720
view:
1821
label: View
1922
description: |

settings.yml

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ header_links:
5050
- vdm
5151
- vdo
5252
- vmo
53+
width:
54+
type: integer
55+
required: false
5356
target:
5457
type: enum
5558
choices:

spec/system/discourse_icon_header_links_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@
2525
end
2626
end
2727

28+
context "when using the width attribute" do
29+
before do
30+
component.update_setting(
31+
:header_links,
32+
[
33+
{
34+
icon: "fab-facebook",
35+
url: "https://facebook.com",
36+
title: "Desktop and mobile link",
37+
width: 200,
38+
view: "vdm",
39+
},
40+
],
41+
)
42+
component.save!
43+
end
44+
45+
it "renders the icon with the correct width" do
46+
visit("/")
47+
48+
expect(page.find(".custom-header-icon-link .icon").style("width")).to eq(
49+
{ "width" => "200px" },
50+
)
51+
end
52+
end
53+
2854
context "when in mobile", mobile: true do
2955
it "renders the correct icon" do
3056
visit("/")

0 commit comments

Comments
 (0)