Skip to content

Support SVG icon in action menu#12403

Merged
nathanwoulfe merged 6 commits intoumbraco:v10/contribfrom
bjarnef:v10/feature/umb-content-menu-svg-icon
May 16, 2022
Merged

Support SVG icon in action menu#12403
nathanwoulfe merged 6 commits intoumbraco:v10/contribfrom
bjarnef:v10/feature/umb-content-menu-svg-icon

Conversation

@bjarnef
Copy link
Copy Markdown
Contributor

@bjarnef bjarnef commented May 16, 2022

Prerequisites

  • I have added steps to test this contribution in the description below

Description

This is a new PR of closed #10757 to v9 and #12063 re-based to v10, but caused a few merge conflicts, so starting on a fresh branch.

At the moment it is assumed the icon (font icon or SVG icon) always is prefixed with icon- which is not always the case and not a requirement in other part of the UI).

It also fixes an issue with separator at menu item due a typo in view.

public class ApplicationComposer : IComposer
{
	public void Compose(IUmbracoBuilder builder)
	{
		builder.AddNotificationHandler<MenuRenderingNotification, TreeNotificationHandler>();
	}
}


public class TreeNotificationHandler : INotificationHandler<MenuRenderingNotification>
{
	private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;

	public TreeNotificationHandler(IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
	{
		_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
	}

	public void Handle(MenuRenderingNotification notification)
	{
		// for all content tree nodes
		if (notification.TreeAlias == Umbraco.Cms.Core.Constants.Applications.Content &&
			_backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Groups.Any(x =>
				x.Alias == Umbraco.Cms.Core.Constants.Security.AdminGroupAlias) == true)
		{
			var waterMenuItem = new MenuItem("water", "Just Water")
			{
				Icon = "drop",
				SeparatorBefore = false,
			};

			var wineMenuItem = new MenuItem("wine", "Wine Time")
			{
				Icon = "wine-glass",
				SeparatorBefore = false,
				UseLegacyIcon = true
			};

			var coffeeMenuItem = new MenuItem("coffee", "Coffee Time")
			{
				Icon = "coffee-cup",
				SeparatorBefore = true,
				UseLegacyIcon = false,
			};

			var beerMenuItem = new MenuItem("beer", "Beer Time")
			{
				Icon = "icon-beer-glass",
				UseLegacyIcon = false,
			};

			notification.Menu.Items.Insert(5, waterMenuItem);
			notification.Menu.Items.Insert(6, wineMenuItem);
			notification.Menu.Items.Insert(7, beerMenuItem);
			notification.Menu.Items.Insert(8, coffeeMenuItem);
		}
	}
}

image

image

@github-actions
Copy link
Copy Markdown

github-actions bot commented May 16, 2022

Hi there @bjarnef, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@bjarnef bjarnef changed the title Support custom SVG icon in menu item with legacy support Support SVG icon in action menu May 16, 2022
@bjarnef bjarnef marked this pull request as draft May 16, 2022 08:17
@bjarnef bjarnef mentioned this pull request May 16, 2022
1 task
@bjarnef bjarnef marked this pull request as ready for review May 16, 2022 11:07
@bjarnef
Copy link
Copy Markdown
Contributor Author

bjarnef commented May 16, 2022

@nathanwoulfe could you have a look at this PR instead?

@bjarnef
Copy link
Copy Markdown
Contributor Author

bjarnef commented May 16, 2022

@bergmania it would be great if we could include this in v10, white it by default still should use the old/legacy icon format (which assumed icons used a icon- prefix).

This PR #12062 may also be relevant, which does something similar.

@bjarnef
Copy link
Copy Markdown
Contributor Author

bjarnef commented May 16, 2022

@nathanwoulfe hopefully this PR will do 😁 (after submitting it for v8, v9 and now v10 😅 ).

I had a compile error though as mentioned here, but not related to this PR:
#12349 (comment)

@nathanwoulfe nathanwoulfe merged commit d051f85 into umbraco:v10/contrib May 16, 2022
@bjarnef bjarnef deleted the v10/feature/umb-content-menu-svg-icon branch May 16, 2022 22:45
@bjarnef bjarnef mentioned this pull request May 17, 2022
1 task
/// <param name="opensDialog">Whether or not this action opens a dialog</param>
public MenuItem? Add<T>(ILocalizedTextService textService, bool hasSeparator = false, bool opensDialog = false)
/// <param name="useLegacyIcon">Whether or not this action should use legacy icon prefixed with "icon-" or full icon name is specified.</param>
public MenuItem? Add<T>(ILocalizedTextService textService, bool hasSeparator = false, bool opensDialog = false, bool useLegacyIcon = true)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll highlight this one again @nathanwoulfe and @bjarnef - this is a common mistake: you can not add an optional parameter to a public method, this is always a breaking change as the signature of the method is changed.

Luckily the fix for this is easy and I fixed it here: 885ec99

So I took the whole method body and added it to a new method with the optional parameter on it. Then I call the new method from the old method (signature is unchanged), with the default value of useLegacyIcon: false.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nul800sebastiaan okay, I know it would be a breaking change if the order of the parameters changed or if the new options parameter was added before the existing ones, but didn't thought I would be a breaking change with the new parameter as optional and as last parameter.

Must have missed this method when I checked the custom code with various methods before and after this change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a common mistake! All good, fixed it up 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants