Skip to content

[UI Components] Create Modal component #52922

Closed

Description

@huyenltnguyen

This issue is to track the implementation of the new Modal component in the ui-components library. The issue is actually created after the fact, and the PR for the implementation is #51070.

I'm using this issue to document the technical design and decision as I'm reviewing the PR.

Current specs

Behaviors

  • Closes on Escape press

Styling

  • Has 30px top and bottom margin on large screen size (min-width: 768px)
  • Has 10px margin on all sides on smaller screen size
  • The height grows with the content
  • The width follows the Bootstrap sizes (ref):
    • lg or large: 900px wide
    • md: 600px wide
    • sm or small: 300px wide
  • Has a 1px solid border on all sides (set by the modal-content class)
  • Header:
    • The text is centered
    • margin: 0
    • line-height: 1.42857143
    • Has a 1px solid bottom border
  • Body:
    • The content is centered
  • Footer:
    • Has a 15px padding on all sides and a 1px solid top border
    • The content is centered
  • When the modal opens, a backdrop with background-color: #000 and opacity: 0.5 appears and sits on top of the background content
  • Has default and danger variants
    • Default:
      • The border color is --secondary-color
      • The background color of the header, body, and footer is --secondary-background
      • The color of the text content is --secondary-color
      • The variant of the primary and secondary buttons is primary
    • Danger:
      • The border color is --secondary-color
      • The background color of the header is --danger-background
        • Note that this is only for some danger modals (reset-modal is an example), though in the new Modal version we should apply this styling to all danger modals for consistency
      • The color of the text content is --danger-color
      • The primary button has the danger variant
      • The secondary button is optional, and has the primary variant
      • If both the primary and secondary buttons are present, display the secondary button first then primary (see Inconsistent button order in modals #52615)

Modal variants

Modal variants with screenshots
Variant Theme Screenshot
Default Light Screenshot 2024-01-23 at 17 23 47
Default Dark Screenshot 2024-01-23 at 17 17 02
Danger Light Screenshot 2024-01-23 at 17 24 42
Danger Dark Screenshot 2024-01-23 at 17 14 06

Modal position on different screen sizes

Modal position on different screen sizes with screenshots
Screen size Screenshot
Width being 768px or above Screenshot 2024-01-24 at 16 38 06
Width being less than 768px Screenshot 2024-01-24 at 16 37 57

Components

  • Modal
  • Modal.Header
  • Modal.Title
  • Modal.Body
  • Modal.Footer

Props interface

This section contains props that are used in our codebase. The information of the props is copied from https://react-bootstrap-v3.netlify.app/components/modal/#modals-props.

Modal

Prop Type Required Default Description
animation boolean true Open and close the Modal with a slide and fade animation.
backdrop 'static' | true | false Include a backdrop component. Specify 'static' for a backdrop that doesn't trigger an onHide when clicked.
bsSize 'lg' | 'large' | 'sm' | 'small' Component size variations.
dialogClassName A css class to apply to the Modal dialog DOM node.
keyboard boolean Close the modal when escape key is pressed.
onExited function Callback fired after the Modal finishes transitioning out.
onHide function A callback fired when the header close button or non-static backdrop is clicked. Required if either are specified.
show boolean When true the modal will show itself.

Notes:

Modal.Header

Prop Type Required Default Description
closeButton boolean false Specify whether the component should contain a close button.

Modal.Title

Prop Type Required Default Description
children ReactNode Title of the modal.

Modal.Body

Prop Type Required Default Description
children ReactNode Content of the modal.

Modal.Footer

Prop Type Required Default Description
children ReactNode Footer of the modal, usually is a button or a group of buttons.

Implementation plan

Dependencies

We will use the Dialog component of radix-ui.

Doc: https://www.radix-ui.com/primitives/docs/components/dialog.

Components and sub-components

Sub-components:

  • Close: The "x" button
  • Trigger: The button that triggers/opens the modal
    • We don't need this at the moment since we've been triggering dialogs via redux
  • Description: The body of the modal
  • ModalLayer: The Portal that the modal is rendered in
    • Note: The portal must be rendered under an element with the class light-palette or dark-palette in order to be themed properly
  • ModalTitle: The title of the modal
  • ModalHeader: The header of the modal. This section includes ModalTitle and Close
  • ModalFooter: The footer of the modal. This section includes buttons for the actions

We will expose a single Modal component, which encapsulates sub-components.

Public components are:

  • Modal
  • Modal.Header
  • Modal.Title
    • We are exporting this component to ease the migration process. But in the future, I think we could turn this into a title prop of Modal.Header and only use Modal.Header to compose modal
  • Modal.Body
  • Modal.Footer

Props interface

Modal

Prop Type Required Default Description
size 'large' | 'medium' 'medium' Component size variations.
variant 'default' | 'danger' Component style variations.
onExited function A callback fired after the Modal finishes transitioning out.
open boolean When true the modal will show itself.
onClose function A callback responsible for the close action.

Notes:

  • We should build the Bootstrap's animation and backdrop behaviors into the Modal component, so that the modal display is consistent
  • We should build the Bootstrap's keyboard behavior into the Modal component to ensure the component is accessible

Modal.Header

Prop Type Required Default Description
children ReactNode Content of the header, usually the modal title, rendered by the Modal.Title component.
showCloseButton boolean true Whether the header should contain a close button.

Note: We are currently using Modal.Header with closeButton={true} in all modals, except for donation-modal. Setting the default value to true would improve the DX a little as we don't have to specify this prop in most cases.

Modal.Title

Prop Type Required Default Description
children ReactNode Title of the modal.

Modal.Body

Prop Type Required Default Description
children ReactNode Content of the modal.

Modal.Footer

Prop Type Required Default Description
children ReactNode Footer of the modal, usually is a button or a group of buttons.

Usage example

const MyModal = () => (
  <Modal show={show}>
    <Modal.Header>
      <Modal.Title>Modal title</Modal.Title>
    </Modal.Header>

    <Modal.Body>
      // content
    </Modal.Body>

    <Modal.Footer>
      <Button>Button one</Button>
      <Button>Button two<Button>
    </Modal.Footer>
  </Modal>
)

References

Activity

  1. added
    scope: UI
    scope: tools/scripts
    on Jan 3, 2024
  2. self-assigned this
    on Jan 3, 2024
  3. locked and limited conversation to collaborators on Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

scope: UIscope: tools/scripts

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions