Proper Replacement of external CSS
Category
- [x] Question
- [ ] Typo
- [ ] Bug
- [ ] Additional article idea
Expected or Desired Behavior
If you are reporting a bug, please describe the expected behavior. If you are suggesting an enhancement please describe thoroughly the enhancement, how it can be achieved, and expected benefit.
Observed Behavior
I'm using Asciidoctor.js to render text content in a SPFx webpart since we use this a lot in our documentation.
In order to narrow the impact of the asciidoctor.css I've included it in the webpart.module.scss and thus it will rewrite the css attributes to be unique.
The problem aries where Asciidoctor.js renders the orginal class names and I need to substitute them as follows.
private regexp = new RegExp('class=\"(' + Object.keys(styles).join("|") + ')\"', "g");
private asciidoctor = Asciidoctor();
private renderDocument(text: string): void {
var c = this.asciidoctor.convert(text);
var str = c.replace(this.regexp, (full, token) => {
return 'class="' + styles[token] + '"';
});
return str;
}
Is there a preferred of doing such task or is this a good way of doing this to be a good SPFx citizen?
Cheers, Mario
Hi @mariotoffia, the approach you are following could have some perf issues. How big are these documents? Do you notice perf issues with this approach?
Wouldn't it be sufficient to keep the original class names but to wrap them in the web part class name, like:
In the .scss file:
.myWebPartClass {
:global {
.originalClass1 {
}
.originalClass2 {
}
}
}
In your web part you would then use:
<div class="${styles.myWebPartClass}">
<div class="originalClass1"></div>
</div>
Using this approach you wouldn't need to postprocess the document. The only unique class you'd have would be the root web part CSS class which you can easily reference from the styles variable.
Hi @waldekmastykarz, that would work. But that would not make the class names unique. i.e. there will still be chances of class name collisions with other web parts. If that is not a concern, then this should work fine.
While the classes wouldn't be unique, they wouldn't collide with other web parts because they are nested in the .myWebPartClass which would get appended a unique hash when transpiled from .scss to .css. So the only risk is if there were other elements on the page targeting nothing but the global class name. In such case you'd need to override the specific properties.
@waldekmastykarz I like your approach since as someone stated above - my solution is not really a solution since the performance hit. However as a noop in html/css world... can I use the @import in the scss file to accomplish this since asciidoctor do have lots of css specified?
like:
.asciidocviewerwebpart {
:global {
@import "asciidoctor.module";
}
}
today I'm using this import as follows:
.asciidocviewerwebpart {
@import "asciidoctor.module";
}
Cheers, Mario
According to the documentation you should be able to do it.
We closed this issue as it had not activity within last 180 days. This is a generic process we have decided to perform for issues, which have not been explicitly marked still to be "work in progress" based on tags. We are performing this cleaning to make sure that old issues that have already been solved (but not closed) or are no longer relevant are cleaned out and make the issues more manageable. If this issue still valid, we would ask you to open a new issue and follow the guidance in the issue template related on the recommended location. We do apologize any inconveniences this might cause. Please do remember that issues in the issue lists are also messages for others in the community, so you can also check if you can assist on any of them. “Sharing is caring!”
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues