Skip to content

Style of the Custom Library not affecting in Webpart where using custom lbrary #9791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 9 tasks
devanihiren opened this issue Jul 3, 2024 · 4 comments
Open
1 of 9 tasks
Assignees
Labels
Needs: Author Feedback Awaiting response from the original poster of the issue. Marked as stale if no activity for 7 days. Needs: Triage 🔍 Awaiting categorization and initial review. sharepoint-developer-support sharepoint-developer-support type:question Question... if answered, will be tagged as such.

Comments

@devanihiren
Copy link

Target SharePoint environment

SharePoint Online

What SharePoint development model, framework, SDK or API is this about?

💥 SharePoint Framework

Developer environment

None

What browser(s) / client(s) have you tested

  • 💥 Internet Explorer
  • 💥 Microsoft Edge
  • 💥 Google Chrome
  • 💥 FireFox
  • 💥 Safari
  • mobile (iOS/iPadOS)
  • mobile (Android)
  • not applicable
  • other (enter in the "Additional environment details" area below)

Additional environment details

  • browser version
  • SPFx version
  • Node.js version
  • etc

Describe the bug / error

Want to use custom react library to the webpart
below is component of the react library

import * as React from 'react';
import './style.css';

interface Props {
  text: string
}

export const ExampleComponent = ({ text }: Props) => {
  return <div className="heading " >Example Component: {text}</div>
}

below is style.css file
.heading {
color: red !important;
}

above class not change color of the text
text color changes when i apply CSS file from the SPFx webpart or inline CSS only works.

How i can use library CSS class for the styling

Steps to reproduce

npx create-react-library
Cd
Npm pack
Install generated .tgz in SPFx Project
use like other library

Expected behavior

Want to affect style clas from the library stylle.css file

@devanihiren devanihiren added the type:bug-suspected Suspected bug (not working as designed/expected). See “type:bug-confirmed” for confirmed bugs. label Jul 3, 2024
@StfBauer
Copy link
Collaborator

StfBauer commented Jul 3, 2024

create-react-library are almost 4 years old and haven't been updated since then. I wouldn't recommend to use it.

However you cannot use it in this ways in SPFx. SPFx uses CSS Modules

So when you use your component it will become something like:

.header_abcdef{
   color: red !important;
}

Which is not applied on your div element, which still has the className of just heading and not heading_abcdeg.

You could make it work if you import the style file from the library in the SPFx library something like this.

@use 'sass:meta';

:global{
   @include meta.load-css('path-to-library-css/lib.css')
}

@StfBauer StfBauer added type:question Question... if answered, will be tagged as such. and removed type:bug-suspected Suspected bug (not working as designed/expected). See “type:bug-confirmed” for confirmed bugs. labels Jul 3, 2024
@devanihiren
Copy link
Author

Tried CSS Modules
Not working

@VesaJuvonen VesaJuvonen added the Needs: Triage 🔍 Awaiting categorization and initial review. label Jul 29, 2024
@Ashlesha-MSFT Ashlesha-MSFT self-assigned this Jun 10, 2025
@Ashlesha-MSFT Ashlesha-MSFT added the sharepoint-developer-support sharepoint-developer-support label Jun 10, 2025
@Ashlesha-MSFT
Copy link

Hello @devanihiren,
Thank you for bringing this issue to our attention. We will look into it and get back to you shortly.

@Ashlesha-MSFT
Copy link

@devanihiren,
Could you please confirm if you’ve had a chance to review the guidance provided in the Microsoft documentation on referencing third-party CSS styles in SPFx https://learn.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/guidance/reference-third-party-css-styles ?
This might help resolve the issue with the styles from your custom React library not being applied as expected.

@Ashlesha-MSFT Ashlesha-MSFT added the Needs: Author Feedback Awaiting response from the original poster of the issue. Marked as stale if no activity for 7 days. label Jun 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Author Feedback Awaiting response from the original poster of the issue. Marked as stale if no activity for 7 days. Needs: Triage 🔍 Awaiting categorization and initial review. sharepoint-developer-support sharepoint-developer-support type:question Question... if answered, will be tagged as such.
Projects
None yet
Development

No branches or pull requests

4 participants