AdminKitDocs
Components

Next.js Bootstrap Close Button

A Next.js Bootstrap close button: Bootstrap's .btn-close with an aria-label, for the modals, offcanvas sheets and alerts of your App Router pages.

CloseButton is the small X that closes a modal, an offcanvas panel or an alert. It renders Bootstrap's .btn-close with type="button" and aria-label="Close" already set. It is in the free and PRO versions of AdminKit for Next.js.

CloseButton holds no state, so its file has no "use client", and it works in both server and client components. A close button that does something is another matter. Its onClick is a function, and a server component cannot pass functions to the browser. So that button lives in a client component, next to the state it changes, as in a modal. A dismissible alert adds its own.

Example

src/components/close-buttons.tsx
"use client";

import { useState } from "react";
import { CloseButton } from "@/components/bootstrap/close-button";

export function CloseButtons() {
  const [open, setOpen] = useState(true);

  if (!open) {
    return null;
  }

  return (
    <>
      <CloseButton onClick={() => setOpen(false)} />
      <CloseButton disabled />
    </>
  );
}

Here a click removes the buttons. In a real component, onClick usually sets the show of a modal or an offcanvas to false.

Props

PropTypeDefaultDescription
whitebooleanAdds .btn-close-white, a light icon for dark backgrounds

The other props, such as onClick, disabled, className and aria-label, go to the <button>. disabled fades it and stops clicks. The component is in src/components/bootstrap/close-button.tsx.

On a dark background

Use white on a dark surface, where the default icon is hard to see. The toasts of AdminKit PRO set it when their text is light.

<div className="bg-dark p-3 rounded">
  <CloseButton white onClick={close} />
</div>

Accessibility

  • aria-label="Close" gives the icon-only button its name. With several close buttons close together, pass a clearer one, such as aria-label="Close filters".
  • It is a native <button>: focusable, and Enter and Space press it.

Last updated on

On this page