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
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
Comment #2
skaughtComment #4
skaughtConverts logo.svg from file-system into 'data:image/svg+xml' in navigation_theme. also means we are not using
extension.list.moduleservice 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!
Comment #5
skaughtComment #6
bronzehedwickI 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!
Comment #7
ckrinaThanks 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.twigtemplate and add any required logic there if the logo is replaced by a custom image or hidden.Comment #8
bronzehedwickComment #10
bronzehedwickI created a new MR with a new approach. Is this more inline with what you're expecting @ckrina?
Comment #11
skaught-this is completely removing the IMG tag. this will break #3436526: Adjust custom navigation logo dimensions on upload.
------
@bronzehedwick. somewhere between. (:
-. 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.
Comment #12
skaughtComment #13
bronzehedwickAh that makes sense, thanks @SKAUGHT. I pushed a commit that adds back that condition.
Comment #14
bronzehedwickComment #15
skaughtthanks!
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
Comment #16
skaught@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.
Comment #18
bronzehedwick@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 to32is the same as saying 100% of the width and height, since theviewBoxis 32/32.Increasing the width/height on the
<svg>element scales everything inside it based on the ratio of theviewBox, aka the S in SVG ;)As per above, the SVG is displayed at 40/40, but it's scaling baseline is 32/32 (aka, the viewBox). Changing the
viewBoxfrom 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
widthandheightvalues on the<svg>element.Comment #19
skaughtwidth="{{ 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)
Comment #20
skaughtComment #21
bronzehedwickGot it @SKAUGHT, I pushed a commit that hard codes
40as thewidthandheight.Comment #22
bronzehedwickComment #23
skaughtnice.
Comment #24
skaughtComment #25
ckrinaThis has a conflict with previous changes merged. Needs to be updated
Comment #26
m4oliveiThanks for the merge conflict resolution @bronzehedwick. Looks good to me.
Comment #28
m4oliveiComment #29
m4oliveiMerged to 1.x 🎉. Thanks!
Comment #30
skaughtComment #31
skaught