Skip to content
npm ↗ GitHub ↗

Documentation

27/Overlays

Modal

Centered dialog with trapped focus, closing with Esc, backdrop click and data-cb-close buttons.

Confirmation

¿Archivar el proyecto?

Podrás restaurarlo durante 30 días.

HTML
<button class="cb-btn cb-btn--primary" type="button" data-cb-open="demo-modal">Abrir modal</button>

<div class="cb-modal" id="demo-modal" data-cb-modal>
  <div class="cb-modal__panel">
    <div class="cb-modal__head">
      <div>
        <h2 class="cb-modal__title">¿Archivar el proyecto?</h2>
        <p class="cb-modal__desc">Podrás restaurarlo durante 30 días.</p>
      </div>
      <button class="cb-modal__close" type="button" data-cb-close aria-label="Cerrar"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></button>
    </div>
    <div class="cb-modal__body">
      <div class="cb-field">
        <label class="cb-field__label" for="modal-reason">Motivo</label>
        <input class="cb-input" id="modal-reason" placeholder="Opcional">
      </div>
    </div>
    <div class="cb-modal__foot">
      <button class="cb-btn cb-btn--outline" type="button" data-cb-close>Cancelar</button>
      <button class="cb-btn cb-btn--danger" type="button" data-cb-close>Archivar</button>
    </div>
  </div>
</div>

Sizes and static

With data-cb-static, clicking the backdrop does not close it. cb-modal--center centers it vertically.

Términos del servicio

Este modal solo se cierra con los botones o con Esc.

HTML
<button class="cb-btn cb-btn--outline" type="button" data-cb-open="demo-modal-lg">Modal grande</button>

<div class="cb-modal cb-modal--lg cb-modal--center" id="demo-modal-lg" data-cb-modal data-cb-static>
  <div class="cb-modal__panel">
    <div class="cb-modal__head">
      <h2 class="cb-modal__title">Términos del servicio</h2>
      <button class="cb-modal__close" type="button" data-cb-close aria-label="Cerrar"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></button>
    </div>
    <div class="cb-modal__body"><p>Este modal solo se cierra con los botones o con Esc.</p></div>
    <div class="cb-modal__foot"><button class="cb-btn cb-btn--primary" type="button" data-cb-close>Aceptar</button></div>
  </div>
</div>

Classes and modifiers

ClassDescription
.cb-modalBlurred backdrop. .is-open shows it.
.cb-modal--sm, .cb-modal--lgWidths of 400 and 760px (520 by default).
.cb-modal--centerVertical centering.
.cb-modal__panelDialog box.
.cb-modal__head, .cb-modal__title, .cb-modal__descHeader, title and description.
.cb-modal__closeClose button.
.cb-modal__body, .cb-modal__footContent and actions.

data-cb-* attributes

AttributeDescription
data-cb-modalEnables the modal. Needs an id.
data-cb-open="id"On any button, opens that modal or drawer.
data-cb-closeInside the modal, closes it.
data-cb-staticIgnores clicks on the backdrop.
data-cb-open-on-loadOpens it on load.
data-cb-role="alertdialog"Changes the role for critical confirmations.

JavaScript

EventWhen it fires
cb:openBefore opening. Can be canceled with preventDefault().
cb:closeBefore closing. It can also be canceled.

Open and close from JS:

JS
cobalto.modal.open('demo-modal');
cobalto.modal.close('demo-modal');

From Livewire 4: dispatch the event with $this->dispatch('abrir-modal', id: 'confirmar') and listen for it with Livewire.on():

JS
document.addEventListener('livewire:init', () => {
  Livewire.on('abrir-modal', (event) => cobalto.modal.open(event.id));
});

Accessibility

  • The panel gets role="dialog", aria-modal="true" and aria-labelledby pointing to the title.
  • Focus moves to the first control (or the one with autofocus), stays trapped and returns to the button on close.
  • Esc closes only the topmost overlay when several are open.
  • Body scroll is locked while it is open.