modalStore.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import type {FocusTrap} from 'focus-trap';
  2. import {createStore} from 'reflux';
  3. import type {ModalOptions, ModalRenderProps} from 'sentry/actionCreators/modal';
  4. import type {StrictStoreDefinition} from './types';
  5. type Renderer = (renderProps: ModalRenderProps) => React.ReactNode;
  6. type State = {
  7. options: ModalOptions;
  8. renderer: Renderer | null;
  9. focusTrap?: FocusTrap;
  10. };
  11. interface ModalStoreDefinition extends StrictStoreDefinition<State> {
  12. closeModal(): void;
  13. init(): void;
  14. openModal(renderer: Renderer, options: ModalOptions): void;
  15. reset(): void;
  16. setFocusTrap(focusTrap: FocusTrap): void;
  17. }
  18. const storeConfig: ModalStoreDefinition = {
  19. state: {renderer: null, options: {}},
  20. init() {
  21. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  22. // listeners due to their leaky nature in tests.
  23. this.reset();
  24. },
  25. getState() {
  26. return this.state;
  27. },
  28. reset() {
  29. this.state = {
  30. renderer: null,
  31. options: {},
  32. focusTrap: this.state.focusTrap,
  33. };
  34. },
  35. closeModal() {
  36. this.reset();
  37. this.trigger(this.state);
  38. },
  39. openModal(renderer: Renderer, options: ModalOptions) {
  40. this.state = {renderer, options, focusTrap: this.state.focusTrap};
  41. this.trigger(this.state);
  42. },
  43. setFocusTrap(focusTrap: FocusTrap) {
  44. this.state = {
  45. ...this.state,
  46. focusTrap,
  47. };
  48. },
  49. };
  50. const ModalStore = createStore(storeConfig);
  51. export default ModalStore;