Instead of using an img tag for the static Drupal logo in the navigation, use an inline SVG. This saves a network request, increasing page speed, and provides greater styling flexibility.

Instead of:

<img alt="Navigation logo" src="/modules/custom/navigation/assets/images/logo.svg" loading="eager" width="40" height="40">

Include the logo_path in templates/navigation.html.twig directly.

The inline SVG markup should have the same accessibility as the img tag, so the img role should be added to the SVG markup, and an aria-label of 'Navigation logo'|t should be added.

logo.svg.twig has:

  • label
  • bg_color|default('#347efe')
  • fg_color|default('#fff')

reminder: 'label' maybe removed #3441586: Navigation logo link does not communicate where it is taking the user

Issue fork navigation-3441090

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bronzehedwick created an issue. See original summary.

skaught’s picture

skaught’s picture

Converts logo.svg from file-system into 'data:image/svg+xml' in navigation_theme. also means we are not using extension.list.module service in template hook.

re: current summary request.
moving to data does not change img alt (role) or any current css as is still just an image.

other standing work with custom logo will apply still this way!

skaught’s picture

Status: Active » Needs review
bronzehedwick’s picture

Status: Needs review » Reviewed & tested by the community

I think this is a reasonable approach, removing the extra network request. We don't get the added styling benefits, but those don't have a plan to be taken advantage of right now, and this will remove refactoring. Marking RBTC!

ckrina’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for working on this! But I'm not sure about this: moving markup to a .module file makes things strange: as a themer, I'd expect this in a template itself with the rest of the elements if it's not an image. It works? Yes. Is this the expected place for this? Nope :)
I'd go just add the SVG on the navigation.html.twig template and add any required logic there if the logo is replaced by a custom image or hidden.

bronzehedwick’s picture

Assigned: Unassigned » bronzehedwick

bronzehedwick’s picture

Assigned: bronzehedwick » Unassigned
Status: Needs work » Needs review

I created a new MR with a new approach. Is this more inline with what you're expecting @ckrina?

skaught’s picture

-this is completely removing the IMG tag. this will break #3436526: Adjust custom navigation logo dimensions on upload.

------
@bronzehedwick. somewhere between. (:

 */
function navigation_theme($existing, $type, $theme, $path) {
[remove first lines]
 ....

  $items['navigation'] = [
    'variables' => [
      'hide_logo' => FALSE,
      'logo_path' => FALSE,    <--
      'logo_width' => 40,
      'logo_height' => 40,
      'menu_content' => [],
      'menu_footer' => [],
    ],
  ];

-. make path empty.
- if twig sees empty load 'new icon twig' into PATH, otherwise PATH would be a custom logo (passed by NaviagaionRenderer if user has chosen to adds an icon).
- then url encode the 'the svg load' in twig to be ready as image data. this would keep the template readable.

skaught’s picture

Status: Needs review » Needs work
bronzehedwick’s picture

Ah that makes sense, thanks @SKAUGHT. I pushed a commit that adds back that condition.

bronzehedwick’s picture

Status: Needs work » Needs review
skaught’s picture

Status: Needs review » Reviewed & tested by the community

thanks!
looks good. I'll move to RTBC!

-----

if we want to come back about sending to data (always only image tag) , this would be a sample for the logo.svg.twig contents

{% set svg_content %}
{% apply spaceless %}
<svg xmlns="http://www.w3.org/2000/svg" width="{{ width|default(40) }}" height="{{ height|default(40) }}" viewBox="0 0 32 32">
  <rect fill="{{ bg_color|default('#347efe') }}" width="32" height="32" rx="8"/>
  <path fill="{{ fg_color|default('#fff') }}" d="M19,10.3C17.67,9,16.38,7.68,16,6.23,15.62,7.67,14.33,9,13,10.3c-2,2-4.31,4.31-4.31,7.74a7.32,7.32,0,0,0,14.64,0C23.32,14.61,21,12.32,19,10.3Zm-7.22,9.44c-.45,0-2.11-2.87,1-5.91l2,2.22a.18.18,0,0,1,0,.25h0A19.3,19.3,0,0,0,12,19.6C11.92,19.75,11.83,19.74,11.79,19.74ZM16,23.51A2.52,2.52,0,0,1,13.48,21a2.56,2.56,0,0,1,.63-1.66c.45-.56,1.89-2.12,1.89-2.12s1.41,1.59,1.89,2.11A2.5,2.5,0,0,1,18.52,21,2.52,2.52,0,0,1,16,23.51Zm4.82-4.09c-.06.12-.18.32-.35.33s-.32-.14-.55-.47c-.48-.71-4.67-5.09-5.46-5.94s-.09-1.27.18-1.55L16,10.44a35.72,35.72,0,0,1,4.25,4.8A4.5,4.5,0,0,1,20.82,19.42Z"/>
</svg>
{% endapply %}
{% endset %}
data:image/svg+xml,{{ svg_content|url_encode }}
skaught’s picture

Status: Reviewed & tested by the community » Needs work

@bronzehedwick
- moving width/height to var seems not needed [as this doesn't recenter, re-size the actual paths]
- also noticed viewbox is "0 0 32 32" -- i think should be 40?\

I'm assuming the color var would be useful for light/dark modes down the road, indeed could be useful.

SKAUGHT changed the visibility of the branch 3441090-use-inline-svg to hidden.

bronzehedwick’s picture

moving width/height to var seems not needed [as this doesn't recenter, re-size the actual paths]

@SKAUGHT This will re-size/re-center the entire graphic, as intended.

All width/height sizing and any drawing code inside an SVG is relative to the viewBox. So the width and height on <rect fill="#347efe" width="32" height="32" rx="8"/> being set to 32 is the same as saying 100% of the width and height, since the viewBox is 32/32.

Increasing the width/height on the <svg> element scales everything inside it based on the ratio of the viewBox, aka the S in SVG ;)

also noticed viewbox is "0 0 32 32" -- i think should be 40

As per above, the SVG is displayed at 40/40, but it's scaling baseline is 32/32 (aka, the viewBox). Changing the viewBox from 32/32 will make the internal scaling math off, in this case, adding extra white space around the SVG, optically shrinking it.


You can test all this by changing the width and height values on the <svg> element.

skaught’s picture

Status: Needs work » Needs review

width="{{ width|default(40) }}" height="{{ height|default(40) }}"
we just don't need to be allowing the size to be changed. some one could send in a smaller size than the viewbox.

re:viewbox. -- i've just got thrown off in my own head (viewport of svg)

skaught’s picture

Status: Needs review » Needs work
bronzehedwick’s picture

Got it @SKAUGHT, I pushed a commit that hard codes 40 as the width and height.

bronzehedwick’s picture

Status: Needs work » Needs review
skaught’s picture

Status: Needs review » Reviewed & tested by the community

nice.

skaught’s picture

ckrina’s picture

Status: Reviewed & tested by the community » Needs work

This has a conflict with previous changes merged. Needs to be updated

m4olivei’s picture

Status: Needs work » Reviewed & tested by the community

Thanks for the merge conflict resolution @bronzehedwick. Looks good to me.

m4olivei’s picture

m4olivei’s picture

Status: Reviewed & tested by the community » Fixed

Merged to 1.x 🎉. Thanks!

skaught’s picture

Issue summary: View changes
skaught’s picture

Issue summary: View changes

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.