Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style input tag without class with JSS

Tags:

css

reactjs

jss

I am using the react-select component on my app. I am also only styling my app with JSS. My issue is that since react-select is an npm package, I don't have the capability to modify class names in the component. So there is an input in there that I need to target with my styles.

<div class="Select-input"><input type="text" name="style-me" /></div>

And my JSS is a little something like this:

jss.setup(preset());

const stylus = {
   'Select-input': {
       background: 'red'
   }
}

const { classes } = jss.createStyleSheet(stylus).attach();

What do I need to do in JSS to style that child input tag?

like image 381
Brady Edgar Avatar asked Dec 14 '16 03:12

Brady Edgar


People also ask

Is JSS better than CSS?

JSS is a better abstraction over CSS. It uses JavaScript as a language to describe styles in a declarative and maintainable way. It is a high performance JS to CSS compiler which works at runtime and server-side. You can use it with React or with any other library.

Is JSS deprecated?

JSS and the default preset are already built in. Try it out in the playground. The HOC based API is deprecated as of v10 and may be removed in a future version.

What is react JSS?

JSS is an authoring tool for CSS which allows you to use JavaScript to describe styles in a declarative, conflict-free and reusable way. It can compile in the browser, server-side or at build time in Node. JSS is framework agnostic. It consists of multiple packages: the core, plugins, framework integrations and others.

What is CSS in JS pattern?

As CSS-in-JS is essentially JavaScript code, you can apply complex logic to your style rules, such as loops, conditionals, variables, state-based styling, and more. Therefore, it's an ideal solution if you need to create a complicated UI that requires dynamic functionality.


3 Answers

According to this answer, you can pass in a class name for react-select. The rest of my answer shows how to target child elements.


I checked the github page for JSS here:
https://github.com/cssinjs/jss

They have a live example for nested CSS rules here:
https://github.com/cssinjs/examples/blob/gh-pages/plugins/jss-nested/simple/app.js

In the code to target a nested <button> element, it uses a property named & button. Notice the space between the ampersand and button. So for your specific code, you can target the <input> like this:

jss.setup(preset());

const stylus = {
   'Select-input': {
       background: 'red',
       '& input': {
            /* your input styles here */
       }
   }
}

const { classes } = jss.createStyleSheet(stylus).attach();
like image 102
Cave Johnson Avatar answered Oct 14 '22 16:10

Cave Johnson


You have always at least 2 ways:

  1. Pass the generated class name

Use JSS as you should by default, avoid unscoped class names. Use generated class name and pass it to the component you want to use

const {classes} = jss.createStyleSheet({
  input: {background: 'red'}
}).attach()


<Input className={classes.input} />
  1. Use scoped global selector

If its impossible to pass a class name, you can still have a locally scoped global selector

const {classes} = jss.createStyleSheet({
  container: {
    '@global': {
      input: {background: 'red'}
    }
  }
}).attach()

<div className={classes.container}>
  <Input />
</div>
like image 37
Oleg Isonen Avatar answered Oct 14 '22 17:10

Oleg Isonen


Assuming you're referring to this package:

https://github.com/JedWatson/react-select

You can in fact pass in className as a prop

like image 41
ioseph Avatar answered Oct 14 '22 18:10

ioseph