-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: The phrases nNetwork, nTicket, nTrain and nBus now show the 'n'…
… as an SVG icon (#782) * Added a new page for the n-icon * Added scss and js for n-icon * Updated mixin to now support correct scaling of n-icon * Reworked js for n-icon logic. * nIcons is now working and commented * Removed n-icons test page * fix typo in variable name Co-authored-by: Catia Costa <34938764+catiarodriguescosta@users.noreply.github.com> Co-authored-by: Gilad Grant <ggrant966@gmail.com>
- Loading branch information
1 parent
5ce3a3b
commit 97f4361
Showing
5 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const nIcons = () => { | ||
// Function to replace text with our icon | ||
const replaceTextWithIcon = (textNode, phraseToReplace) => { | ||
if (!textNode.includes(phraseToReplace)) return textNode; // If textNode doesn't include our matched text, then return | ||
|
||
// Otherwise... | ||
const textWithoutN = phraseToReplace.substring(1); // Chop the first character from our string (the 'n') as it will be replaced with an icon | ||
// HTML/SVG of icon we will use | ||
const svgIcon = ` | ||
<svg class="wmnds-n-icon__svg"> | ||
<title>N-Network icon</title> | ||
<desc>A hexagon with the letter 'n' inside of it.</desc> | ||
<use xlink:href="#wmnds-general-n-ticket" href="#wmnds-general-n-ticket"></use> | ||
</svg>`; | ||
// Then replace our found text with 'n' icon and text without 'n' | ||
const modifiedTextNode = textNode.replace( | ||
new RegExp(phraseToReplace, 'ig'), | ||
`<span class="wmnds-n-icon">${svgIcon}${textWithoutN}</span>` | ||
); | ||
|
||
return modifiedTextNode; // Return modified textNode back | ||
}; | ||
|
||
function replacePhraseInHTML(htmlEle, phrase) { | ||
const phraseToReplace = phrase; | ||
const insideHTMLTags = new RegExp('(<.+?>|&\\w+;)'); // Get items within HTML </> tags | ||
const textNodes = htmlEle.split(insideHTMLTags).filter(Boolean); // Split on the HTML tags to avoid manipulating attributes etc. | ||
|
||
// Loop through textNodes and any that match 'insideHTMLTags', return straight away | ||
// Otherwise, that textNode is ready to be searched for our textToChange | ||
const updatedDOM = textNodes | ||
.map(DOMText => | ||
insideHTMLTags.test(DOMText) ? DOMText : replaceTextWithIcon(DOMText, phraseToReplace) | ||
) | ||
.join(''); | ||
|
||
return updatedDOM; | ||
} | ||
|
||
// START HERE | ||
const bodyElement = document.querySelector('body'); | ||
const phrasesToReplace = ['nNetwork', 'nTicket', 'nTrain', 'nBus']; // These are the phrases we want to replace the first char with the 'n' icon SVG (they are not case sensitive - we match on upper and lowercase with regex 'i' on line 17) | ||
|
||
// Loop through each phrase and update the DOM body innerHTML with the replaced phrase | ||
phrasesToReplace.forEach(phraseToReplace => { | ||
bodyElement.innerHTML = replacePhraseInHTML(bodyElement.innerHTML, phraseToReplace); | ||
}); | ||
}; | ||
|
||
export default nIcons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
$base-height: 1rem; | ||
$width-multiplier: 0.9995467185761957; // If we times the height by this value we got the correct width for the n-icon svg | ||
|
||
.wmnds-n-icon { | ||
font-size: inherit; | ||
line-height: inherit; | ||
|
||
&__svg { | ||
display: inline-block; | ||
width: calc($base-height * #{$width-multiplier}); | ||
height: $base-height; | ||
fill: currentColor; | ||
vertical-align: revert; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters