hook.js 630 B

12345678910111213141516171819202122
  1. import * as React from 'react';
  2. import { stylesheetSingleton } from './singleton';
  3. /**
  4. * creates a hook to control style singleton
  5. * @see {@link styleSingleton} for a safer component version
  6. * @example
  7. * ```tsx
  8. * const useStyle = styleHookSingleton();
  9. * ///
  10. * useStyle('body { overflow: hidden}');
  11. */
  12. export var styleHookSingleton = function () {
  13. var sheet = stylesheetSingleton();
  14. return function (styles, isDynamic) {
  15. React.useEffect(function () {
  16. sheet.add(styles);
  17. return function () {
  18. sheet.remove();
  19. };
  20. }, [styles && isDynamic]);
  21. };
  22. };