Skip to main content

Modal

Overview​

The Modal component provides an empty, accessible overlay for arbitrary content — for a ready-made confirm or alert, reach for Dialog instead.

It supports both Bulma's modal-card and modal-content variants, with options for title, footer, custom content, color helpers, and close callbacks. Easily control visibility via the active or isActive prop and handle closing with onClose.

The Modal component supports two APIs:

  • Legacy Props API: Use modalCardTitle and modalCardFoot props for simple modals
  • Compound Components API: Use Modal.Background, Modal.Card, Modal.Content, and Modal.Close for full control and better readability
info

Use Modal for forms or custom content — an empty overlay you build the contents of. For a ready-made confirm or alert, use Dialog instead. For a panel anchored to the button that opens it, use Popover. Modal supports card-style layouts (header/body/footer) or arbitrary content modals.


Import​

import {
Modal,
ModalBackground,
ModalContent,
ModalClose,
ModalCard,
ModalCardHead,
ModalCardTitle,
ModalCardBody,
ModalCardFoot,
} from '@allxsmith/bestax-bulma';

Usage​

To display a modal dialog with a header and footer, use the Modal component with the active prop to control visibility, and provide modalCardTitle and modalCardFoot for the card layout. The onClose prop handles closing the modal, and you can use Bulma color helpers for further customization. The close button it renders is named "close" for screen readers; ariaCloseLabel gives it a name of your own, such as one in your app's language. This pattern is ideal for forms or any content that requires user attention in a focused overlay.

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

  return (
    <>
      <Button color="success" onClick={() => setOpen(true)}>
        Show Modal
      </Button>
      <Modal
        active={open}
        onClose={() => setOpen(false)}
        modalCardTitle="Modal Card Title"
        modalCardFoot={
          <Buttons>
            <Button color="success">Save</Button>
            <Button>Cancel</Button>
          </Buttons>
        }
      >
        Modal card body content goes here...
      </Modal>
    </>
  );
}


Provide only the modalCardTitle prop to display a modal card with a header and body, but no footer. This is useful for simple dialogs or information popups that do not require actions in the footer area.

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

  return (
    <>
      <Button color="success" onClick={() => setOpen(true)}>
        Show Modal
      </Button>
      <Modal
        active={open}
        onClose={() => setOpen(false)}
        modalCardTitle="Modal Card Title Only"
      >
        Modal body content...
      </Modal>
    </>
  );
}


Provide only the modalCardFoot prop to display a modal card with a footer and body, but no header. This is useful for footer-driven actions that do not require a title. With no title to name the dialog, give it an aria-label.

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

  return (
    <>
      <Button color="success" onClick={() => setOpen(true)}>
        Show Modal
      </Button>
      <Modal
        active={open}
        onClose={() => setOpen(false)}
        modalCardFoot={<Button color="success">Save</Button>}
        aria-label="Save changes"
      >
        Modal body content...
      </Modal>
    </>
  );
}


Omit both modalCardTitle and modalCardFoot to render a modal with only custom content. This is ideal for custom layouts, or when you want full control over the modal's appearance. Name the dialog by pointing aria-labelledby at a heading inside it, or with an aria-label.

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

  return (
    <>
      <Button color="success" onClick={() => setOpen(true)}>
        Show Modal
      </Button>
      <Modal
        active={open}
        onClose={() => setOpen(false)}
        aria-labelledby="content-modal-title"
      >
        <Box>
          <Title size="4" id="content-modal-title">
            Custom Content
          </Title>
          <Paragraph>Put any content here!</Paragraph>
        </Box>
      </Modal>
    </>
  );
}


Explicit Modal Types​

Force content style​

Set the type prop to content to force the modal to use the content style, regardless of whether a title or footer is provided. This is useful for custom layouts or when you want to avoid the card structure.

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

  return (
    <>
      <Button color="success" onClick={() => setOpen(true)}>
        Show Modal
      </Button>
      <Modal
        active={open}
        onClose={() => setOpen(false)}
        type="content"
        aria-label="Content style example"
      >
        <div>Custom content modal (forced type="content")</div>
      </Modal>
    </>
  );
}

Force card style​

Set the type prop to card to force the modal to use the card style, even if no title or footer is provided. This is helpful for consistent styling across your app.

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

  return (
    <>
      <Button color="success" onClick={() => setOpen(true)}>
        Show Modal
      </Button>
      <Modal
        active={open}
        onClose={() => setOpen(false)}
        type="card"
        aria-label="Card style example"
      >
        Modal card body (forced type="card")
      </Modal>
    </>
  );
}


Portal​

Set portal to render the modal through a portal (into document.body by default, or a custom target) instead of inline, so it isn't clipped by an ancestor with overflow: hidden, filter or transform. It's opt-in: omit it to keep the current inline behavior.

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

  return (
    <>
      <Button color="success" onClick={() => setOpen(true)}>
        Show Portaled Modal
      </Button>
      <Modal
        active={open}
        onClose={() => setOpen(false)}
        modalCardTitle="Portaled Modal"
        portal
      >
        This modal is portaled, so it escapes any ancestor with an `overflow:
        hidden` style.
      </Modal>
    </>
  );
}

portal also accepts a document.querySelector selector string or an HTMLElement to target a specific container. With portal set to true, a ConfigProvider portalContainer takes the place of document.body. Portaled outside <main>, as this one is, a modal card's title bar is a banner landmark inside its dialog: Accessibility says how to avoid that.

It's SSR-safe: a portal has no server-rendered counterpart, so the modal renders inline on the server and during hydration, then moves into the portal once the client takes over. Hydration matches instead of trading an SSR crash for a hydration mismatch.


Forwarded ref​

Modal forwards a ref to the root .modal element, so you can measure it or move focus without wrapping it.

function example() {
  const [open, setOpen] = React.useState(false);
  const [width, setWidth] = React.useState<number | null>(null);
  const modalRef = React.useRef<HTMLDivElement>(null);

  return (
    <>
      <Button color="info" onClick={() => setOpen(true)}>
        Open and measure
      </Button>
      <Modal
        active={open}
        onClose={() => setOpen(false)}
        ref={modalRef}
        modalCardTitle="Measured through the ref"
      >
        <Button
          onClick={() =>
            setWidth(modalRef.current?.getBoundingClientRect().width ?? null)
          }
        >
          Measure
        </Button>
        <p>Width: {width === null ? '—' : `${Math.round(width)}px`}</p>
      </Modal>
    </>
  );
}

The ref keeps working with portal. Moving into the portal remounts the subtree, so the ref detaches from the inline node and re-points at the portaled one rather than leaving you holding a detached element.

Callback refs behave the same on React 18 and 19: if your callback returns a cleanup function, it runs on detach instead of the ref being called with null.

Compound (dot-notation) usage​

Modal.Card with compound components​

The compound components API provides more flexibility and better readability for complex modals. Use Modal.Background, Modal.Card, and related sub-components for full control over the modal structure.

The same parts are named exports: ModalBackground, ModalContent, ModalClose, ModalCard, ModalCardHead, ModalCardTitle, ModalCardBody, and ModalCardFoot. Each is the same component as the dotted name (Modal.Card.Head is ModalCardHead). A Next.js Server Component imports them by name, since a dotted name is undefined there. Next.js Specific has more.

Pass onClose to the Modal here too. Escape calls it, and so does Modal.Close, unless you give the button an onClick of your own: yours then replaces the close, so call onClose from it if the button should still close the modal. Modal.Background doesn't call onClose on its own, so a click on the background closes the modal only once you pass it onClick={onClose}.

function example() {
  const [open, setOpen] = useState(false);
  const closeModal = () => setOpen(false);

  return (
    <>
      <Button color="success" onClick={() => setOpen(true)}>
        Show Modal
      </Button>
      <Modal
        isActive={open}
        onClose={closeModal}
        aria-labelledby="compound-modal-title"
      >
        <Modal.Background onClick={closeModal} />
        <Modal.Card>
          <Modal.Card.Head>
            <Modal.Card.Title id="compound-modal-title">
              Compound Component Modal
            </Modal.Card.Title>
            <Modal.Close />
          </Modal.Card.Head>
          <Modal.Card.Body>
            This modal uses the compound components API for better control and
            readability.
          </Modal.Card.Body>
          <Modal.Card.Foot>
            <Buttons>
              <Button color="success" onClick={closeModal}>
                Save
              </Button>
              <Button onClick={closeModal}>Cancel</Button>
            </Buttons>
          </Modal.Card.Foot>
        </Modal.Card>
      </Modal>
    </>
  );
}

Modal.Content with compound components​

For custom content modals, use Modal.Content with Modal.Close (use variant="floating" for the overlay close button):

function example() {
  const [open, setOpen] = useState(false);
  const closeModal = () => setOpen(false);

  return (
    <>
      <Button color="success" onClick={() => setOpen(true)}>
        Show Modal
      </Button>
      <Modal
        isActive={open}
        onClose={closeModal}
        aria-labelledby="custom-content-title"
      >
        <Modal.Background onClick={closeModal} />
        <Modal.Content>
          <Box>
            <Title size="4" id="custom-content-title">
              Custom Content
            </Title>
            <Paragraph>Using compound components for full control!</Paragraph>
          </Box>
        </Modal.Content>
        <Modal.Close variant="floating" />
      </Modal>
    </>
  );
}


Accessibility​

  • The modal root gets role="dialog" and aria-modal="true" while active — pass an explicit role to override (e.g. Dialog passes role="presentation" on the Modal it wraps so its own role="alertdialog" is the only one announced).

  • Give the dialog an accessible name. With the legacy API, modalCardTitle is wired up as aria-labelledby for you, and with type="content", which shows no card head, a title that's a string names the dialog as its aria-label. Without one, and with the compound API, pass your own aria-label or aria-labelledby on <Modal> — a role="dialog" with no name is announced as just "dialog". An explicit aria-labelledby always wins over the generated one. A development build warns about an open modal nothing names.

  • Escape closes the modal (calls onClose) by default; opt out with closeOnEscape={false}. One Escape closes the topmost overlay only, whichever open Modal, Dialog, Sidebar or full-page Loading opened last, and the next Escape closes the one under it. A modal on top with closeOnEscape={false} still keeps Escape from the layers under it. A Toast waits until no overlay is open.

  • The modal calls preventDefault() on the Escape it closes on, so a listener of your own that runs after it can tell the key was used.

  • A popup inside the modal takes its own Escape first. With an Autocomplete list, a Dropdown menu, a Popover or a date picker's calendar open, the first Escape closes that and the next one closes the modal.

  • A popup of your own inside a modal should call preventDefault() on the Escape it closes on. The modal leaves an Escape that was prevented alone, whether your popup hears it in a React onKeyDown or in a listener on document. Call it only when the popup is open and the key was pressed in it, so an Escape with nothing to close still closes the modal, and so does one pressed in a modal opened over your popup.

  • The page doesn't scroll under the modal while it's active; opt out with lockScroll={false}. The lock is ref-counted and shared with Dialog, Sidebar and Loading, so whichever overlay closes first doesn't unlock the page underneath one that is still open.

  • Focus moves to the first tab stop (or the modal itself) on open, and is restored to the previously focused element on close — unless another modal is still open on top, in which case that one keeps focus.

  • A popup opened inside the modal, such as a Popover, HoverCard or ContextMenu panel, or a Combobox menu or picker popover with appendToBody, is portaled into the modal's root rather than to the end of <body>, so a screen reader that honors aria-modal reaches it, and it's inside the Tab cycle below. A portalContainer a ConfigProvider sets still takes it. A Lightbox is a Modal, so the same goes for it.

  • Tab and Shift+Tab cycle within the modal while it is the topmost modal overlay, so the keyboard order agrees with what aria-modal="true" tells assistive technology. They wrap at the same tab stops useFocusTrap does, so disabled, hidden and tabIndex={-1} elements are skipped, and so is inert content, such as a closed Sidebar inside the modal, and the content of a closed <details>. A radio group counts once. A Tab from outside the modal brings focus back in, and so does a Tab the browser takes out anyway, as Safari does past links when its settings say to. With another Modal or Dialog, a Sidebar with an overlay or a full-page Loading opened over it, the modal leaves Tab alone until that one closes. A Sidebar without an overlay isn't modal: opened over the modal, it takes Escape first, but the modal keeps holding Tab and keeps focus, so the sidebar doesn't take focus when it opens, and a Tab from its controls comes back into the modal. A focus trap of your own inside it, such as a portaled panel's, keeps a Tab it calls preventDefault() on, and the modal leaves focus where your trap put it. Note that aria-modal does not by itself make background content inert, and no inert attribute is applied, so background content remains reachable by pointer and by browser UI such as find-in-page.

  • In the props form, the background closes the modal on click, and a close button is provided for both modal-card and modal-content variants (both call onClose, so pass it). The close button is named "close" unless ariaCloseLabel names it. It's drawn as an icon, so its name is all a screen reader says about it: in an app in another language, give it one in the page's language. An aria-label on Modal names the dialog, not the button, and a Modal.Close in the compound form takes an aria-label of its own. The close buttons, the card's and the floating one, and Modal.Close in either variant, take presses on a box at least 24px square centred on the button Bulma draws, as Delete does. A Modal.Close given dangerouslySetInnerHTML has no box, since React won't render it beside that.

  • In the compound form, Modal.Close calls the modal's onClose unless you give it an onClick of your own, and Modal.Background closes the modal only when you pass it onClick={onClose}.

  • A modal card's head and foot are a <header> and a <footer>. Outside <main>, <article>, <aside>, <nav> or <section>, a browser exposes those as the page's banner and content info landmarks. So when portal renders a modal card at the end of <body>, its head and foot are landmarks inside its dialog, and a screen reader user moving through the page by landmark is offered its title bar as the page's banner. Any of these avoids them:

    • Render the modal inline inside the page's <main>, or point portal at an element inside it that doesn't clip the modal.
    • In the compound form, pass role="none" to Modal.Card.Head and Modal.Card.Foot, which drops them wherever the modal is. A browser ignores that role on an element with an aria-label or other global ARIA attribute, or a tabIndex, so give them none of those. The compound form leaves the dialog's name to you: point an aria-labelledby on <Modal> at the Modal.Card.Title.

    The props form's head and foot, from modalCardTitle and modalCardFoot, can't take a role.

  • In forced colors (Windows' contrast themes), which repaint the card's backgrounds and the backdrop's, a modal card keeps a one pixel edge in the system's text color, from bestax's components/modal partial, which bestax.css includes.

note

Always provide an onClose handler for accessibility and to allow users to dismiss the modal.


  • Dialog: Confirm/alert — use this unless you need arbitrary modal content.
  • Popover: A non-modal panel anchored to its trigger, for content that shouldn't block the page.
  • Button: Use for actions in modal footers.
  • Field, Input: For forms inside modals.
  • Helper Props: Bulma helper props for spacing and other utilities.

Additional Resources​

Pro Tip

You can use Bulma helper props with <Modal /> for powerful utility-based styling.


Props​

Main Modal Props​

PropTypeDefaultDescription
activebooleanfalseWhether the modal is open/visible.
isActivebooleanfalseAlias for active. Whether the modal is open/visible.
onClose() => void—Called to ask for the modal to close. The props form calls it from the background and the close button it renders. In the compound form, Modal.Close calls it unless you give it an onClick of your own, and Modal.Background doesn't call it on its own: pass it onClick={onClose} for a click on the background to close the modal. Escape calls it in both forms unless closeOnEscape is false.
classNamestring—Additional CSS classes for the modal.
ariaCloseLabelstring'close'ARIA label for the close button the props form renders with onClose, the card header's or the floating one. It's drawn as an icon, so its name is all a screen reader says about it: give it one in the page's language. An empty one falls back to the default. An aria-label on Modal names the dialog, not the button, and a Modal.Close in the compound form takes an aria-label of its own.
textColorBulma color | 'inherit' | 'current'—Text color for modal content.
bgColorBulma color | 'inherit' | 'current'—Background color for modal content.
modalCardTitleReact.ReactNode—Title/header for modal-card variant, which names the dialog through aria-labelledby. With type="content" it isn't shown, and a title that's a string still names the dialog, as its aria-label. Without one, give the modal an aria-label, or an aria-labelledby pointing at a heading inside it: a development build warns about an open modal nothing names. It's drawn in the card's <header>, which is a banner landmark unless the modal sits inside <main> or a sectioning element, as it doesn't when portal renders it at the end of <body>. The props form can't give the header a role, so point portal at an element inside <main> that doesn't clip the modal, or use the compound form with role="none" on Modal.Card.Head. (Legacy API only)
modalCardFootReact.ReactNode—Footer for modal-card variant. Group two or more buttons in Buttons so Bulma spaces them. It's drawn in a <footer>, which is a content info landmark unless the modal sits inside <main> or a sectioning element, as it doesn't when portal renders it at the end of <body>. The props form can't give the footer a role, so point portal at an element inside <main> that doesn't clip the modal, or use the compound form with role="none" on Modal.Card.Foot. (Legacy API only)
type'card' | 'content'autoModal style: 'card' for modal-card, 'content' for modal-content. (Legacy API only)
childrenReact.ReactNode—Modal body/content or compound components.
closeOnEscapebooleantrueClose the modal when the Escape key is pressed (calls onClose). Only the topmost open overlay responds, whichever Modal, Dialog, Sidebar or full-page Loading opened last, so Escape closes one layer at a time. The modal marks the Escape it closes on as used with preventDefault(), and a Toast waits until no overlay is open. Turned off, a modal on top still keeps Escape from the layers under it. A popup inside it, such as a Dropdown's menu or a date picker's calendar, takes its own Escape first, and so does content that calls preventDefault() on it.
lockScrollbooleantrueLock page scroll while the modal is active, padding the page by the room its scrollbar took so the content stays put. A fixed Navbar, the toast and notification stacks and an Affix stay put too. The padding doesn't reach an element of your own fixed to the window's edge, which moves by the scrollbar's width unless it adds var(--bulma-scroll-lock-offset-right, 0px) to its right (the -left one to its left), which the lock sets on <html> while it's on. Ref-counted and shared with Dialog, Sidebar and Loading, so whichever overlay closes first does not unlock the page underneath one that is still open.
portalboolean | string | HTMLElementfalseRenders the modal into a portal target instead of inline, so it isn't clipped by an ancestor with overflow: hidden, filter or transform. true portals to the portalContainer a ConfigProvider sets, document.body by default; a string is used as a document.querySelector selector; an element is used directly. Renders inline on the server and while hydrating, moving into the portal once the client takes over, so hydration matches.
refReact.Ref<HTMLDivElement>—Ref forwarded to the root .modal element.
...All standard <div> attributes and Bulma helper props—See Helper Props

Subcomponents:

  • Modal.Background: Modal.Background - Renders the modal background overlay. Pass it onClick={onClose} for a click on it to close the modal.
  • Modal.Content: Modal.Content - Renders modal content wrapper for custom content.
  • Modal.Card: Modal.Card - Renders modal card wrapper with compound components. Use with Modal.Card.Head, Modal.Card.Title, Modal.Card.Body, and Modal.Card.Foot.
  • Modal.Card.Head: Modal.Card.Head - Renders modal card header section, a <header>. Outside <main> or a sectioning element, as when portal renders the modal at the end of <body>, that's a banner landmark inside the dialog: pass role="none" to drop it, with no aria-label or other global ARIA attribute and no tabIndex beside it, since a browser ignores the role then.
  • Modal.Card.Title: Modal.Card.Title - Renders modal card title.
  • Modal.Card.Body: Modal.Card.Body - Renders modal card body section.
  • Modal.Card.Foot: Modal.Card.Foot - Renders modal card footer section. Group two or more buttons in Buttons so Bulma spaces them. It's a <footer>, which outside <main> or a sectioning element is a content info landmark inside the dialog: pass role="none" to drop it, with no aria-label or other global ARIA attribute and no tabIndex beside it, since a browser ignores the role then.
  • Modal.Close: Modal.Close - Renders modal close button with two variant styles. It calls the modal's onClose unless given an onClick of its own.

Modal.Background​

PropTypeDefaultDescription
classNamestring—Additional CSS classes.
onClickReact.MouseEventHandler<HTMLDivElement>—Called when the background is clicked. The background doesn't call the modal's onClose on its own, so pass onClick={onClose} for a click on it to close the modal.
...All standard <div> attributes—

Modal.Content​

PropTypeDefaultDescription
classNamestring—Additional CSS classes.
...All standard <div> attributes—

Modal.Card​

PropTypeDefaultDescription
classNamestring—Additional CSS classes.
...All standard <div> attributes—

Modal.Card.Head​

PropTypeDefaultDescription
classNamestring—Additional CSS classes.
...All standard HTML attributes—

Modal.Card.Title​

PropTypeDefaultDescription
classNamestring—Additional CSS classes.
...All standard <p> attributes—

Modal.Card.Body​

PropTypeDefaultDescription
classNamestring—Additional CSS classes.
...All standard HTML attributes—

Modal.Card.Foot​

PropTypeDefaultDescription
classNamestring—Additional CSS classes.
...All standard HTML attributes—

Modal.Close​

PropTypeDefaultDescription
classNamestring—Additional CSS classes.
size'small' | 'medium' | 'large''large'Size of the close button (only applies to 'floating' variant).
variant'delete' | 'floating''delete'Button variant. 'delete' (default) for modal card headers, 'floating' for overlay close button.
type'button' | 'submit' | 'reset''button'Button type. Defaults to 'button', so a close button inside a form does not submit it. Pass 'submit' or 'reset' and yours is used; any other value, or a spread carrying type: undefined, renders 'button'.
aria-labelstring'close'Accessible name. Pass your own to replace it. An empty one, or a spread carrying 'aria-label': undefined, keeps the default rather than leaving the button unnamed.
onClickReact.MouseEventHandler<HTMLButtonElement>—Called when the button is clicked. Without one, the button calls the onClose of the Modal around it. Yours replaces that rather than running before it, so call onClose from it if the button should still close the modal.
...All standard <button> attributes—

Compound component props​

ComponentDescription
Modal.BackgroundModal background overlay. Pass onClick={onClose} for a click on it to close the modal
Modal.ContentModal content wrapper
Modal.CardModal card wrapper
Modal.Card.HeadModal card header
Modal.Card.TitleModal card title
Modal.Card.BodyModal card body
Modal.Card.FootModal card footer
Modal.CloseModal close button. Calls the modal's onClose unless given an onClick of its own. Props: variant ('delete' [default] for card headers, 'floating' for overlay), size ('small', 'medium', 'large') for floating variant

CSS & Sass Variables​

Modal registers these variables on its own .modal element. Override them there (or via className) — a value set on an ancestor is only inherited, and loses to the component-level declaration. See Theme.

CSS VariableSass VariableDefault
--bulma-modal-z$modal-z40
--bulma-modal-background-background-color$modal-background-background-colorhsla(var(--bulma-scheme-h), var(--bulma-scheme-s), var(--bulma-scheme-invert-l), 0.86)
--bulma-modal-content-width$modal-content-width40rem
--bulma-modal-content-margin-mobile$modal-content-margin-mobile1.25rem
--bulma-modal-content-spacing-mobile$modal-content-spacing-mobile10rem
--bulma-modal-content-spacing-tablet$modal-content-spacing-tablet2.5rem
--bulma-modal-close-dimensions$modal-close-dimensions2.5rem
--bulma-modal-close-right$modal-close-right1.25rem
--bulma-modal-close-top$modal-close-top1.25rem
--bulma-modal-card-spacing$modal-card-spacing2.5rem
--bulma-modal-card-head-background-color$modal-card-head-background-colorvar(--bulma-scheme-main)
--bulma-modal-card-head-padding$modal-card-head-padding2rem
--bulma-modal-card-head-radius$modal-card-head-radiusvar(--bulma-radius-large)
--bulma-modal-card-title-color$modal-card-title-colorvar(--bulma-text-strong)
--bulma-modal-card-title-line-height$modal-card-title-line-height1
--bulma-modal-card-title-size$modal-card-title-sizevar(--bulma-size-4)
--bulma-modal-card-foot-background-color$modal-card-foot-background-colorvar(--bulma-scheme-main-bis)
--bulma-modal-card-foot-radius$modal-card-foot-radiusvar(--bulma-radius-large)
--bulma-modal-card-body-background-color$modal-card-body-background-colorvar(--bulma-scheme-main)
--bulma-modal-card-body-padding$modal-card-body-padding2rem