# How To Show Modal Content

The [ModalManager](/application/terra-application/components/modal-manager) should be used to manage modal content presentation.

A ModalManager should be rendered around content that requires modal disclosure capabilities. Multiple components can be wrapped with a single
ModalManager; every wrapped component (and their descendents) will communicate with the same ModalManager instance through the
[DisclosureManagerContext](/application/terra-application/contexts/disclosure-manager-context).

Components rendered within the ModalManager's modal will be able to access the same Contexts as the ModalManager itself.
Additional Context providers rendered within the ModalManager's children will not be available to the modal components.

The rendering of a ModalManager is typically an application-level concern. However, additional ModalManagers may be rendered to re-establish
the contexts available to disclosed contents. Before rendering a ModalManager yourself, please ensure that the application isn't already providing
a ModalManager that suits your component's needs.

```jsx
import React from 'react';
import ApplicationBase from 'terra-application/lib/application-base';
import ModalManager from 'terra-application/lib/modal-manager';

import ComponentWithModal from './ComponentWithModal';

const ExampleApp = () => (
  <ApplicationBase>
    <ModalManager>
      <ComponentWithModal />
    </ModalManager>
  </ApplicationBase>
);

export default ExampleComponent;
```

Components rendered within a ModalManager should access the value provided by the DisclosureManagerContext. That value contains APIs used
to communicate with the ModalManager and disclose additional content.

```jsx
import React, { useContext } from 'react';
import { DisclosureManagerContext } from 'terra-application/lib/disclosure-manager';

import ModalContentComponent from './ModalContentComponent';

const ComponentWithModal = () => {
  const disclosureManager = useContext(DisclosureManagerContext);

  return (
    <button
      onClick={() => {
        disclosureManager.disclose({
          preferredType: 'modal',
          size: 'large',
          content: {
            key: 'component-with-modal.modal-content',
            component: <ModalContentComponent />,
          },
        });
      }}
    >
      Show Modal
    </button>
  );
};
```

Components that need more control over the presentation of the modal have access to additional APIs that are returned upon a successful disclosure.

```jsx
import React, { useContext, useRef } from 'react';
import { DisclosureManagerContext } from 'terra-application/lib/disclosure-manager';

import ModalContentComponent from './ModalContentComponent';

const ComponentWithModal = () => {
  const disclosureManager = useContext(DisclosureManagerContext);
  const modalDismissRef = useRef();

  return (
    <button
      onClick={() => {
        disclosureManager.disclose({
          preferredType: 'modal',
          size: 'large',
          content: {
            key: 'component-with-modal.modal-content',
            component: (
              <ModalContentComponent
                onSubmit={() => {
                  if (modalDismissRef.current) {
                    modalDismissRef.current();
                    modalDismissRef.current = undefined;
                  }
                }}
              />
            ),
          },
        }).then(({ dismissDisclosure }) => {
          modalDismissRef.current = dismissDisclosure;
        });
      }}
    >
      Show Modal
    </button>
  );
};
```
