For those seeking a way to customize dynamic strings created with template literals, here's an interesting approach. You can utilize a tag function that applies CSS class styles exclusively to the variables within the template string, allowing for unique styling of dynamic content only.
export const highlight = (strings: TemplateStringsArray, ...values: (string | number)[]) => {
let str = '';
strings.forEach((string, i) => {
str += `${string} <span class='hl'>${values[i] || ''}</span>`;
});
return str;
};
In React, you can implement it like this:
<p
dangerouslySetInnerHTML={{
__html: highlight`User ${userAddress} claimed ${tokenAmount} ${token.symbol}.`
}}
/>
I found guidance in this article by Wes Bos while tackling this issue.