Vue+Bootstrap: Prevent modal closing with a flag

I have a modal component that displays a form:

<template>
<div ref="modal" class="modal fade" :id="id" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title h5">New Ladder</h1>
                <button type="button" class="btn-close" aria-label="Close"
                        data-bs-dismiss="modal"></button>
            </div>
            <div ref="alert"></div>
            <form @submit.prevent="handleSubmit">
                <div class="modal-body">
                    <div class="mb-1">
                        <label for="addLadderName" class="form-label" hidden>Name</label>
                        <input type="text" class="form-control" id="addLadderName" placeholder="Enter name"
                               v-model.trim="addLadderForm.name"/>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary me-auto"
                            data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">
                        {{ processing ? 'One moment please...' : 'Create' }}
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
</template>

On submission, I set the processing flag to true, and set it back to false once I receive a response from the server.

const modal = ref(null);
const alert = ref(null);
const processing = ref(false);

const addLadderForm = reactive({name: ''});

function handleSubmit() {
  if (processing.value) {
    return;
  }
  processing.value = true
  const payload = {
    name: addLadderForm.name,
  };
  addLadder(payload);
}

function addLadder(payload) {
  const path = 'http://localhost:5001/new-ladder';
  axios.post(path, payload)
    .then(() => {
      processing.value = false;
      Modal.getInstance(modal.value).hide();
    })
    .catch((error) => {
      console.log(error);
      showAlert('danger', error.toString());
      processing.value = false;
    });
}

While waiting for the server response, I would like to force the modal to stay open, even if the user attempts to close it.

I have tried v-binding various data-bs-* properties to temporarily get the desired effect:

<template>
<div ref="modal" class="modal fade" :id="id" tabindex="-1" role="dialog"
        :data-bs-backdrop="processing ? 'static' : 'true'"
        :data-bs-keyboard="processing ? 'static' : 'true'">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title h5">New Ladder</h1>
                <button type="button" class="btn-close" aria-label="Close"
                        :data-bs-dismiss="modal"></button>
            </div>
            <div ref="alert"></div>
            <form @submit.prevent="handleSubmit">
                <div class="modal-body">
                    <div class="mb-1">
                        <label for="addLadderName" class="form-label" hidden>Name</label>
                        <input type="text" class="form-control" id="addLadderName" placeholder="Enter name"
                               v-model.trim="addLadderForm.name"/>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary me-auto"
                            :data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">
                        {{ processing ? 'One moment please...' : 'Create' }}
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
</template>

But that doesn’t seem to work; I can still close the modal during a hard-coded 10-second pause on the server side. It occurs to me that I could wire a bunch of the UI into to my own closeModal and do the appropriate checks within instead of relying on data-bs-dismiss. However, that seems like a duplication of existing work, especially since I’d have to figure out how to replicate whatever data-bs-backdrop="static" does.

I am using Vue 3.4.29 and Bootstrap 5.3.3, but not bootstrap-vue.

New contributor

tallow_amnesty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật