LifeSG / react-design-system

The repository for our design system in React
MIT License
13 stars 13 forks source link

Modal-aware floating elements (popover, select dropdown etc.) #552

Open qroll opened 1 week ago

qroll commented 1 week ago

Describe the feature request When rendering Popover or Form.Select as children of a Modal, allow the floating element to display over the Modal

Given the following example:

<Modal show>
  <Modal.Box>
    <div style={{ padding: "3rem 1.5rem" }}>
      <Form.Select
        options={["Option 1"]}
        label={{
          children: "I am a label",
          addon: { type: "popover", content: "I am a popover" },
        }}
      />
    </div>
  </Modal.Box>
</Modal>

It results in:

Screenshot 2024-09-03 at 11 28 57 PM

Observe that the dropdown menu isn't visible even though the input is focused and expanded.

It only works when dropdownZIndex={100000} is set.

Screenshot 2024-09-03 at 11 30 48 PM

Ideally, the dropdown menu would automatically display over the Modal

Additional context These components use floating-ui to handle positioning of the floating elements. Under the hood, they're rendered in the document body and have a default z-index of 50. Meanwhile the Modal is also appended to the document body and has a z-index of 99999.

Since the modal and floating element share the same stacking context, the floating element always renders below the modal.

qroll commented 1 week ago

A possible way to achieve this is using Context so that the child can detect the parent modal. Then the child can increment its default z-index (or set the modal as the root node)

But due to the project build setup, it's sadly not that straightforward to create a shared Context between components in this library (see this issue)