SharePoint / sp-dev-docs

SharePoint & Viva Connections Developer Documentation
https://docs.microsoft.com/en-us/sharepoint/dev/
Creative Commons Attribution 4.0 International
1.23k stars 992 forks source link

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

Open devanihiren opened 3 days ago

devanihiren commented 3 days ago

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

Additional environment details

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

StfBauer commented 3 days ago

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')
}
devanihiren commented 3 days ago

Tried CSS Modules Not working