Add optional parameter to prefix all IDs within SVG to avoid clashes #22
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
Provide a way to prevent clashes between IDs used in SVGs.
The problem it solves
When an SVG is used more than once on a page, any IDs used within in the SVG will not be unique. This can cause problems when an SVG is relying on an ID (eg. for a
clip-path
orlinear-gradient
), as it can be referencing the wrong one. (Particularly a problem when you're animating SVGs!)ID clashes often occur across multiple SVGs too, particularly when the IDs used are vague (eg.
id="gradient1"
) or have been exported from another tool (eg.id="Layer1"
).The solution
I've added another parameter to the
@svg
directive called$id_prefix
. It allows the developer to provide a string that will be prefixed to all the IDs used within the SVG.Usage and example
You may be using the same logo in the header and footer of the site. If the logo relies on an ID (eg. for a linear-gradient), there will be an ID clash. To get around this, you'd just pass different prefixes to the directive.
A detailed example
Without providing an ID prefix, my SVG markup looks like this. (This SVG was exported from Illustrator, and run manually through SVGOMG which simplified the ID to just
a
– optimised, but not super helpful...Passing an $id_prefix parameter to the
@svg
directive turns my SVG's unhelpful ID ofa
into the more specificheader-logo-a
, both where it's declared on thelinearGradient
indefs
and where it's referenced as thefill
on thepath
.tldr;
With the addition of an
$id_prefix
parameter to the@svg
directive, the developer is back in control of how unique an SVG's IDs are, even if that SVG is stored in the CMS.