Displaying a Modal

I am trying to get a minimal example of using Bootstrap Modals, and managing that modal using state.

Here is
routes/Index.tsx
:

import { Head } from "$fresh/runtime.ts";

import Modal from "../islands/Modal.tsx";

export default function Home() {
  return (
    <>
      <Head>
        <link
          href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
          rel="stylesheet"
          integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
          crossorigin="anonymous"
        />
      </Head>
      <Modal visible={true}/>
      <script
        src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
        crossorigin="anonymous"
      >
      </script>
    </>
  );
}


And here is
islands/Modal.tsx
: https://paste.jlcarveth.dev/api/02dcac97-dc65-4802-ad77-81e8ac0a8de9


The modal can open and close fine. The issue is that the state of the Modal can't be controlled programatically. When I have used Bootstrap modals in the past (with React), it was as simple as adding / removing the
show
class from the modal. That approach no longer seems to work.

So I try to hide / show the modal via the bootstrap API, which you can see me try in the
onClick
of that close button. But the island can't seem to access the bootstrap API. I have tried importing bootstrap via
npm:bootstrap
, as well as via
https://esm.sh/bootstrap


In short; I want to be able to toggle the Modal on and off (visible / hidden) without having to rely on the
data-bs-toggle
and
data-bs-target
HTML attributes since that prevents me from managing the modal state.
Was this page helpful?