@k8ordo/ui

Modal

<dialog>で画面の前面に開くオーバーレイです。DialogとDrawerの土台で、中身を自由に置くときは直接使います。

インポート

ts
import { Modal } from '@k8ordo/ui';

使い方

基本的な使い方

Confirmation

Are you sure you want to proceed?

tsx
const [isOpen, setIsOpen] = useState(false);

<Button onClick={() => setIsOpen(true)}>
  Open Modal
</Button>
<Modal
  isOpen={isOpen}
  onClose={() => setIsOpen(false)}
  side="center"
>
  <Dialog.Root>
    <Dialog.Header
      onClose={() => setIsOpen(false)}
      title="Confirmation"
    />
    <Dialog.Content>
      <p>Are you sure you want to proceed?</p>
    </Dialog.Content>
  </Dialog.Root>
</Modal>

出る位置

Center Modal

Centered on screen

Bottom Modal

Slides up from bottom

Right Modal

Slides in from right
tsx
<Button onClick={() => setCenterOpen(true)}>Center</Button>
<Button onClick={() => setBottomOpen(true)}>Bottom</Button>
<Button onClick={() => setRightOpen(true)}>Right</Button>

<Modal isOpen={centerOpen} onClose={() => setCenterOpen(false)} side="center">
  <Dialog.Root>
    <Dialog.Header onClose={() => setCenterOpen(false)} title="Center Modal" />
    <Dialog.Content>Centered on screen</Dialog.Content>
  </Dialog.Root>
</Modal>

<Modal isOpen={bottomOpen} onClose={() => setBottomOpen(false)} side="bottom">
  <Dialog.Root>
    <Dialog.Header onClose={() => setBottomOpen(false)} title="Bottom Modal" />
    <Dialog.Content>Slides up from bottom</Dialog.Content>
  </Dialog.Root>
</Modal>

<Modal isOpen={rightOpen} onClose={() => setRightOpen(false)} side="right">
  <Dialog.Root>
    <Dialog.Header onClose={() => setRightOpen(false)} title="Right Modal" />
    <Dialog.Content>Slides in from right</Dialog.Content>
  </Dialog.Root>
</Modal>

既定で開く

tsx
<Modal defaultOpen side="center">
  <Dialog.Root>
    <Dialog.Header
      onClose={() => {}}
      title="Default Open Modal"
    />
    <Dialog.Content>
      <p>This modal is open by default.</p>
    </Dialog.Content>
  </Dialog.Root>
</Modal>

トップレイヤーとポータル

Modalはブラウザのトップレイヤーの<dialog>に表示されます。そのため、document.bodyにポータルした要素はModalの後ろに隠れます。Modalは自分の<dialog>をポータルのルートとしてコンテキストで渡しています。usePortalRootで取り出してポータル先にすれば、Modalの中でも前面に出ます。Modalの中のuseToastはこの仕組みを使うので、対応が要るのは自前のポータルだけです。

tsx
import { usePortalRoot } from '@k8ordo/ui';
import { createPortal } from 'react-dom';

function FloatingLayer({ children }) {
  // Inside a Modal this is the top-layer <dialog>; outside it is undefined.
  const portalRoot = usePortalRoot();
  return createPortal(children, portalRoot?.current ?? document.body);
}

Props

aria-describedby?
Type: string
Default: -
aria-label?
Type: string
Default: -
aria-labelledby?
Type: string
Default: -
children?
Type: ReactNode
Default: -
defaultOpen?
Type: boolean
Default: -
isOpen?
Type: boolean
Default: -
onClose?
Type: () => void
Default: -
ref?
Type: Ref<HTMLDialogElement>
Default: -
side?
Type: ModalSide
Default: 'center'
PropTypeDefault
aria-describedby?string-
aria-label?string-
aria-labelledby?string-
children?ReactNode-
defaultOpen?boolean-
isOpen?boolean-
onClose?() => void-
ref?Ref<HTMLDialogElement>-
side?ModalSide'center'
k8ordo

Baselineに入った機能を、制限なく使うReactのライブラリ群。