모달

스크림, 포커스 트랩, Escape 닫기를 가진 손그림 스타일의 모달 대화상자입니다.

개요

Modal(대화상자)은 흐름을 중단하고 단일 작업 — 확인, 짧은 폼, 상세 보기 — 에 집중해요. 닫힐 때까지 포커스를 가두고 스크림으로 페이지의 나머지를 어둡게 해요.

구조

전체 뷰포트 스크림(comp.modal.scrim.*) 뒤에 중앙에 배치된 손그림 패널(@ghds/sketch-core)로 선택적 제목과 본문이 있어요. role="dialog" + aria-modal="true"이며 aria-labelledby로 제목과 연결돼요.

변형 및 상태

State Status Notes
열림 Implemented 페이지 위에 포털; 포커스가 안으로 이동하고 가둬져요.
닫힘 Implemented 아무것도 렌더하지 않음(React) / 표시 안 함(WC/RN).
스크림 닫기 Implemented 스크림 클릭 시 닫기 요청(closeOnScrimClick={false}로 옵트아웃).

사용법

Do

  • 제어 방식으로 유지: 열린 상태를 소유하고 onClose에서 닫아요.
  • 접근성 이름을 가지도록 제목을 부여하세요.
  • 모달은 집중되고 흐름을 중단하는 작업에만 사용; 콘텐츠를 짧게 유지하세요.

Don't

  • 여러 모달을 겹치지 마세요.
  • 길고 스크롤 가능한 페이지 콘텐츠를 모달에 넣지 마세요 — 페이지를 사용하세요.

접근성

Key Action
Tab / Shift+Tab 대화상자 안에서 포커스 순환(갇힘).
Escape 닫기 요청.

콘텐츠

간결한 제목과 집중된 본문. 본문 끝에 primary/secondary 동작을 배치하세요.

Props API

React@ghds/react

PropTypeDefaultDescription
openboolean—대화상자 표시 여부.
onClose() => void—닫기 요청(스크림 클릭 / Escape).
titlestring—접근성 제목(heading + aria-labelledby).
closeOnScrimClickbooleantrue스크림 클릭 시 닫아요.
childrenReactNode—대화상자 본문.

Web Components@ghds/web-components

PropTypeDefaultDescription
openboolean—대화상자 표시 여부(reflected).
headingstring—접근성 제목.
closeOnScrimClickbooleantrue백드롭 클릭 시 닫아요.
close eventCustomEvent—Escape / 백드롭 클릭 시 발생.

React Native@ghds/react-native

PropTypeDefaultDescription
openboolean—대화상자 표시 여부.
onClose() => void—닫기 요청(스크림 탭 / 백).
titlestring—접근성 제목.
closeOnScrimClickbooleantrue스크림 탭 시 닫아요.
testIDstring—쿼리용 테스트 핸들.

코드 예시

// React — @ghds/react
import { Modal } from '@ghds/react/modal';
import { Button } from '@ghds/react/button';
import { useState } from 'react';

const [open, setOpen] = useState(false);

<>
  <Button onClick={() => setOpen(true)}>Open dialog</Button>
  <Modal open={open} onClose={() => setOpen(false)} title="Delete item?">
    <p>This action cannot be undone.</p>
    <Button variant="danger" onClick={() => setOpen(false)}>Delete</Button>
  </Modal>
</>;
<!-- Web Components — @ghds/web-components -->
<script type="module">
  import '@ghds/web-components/modal';
  const modal = document.getElementById('dialog');
  // `title`이 아니라 `heading`을 써요. `open`은 프로퍼티이고, `close` 이벤트 리스너를 등록하세요
  modal.open = true;
  modal.addEventListener('close', () => {
    modal.open = false;
  });
</script>
<gh-modal id="dialog" heading="Delete item?">
  <p>This action cannot be undone.</p>
</gh-modal>
// React Native — @ghds/react-native
import { Modal } from '@ghds/react-native/modal';
import { Text } from '@ghds/react-native/theme';
import { useState } from 'react';

const [open, setOpen] = useState(false);

<Modal open={open} onClose={() => setOpen(false)} title="Delete item?">
  <Text>This action cannot be undone.</Text>
</Modal>;

라이브 데모

React @ghds/react

Web Components @ghds/web-components

Open dialog

This action cannot be undone.