Modify my SPFx web part to read css file instead of writing the css code inside the web part

john john 1,021 Reputation points
2021-11-14T23:51:17.507+00:00

I have built a web part which is similar to this web part from github @ https://github.com/pnp/sp-dev-fx-webparts/tree/main/samples/react-enhanced-list-formatting . The web part allow to embed custom css code inside modern pages. But is it possible to modify the web part, to referecne a css file instead of having to write the actual css code inside the web part? So instead of writing the css code as follow:-

149109-css.png

to reference the css file as follow:-

149203-listsref.png

I attached the code for the SPFx web part, as i am unable to add it inside my question.

any idea, how i can achieve this?

Thanks

149069-enhancedlistformattingwebpart.txt149070-enhancedlistformatting.txt149069-enhancedlistformattingwebpart.txt149070-enhancedlistformatting.txt

Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2021-11-15T08:03:40.33+00:00

    Hi @john john ,
    I would recommend you react-add-js-css-ref. This web part provides an interface to JS and CSS file references so that we don't have to modify code when we need to change references or add new references in the future. As part of security measures, this actions on web part can be only accessed by users who have Manage web permission on site.
    Here is the document:
    https://github.com/pnp/sp-dev-fx-webparts/tree/main/samples/react-add-js-css-ref


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.