fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

Modifying CSS style of imported Fabric components

Open rbtkoz opened this issue 10 years ago • 7 comments

Hi Guys, I am importing a component import { TextField } from 'office-ui-fabric-react/lib/TextField';

in the return( <TextField className={ searchInputCSS['search-textfield'] } placeholder="I want to know how to..." ariaLabel="Enter search text" value={ this.state.searchText } onChanged={ this.autosuggestHandler } onKeyPress ={ this._handleKeyPress } /> )

Even though I have assigned a different font size to my search-textfield class, it is being overriden by the MS styles. What is the best way to override this?

rbtkoz avatar Oct 24 '16 20:10 rbtkoz

can you define your rule as:

.ms-TextField.search-textfield { ... }

/* if you're using css modules */
:global(.ms-TextField).search-textfield { ... }

dzearing avatar Oct 24 '16 21:10 dzearing

BTW I don't like this answer but css specificity is fighting us here. Open to suggestions.

I really, really don't want people to fallback to !important...

Inline styles would solve it, but I don't really want to go in that direction.

dzearing avatar Oct 24 '16 21:10 dzearing

@dzearing I hear you. Had tried increasing specificity but actually the component hides the <input> field which needs to be targeted to change the font size. In this case had to go target <input> specifically by referencing the parent class and increasing specificity that way. This works(even if a bit ugly) .search-textfield input{ font-size:1.2em; }

rbtkoz avatar Oct 24 '16 21:10 rbtkoz

We can always add more classNames to props to pass through. Or things like "inputProps" so you can pass through additional things targeted for specific places.

dzearing avatar Oct 24 '16 22:10 dzearing

something like inputProps sounds pretty good. If standardized it would mean easy overrides.

rbtkoz avatar Oct 25 '16 14:10 rbtkoz

Another possible option would be to load your customized css file after any component using a fabric component. This should theoretically put your css override after the fabric component css which means as the browser computes it the override would now win.

My guess is that the overrides file is currently before the component.

Of course all this depends on how your overrides file is being consumed. Are you bringing it in via require or writing it directly in the of the html?

rickyp-ms avatar Oct 26 '16 18:10 rickyp-ms

I've added inputClassName on TextField to push a specific class to the input element. That will help with the specific scenario.

dzearing avatar Nov 24 '16 01:11 dzearing