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.3 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # @emotion/sheet
  2. > A StyleSheet for css-in-js libraries
  3. ```bash
  4. yarn add @emotion/sheet
  5. ```
  6. ```jsx
  7. import { StyleSheet } from '@emotion/sheet'
  8. const sheet = new StyleSheet({ key: '', container: document.head })
  9. sheet.insert('html { color: hotpink; }')
  10. ```
  11. > **Note:**
  12. > This is not useful for server-side rendering, you should implement SSR seperately
  13. ## StyleSheet
  14. ### Options
  15. ```ts
  16. type Options = {
  17. nonce?: string
  18. key: string
  19. container: HTMLElement
  20. speedy?: boolean
  21. maxLength?: number
  22. }
  23. ```
  24. #### nonce
  25. A nonce that will be set on each style tag that the sheet inserts for [Content Security Policies](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP).
  26. #### container
  27. A DOM Node that the sheet will insert all of it's style tags into, this is useful for inserting styles into iframes.
  28. #### key
  29. This will be set as the value of the `data-emotion` attribute on the style tags that get inserted. This is useful to identify different sheets.
  30. #### speedy
  31. This defines how rules are inserted. If it is true, rules will be inserted with [`insertRule`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule) which is very fast but doesn't allow rules to be edited in DevTools. If it is false, rules will be inserted by appending text nodes to style elements which is much slower than insertRule but allows rules to be edited in DevTools. By default, speedy is enabled in production and disabled in development.
  32. #### maxLength
  33. This defines the number of rules that are inserted into each style tag. This generally shouldn't be modified.
  34. ### Methods
  35. #### insert
  36. This method inserts a single rule into the document. It **must** be a single rule otherwise an error will be thrown in speedy mode which is enabled by default in production.
  37. #### flush
  38. This method will remove all style tags that were inserted into the document.
  39. ### Example with all options
  40. ```jsx
  41. import { StyleSheet } from '@emotion/sheet'
  42. const container = document.createElement('div')
  43. document.head.appendChild(container)
  44. const sheet = new StyleSheet({
  45. nonce: 'some-nonce',
  46. key: 'some-key',
  47. container,
  48. maxLength: 20
  49. })
  50. sheet.insert('html { color: hotpink; }')
  51. sheet.flush()
  52. ```
  53. # Thanks
  54. This StyleSheet is based on [glamor's StyleSheet](https://github.com/threepointone/glamor) written by [Sunil Pai](https://github.com/threepointone). ❤️