You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 2.7 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # JSS plugin enables support for nested rules
  2. [![Gitter](https://badges.gitter.im/JoinChat.svg)](https://gitter.im/cssinjs/lobby)
  3. Make sure you read [how to use
  4. plugins](https://github.com/cssinjs/jss/blob/master/docs/setup.md#setup-with-plugins)
  5. in general.
  6. ## Use `&` to reference selector of the parent rule.
  7. ```javascript
  8. const styles = {
  9. container: {
  10. padding: 20,
  11. '&:hover': {
  12. background: 'blue'
  13. },
  14. // Add a global .clear class to the container.
  15. '&.clear': {
  16. clear: 'both'
  17. },
  18. // Reference a global .button scoped to the container.
  19. '& .button': {
  20. background: 'red'
  21. },
  22. // Use multiple container refs in one selector
  23. '&.selected, &.active': {
  24. border: '1px solid red'
  25. }
  26. }
  27. }
  28. ```
  29. Compiles to:
  30. ```css
  31. .container-3775999496 {
  32. padding: 20px;
  33. }
  34. .container-3775999496:hover {
  35. background: blue;
  36. }
  37. .container-3775999496.clear {
  38. clear: both;
  39. }
  40. .container-3775999496 .button {
  41. background: red;
  42. }
  43. .container-3775999496.selected, .container-3775999496.active {
  44. border: 1px solid red;
  45. }
  46. ```
  47. ## Use `$ruleName` to reference a local rule within the same style sheet.
  48. ```javascript
  49. const styles = {
  50. container: {
  51. // Reference the local rule "button".
  52. '& $button': {
  53. padding: '10px'
  54. },
  55. // Multiple local refs in one rule.
  56. '&:hover $button, &:active $button': {
  57. color: 'red',
  58. },
  59. '&:focus $button': {
  60. color: 'blue'
  61. }
  62. },
  63. button: {
  64. color: 'grey'
  65. }
  66. }
  67. ```
  68. Compiles to:
  69. ```css
  70. .button-3940538223 {
  71. color: grey;
  72. }
  73. .container-2645419599 .button-3940538223 {
  74. padding: 10px;
  75. }
  76. .container-2645419599:hover .button-3940538223, .container-2645419599:active .button-3940538223 {
  77. color: red;
  78. }
  79. .container-2645419599:focus .button-3940538223 {
  80. color: blue;
  81. }
  82. ```
  83. ## Use at-rules inside of regular rules.
  84. ```javascript
  85. const styles = {
  86. button: {
  87. color: 'red',
  88. '@media (min-width: 1024px)': {
  89. width: 200
  90. }
  91. }
  92. }
  93. ```
  94. Compiles to:
  95. ```css
  96. .button-2683044438 {
  97. color: red;
  98. }
  99. @media (min-width: 1024px) {
  100. .button-2683044438 {
  101. width: 200px;
  102. }
  103. }
  104. ```
  105. ## Deep nesting
  106. ```javascript
  107. const styles = {
  108. button: {
  109. '&$warn': {
  110. color: 'red',
  111. '&:hover, &:focus': {
  112. color: 'white',
  113. background: 'red'
  114. }
  115. }
  116. },
  117. warn: {}
  118. }
  119. ```
  120. Compiles to:
  121. ```css
  122. .button-274964227.warn-2315792072 {
  123. color: red;
  124. }
  125. .button-274964227.warn-2315792072:hover, .button-274964227.warn-2315792072:focus {
  126. color: white;
  127. background: red;
  128. }
  129. ```
  130. ## Demo
  131. [Simple](http://cssinjs.github.io/examples/plugins/jss-nested/simple/index.html)
  132. ## Issues
  133. File a bug against [cssinjs/jss prefixed with \[jss-nested\]](https://github.com/cssinjs/jss/issues/new?title=[jss-nested]%20).
  134. ## Run tests
  135. ```bash
  136. npm i
  137. npm run test
  138. ```
  139. ## License
  140. MIT